Ask Your Question

I am trying to detect red color using inRange keyword. But I cannot check it inside If statement. If red color comes then print It is red color

asked 2019-04-14 00:36:12 -0500

Abdusoli gravatar image
  1. Mat1b mask1, mask2;
  2. inRange(hsv, Scalar(173, 70, 50), Scalar(179, 255, 255), mask1);
  3. imshow("RedDetect", mask1);
edit retag flag offensive close merge delete


sturkmen gravatar imagesturkmen ( 2019-04-14 03:59:01 -0500 )edit

But how can I check inside If statement. For example If color is red then call some function

Abdusoli gravatar imageAbdusoli ( 2019-04-14 04:41:16 -0500 )edit

Also, notice that the H of red color in HSV color space may range from 0~10 and 170~180, you might have to consider these two conditions.

gino0717 gravatar imagegino0717 ( 2019-04-17 00:40:58 -0500 )edit

Thank you for help. I will take into account

Abdusoli gravatar imageAbdusoli ( 2019-04-18 03:16:21 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2019-04-15 04:47:35 -0500

inRange(hsv, Scalar(173, 70, 50), Scalar(179, 255, 255), mask1) returns a mask with white pixels giving you the location of the pixels that are within this range. You now have to make logic that has some sort of boolean return value.

Now do int number_pixels = countNonZero(mask1) and then threshold that value. For example, if more than 40 pixels respond, this is not noise and we execute the function.

edit flag offensive delete link more


Sorry, I am new to OpenCV. Should I threshold number_pixels? How can threshold it?

Abdusoli gravatar imageAbdusoli ( 2019-04-15 08:18:12 -0500 )edit

By basically creating an if condition. So for example: if( number_pixels > 50 ){ execute_your_function(); }

StevenPuttemans gravatar imageStevenPuttemans ( 2019-04-16 02:44:13 -0500 )edit

Thank you bro for your help. I have another question appeared. How can I detect only red circle shapes. my car stops when red color comes regardless of shapes, including triangle or any shape. How can I detect shape and color together. Bro please help me

Abdusoli gravatar imageAbdusoli ( 2019-04-18 01:58:32 -0500 )edit

Question Tools

1 follower


Asked: 2019-04-14 00:36:12 -0500

Seen: 148 times

Last updated: Apr 15 '19