Finding all the points that lie inside a polygon

Hello, I have separated some portion of a rectangular image which is now a polygon. I would like to consider only the pixels that lie inside the polygon. Could anyone suggest how that can be implemented? Thanks.

Where would you like to consider that? On the CPU? Or on the GPU (e.g. in a shader)?

If the latter, tessellate the polygon, rasterize the component triangles, and process the covered pixels in those triangles.

If the former, do something similar, but you need to implement a triangle rasterizer to generate pixel spans.

Thanks for the reply, but if I have a lot of boundary points, then I shall have too many triangles. In image processing, there is a function bwconvexhull, which can separate the region, but I don’t know it’s underlying algorithm, whether it is similar to triangulation or not.

Why too many triangles? If convex hull gives you your polygon, triangulation is easy (since it’s convex) – pick 2 adjacent verts and then iteratively select one of the remaining verts for a 3rd. The GPU is fast, so don’t worry about it.

On the flip side, if you have your region in pixel form rather than vector form (implied by your mention of bwconvexhull), then you could transfer it to the stencil buffer or alpha channel to “mask off” pixel ops so they only take affect within your region.

[QUOTE=jenny_wui;1252733]I have separated some portion of a rectangular image which is now a polygon. I would like to consider only the pixels that lie inside the polygon. Could anyone suggest how that can be implemented?[/QUOTE] It’s an interesting problem. Can give more specifics on the ‘polygon’ you are working with. Is it defined by vertices with known x, y coordinates? Do you know the ordering of these vertices? Or - is it just a blob of contiguous pixels? If it is a blob, seems to me it would be more difficult. How would you even know which pixels are polygon vertices, polygon edges, or internal? A convex hull routine would only identify some of the vertices. What do you mean by ‘consider’ the inside pixels. A picture of the polygon you are starting with would be helpful.

Thanks for the reply. I think I need to clarify a bit. Suppose my convex hull consists of 30 boundary points. i know the x,y coordinates of all boundary points of convex hull. I am trying to extract all the pixels (may be of different intensity value) inside the convex hull. Now the triangles may be too thin, pixels may be duplicated when I shall try to process triangles containing the pixels inside it. and I may need to process 28 triangles.

[QUOTE=jenny_wui;1252810]Thanks for the reply. I think I need to clarify a bit. Suppose my convex hull consists of 30 boundary points. i know the x,y coordinates of all boundary points of convex hull. I am trying to extract all the pixels (may be of different intensity value) inside the convex hull. Now the triangles may be too thin, pixels may be duplicated when I shall try to process triangles containing the pixels inside it. and I may need to process 28 triangles.[/QUOTE]I didn’t know the polys you’re working with are convex. That makes things simpler. Are you SURE this is the case? Now, all you have to do is loop thru all the pixels in the image (converting to x,y), and test to see if it’s in the hull. That’s really not too complicated. I’m sure you can find several ways to do that on the web.