Fill in specified region of interest (ROI) polygon in grayscale image
roifill function to fill in a specified region of
interest (ROI) polygon in a grayscale image.
interpolates inward from the pixel values on the boundary of the polygon by solving
Laplace's equation. The boundary pixels are not modified.
be used, for example, to erase objects in an image.
J = roifill
J = roifill(I)
J = roifill(I,mask)
J = roifill(I,xi,yi)
J = roifill(x,y,I,xi,yi)
[J,BW] = roifill(___)
[x2,y2,J,BW,xi2,yi2] = roifill(___)
creates an interactive
polygon selection tool associated with the image displayed in the current
figure. With this syntax and the other interactive syntaxes, the polygon
selection tool blocks the MATLAB® command line until you complete the operation.
J = roifill
roifill fills the selected polygon and returns the filled
For more information about using the polygon selection tool to define and fill ROIs, see Interactive Behavior.
roifill() without an output argument displays the filled
image in a new figure window.
This example uses
roifill to fill a region in the input image,
I. For more examples, especially of the interactive syntaxes,
see Fill Region of Interest in an Image.
I = imread('eight.tif'); c = [222 272 300 270 221 194]; r = [21 21 75 121 121 75]; J = roifill(I,c,r); imshow(I) figure imshow(J)
I— Grayscale image
Grayscale image, specified as a numeric matrix.
Mask, specified as a numeric or logical matrix of the same size as the
yi— x- or y-coordinate of vertices
y— Image extent in world coordinates
Image extent in world X-Y coordinates, specified as a 2-element numeric
vector of the form
[min max]. The two elements of
x give the x-coordinates
(horizontal) of the first and last columns of image
respectively. The two elements of
y give the
y-coordinates (vertical) of the first and last rows
J— Filled image
Filled image, returned as a numeric matrix.
BW— Binary image
Binary image, returned as a logical matrix of the same size as the input
yi2— x- or y-coordinate of vertices
x- or y-coordinate of polygon
vertices, specified as numeric vectors.
yi are empty if you specify the polygon ROI using the
y2— Image extent in world coordinates
When you call
roifill with an interactive
syntax, a polygon selection tool opens that enables you to select and adjust polygon
vertices interactively using the mouse.
When the polygon tool is active, the pointer changes to cross hairs when you move the pointer over the image in the figure. Using the mouse, specify the region by selecting vertices of the polygon. You can move or resize the polygon using the mouse. When you are finished positioning and sizing the polygon, fill the polygon by double-clicking, or by right-clicking inside the region and selecting Fill Area from the context menu.
The following figure illustrates a polygon defined by multiple vertices.
|Closing the polygon. (Completing the region-of-interest.)|
Use any of the following mechanisms:
|Deleting the polygon|
Press Backspace, Escape or Delete, or right-click inside the region and select Cancel from the context menu.
Note: If you delete the ROI, the function returns empty values.
|Moving the polygon||Move the pointer inside the region. The pointer changes to a fleur . Click and drag the mouse to move the polygon.|
|Changing the color of the polygon||Move the pointer inside the region. Right-click and select Set color from the context menu.|
|Adding a new vertex.||Move the pointer over an edge of the polygon and press the A key. The shape of the pointer changes . Click the left mouse button to create a new vertex at that position on the line.|
|Moving a vertex. (Reshaping the region-of-interest.)||Move the pointer over a vertex. The pointer changes to a circle . Click and drag the vertex to its new position.|
|Deleting a vertex.||Move the pointer over a vertex. The pointer changes to a circle . Right-click and select Delete Vertex from the context menu. This action deletes the vertex and adjusts the shape of the polygon, drawing a new straight line between the two vertices that were neighbors of the deleted vertex.|
|Retrieving the coordinates of the vertices||Move the pointer inside the region. Right-click and select Copy position from the context menu to copy the current position to the Clipboard. Position is an n-by-2 array containing the x- and y-coordinates of each vertex, where n is the number of vertices you selected.|