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):
The following are the same images enlarged to 100px*100px
in MS Paint, for viewing purposes.
Then after executing the following code, the image I got changedMat.bmp
is given as follows, followed by its enlarged version.
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