尝试更改像素的 HSV 颜色,结果得到意想不到的颜色。为什么?

Trying to change HSV color of a pixel, and getting unexpected color as a result. Why?

以下两张图片中的第一张 rgbaMat.bmp5px*5px .bmp 格式的 RGBA 图片。此图像是使用 Highgui.imread 从 sdcard 读取的,然后使用 Imgproc.cvtColor(rgbaMat, hsvMat, Imgproc.COLOR_BGR2HSV); 转换为 HSV 导致 hsvMat.bmp (第二张图像):

以下是在 MS Paint 中放大到 100px*100px 的相同图像,以供查看。

然后执行下面的代码后,得到的图片changedMat.bmp如下,后面是放大图

问题是我在编写代码时期望的是交替像素应该有 white (H=0, S=0, V=255) 和 black (``) 色调,可以检查 in the HSV section on this website。但是我得到的是红色和黑色。

问题是为什么?我哪里错了?

public void doProcessing(View view) {
    Mat rgbaMat = Highgui.imread("/mnt/sdcard/DCIM/rgbaMat.bmp");

    Mat hsvMat = new Mat();
    Imgproc.cvtColor(rgbaMat, hsvMat, Imgproc.COLOR_BGR2HSV);
    Highgui.imwrite("/mnt/sdcard/DCIM/hsvMat.bmp", hsvMat);//check

    int counter=1;
    for (int firstCoordinate = 0; firstCoordinate<hsvMat.rows(); firstCoordinate++) {
        for (int secondCoordinate = 0; secondCoordinate<hsvMat.cols(); secondCoordinate++) {
            Log.i(TAG, "HAPPY " + Arrays.toString(hsvMat.get(firstCoordinate, secondCoordinate)));//check

            double[] pixelChannels = hsvMat.get(firstCoordinate, secondCoordinate);

            if (counter%2 != 0) {
                pixelChannels[0]=0;
                pixelChannels[1]=0;
                pixelChannels[2]=255;
            } else {
                pixelChannels[0]=0;
                pixelChannels[1]=0;
                pixelChannels[2]=0;
            }

            hsvMat.put(firstCoordinate, secondCoordinate, pixelChannels);

            counter++;
            Log.i(TAG, "HAPPY PAPPY" + Arrays.toString(hsvMat.get(firstCoordinate, secondCoordinate)));//check
        }
    }
    Highgui.imwrite("/mnt/sdcard/DCIM/matChanged.bmp", hsvMat);//check
}

................................................ ..................................................... ..................................................... .....................


编辑:

第 11 行和第 28 行的 Log.i 语句打印出以下输出,很好

