Convert region of interest (ROI) polygon to region mask

## Syntax

``BW = poly2mask(xi,yi,m,n)``

## Description

example

````BW = poly2mask(xi,yi,m,n)` computes a binary region of interest (ROI) mask, `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.```

## Examples

collapse all

Specify the x- and y-coordinates of the polygon.

```x = [63 186 54 190 63]; y = [60 60 209 204 60];```

Create the mask specifying the size of the image.

`bw = poly2mask(x,y,256,256);`

Display the mask, drawing a line around the polygon.

```imshow(bw) hold on plot(x,y,'b','LineWidth',2) hold off``` Define two sets of random points for the x- and y-coordinates.

```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);`

Display the mask and draw a line around the polygon.

```imshow(bw) hold on plot(x,y,'b','LineWidth',2) hold off``` ## Input Arguments

collapse all

x-coordinate of polygon vertices, specified as a numeric vector. The length of `xi` and `yi` must match.

Data Types: `double`

y-coordinate of polygon vertices, specified as a numeric vector. The length of `xi` and `yi` must match.

Data Types: `double`

Number of rows in the mask, specified as a nonnegative integer.

Data Types: `double`

Number of columns in the mask, specified as a nonnegative integer.

Data Types: `double`

## Output Arguments

collapse all

Binary image, returned as an `m`-by-`n` logical matrix.

Data Types: `logical`

## 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```

## Algorithms

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:

1. 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. 2. 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". 3. 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. 4. 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. 