I have an error in my program, which i'll state below:
int[][]image =
{
{0,0,2,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,5,5,5,5,5,5,5,5,0,0},
{0,0,5,5,5,5,5,5,5,5,0,0},
{0,0,5,5,5,5,5,5,5,5,0,0},
{0,0,5,5,5,5,5,5,5,5,0,0},
{0,0,5,5,5,5,5,5,5,5,0,0},
{0,0,5,5,5,5,5,5,5,5,0,0},
{0,0,5,5,5,5,5,5,5,5,0,0},
{0,0,5,5,5,5,5,5,5,5,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0}//assume this rectangular image
};
int[][]smooth = new int[image.length][image[0].length]; //new array equal to image[][]
Notice image[][]. It is a 2D array composed of a series of numbers. The code right below it, initializes a new 2D array called smooth[][] that is identical to image[][].
I replace each element in smooth[][] with the numerical average of the eight elements that surround it (plus the element itself) in the array. This, I have done.
However, note the elements in image[][] that are at the edge of the array. These elements are located within row 0 and column 0. Any of these edge-elements, I want to keep the same in smooth[][]. I attempt to do this with if-statements, but it doesn't work. How do I make this work?
// compute the smoothed value of non-edge locations insmooth[][]
for (int r = 0; r < image.length - 1; r++) {// x-coordinate of element
for (int c = 0; c < image[r].length - 1; c++) { // y-coordinate of
// element
int sum1 = 0;// sum of each element's 8 bordering elements and
// itself
if (r == 0 && c == 0) {
smooth[r][c] = image[r][c];
}
if (r >= 1 && c >= 1) {
sum1 = image[r - 1][c - 1] + image[r - 1][c]
+ image[r - 1][c + 1] + image[r] [c - 1]
+ image[r] [c] + image[r] [c + 1]
+ image[r + 1][c - 1] + image[r + 1][c]
+ image[r + 1][c + 1];
smooth[r][c] = sum1 / 9; // average of considered elements
// becomes new elements
}
}
}
As Phil indicated your condition should be checking for row==0 OR col==0
//compute the smoothed value of non-edge locations insmooth[][]
for(int r=0; r<image.length-1; r++){// x-coordinate of element
for(int c=0; c<image[r].length-1; c++){ //y-coordinate of element
int sum1 = 0; //sum of each element's 8 bordering elements and itself
if(r == 0 || c == 0) {
smooth[r][c] = image[r][c];
}
else {
sum1 = image[r-1][c-1] + image[r-1][c] + image[r-1][c+1] + image[r][c-1] + image[r][c] + image[r][c+1] +image[r+1][c-1] + image[r+1][c] + image[r+1][c+1];
smooth[r][c]= sum1 / 9; //average of considered elements becomes new elements
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With