I am trying to convert RGB image into HSV but I get very strange results. So as a test I made an image with one solid colour:
red (64) green (46) blue (120)
According to gimp, the hsv values should be 255 62 47
Here is some code…
// coloured image
tempColorImage = cvCreateImage(cvSize(width,height), IPL_DEPTH_8U, 3);
cvSet(tempColorImage, cvScalar(64, 46, 120));
// hsv image
hsvImage = cvCreateImage(cvSize(width,height), IPL_DEPTH_8U, 3);
// convert
cvCvtColor( tempColorImage, hsvImage , CV_RGB2HSV);
// print rgb values of first pixel
int r = (int)tempColorImage->imageData[0];
int g = (int)tempColorImage->imageData[1];
int b = (int)tempColorImage->imageData[2];
printf("r %i g %i b %i\n", r,g,b);
// print hsv values of first pixel
int h = (int)hsvImage->imageData[0];
int s = (int)hsvImage->imageData[1];
int v = (int)hsvImage->imageData[2];
printf("h %i s %i v %i\n", h, s, v);
The rgb values are shown fine.
When using CV_RGB2HSV the hsv are 127 -99 120
When using CV_BGR2HSV the hsv show as -83 -99 120
the calls look right, but the negative numbers suggest that maybe you are doing something wrong with how you are printing out the values. it’s a char * pointer, not an unsigned char pointer, so printing it as an int will not get you the right values (they go betw. -127 & 127). for example, this:
((float*)(img->imageData))[i]
is not this,
(float)img->imageData[i]
where the first convert img->imageData into a pointer and grabs the i
th value stepping i amount in floats, the second takes the ith char
(-127 to 127) and converts it to a float.
I think you are not getting values you would expect because you either
have to (a) & 0xff
(http://darksleep.com/player/JavaAndUnsignedTypes.html) or (b) cast
imageData pointer to a unsigned char array before you get the pixels
out manually.
int r = (int) ((unsigned char *)tempColorImage->imageData)[0];