(Not recommended) Fill in specified region of interest (ROI) polygon in grayscale image
roifill is not recommended. Use
regionfill instead. If you want to define the polygon interactively with
regionfill, then use
roipoly or with
drawpolygon followed by
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.
can be used, for example, to erase objects in an image.
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
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.
Fill Region in Grayscale Image
Read and display a grayscale image.
I = imread('eight.tif'); imshow(I)
Specify the x- and y-coordinates of a polygon that completely surrounds one of the coins in the image.
x = [222 272 300 270 221 194]; y = [21 21 75 121 121 75];
Fill the polygon by using the
J = regionfill(I,x,y);
Display the filled image.
imshow(J) title('Filled Image with One Fewer Coin')
I — Grayscale image
Grayscale image, specified as a numeric matrix.
mask — Mask
numeric matrix | logical 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
2-element numeric vector
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
2-element numeric vector
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 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.|
Introduced before R2006a