Image Processing Toolbox User's Guide Previous page   Next Page
poly2mask

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

Create a mask using random points.

See Also

roipoly

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:

  1. Divide each pixel (unit square) into a 5-by-5 grid. See Dividing Pixels into a 5-by-5 Subpixel Grid for an illustration.
  2. Adjust the position of the vertices to be on the intersections of the subpixel grid. See Adjusting the Vertices to the Subpixel Grid for an illustration.
  3. Draw a path from each adjusted vertex to the next, following the edges of the subpixel grid. See Drawing a Path Between the Adjusted Vertices for an illustration
  4. Determine which border pixels are inside the polygon using this rule: if a pixel's central subpixel is inside the boundaries defined by the path between adjusted vertices, the pixel is considered inside the polygon. See Determing Which Pixels Are in the Region for an illustration

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.


Previous page  pixval psf2otf Next page

© 1994-2005 The MathWorks, Inc.