0

I want to convert the yuv_min and yuv_max values to hsv_min and hsv_max. My code is as follows

yuv_thresh = [97, 43, 148, 188, 81, 167] // [y_min, u_min, v_min, y_max, u_max, v_max]

cv::Mat rgb_min;
cv::Mat hsv_min;
cv::Mat yuv_min(1,1,CV_8UC3,cv::Scalar(yuv_thresh[0],yuv_thresh[1],yuv_thresh[2]));

cvtColor(yuv_min, rgb_min, cv::COLOR_YUV2BGR);
cvtColor(rgb_min,rgb_min,cv::COLOR_BGR2RGB);
cvtColor(rgb_min, hsv_min, CV_BGR2HSV);
cv::Vec3b hsv_min_vec = hsv_min.at<cv::Vec3b>(0,0);

for (int i = 0; i < 3; i++)
{
  cout<< "hsv_min: "<< i << " = " <<(float)hsv_min_vec.val[i] << endl;
}

cv::Mat rgb_max;
cv::Mat hsv_max;
cv::Mat yuv_max(1,1,CV_8UC3,cv::Scalar(yuv_thresh[3],yuv_thresh[4],yuv_thresh[5]));

cvtColor(yuv_max, rgb_max, cv::COLOR_YUV2BGR);
cvtColor(rgb_max,rgb_max,cv::COLOR_BGR2RGB);
cvtColor(rgb_max, hsv_max, CV_BGR2HSV);
cv::Vec3b hsv_max_vec = hsv_max.at<cv::Vec3b>(0,0);

for (int i = 0; i < 3; i++)
{
  cout<< "hsv_max: "<< i << " = " <<(float)hsv_max_vec.val[i] << endl;
}

The output I get is

hsv_min: 0 = 30

hsv_min: 1 = 255

hsv_min: 2 = 120

hsv_max: 0 = 20

hsv_max: 1 = 154

hsv_max: 2 = 232

It can be seen that hsv_min1 > hsv_max1.

Am I doing something wrong in my code?

Note: If in case anyone is thinking that why am I converting converting BGR2RGB in line 9 and and line 23, please refer this link

Community
  • 1
  • 1
vacky
  • 277
  • 1
  • 4
  • 16
  • 1
    You are converting BGR values to RGB values but then you use BGR2HSV instead of RGB2HSV. – Axel B. Mar 28 '17 at 06:56
  • You have a 2 step algorithm. Have you verified the middle step (YUV -> BGR) before posting? – Michał Gacka Mar 28 '17 at 06:58
  • @m3h0w yes I have verified the middle step the BGR range is correct. Is there a direct way of converting YUV to HSV? – vacky Mar 28 '17 at 14:57
  • @AxelB. Please check the link that I have mentioned at the end. Due to the bug in opencv 2.4 im converting BGR2RGB actually to get BGR value. – vacky Mar 28 '17 at 15:00
  • i'm not sure this is your problem but try adding `_FULL` to the `cvtColor` colorspace parameter, like `BGR2HSV_FULL`. it's almost not documented at all but it gives you different result. look [here](http://stackoverflow.com/questions/11504272/calculate-the-minimum-and-maximum-in-cvinranges) for example. – user2999345 Mar 28 '17 at 16:28

0 Answers0