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

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 close merge delete

Sort by » oldest newest most voted

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!

more

3

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

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

Thank you @berak I missed that!

( 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)

( 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?

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

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

( 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;

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?

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

Have you tried COLOR_BGR2HSV_FULL?

( 2016-01-11 05:17:11 -0500 )edit

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

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

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

Mat pictureBGR;

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.

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

( 2016-01-12 09:17:08 -0500 )edit

Official site

GitHub

Wiki

Documentation