gpt4 book ai didi

java - 自行计算的 RGB 值与实际 RGB 值不匹配

转载 作者:行者123 更新时间:2023-12-02 13:31:04 26 4
gpt4 key购买 nike

我正在开发一个程序,在该程序中我获取图像的一部分并计算该图像的平均 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);

下面打印出准确的值,但我无法获取单独的 channel 或找到平均 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]

最佳答案

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

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

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

此方法执行您所要求的操作。希望这有帮助!!!

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

关于java - 自行计算的 RGB 值与实际 RGB 值不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43176517/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com