Convert region of interest (ROI) polygon to region mask
computes a binary region of interest (ROI) mask, BW
= poly2mask(xi
,yi
,m
,n
)BW
, of size
m
-by-n
, from an ROI polygon with
vertices at coordinates xi
and yi
. If the
polygon is not already closed, then poly2mask
closes the
polygon automatically.
The poly2mask
function sets pixels that are inside the polygon
to 1
and sets pixels outside the polygon to 0
.
For more information about classifying pixels that are partially enclosed by the
ROI, see Algorithm.
To specify a polygon that includes a given rectangular set of pixels, make the edges of the polygon lie along the outside edges of the bounding pixels, instead of the center of the pixels.
For example, to include pixels in columns 4 through 10 and rows 4 through 10, you might specify the polygon vertices like this:
x = [4 10 10 4 4]; y = [4 4 10 10 4]; mask = poly2mask(x,y,12,12)
mask = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
In this example, the polygon goes through the center of the bounding pixels, with the result that only some of the desired bounding pixels are determined to be inside the polygon (the pixels in row 4 and column 4 and not in the polygon). To include these elements in the polygon, use fractional values to specify the outside edge of the 4th row (3.5) and the 10th row (10.5), and the outside edge of the 4th column (3.5) and the outside edge of the 10th column (10.5) as vertices, as in the following example:
x = [3.5 10.5 10.5 3.5 3.5]; y = [3.5 3.5 10.5 10.5 3.5]; mask = poly2mask(x,y,12,12)
mask = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
When creating a region of interest (ROI) 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:
Divide each pixel into a 5-by-5 subpixel grid.
The figure shows the pixel that contains the vertex of the ROI shown previously with this 5-by-5 subpixel grid.
Adjust the position of the vertices.
poly2mask
moves each vertex of the polygon to the nearest
intersection of the subpixel grid. Note how poly2mask
rounds
x and y coordinates to the nearest
subpixel grid corner. This creates a second, modified polygon. The figure shows
the modified vertex with a red "X".
Draw a path between adjusted vertices.
poly2mask
forms a path from each adjusted vertex to the
next, following the edges of the subpixel grid. The figure shows a portion of
this modified polygon by the thick dark lines.
Determine which border pixels are inside the polygon.
poly2mask
uses the following rule to determine which border
pixels are inside the polygon: if the pixel's central subpixel is inside the
boundaries defined by the path between adjusted vertices, then 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.