自我计算的RGB值与实际RGB值不匹配

时间:2017-04-03 04:32:25

标签: java colors rgb

我正在制作一个程序,我会拍摄一部分图像并计算该图像的平均RGB。当我计算它时,我得到完全不同的值,就像我使用内置函数一样。当我测试我的数字并将它们放入RGB颜色图表时,它们会关闭,而内置函数非常准确。使用内置函数的问题在于它几乎没有功能。它只打印出我计算的内容,但我无法使用该数据。 link to function called dump()

public void readSquares(Mat img){

    int width = 20;
    int height = 20;
    int rSum = 0;
    int gSum = 0;
    int bSum = 0;
    int rAvg = 0;
    int gAvg = 0;
    int bAvg = 0;

    Imgproc.cvtColor(img, img, Imgproc.COLOR_BGR2RGB);   
    int channels = img.channels();

    int totalBytes = (int)(img.total() * img.channels());

    byte buff[] = new byte[totalBytes];
    img.get(0, 0, buff);

    for (int i=0; i< height; i++) {
        //stride is the number of bytes in a row of smallImg
        int stride = channels * width;
        for (int j=0; j<stride; j+=channels) {
            int r = buff[(i * stride) + j];
            int g = buff[(i * stride) + j + 1]; 
            int b = buff[(i * stride) + j + 2];

            if(r < 0 || g < 0 || b < 0){
                r = Math.abs(r);
                g = Math.abs(g);
                b = Math.abs(b);    
            }

            rSum += r;
            gSum += g;
            bSum += b;

        }
    }   

    rAvg = (int) (rSum / img.total()); //total pixels in picture
    gAvg = (int) (gSum / img.total());
    bAvg = (int) (bSum / img.total());
    System.out.println("R: " + rAvg);
    System.out.println("G: " + gAvg);
    System.out.println("B: " + bAvg);

下面打印出准确的值,但我无法获得单独的通道或找到平均RGB,只是将所有内容都吐出来作为我无法使用的字符串。

System.out.println(img.dump());
}

这是一个例子;我读取了像素的红色图像,这是输出:

我的计算

R:18
G:27
B:64

功能 第一个数字表示R秒为G,第三个表示B.它对图像中的每个像素都这样做。

