【问题标题】:Java Histogram Equalisation - Can't get Pixel from Image RasterJava直方图均衡 - 无法从图像光栅中获取像素
【发布时间】:2016-08-04 20:14:06
【问题描述】:

我一直在做一个直方图均衡方法。我使用this question 作为构建的基础。但是我无法让这段代码运行,而且谷歌在帮助我找到问题方面也没有太大帮助。我传入一个 JPG BufferedImage 对象。我首先显示图像,以便查看我正在处理的内容,然后对其进行处理。但是它总是在 int valueBefore=img.getRaster().getPixel(x, y,iarray)[0]; 线上失败,我不知道为什么。我得到的错误是Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1,但我看不到为什么它给出了这个错误,图片在那里并且充满了像素!

public BufferedImage hisrogramNormatlisation(BufferedImage img) {
        // To view image we're working on
        JFrame frame = new JFrame();
        frame.getContentPane().setLayout(new FlowLayout());
        frame.getContentPane().add(new JLabel(new ImageIcon(img)));
        frame.pack();
        frame.setVisible(true);

        int width =img.getWidth();
        int height =img.getHeight();
        int anzpixel= width*height;
        int[] histogram = new int[255];
        int[] iarray = new int[1];
        int i =0;

        // Create histogram
        for (int x = 50; x < width; x++) {
            for (int y = 50; y < height; y++) {
                int valueBefore=img.getRaster().getPixel(x, y,iarray)[0];
                histogram[valueBefore]++;
                System.out.println("here");
            }
        }

         int sum = 0;

         float[] lut = new float[anzpixel];
         for ( i=0; i < 255; ++i )
         {
             sum += histogram[i];
             lut[i] = sum * 255 / anzpixel;
         }

         i=0;
         for (int x = 1; x < width; x++) {
             for (int y = 1; y < height; y++) {
                 int valueBefore=img.getRaster().getPixel(x, y,iarray)[0];
                 int valueAfter= (int) lut[valueBefore];
                 iarray[0]=valueAfter;
                  img.getRaster().setPixel(x, y, iarray); 
                  i=i+1;
             }
         }
         return img;
    }

错误描述:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at java.awt.image.ComponentSampleModel.getPixel(ComponentSampleModel.java:n)
    at java.awt.image.Raster.getPixel(Raster.java:n)
    at MainApp.hisrogramNormatlisation(MainApp.java: * line described *)
    at MainApp.picture(MainApp.java:n)
    at MainApp.<init>(Main.java:n)
    at MainApp.main(Main.java:n)

【问题讨论】:

    标签: java image image-processing histogram normalization


    【解决方案1】:

    您发布的堆栈跟踪显示您的超出范围索引为 1。 异常不会在您认为的位置引发。 getPixel(int x, int y, int[] iarray) 用像素的强度值填充 iarray。如果您使用的是 rgb 图像,每个通道将至少有三个强度值,如果您使用带 alpha 的 rgb,则将有 4 个强度值。您的 iarray 的大小仅为 1,因此当 raster 想要访问更多元素以存储附加值时,会引发 IndexOutOfBoundsException。 增加iarray的大小,异常就会消失。

    【讨论】:

      【解决方案2】:

      不要使用 getPixel(),而是使用 getSample()。

      所以你的代码是:final int valueBefore = img.getRaster().getSample(x, y, 0) ; 甚至是histogram[img.getRaster().getSample(x, y, 0)]++ ;

      顺便说一句,您可能需要先检查图像类型以确定通道/波段的数量并为每个通道执行此过程。

      【讨论】:

        猜你喜欢
        • 2019-07-21
        • 1970-01-01
        • 2010-11-02
        • 1970-01-01
        • 2013-11-20
        • 2017-09-19
        • 1970-01-01
        • 2020-01-27
        • 1970-01-01
        相关资源
        最近更新 更多