Function File: BW = poly2mask (x,y,m,n)
Convert a polygon to a region mask.
BW=poly2mask(x,y,m,n) converts a polygon, specified by a list of vertices in x and y and returns in a m-by-n logical mask BW the filled polygon. Region inside the polygon is set to 1, values outside the shape are set to 0.
x and y should always represent a closed polygon, first and last points should be coincident. If they are not poly2mask will close it for you. If x or y are fractional they are nearest integer.
If all the polygon or part of it falls outside the masking area (1:m,1:n), it is discarded or clipped.
This function uses scan-line polygon filling algorithm as described in http://www.cs.rit.edu/~icss571/filling/ with some minor modifications: capability of clipping and scan order, which can affect the results of the algorithm (algorithm is described not to reach ymax, xmax border when filling to avoid enlarging shapes). In this function we scan the image backwards (we begin at ymax and end at ymin), and we don't reach ymin, xmin, which we believe should be compatible with MATLAB.
The following code
s = [0:pi/4:2*pi]; x = cos (s) * 90 + 101; y = sin (s) * 90 + 101; bw = poly2mask(x, y, 200, 200); imshow (bw);
Produces the following figure
Figure 1 |
---|
The following code
s = [0:2*pi/5:pi*4]; s = s ([1, 3, 5, 2, 4, 6]); x = cos (s) * 90 + 101; y = sin (s) * 90 + 101; bw = poly2mask (x, y, 200, 200); imshow (bw);
Produces the following figure
Figure 1 |
---|
Package: image