01-12 13:46:44.445: I/MainActivity(29690): HAPPY [0.0, 0.0, 164.0]
01-12 13:46:44.445: I/MainActivity(29690): HAPPY PAPPY[0.0, 0.0, 255.0]
01-12 13:46:44.445: I/MainActivity(29690): HAPPY [29.0, 252.0, 255.0]
01-12 13:46:44.445: I/MainActivity(29690): HAPPY PAPPY[0.0, 0.0, 0.0]
01-12 13:46:44.445: I/MainActivity(29690): HAPPY [0.0, 0.0, 164.0]
01-12 13:46:44.446: I/MainActivity(29690): HAPPY PAPPY[0.0, 0.0, 255.0]
01-12 13:46:44.446: I/MainActivity(29690): HAPPY [29.0, 252.0, 255.0]
01-12 13:46:44.446: I/MainActivity(29690): HAPPY PAPPY[0.0, 0.0, 0.0]
01-12 13:46:44.446: I/MainActivity(29690): HAPPY [0.0, 0.0, 164.0]
01-12 13:46:44.446: I/MainActivity(29690): HAPPY PAPPY[0.0, 0.0, 255.0]
01-12 13:46:44.446: I/MainActivity(29690): HAPPY [29.0, 252.0, 255.0]
01-12 13:46:44.447: I/MainActivity(29690): HAPPY PAPPY[0.0, 0.0, 0.0]
01-12 13:46:44.447: I/MainActivity(29690): HAPPY [0.0, 0.0, 164.0]
01-12 13:46:44.447: I/MainActivity(29690): HAPPY PAPPY[0.0, 0.0, 255.0]
01-12 13:46:44.447: I/MainActivity(29690): HAPPY [29.0, 252.0, 255.0]
01-12 13:46:44.447: I/MainActivity(29690): HAPPY PAPPY[0.0, 0.0, 0.0]
01-12 13:46:44.448: I/MainActivity(29690): HAPPY [0.0, 0.0, 164.0]
01-12 13:46:44.448: I/MainActivity(29690): HAPPY PAPPY[0.0, 0.0, 255.0]
01-12 13:46:44.448: I/MainActivity(29690): HAPPY [29.0, 252.0, 255.0]
01-12 13:46:44.448: I/MainActivity(29690): HAPPY PAPPY[0.0, 0.0, 0.0]
01-12 13:46:44.448: I/MainActivity(29690): HAPPY [0.0, 0.0, 164.0]
01-12 13:46:44.448: I/MainActivity(29690): HAPPY PAPPY[0.0, 0.0, 255.0]
01-12 13:46:44.448: I/MainActivity(29690): HAPPY [29.0, 252.0, 255.0]
01-12 13:46:44.448: I/MainActivity(29690): HAPPY PAPPY[0.0, 0.0, 0.0]
01-12 13:46:44.448: I/MainActivity(29690): HAPPY [0.0, 0.0, 164.0]
01-12 13:46:44.448: I/MainActivity(29690): HAPPY PAPPY[0.0, 0.0, 255.0]
01-12 13:46:44.449: I/MainActivity(29690): HAPPY [29.0, 252.0, 255.0]
01-12 13:46:44.449: I/MainActivity(29690): HAPPY PAPPY[0.0, 0.0, 0.0]
01-12 13:46:44.449: I/MainActivity(29690): HAPPY [0.0, 0.0, 164.0]
01-12 13:46:44.450: I/MainActivity(29690): HAPPY PAPPY[0.0, 0.0, 255.0]
01-12 13:46:44.450: I/MainActivity(29690): HAPPY [29.0, 252.0, 255.0]
01-12 13:46:44.450: I/MainActivity(29690): HAPPY PAPPY[0.0, 0.0, 0.0]
01-12 13:46:44.450: I/MainActivity(29690): HAPPY [0.0, 0.0, 164.0]
01-12 13:46:44.450: I/MainActivity(29690): HAPPY PAPPY[0.0, 0.0, 255.0]
01-12 13:46:44.450: I/MainActivity(29690): HAPPY [29.0, 252.0, 255.0]
01-12 13:46:44.451: I/MainActivity(29690): HAPPY PAPPY[0.0, 0.0, 0.0]
01-12 13:46:44.451: I/MainActivity(29690): HAPPY [0.0, 0.0, 164.0]
01-12 13:46:44.451: I/MainActivity(29690): HAPPY PAPPY[0.0, 0.0, 255.0]
01-12 13:46:44.451: I/MainActivity(29690): HAPPY [29.0, 252.0, 255.0]
01-12 13:46:44.451: I/MainActivity(29690): HAPPY PAPPY[0.0, 0.0, 0.0]
01-12 13:46:44.451: I/MainActivity(29690): HAPPY [0.0, 0.0, 164.0]
01-12 13:46:44.452: I/MainActivity(29690): HAPPY PAPPY[0.0, 0.0, 255.0]
01-12 13:46:44.452: I/MainActivity(29690): HAPPY [29.0, 252.0, 255.0]
01-12 13:46:44.452: I/MainActivity(29690): HAPPY PAPPY[0.0, 0.0, 0.0]
01-12 13:46:44.452: I/MainActivity(29690): HAPPY [0.0, 0.0, 164.0]
01-12 13:46:44.452: I/MainActivity(29690): HAPPY PAPPY[0.0, 0.0, 255.0]
01-12 13:46:44.452: I/MainActivity(29690): HAPPY [29.0, 252.0, 255.0]
01-12 13:46:44.452: I/MainActivity(29690): HAPPY PAPPY[0.0, 0.0, 0.0]
01-12 13:46:44.452: I/MainActivity(29690): HAPPY [0.0, 0.0, 164.0]
01-12 13:46:44.452: I/MainActivity(29690): HAPPY PAPPY[0.0, 0.0, 255.0]

为了检查,我将像素的 HSV 转换为 Android Color 并使用

记录了 Android 颜色的值
//CONVERTING HSV COLOR TO ANDROID COLOR
//Step 1: Convert double[] to float[]. Step 2: Scale the float[]. STep 3: Use Android's HSVToColor method
float[] channelsFloatArrayScaled = new float[3];
for (int i = 0; i < hsvMat.get(firstCoordinate, secondCoordinate).length; i++) {
    if (i == 0) {
        channelsFloatArrayScaled[i] = ((float) hsvMat.get(firstCoordinate, secondCoordinate)[i]) * 2;// TODO Wrap an ArrayIndexOutOfBoundsException wrapper
    } else if (i == 1 || i == 2) {
        channelsFloatArrayScaled[i] = ((float) hsvMat.get(firstCoordinate, secondCoordinate)[i]) / 255;// TODO Wrap an ArrayIndexOutOfBoundsException wrapper
    }
}
int androidColor = Color.HSVToColor(channelsFloatArrayScaled);
Log.i(TAG, "HAPPY PAPPY FAPPY" + androidColor);//check

结果是交替显示 -1-1777216,分别代表白色和黑色。

所以一切都很好,除了我不知道 OpenCV 在将图像写入 sdcard 时在做什么(最后 imwrite)。