Use mask in opencv to detect color similarity

asked 2019-09-24 02:23:14 -0500

daqs gravatar image

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).

edit retag flag offensive close merge delete

Comments

1
LBerger gravatar imageLBerger ( 2019-09-24 02:37:28 -0500 )edit

Thanks for your reply LBerger. Could you please give an example of the arguments I need to put in while executing? Thanks

daqs gravatar imagedaqs ( 2019-09-24 08:24:49 -0500 )edit

Nothing it's only a loop to fix a threshold

LBerger gravatar imageLBerger ( 2019-09-24 08:30:06 -0500 )edit

I don't think I understand. So, I can't add arguments in your template to find matching color images? I thought that's why you linked me the template. Masks, inRange don't seem to help one bit.

daqs gravatar imagedaqs ( 2019-09-24 08:33:42 -0500 )edit

thanks for your help

daqs gravatar imagedaqs ( 2019-09-24 08:34:04 -0500 )edit

Have you complied and run sample? There is two parts when you try matchTemplate using correlation (grayscale or color):

  1. cross corellation template image
  2. select roiwith a thresh >n % It means roi where template is found in image
LBerger gravatar imageLBerger ( 2019-09-24 08:36:35 -0500 )edit