Hey I am suppose to build a low pass filter for a 2-d array for a pgm file. The program is suppose to replace each pixel with the average of it and its 8 nearest neighbors. I am stuck and have no idea what i'm doing wrong. thanks for the help. Now it is giving me a java.lang.ArrayIndexOutOfBoundsException: -1 error.
public static int[][] lowpass(int[][] image)
{
int height = image.length;
int width = image[0].length;
int[][] result = new int[height][width];
int sum;
int avg;
for (int col = 0; col < image.length; col++)
{
result[0][col] = image[0][col];
result[height - 1][col] = image[height - 1][col];
}
for (int row = 0; row < image[0].length; row++)
{
result[row][0] = image[row][0];
result[row][width - 1] = image[row][width - 1];
}
for (int row = 1; row < height - 1; row++)
{
for (int col = 1; col < width - 1; col++)
{
sum = image[row - 1][col - 1] + image[row - 1][col] + image[row - 1][col + 1] + image[row][col - 1] + image[row][col] + image[row][col + 1] + image[row +1][col - 1] + image[row +1][col] + image[row + 1][col + 1];
avg = sum / 9;
result[row][col] = avg;
}
}
return result;
}