Ask Your Question
0

getting area points inside contour

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

MichaelHecht gravatar image

updated 2014-10-31 04:43:09 -0600

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):
  mask = np.zeros(image.shape,dtype="uint8")
  cv2.drawContours(mask, [contour], -1, 255, -1)
  mean,stddev = cv2.meanStdDev(image,mask=mask)
  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 flag offensive close merge delete

Comments

Do you refer to the pixels within the area?

FooBar gravatar imageFooBar ( 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.

MichaelHecht gravatar imageMichaelHecht ( 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.

FooBar gravatar imageFooBar ( 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.

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

1 answer

Sort by ยป oldest newest most voted
1

answered 2014-10-30 13:31:15 -0600

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

edit flag offensive delete link more

Comments

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

MichaelHecht gravatar imageMichaelHecht ( 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.

MichaelHecht gravatar imageMichaelHecht ( 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.

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

It should be like this: thickness=cv2.FILLED

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

Question Tools

Stats

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

Seen: 20,255 times

Last updated: Oct 31 '14