Image Processing Toolbox User's Guide |
Convert region polygon to region mask
Syntax
Purpose
BW = poly2mask(x,y,m,n)
computes a binary region-of-interest mask BW
from a region-of-interest polygon represented by the vectors x
and y
. The size of BW
is m
-by-n
. Pixels in BW
that are inside the polygon (x,y)
are set to 1
; pixels outside the polygon are set to 0
(zero). The class of BW
is logical
.
poly2mask
closes the polygon automatically if it isn't already closed.
Example
x = [63 186 54 190 63]; y = [60 60 209 204 60]; bw = poly2mask(x,y,256,256); imshow(bw) hold on plot(x,y,'b','LineWidth',2) hold off
Create a mask using random points.
x = 256*rand(1,4); y = 256*rand(1,4); x(end+1) = x(1); y(end+1) = y(1); bw = poly2mask(x,y,256,256); imshow(bw) hold on plot(x,y,'b','LineWidth',2) hold off
See Also
Algorithm
When creating a region-of-interest mask, poly2mask
must determine which pixels are included in the region. This determination can be difficult when pixels on the edge of a region are only partially covered by the border line. The following figure illustrates a triangular region of interest, examining in close-up one of the vertices of the ROI. The figure shows how pixels can be partially covered by the border of a region-of-interest.
Pixels on the Edge of an ROI Are Only Partially Covered by Border
To determine which pixels are in the region, poly2mask
uses the following algorithm:
Dividing Pixels into a 5-by-5 Subpixel Grid
The following figure shows the pixel that contains the vertex of the ROI shown previously with this 5-by-5 subpixel grid.
Adjusting the Vertices to the Subpixel Grid
poly2mask
adjusts each vertex of the polygon so that the vertex lies on the subpixel grid. Note how poly2mask
rounds up x and y coordinates to find the nearest grid corner. This creates a second, modified polygon, slightly smaller, in this case, than the original ROI. A portion is shown in the following figure.
Drawing a Path Between the Adjusted Vertices
poly2mask
forms a path from each adjusted vertex to the next, following the edges of the subpixel grid. In the following figure, a portion of this modified polygon is shown by the thick dark lines.
Determing Which Pixels Are in the Region
poly2mask
uses the following rule to determine which border pixels are inside the polygon: if the pixel's central subpixel is inside the modified polygon, the pixel is inside the region.
In the following figure, the central subpixels of pixels on the ROI border are shaded a dark gray color. Pixels inside the polygon are shaded a lighter gray. Note that the pixel containing the vertex is not part of the ROI because its center pixel is not inside the modified polygon.
pixval | psf2otf |
© 1994-2005 The MathWorks, Inc.