[英]java.lang.IllegalArgumentException: More than one component per pixel
I'm new to image processing in Java. 我是Java图像处理的新手。 I'm trying to compare two images with the code below and getting the message following the code. 我正在尝试将两个图像与下面的代码进行比较,并在代码之后获取消息。 Any help is greatly appreciated. 任何帮助是极大的赞赏。 Thanks. 谢谢。
BufferedImage imgOrig = ImageIO.read(new URL(imgOrigUrl));
BufferedImage imgComp = ImageIO.read(new URL(imgCompUrl));
byte[] pixelsOrig = ((DataBufferByte) imgOrig.getRaster().getDataBuffer()).getData();
byte[] pixelsComp = ((DataBufferByte) imgComp.getRaster().getDataBuffer()).getData();
//System.out.println("Number of pixels orig:"+pixelsOrig.length);
//System.out.println("Number of pixels comp:"+pixelsComp.length);
ColorModel cmImgOrig = imgOrig.getColorModel();
ColorModel cmImgComp = imgComp.getColorModel();
int sum1 = 0;
int sum2 = 0;
for(int i:pixelsOrig){
System.out.println(cmImgOrig.getGreen(i)); //ERROR OCCURS HERE
//System.out.println(i);
}
ERROR: 错误:
Testcase: testCompareImages(com.myapp.img.compare.service.CompareServiceTest): Caused an ERROR
More than one component per pixel
java.lang.IllegalArgumentException: More than one component per pixel
at java.awt.image.ComponentColorModel.getRGBComponent(ComponentColorModel.java:594)
at java.awt.image.ComponentColorModel.getGreen(ComponentColorModel.java:675)
at com.scottmacri.img.compare.service.CompareService.compareImages(CompareService.java:42)
at com.scottmacri.img.compare.service.CompareServiceTest.testCompareImages(CompareServiceTest.java:45)
Like @Nathan Villaescusa said, the method you are using is expecting a single channel. 就像@Nathan Villaescusa所说的那样,您所使用的方法期望一个单一的通道。 Do you need the byte array or the color channel? 您需要字节数组还是颜色通道? If you only need color components you can do the following: 如果只需要颜色组件,则可以执行以下操作:
BufferedImage imgOrig = ImageIO.read(new URL(imgOrigUrl));
BufferedImage imgComp = ImageIO.read(new URL(imgCompUrl));
for (int y = 0; y < imgOrig.getHeight(); y++)
{
for (int x = 0; x < imgOrig.getWidth(); x++)
{
System.out.println(imgOrig.getRGB(x, y) >> 8 & 0xff);
}
}
where the int returned by getRGB(x, y) can be shifted to get the RGB and alpha components like so: 可以将getRGB(x,y)返回的int进行移位以获取RGB和alpha分量,如下所示:
int a = rgb >> 32 & 0xff;
int r = rgb >> 16 & 0xff;
int g = rgb >> 8 & 0xff;
int b = rgb & 0xff;
It looks like that error is being thrown because your ColorSpace
has more than 1 component, yet you are only passing in a single value to check. 由于您的ColorSpace
具有多个组件,因此似乎引发了该错误,但您只传递了一个值进行检查。
You want to use the getGreen()
method of ColorComponentModel that accepts a Object
, not the one that accepts an int
. 您想使用getGreen()
方法来接受一个Object
,而不是接受一个int
。 I think the method one that accepts an int
is for use with gray scale. 我认为接受int
的方法是用于灰度。
According to this answer , here is how to get pixel data using this method: 根据此答案 ,以下是使用此方法获取像素数据的方法:
Raster r = imgOrig.getData();
SampleModel sm = r.getSampleModel();
int width = sm.getWidth();
int height = sm.getHeight();
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
Object pixel = sm.getPixel(x, u, (int[])null, r.getDataBuffer());
System.out.println(cmImgOrig.getGreen(pixel));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.