[237,  24,  60, 236,  23,  59, 236,  26,  56, 234,  25,  55, 238,  27,  62, 238,  27,  62, 238,  27,  62, 236,  25,  60, 234,  24,  59, 233,  23,  57, 234,  24,  59, 234,  24,  59, 235,  22,  59, 235,  22,  59, 237,  21,  59, 235,  20,  58, 237,  19,  56, 237,  19,  56, 239,  19,  52, 237,  18,  51;
 236,  24,  55, 237,  25,  56, 237,  25,  54, 237,  25,  54, 236,  26,  56, 237,  27,  57, 238,  28,  58, 239,  29,  59, 238,  27,  62, 234,  24,  59, 237,  24,  62, 237,  24,  62, 238,  22,  63, 237,  20,  61, 235,  20,  58, 235,  20,  58, 237,  19,  58, 239,  21,  61, 237,  19,  56, 236,  18,  55;
 238,  26,  57, 238,  26,  57, 234,  25,  53, 232,  23,  50, 236,  26,  54, 237,  27,  55, 237,  27,  55, 237,  27,  55, 237,  26,  61, 236,  25,  60, 237,  24,  62, 237,  24,  62, 238,  22,  63, 237,  20,  61, 238,  22,  63, 238,  22,  63, 238,  20,  61, 238,  20,  61, 237,  19,  56, 236,  18,  55;
 242,  30,  60, 240,  27,  58, 235,  24,  49, 233,  21,  47, 234,  22,  48, 237,  26,  52, 236,  25,  50, 237,  26,  52, 238,  26,  57, 237,  25,  56, 239,  23,  62, 239,  23,  62, 238,  22,  63, 238,  22,  63, 238,  19,  63, 239,  20,  65, 238,  20,  61, 239,  21,  63, 238,  20,  59, 236,  18,  57;
 240,  27,  62, 236,  23,  59, 236,  24,  53, 236,  24,  53, 235,  23,  51, 234,  22,  50, 235,  23,  51, 236,  24,  53, 237,  24,  60, 236,  23,  59, 237,  24,  64, 237,  24,  64, 237,  24,  64, 236,  22,  63, 238,  22,  63, 238,  22,  63, 237,  20,  61, 237,  20,  61, 237,  21,  59, 235,  20,  58;
 238,  25,  61, 237,  24,  60, 237,  24,  60, 237,  24,  60, 235,  22,  57, 236,  23,  59, 235,  22,  57, 235,  22,  57, 234,  21,  56, 236,  23,  59, 237,  24,  62, 236,  23,  61, 237,  20,  61, 237,  20,  61, 238,  21,  65, 238,  21,  65, 238,  22,  63, 237,  20,  61, 235,  22,  59, 235,  22,  59;
 238,  27,  62, 237,  26,  61, 237,  24,  60, 236,  23,  59, 236,  24,  55, 236,  24,  55, 236,  24,  55, 234,  22,  52, 237,  22,  53, 237,  22,  53, 237,  21,  57, 237,  21,  57, 237,  21,  59, 237,  21,  59, 237,  20,  61, 237,  20,  61, 236,  22,  63, 236,  22,  63, 236,  22,  63, 235,  21,  61;
 237,  26,  63, 236,  25,  62, 237,  24,  62, 237,  24,  62, 238,  25,  63, 237,  24,  62, 237,  21,  57, 238,  22,  59, 237,  21,  57, 237,  21,  57, 235,  20,  56, 235,  20,  56, 235,  20,  56, 237,  21,  57, 237,  21,  57, 238,  22,  59, 235,  23,  53, 236,  24,  55, 236,  24,  53, 236,  24,  53;
 236,  29,  69, 235,  28,  68, 238,  26,  70, 239,  27,  71, 240,  25,  68, 238,  24,  67, 237,  24,  64, 236,  22,  63, 237,  20,  61, 235,  19,  60, 237,  19,  58, 236,  18,  57, 237,  21,  59, 237,  21,  59, 238,  22,  59, 240,  25,  61, 237,  25,  56, 236,  24,  55, 236,  24,  53, 237,  25,  54;
 235,  28,  66, 235,  28,  66, 237,  25,  65, 240,  29,  68, 241,  27,  67, 238,  25,  65, 239,  23,  64, 238,  22,  63, 237,  19,  58, 237,  19,  58, 237,  17,  57, 237,  17,  57, 240,  20,  57, 241,  21,  59, 238,  21,  53, 239,  22,  55, 240,  26,  55, 238,  23,  53, 237,  22,  51, 237,  22,  51;
 237,  25,  69, 237,  25,  69, 239,  27,  73, 239,  27,  73, 240,  25,  70, 241,  26,  71, 239,  22,  66, 237,  20,  63, 238,  20,  61, 237,  18,  60, 236,  17,  59, 237,  18,  60, 238,  20,  59, 239,  21,  61, 238,  22,  59, 238,  22,  59, 239,  25,  54, 237,  22,  51, 237,  23,  49, 235,  22,  48;
 237,  25,  65, 238,  27,  66, 238,  27,  66, 238,  27,  66, 238,  27,  66, 237,  25,  65, 236,  22,  63, 235,  21,  61, 235,  22,  59, 235,  22,  59, 234,  18,  57, 237,  21,  59, 238,  22,  61, 238,  22,  61, 238,  22,  59, 238,  22,  59, 238,  20,  57, 238,  20,  57, 237,  20,  52, 237,  20,  52;
 236,  25,  60, 236,  25,  60, 238,  27,  62, 239,  29,  63, 237,  26,  61, 236,  25,  60, 237,  26,  61, 236,  25,  60, 236,  23,  59, 235,  22,  57, 236,  23,  59, 235,  22,  57, 237,  24,  60, 236,  23,  59, 238,  22,  59, 237,  21,  57, 237,  21,  57, 235,  20,  56, 236,  18,  55, 235,  17,  54;
 237,  25,  56, 238,  26,  57, 237,  27,  57, 237,  27,  57, 236,  26,  56, 237,  27,  57, 238,  28,  58, 237,  27,  57, 237,  27,  57, 238,  28,  58, 240,  27,  58, 237,  25,  56, 236,  23,  59, 236,  23,  59, 237,  21,  57, 237,  21,  57, 235,  20,  58, 235,  20,  58, 236,  18,  55, 236,  18,  55;
 237,  25,  54, 237,  25,  54, 237,  25,  54, 235,  23,  51, 237,  25,  56, 240,  27,  58, 237,  26,  61, 236,  25,  60, 237,  26,  63, 237,  26,  63, 238,  27,  66, 236,  24,  64, 237,  24,  64, 236,  22,  63, 237,  20,  61, 235,  19,  60, 236,  17,  59, 236,  17,  59, 236,  18,  57, 235,  16,  56;
 237,  25,  56, 237,  25,  56, 236,  23,  59, 238,  25,  61, 237,  24,  62, 238,  25,  63, 237,  24,  64, 238,  25,  65, 240,  25,  68, 238,  24,  67, 237,  23,  66, 237,  23,  66, 238,  21,  65, 238,  21,  65, 237,  20,  63, 237,  20,  63, 235,  19,  62, 235,  19,  62, 235,  19,  60, 235,  19,  60;
 237,  25,  56, 237,  25,  56, 237,  24,  60, 237,  24,  60, 237,  24,  64, 234,  20,  60, 237,  23,  66, 238,  24,  67, 238,  24,  69, 237,  23,  68, 236,  23,  70, 234,  22,  69, 236,  22,  67, 233,  18,  63, 236,  22,  65, 235,  21,  63, 236,  22,  63, 234,  20,  60, 235,  22,  59, 235,  22,  59;
 238,  27,  55, 238,  27,  55, 239,  24,  56, 241,  27,  58, 237,  24,  62, 237,  24,  62, 237,  23,  66, 238,  24,  67, 237,  24,  71, 237,  24,  71, 236,  23,  72, 234,  22,  71, 231,  19,  65, 236,  23,  70, 236,  24,  68, 236,  24,  68, 237,  24,  62, 236,  23,  61, 236,  24,  55, 238,  26,  57;
 239,  29,  63, 236,  25,  60, 236,  25,  62, 236,  25,  62, 236,  24,  64, 237,  25,  65, 234,  27,  67, 232,  26,  66, 238,  31,  74, 236,  29,  71, 234,  26,  71, 231,  24,  69, 232,  25,  70, 234,  26,  71, 236,  29,  71, 238,  31,  74, 235,  28,  68, 232,  26,  66, 237,  26,  63, 236,  25,  62;
 237,  24,  60, 237,  24,  60, 237,  26,  61, 236,  25,  60, 233,  22,  59, 234,  24,  61, 232,  26,  62, 234,  28,  63, 234,  27,  65, 234,  27,  65, 234,  26,  69, 234,  26,  69, 234,  26,  73, 234,  26,  73, 237,  29,  79, 238,  30,  80, 236,  29,  71, 234,  26,  69, 237,  26,  63, 238,  27,  64]

1 个答案:

答案 0 :(得分:1)

严格地说,Java语言规范规定字节数据类型的范围是-128-127,这就是Java将如何解释一个字节。例如,如果一个字节被提升或转换为int,Java会将第一位解释为符号并使用符号扩展名。没有语言支持可以帮助您以不同方式解释字节。 (例如,Java中没有unsigned关键字。)

话虽如此,没有什么可以阻止您将字节视为8位值并将这些位解释为无符号。请记住,你无法用别人的方法强制解释。如果方法接受一个字节,那么该方法接受介于-128和127之间的值,除非另有明确说明。

    public static int unsignedToBytes(byte b) {
        return b & 0xFF;
    }

此方法可以满足您的要求。希望这会有所帮助!!!

编辑:Stack Overflow和其他地方有很多好的答案。我建议你在发布问题之前尝试找到答案。感谢。