pgm图像的中值过滤器

时间:2013-11-17 22:42:18

标签: java arrays median pgm

我必须编写一个程序,用它的中间值及其8个邻居替换每个像素。我有什么会编译,但当我尝试创建一个新的图像,我得到多个错误。感谢帮助。

这是堆栈跟踪:

Exception in thread "main" java.lang.ClassCastException: [I cannot be cast to java.lang.Comparable
at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:290)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:171)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:146)
at java.util.Arrays.sort(Arrays.java:472)
at ImageProcessing.median(ImageProcessing.java:25

这是我的代码:

public static int [] [] median(int [] [] image) {
    int height = image.length;
    int width = image[0].length;
    int [] [] result = new int [height] [width];

    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++) {
            Arrays.sort(image);
            result[row][col] = image[row][col] / 2;
        }
    }
    return result;
}

1 个答案:

答案 0 :(得分:0)

您获得的错误是因为在最后一对循环中,您对Arrays.sort(image)的调用正在尝试对图像的行进行排序。

您需要建立一个您想要查看的九个像素值的列表(像素本身及其八个邻居),而不是调用Arrays.sort(image)。然后对那个进行排序,并将中值写入result