Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

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 Mid-green and Mid-Blue hues respectively, as shown in this OpenCV HSV color wheel in this post and the first answer. But what I am getting is lighter and darker tones of yellow.

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]=60;
                pixelChannels[1]=255;
                pixelChannels[2]=255;
            } else {
                pixelChannels[0]=120;
                pixelChannels[1]=255;
                pixelChannels[2]=255;
            }

            hsvMat.put(firstCoordinate, secondCoordinate, pixelChannels);

            counter++;
        }
    }
    Highgui.imwrite("/mnt/sdcard/DCIM/matChanged.bmp", hsvMat);//check
}

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

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 hereenter image description here

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 Mid-greenwhite (H=0, S=0, V=255) and Mid-Blueblack (``) hues respectively, as shown can be checked in the HSV section on this OpenCV HSV color wheel in this post and the first answerwebsite. But what I am getting is lighter red and darker tones of yellow.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]=60;
                pixelChannels[1]=255;
pixelChannels[0]=0;
                pixelChannels[1]=0;
                pixelChannels[2]=255;
            } else {
                pixelChannels[0]=120;
                pixelChannels[1]=255;
                pixelChannels[2]=255;
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.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]

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

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.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]

EDIT # 2:

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

This resulted in

image description

image description