1. Convert RGB image to HSV for better colour identification.
As a side note, in OpenCV H has values from 0 to 180, S and V from 0 to 255. The red color, in OpenCV, has the hue values approximately in the range of 0 to 10 and 160 to 180.
In HSV space:
red primary at 0°
green primary at 120° (60 - opencv)
blue primary at 240° (120 - opencv)
yellow (30 - opencv)
// Convert input image to HSV
cv::Mat hsv_image;
cv::cvtColor(bgr_image, hsv_image, cv::COLOR_BGR2HSV);
2. Use
cv::inRange function to filter out colour
// Threshold the HSV image, keep only the red pixels
cv::Mat lower_red_hue_range;
cv::Mat upper_red_hue_range;
cv::inRange(hsv_image, cv::Scalar(0, 100, 100), cv::Scalar(10, 255, 255), lower_red_hue_range);
cv::inRange(hsv_image, cv::Scalar(160, 100, 100), cv::Scalar(179, 255, 255), upper_red_hue_range);
3. Use cv::addWeighted function to filter out colour
cv::Mat red_hue_image;
cv::addWeighted(lower_red_hue_range, 1.0, upper_red_hue_range, 1.0, 0.0, red_hue_image);
Reference:
[1] Detect red circles in an image using OpenCV
excellent job ! can i get range for green and blue colors for the same code ?
ReplyDeleteYes, I think so, just modify in threshold for inRange function.
DeleteThis comment has been removed by the author.
Delete