George
George

Reputation: 337

IndexOutOfBounds Exception Handling

Im doing an assignment that needs to calculate NxN average of the pixels around the position (x,y).

Im trying to make one general function that will do this, when i pass in a size variable.

Im only allowing the size or NxN matrix to be 3x3, 5x5 or 7x7.

What im trying to do is make some general loop for every pixel where it checks if it is a corner case then a side case and if not any of those its a default case.

Example

0  1  2  3  4
5  6  7  8  9
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24

If i wanted to do a 5x5 average it would need these 25 pixel values to calculate. But if my (x,y) position is at (1,2) it will go out of bounds.

Im trying to figure out the cases for every scenario but im finding it complicated to solve as some cases arise for 5x5 which didnt exist for 3x3. If my (x,y) was at (1,1) it doesnt fall under my corner case but it does fall under my two side cases (top and left).

I wanted to know if it was safe/worthy to just throw everything into a try catch statement, and if the array position is out of bounds, no biggie.

I wouldnt have to make any cases at all and i could just check the 9 (3x3), 25 (5x5) or 49 (7x7) individual array positions if they are out of bounds.

Ex for 3x3

try{
      imageBuffer.getRBG(x - 1, y -1);
 }catch{...}
try{
      imageBuffer.getRBG(x, y -1);
 }catch{...}
try{
      imageBuffer.getRBG(x + 1, y -1);
 }catch{...}

This seems really bad, but im losing my mind trying to figure out these cases for one general function for any average.

If a NxN average around a pixel (x,y) goes out of bounds, it will calculate the average of the pixels in bounds. If it was a 3x3 average and its the first corner case (top left at (0,0)) i would calculate the 4 pixels i can access. So

0 1 2
3 4 5
6 7 8

i would calculate the average of 4,5,7 and 8

Upvotes: 1

Views: 184

Answers (1)

user4668606
user4668606

Reputation:

No, don't provoke throwing exceptions, if not necessary. That's a matter of codingstyle. In this case, the solution is actually pretty simple:

public int avg(int[][] matrix , int n , int x , int y){
     int sum = 0;

     //the number of pixels to go left and right from the center of
     //the nxn-matrix
     int nHelper = n / 2;

     //ensure the left upper corner of your matrix is in bounds
     int x_start = (x - nHelper < 0 ? 0 : x - nHelper);
     int y_start = (y - nHelper < 0 ? 0 : y - nHelper);

     //create sum of all elements in the nxn-matrix and in bounds of
     //the big matrix (assumption: the matrix is an array of columns
     int i , j;
     for(i = x_start ; i < x + nHelper + 1 && i < matrix.length ; i++)
          for(j = y_start ; j < y + nHelper + 1 && j < matrix[i].length ; j++)
                sum += matrix[i][j];

     //calculate the average
     return sum / ((i - x_start)  * (j - y_start));
}

This calculates the average of the nxn-matrix with x,y as center. shouldn't be too difficult for you to transfer that to usage with images. Though a different approach might be more efficient, if you want to calculate the average of all cells in the matrix. But it shows the basic idea.

Upvotes: 3

Related Questions