嘿,我想为 pgm 文件的二维数组构建一个低通滤波器。该程序假设用它及其 8 个最近邻的平均值替换每个像素。我被卡住了,不知道我做错了什么。谢谢您的帮助。现在它给了我一个 java.lang.ArrayIndexOutOfBoundsException: -1 错误。
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;
}