Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert bgr to hsv in opencv, C++

Tags:

c++

opencv

hsv

I'm trying to have a webcam take a picture of someone's face in BGR, convert the picture into HSV, and analyze these HSV values that will later be used in a skin detection algorithm. Unfortunately, the picture seems to be analyzed in BGR, even after I try to convert it using cvtColor().

I use the code below to test whether or not I'm using the right color space. Note the part where I try to set saturation and value to 0:

Mat faceROI = findFace(first); //basic Mat, region of interest for face (code not included)

Mat temp;
faceROI.convertTo(temp, CV_8UC3); //making sure this has right no. of channels and such

CvScalar s;

IplImage face_ipl = temp; //new header
IplImage* aNew = cvCreateImage(cvGetSize(&face_ipl), face_ipl.depth, 3);

cvCvtColor(&face_ipl, aNew, CV_BGR2HSV);

for(int x = 0; x < faceROI.cols; x++){
    for (int y = 0; y < faceROI.rows; y++){
        s = cvGet2D(aNew, x, y);

        //vvvvvvvvvvv
        s.val[1] = 0; //should be saturation
        s.val[2] = 0; //should be value
        //^^^^^^^^^^^

        cvSet2D(aNew, x, y, s);
    }
}

Mat again(aNew); //<--- is this where something is set back to BGR?
imshow("white", again);

cvReleaseImage(&aNew);

This produces a completely blue picture of my face, so it seems likes I'm editing the G and R channels of a BGR image, instead of the S and V channels of an HSV image. (I'd post the image, but this is my first post so I don't have enough reputation yet.)

Does anybody know why this is happening? Any and all thoughts are appreciated.

like image 712
Connor Avatar asked Jun 17 '13 20:06

Connor


1 Answers

You're mixing up the C++ Mat style with the old C IplImage*, this makes it confusing to see what exactly is going on. Here is the code to turn inputImage into HSV:

Mat fullImageHSV;
cvtColor(inputImage, fullImageHSV, CV_BGR2HSV);

Be aware that the OpenCV HSV values are H from 0-180 while S and V are from 0-255 while other programs tend to use different values. ALso note that OpenCV is unable to show HSV images normally, this distorts the color because they are being interpreted as RGB.

like image 132
diip_thomas Avatar answered Oct 16 '22 16:10

diip_thomas