# getting area points inside contour

Hello,

is it possible not only to get the area inside a contour but also the corresponding points of the area?

EDIT -- Answer to my original problem (see below):

def getContourStat(contour,image):
cv2.drawContours(mask, [contour], -1, 255, -1)
return mean, stddev

Even if I dont get the points itself, I get the statistics I need. Nevertheless I think it's quite inefficient for many contours.

edit retag close merge delete

Do you refer to the pixels within the area?

( 2014-10-30 08:40:34 -0600 )edit

Yes. cv2.contourArea() only delivers the size of the area, i.e. number of pixels, but not the pixels itself. If I want to do some processing of the area, I need the pixels with its positions, colors or graylevels.

( 2014-10-30 10:21:38 -0600 )edit

Then do the thing petititi proposes in his answer. Depending on your speed, you could implement some kind of BFS/DFS to find the points in your contour (starting from its center) so that you don't have to check all pixels in the new image.

( 2014-10-30 13:47:34 -0600 )edit

I'm working with Python, so speed is not available for me and I need to use pure OpenCV and numpy implementations.

( 2014-10-30 16:34:27 -0600 )edit

Sort by ยป oldest newest most voted

You can get the pixels inside the shape using drawcontours: you will create a black image of the same size of your image, then draw the shape on it (be careful to use thickness=CV_FILLED) in white. You will then be able to detect if pixel (x,y) is inside the shape or not...

You can also use pointpolygontest to test points, but the performances are probably worse than the previous version...

more

Well, this seems to be slightly complicated. In detail: I need to calculate the mean and stdev of the gray values inside one contour for dozens of contours. Marking the points in a mask via drawContours and then getting the points in the image for my calculation (how?) and then deleting the mask and stating with the next may be quite inefficient. Could you add a code snippet for me (I'm rather new to OpenCV)?

( 2014-10-30 16:31:14 -0600 )edit

I found a solution now for my original Problem and added it to my question, since I'm not allowed (as new user) to answer my own question.

( 2014-10-31 04:37:06 -0600 )edit
1

Why haven't you posted the right question from the beginning? I think it's much better to really ask what you want to know instead of asking for one step of your current approach as you prevent others from suggesting a better approach.

( 2014-10-31 04:56:51 -0600 )edit

It should be like this: thickness=cv2.FILLED

( 2020-12-09 21:18:17 -0600 )edit

Official site

GitHub

Wiki

Documentation

## Stats

Asked: 2014-10-30 08:17:20 -0600

Seen: 18,849 times

Last updated: Oct 31 '14