poly2mask
Convert region of interest (ROI) polygon to region mask
Description
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 on the ROI
boundary, see Classify Pixels That Are Partially Enclosed by ROI.
Examples
Input Arguments
Output Arguments
Tips
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