私は画像の一部を取り、その画像の平均RGBを計算するプログラムに取り組んでいます。私はそれを計算するとき、私は組み込みの関数を使用するかのようにまったく異なる値を得ます。私が自分の数値をテストしてRGBカラーチャートに入れると、機能は非常に正確ですが、オフになります。組み込み関数を使用する際の問題は、文字通り機能がないことです。私は計算したものを印刷しますが、そのデータは使用できません。 link to function called dump()自己計算されたRGB値が実際のRGB値と一致しない
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]
値は正しいです。あなたが認識していないのは、符号付きバイトを使用しているということです。つまり、127より上の赤の値がすべて負であることを意味します。 240は-15等になります。そして、あなたは絶対的にそれらを平均します。それはおそらくあなたが望むものではありません。符号なしと平均に変換します。単純なデバッグセッションでは、値の変化をすぐに表示できます。 –
どうすれば符号なしバイトに変換できますか? @SamKuhmonen – cuber
私は自分のコンバージョンに何をしますか? @SamKuhmonen – cuber