【发布时间】: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