0

I am trying to detect range of Red color using C++ and OpenCV. I have converted my image from cv::COLOR_BGR2HSV colorspace already. Now I am trying to convert a single color from BGR to HSV colorspace using the code below:

cv::Mat3f red_lower(cv::Vec3f(54, 41, 203)); // BGR
cv::Mat3f red_upper(cv::Vec3f(123, 109, 255));// BGR
cv::Mat3f red_lower_hsv, red_upper_hsv;

LOG("red_lower: " << red_lower);
cv::cvtColor(red_lower, red_lower_hsv, cv::COLOR_BGR2HSV);
LOG("red_lower_hsv: " << red_lower_hsv);
LOG("red_upper: " << red_upper);
cv::cvtColor(red_upper, red_upper_hsv, cv::COLOR_BGR2HSV);
LOG("red_upper_hsv: " << red_upper_hsv);

// get mask
cv::inRange(image_hsv, red_lower_hsv, red_upper_hsv, OutputImage);

The output I get is:

red_lower: [54, 41, 203]
red_lower_hsv: [355.18518, 0.79802954, 203]
red_upper: [123, 109, 255]
red_upper_hsv: [354.24658, 0.57254905, 255]

The values are not correct and the image mask is not generated. I even tried with a simple image with just two red boxes.

enter image description here

Manmohan Bishnoi
  • 791
  • 13
  • 37
  • Your lower bound on hue is greater than the upper bound, same with saturation. That means there are no values that can fall in that range. – Dan Mašek Nov 30 '18 at 13:52
  • @DanMašek official documentation on https://docs.opencv.org/4.0.0/df/d9d/tutorial_py_colorspaces.html says that H should be in range of [0...179] but values returned by cvtColor() are in [0...360] range. Are we supposed to manually convert the values to fall in range or am I using the function incorrectly ? – Manmohan Bishnoi Dec 01 '18 at 06:28
  • 1
    That is true for 8bit. For floats the range is different. – Dan Mašek Dec 01 '18 at 12:27

0 Answers0