How to change the values of HSV channels of a pixel using OpenCV4Android?

asked 2016-01-12 01:54:46 -0600

Solace gravatar image

updated 2016-01-12 03:01:33 -0600

The first of the following two images, rgbaMat.bmp, is a 5px*5px .bmp image in RGBA format. This image was read from sdcard using Highgui.imread and then converted to HSV using Imgproc.cvtColor(rgbaMat, hsvMat, Imgproc.COLOR_BGR2HSV); resulting in hsvMat.bmp (the second image):

enter image description here enter image description here

The following are the same images enlarged to 100px*100px in MS Paint, for viewing purposes.

enter image description here enter image description here

Then after executing the following code, the image I got changedMat.bmp is given as follows, followed by its enlarged version.

enter image description here

enter image description here

The problem is that what I expected when writing the code was that the alternate pixels should have white (H=0, S=0, V=255) and black (``) hues respectively, as can be checked in the HSV section on this website. But what I am getting is red and black.

The question is why? Where am I going wrong?

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
}

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


EDIT:

The Log.i statements on Line#11 and Line#28 print out the following output, which is good.

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 ...
(more)
edit retag flag offensive close merge delete

Comments

1

I guess the problem is that when writing a Mat, imwrite expects it to be in BGR order. Try to back-convert it before saving it (cvtColor + CV_HSV2BGR)

LorenaGdL gravatar imageLorenaGdL ( 2016-01-12 02:38:41 -0600 )edit

@LorenaGdL Thank you. Please see Edit # 2 in the question. This result is weirderrr.

Solace gravatar imageSolace ( 2016-01-12 03:03:14 -0600 )edit

@LorenaGdL I just converted the color of each pixel to Android Color and logged out the values. The result was alternate pixels having -1 and -16777216, which stand for white and black. It means all is good in the hood, except that I don't know what is OpenCV doing when writing the image to sdcard (the last imwrite).

Solace gravatar imageSolace ( 2016-01-12 04:57:29 -0600 )edit