Main Content

roipoly

Specify polygonal region of interest (ROI)

Description

Create Polygon Interactively

BW = roipoly creates an interactive polygon tool associated with the image displayed in the current figure. roipoly returns the mask as a binary image, setting pixels inside the ROI to 1 and pixels outside the ROI to 0.

With this syntax and the other interactive syntaxes, the polygon selection tool blocks the MATLAB® command line until you complete the operation. For more information about using the polygon selection tool, see Interactive Behavior.

BW = roipoly(I) displays the grayscale or RGB image I in a figure window and creates an interactive polygon selection tool associated with the image.

Create Polygon by Specifying Vertices

BW = roipoly(I,xi,yi) specifies the (x, y) coordinates of polygon vertices as (xi, yi).

example

BW = roipoly(xref,yref,I,xi,yi) specifies the coordinates of polygon vertices in the world coordinate system defined by xref and yref. The polygon vertices have (x, y) coordinates xi and yi in this coordinate system.

Specify Additional Output Options

[BW,xi2,yi2] = roipoly(___) also returns the coordinates of the vertices of the closed polygon, xi2 and yi2. You can use the input arguments of any other syntax.

[xrefout,yrefout,BW,xi2,yi2] = roipoly(___) also returns the image limits in xrefout and yrefout.

roipoly(___) without output arguments displays the resulting mask image in a new figure window.

Examples

collapse all

Read an image into the workspace.

I = imread('eight.tif');

Define the vertices of the mask polygon.

c = [222 272 300 270 221 194];
r = [21 21 75 121 121 75];

Create the binary mask image.

BW = roipoly(I,c,r);

Display the original image and the polygonal mask.

imshow(I)

Figure contains an axes object. The hidden axes object contains an object of type image.

figure
imshow(BW)

Figure contains an axes object. The hidden axes object contains an object of type image.

Input Arguments

collapse all

Grayscale or RGB image, specified as an m-by-n numeric matrix or an m-by-n-by-3 numeric array, respectively.

x-coordinate of polygon vertices, specified as a numeric vector of the same length as yi. If you specify image limits in a world coordinate system using xref, then xi is in this coordinate system. Otherwise, xi is in the default coordinate system.

y-coordinate of polygon vertices, specified as a numeric vector of the same length as xi. If you specify image limits in a world coordinate system using yref, then yi is in this coordinate system. Otherwise, yi is in the default coordinate system.

Image limits in world coordinates along the x-dimension, specified as a 2-element numeric vector of the form [xmin xmax]. The value of xref sets the image XData.

Image limits in world coordinates along the y-dimension, specified as a 2-element numeric vector of the form [ymin ymax]. The value of yref sets the image YData.

Output Arguments

collapse all

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

Data Types: logical

x-coordinate of vertices of the closed polygon, returned as a numeric vector of the same length as yi2. The first and last element in the vector are identical, so that the polygon is closed. If you specify image limits in a world coordinate system using xref, then xi2 is in this coordinate system. Otherwise, xi2 is in the default coordinate system.

y-coordinate of vertices of the closed polygon, returned as a numeric vector of the same length as xi2. The first and last element in the vector are identical, so that the polygon is closed. If you specify image limits in a world coordinate system using yref, then yi2 is in this coordinate system. Otherwise, yi2 is in the default coordinate system.

Image limits in world coordinates along the x-dimension, returned as a 2-element numeric vector of the form [xmin xmax]. If you specify image limits in a world coordinate system using xref, then xrefout is equal to xref. Otherwise, xrefout is equal to the original image XData.

Image limits in world coordinates along the y-dimension, returned as a 2-element numeric vector of the form [ymin ymax]. If you specify image limits in a world coordinate system using yref, then yrefout is equal to yref. Otherwise, yrefout is equal to the original image YData.

More About

collapse all

Tips

  • roipoly always produces a closed polygon. If you specify input vertex positions of a closed polygon (such that the last pair of coordinates is identical to the first pair), then the length of the output coordinate vectors is equal to the number of points specified. If the points specified do not describe a closed polygon, then roipoly adds a final point having the same coordinates as the first point. In this case the length of the output coordinate vectors is one greater than the number of points specified.

  • For more information about classifying pixels on the ROI boundary, see Classify Pixels That Are Partially Enclosed by ROI.

  • For any of the roipoly syntaxes, you can replace the input image I with two arguments, m and n, that specify the row and column dimensions of an arbitrary image. For example, these commands create a 100-by-200 binary mask.

    c = [112 112 79 79];
    r = [37 66 66 37];
    BW = roipoly(100,200,c,r);

    If you specify m and n with an interactive form of roipoly, an m-by-n black image is displayed. Use the mouse to specify a polygon within this image.

Extended Capabilities

expand all

Version History

Introduced before R2006a

expand all