2

I have following picture

enter image description here

which I extract the contour from. The contour looks like following

enter image description here

I am wondering, for any pixel in the image, how do I determine if it is inside the contour or not? Say "inside" here means any pixel in the body.

If possible, I'd prefer simple, direct, and elegant algorithm. Thanks.

  • This question belongs to https://www.stackoverflow.com – Deepesh Meena Sep 01 '18 at 19:30
  • 1
    No it has nothing to do with coding. Someone telling me how to do it with OpenCV is not what I am after. I need algorithm explanation. – Nick X Tsui Sep 01 '18 at 19:58
  • You could use floodfill to determine all inner pixels at once. A pixel is inside if its floodfill region does not touch the boundary of the picture. Maybe you can even tweak the contour algorithm to also output the inner pixels. Or do you really just want to check this for a single pixel? – M. Winter Sep 01 '18 at 20:53

1 Answers1

3

Assuming that the contour doesn’t have any loops, shoot a ray in some convenient direction from the pixel to the edge of the image and count the number of times that it crosses the contour: an odd number of crossings means that it’s inside, an even number, outside. You’ll need to be a bit careful about how you count when the ray coincides with the contour for a while—there might not be an actual crossing in that case.

amd
  • 53,693