I have some problems in the below code regarding the if-statement and possibly the 2D arrays, which I state below:
int[][]image =
{
{0,0,2,0,0,0,0,0,0,0,0,2},
{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},
{2,0,5,5,5,5,5,5,5,5,0,2},
{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,2,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. Below it, I initialize an identical array: smooth[][]. Each of smooth[][]'s elements are replaced with the numerical average of the 8 bordering elements plus itself.
The edge elements in smooth[][] (elements on outside border of array) should not be changed.
I attempt to do this with if-statements, but am only half successful. The numbers on the top and left borders do not change (r == 0 || c == 0), but any number on the bottom or right border will change to become the average value.
//compute the smoothed value of non-edge locations in smooth[][]
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 || r == (image[c].length) || c == (image[r].length))
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
Aucun commentaire:
Enregistrer un commentaire