简体   繁体   English

java.lang.IllegalArgumentException:每个像素有多个组件

[英]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.

相关问题 java.lang.IllegalArgumentException:找到了多个参数类型候选:[java.lang.String]和[java.lang.Long] - java.lang.IllegalArgumentException: Found more than one parameter type candidate: [java.lang.String] and [java.lang.Long] Spring Boot:java.lang.IllegalArgumentException:找到了多个名为 [spring_web] 的片段 - Spring Boot : java.lang.IllegalArgumentException: More than one fragment with the name [spring_web] was found 原因:java.lang.IllegalArgumentException:找到多个名为 [admin_theme_fragment] 的片段 - Caused by: java.lang.IllegalArgumentException: More than one fragment with the name [admin_theme_fragment] was found HSSFWorkbook java.lang.IllegalArgumentException:可以指定不超过3个规则 - HSSFWorkbook java.lang.IllegalArgumentException: No more than 3 rules may be specified java.lang.IllegalArgumentException:不再解析元素 - java.lang.IllegalArgumentException: No more parsing elements java.lang.IllegalArgumentException: 发现不止一个名为 [spring_web] 的片段——如何修复 Spring 相对/绝对排序? - java.lang.IllegalArgumentException: More than one fragment with the name [spring_web] was found -- How to fix Spring Relative/Absolute Ordering? java.lang.IllegalArgumentException - java.lang.IllegalArgumentException java.lang.IllegalArgumentException - java.lang.IllegalArgumentException 线程“主”中的异常java.lang.IllegalArgumentException:非法的组件位置 - Exception in thread “main” java.lang.IllegalArgumentException: illegal component position 由 java.lang.IllegalArgumentException 引起:MediaButtonReceiver 组件可能不为空 - Caused by java.lang.IllegalArgumentException: MediaButtonReceiver component may not be null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM