Ask Your Question
2

How to get OpenCV to store HSV in Matlab notation [0-1]?

asked 2016-01-08 05:25:21 -0500

zvonimirb gravatar image

updated 2016-01-08 05:59:43 -0500

I am working on transferring a working Matlab project to C++/OpenCV, but I'm encountering a problem while converting from BGR to HSV, since the project is intended to work with small numbers that Matlab uses to store HSV values.

If i just do a normalization to the HSV matrix (Hue/179, Saturation/255, Value/255) I will lose a part of data that could be important in the image recognition later on.

Is there a better way to convert from BGR - HSV without losing part of the data? Has anyone encountered this problem? What is the most efficient way solve to it?

Thanks

Zvonimir

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
3

answered 2016-01-08 06:02:40 -0500

updated 2016-01-11 12:14:48 -0500

You can simply convert your Image to CV_32FC3 which ranges from 0.0-1.0. Here is the sample usage!

Mat mBGR_8=imread("test.bmp",1);//Range- 0 to 255
Mat mBGR_32;
mBGR_8.convertTo(mBGR_32,CV_32FC3,1.0/255.0);//BGR Range- 0.0 to 1.0

Mat mHsv_32;
cvtColor(mBGR_32, mHsv_32,COLOR_BGR2HSV);

32-Bit HSV Color Space Output Hue Range: 0 ≤ 360 Saturation Range: 0 ≤ 1.0 Value Range: 0 ≤ 1.0

vector<Mat> mHSV;
split(mInput_Hsv32,mHSV);
mHSV[0]*=(1.0/360.0);
merge(mHSV,mInput_Hsv32);

We need to scale the output!

edit flag offensive delete link more

Comments

3

^^ if you want [0..1] BGR range, it also needs a 1./255 scale factor in convertTo()

berak gravatar imageberak ( 2016-01-08 06:13:39 -0500 )edit

Thank you @berak I missed that!

Balaji R gravatar imageBalaji R ( 2016-01-08 06:19:08 -0500 )edit
3

hehe, still not right, you need a double as denominator, not an int, just try: cerr << (int(1)/255) << endl; (sorry, should have highlited that better above)

berak gravatar imageberak ( 2016-01-08 06:25:42 -0500 )edit

I don't really understand how to include the above mentioned line in my code, just copy pasting gives me errors. Could you write the whole convertTO line?

zvonimirb gravatar imagezvonimirb ( 2016-01-08 06:35:34 -0500 )edit
1

@zvonimirb, it should work now as in the answer above ;)

berak gravatar imageberak ( 2016-01-08 07:06:10 -0500 )edit

@berak@Balaji R This still didn't solve my problem, as Hue still appears to be in the 0-180 range. Saturation and Value seem fine. This is my code and output.

Mat pictureBGR;

    pictureBGR = imread("picture1.jpg", CV_LOAD_IMAGE_COLOR); 

    Mat pictureBGR_32;
    pictureBGR.convertTo(pictureBGR_32, CV_32FC3, 1.0/255.0);

    Mat pictureHSV;
    cvtColor(pictureBGR_32, pictureHSV, COLOR_BGR2HSV);

    std::cout << "Hue: "<< pictureHSV.at<float>(0,0) << std::endl;
    std::cout << "Saturation: "<< pictureHSV.at<float>(0,1) << std::endl;
    std::cout << "Value:"<< pictureHSV.at<float>(0,2) << std::endl;

Output:

Hue: 30
Saturation: 0.318182
Value:0.517647

Do you have any ideas why?

zvonimirb gravatar imagezvonimirb ( 2016-01-11 03:50:31 -0500 )edit

Have you tried COLOR_BGR2HSV_FULL?

Balaji R gravatar imageBalaji R ( 2016-01-11 05:17:11 -0500 )edit

Yes, it didn't make any difference to the output.

zvonimirb gravatar imagezvonimirb ( 2016-01-11 06:04:44 -0500 )edit

The latest edit should probably work. I did it like this though:

Mat pictureBGR;

    pictureBGR = imread("slika1.jpg", CV_LOAD_IMAGE_COLOR); 

    namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
    imshow( "Display Image", pictureBGR );

    Mat pictureHSV;
    cvtColor(pictureBGR, pictureHSV, COLOR_BGR2HSV);

    int size [2] = {pictureHSV.rows, pictureHSV.cols}; 
    std::cout << size[0] << " x " << size[1] <<std::endl;

    std::vector<Mat> pictureH_S_V;
    split(pictureHSV, pictureH_S_V); //razdvajamo HSV u tri kanala, H, S i V

    pictureH_S_V[0].convertTo(pictureH_S_V[0], CV_32FC3, 1.0/179.0); 
    pictureH_S_V[1].convertTo(pictureH_S_V[1], CV_32FC3, 1.0/255.0);

Thanks.

zvonimirb gravatar imagezvonimirb ( 2016-01-12 07:57:50 -0500 )edit

Hope this answer helps! Please accept the answer if this solves your problem!

Balaji R gravatar imageBalaji R ( 2016-01-12 09:17:08 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2016-01-08 05:25:21 -0500

Seen: 503 times

Last updated: Jan 11 '16