How to determine if a pixel is inside a contour?

1.2k Views Asked by At

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.

1

There are 1 best solutions below

2
On BEST ANSWER

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.