Ask Your Question
0

How to determine the average BGR values of all the pixels in a closed contour?

asked 2017-02-11 16:02:11 -0600

Hstuart gravatar image

I'm a complete newbie to OpenCV, and trying to distinguish between some brightly-colored targets, and false positives. In both cases, I'm looking at a shape which is recognized as a closed contour, but since the disparity between the color and surrounding area is FAR more dramatic on the target than the false positives, I'm planning on using that to cull the false positives ( I know that this is probably far from the most effective way to do this, but it's simple enough for my beginner skills to grasp most of the idea )

My biggest issue is I'm not sure how I can parse this all the pixels inclosed within the contour so I can determine the average color value, and see if it's a target or not. Does anyone have any extremely newbie-friendly ideas as to how this could be accomplished? Thanks a ton

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-02-11 16:30:19 -0600

Tetragramm gravatar image

I think the easiest is to create a mask for each contour, and then use the mean function to get the average color inside it.

If you create an empty one channel image the same size as your input, you can use drawContours to fill the area of the contour with 255. That is the mask to use with the mean function. Do this for each candidate, and there you go.

edit flag offensive delete link more

Comments

you should take care of contour hierarchy

LBerger gravatar imageLBerger ( 2017-02-11 20:16:24 -0600 )edit

LBerger - how would contour hierarchy affect this? Sorry I'm very very new to OpenCV

Hstuart gravatar imageHstuart ( 2017-02-13 08:39:29 -0600 )edit

see this example contour 3 have got 2 contours inside 0c and 0b

LBerger gravatar imageLBerger ( 2017-02-13 08:48:43 -0600 )edit

LBerger - how do I use each contour as a new mask? Could you give me a quick example of the mask and mean function do you think? I'm struggling to use them properly

alwilcox gravatar imagealwilcox ( 2018-03-12 08:34:32 -0600 )edit

@alwilcox may be you should ask a new question

LBerger gravatar imageLBerger ( 2018-03-12 12:03:37 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2017-02-11 16:02:11 -0600

Seen: 4,618 times

Last updated: Feb 11 '17