0

我必须编写一个程序,用它及其 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;
}
4

1 回答 1

0

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

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

于 2013-11-18T00:00:29.880 回答