【问题标题】:Median filter of a pgm imagepgm 图像的中值滤波器
【发布时间】:2013-11-17 22:42:17
【问题描述】:

我必须编写一个程序,将每个像素替换为其及其 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;
}

【问题讨论】:

  • 请发布您遇到的错误。

标签: java arrays median pgm


【解决方案1】:

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

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

【讨论】:

  • 我将如何建立一个列表?
  • 我的意思是“建立一个列表”这个词是松散的:你可以实例化一个 ArrayListadd 的值,或者实例化一个九元素的 int[] 数组并填充它。这取决于你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-01
  • 2011-10-02
  • 2016-02-13
  • 2023-03-24
相关资源
最近更新 更多