Ask Your Question

Revision history [back]

Unable to convert YUV threshold limits to HSV

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_min[1] > hsv_max[1].

Am I doing something wrong in my code?