You are here : matlabImage Processingpoly2mask

poly2mask() - Image Processing

BW = poly2mask(x, y, m, n) computes
a binary region of interest (ROI) mask, BW, from
an ROI polygon, represented by the vectors x and y.
 The size of BW is m-by-n.
 poly2mask sets pixels in BW that
are inside the polygon (X,Y) to 1 and
sets pixels outside the polygon to 0. poly2mask closes the polygon automatically
if it isn't already closed. Note on Rectangular PolygonsWhen the input polygon goes through the middle of a pixel, sometimes
the pixel is determined to be inside the polygon and sometimes it
is determined to be outside (see Algorithm for details).  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     0In 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


Syntax

BW = poly2mask(x, y, m, n)


Example

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


Output / Return Value


Limitations


Alternatives / See Also


Reference