Ask Your Question

Revision history [back]

Use mask in opencv to detect color similarity

image description

cv::Mat3b bgr = cv::imread("red_test.png");
cv::Mat3b hsv;
cvtColor(bgr, hsv, cv::COLOR_BGR2HSV);    
cv::Mat1b mask1, mask2;
inRange(hsv, cv::Scalar(0, 70, 50), cv::Scalar(10, 255, 255), mask1);
inRange(hsv, cv::Scalar(170, 70, 50), cv::Scalar(180, 255, 255), mask2);    
cv::Mat1b mask = mask1 + mask2;

In order to detect the red heart in the image, the code above when applied is applied which provides with 2 masked images namely 'mask1' and 'mask2'. Then I combine masks generated for both the red color range by doing an OR operation pixel-wise. The following output is generated.

image description

What I need to know is: Is it possible to use the output image to detect red color in other sample images? (ignore the heart shape, it's only the color I'm interested in).