This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Fill in specified region of interest (ROI) polygon in grayscale image

roifill is not recommended. Use regionfill instead.


J = roifill
J = roifill(I)
J = roifill(I, c, r)
J = roifill(I, BW)
[J,BW] = roifill(...)
J = roifill(x, y, I, xi, yi)
[x, y, J, BW, xi, yi] = roifill(...)


Use roifill to fill in a specified region of interest (ROI) polygon in a grayscale image. roifill smoothly interpolates inward from the pixel values on the boundary of the polygon by solving Laplace's equation. The boundary pixels are not modified. roifill can be used, for example, to erase objects in an image.

J = roifill creates an interactive polygon tool, associated with the image displayed in the current figure, called the target image. You use the mouse to define the ROI – see Interactive Behavior. When you are finished defining the ROI, fill in the area specified by the ROI by double-clicking inside the region or by right-clicking anywhere inside the region and selecting Fill Area from the context menu. roifill returns the image, J, which is the same size as I with the region filled in (see Examples).


If you do not specify an output argument, roifill displays the filled image in a new figure.

J = roifill(I) displays the image I and creates an interactive polygon tool associated with the image.

J = roifill(I, c, r) fills in the polygon specified by c and r, which are equal-length vectors containing the row-column coordinates of the pixels on vertices of the polygon. The kth vertex is the pixel (r(k),c(k)).

J = roifill(I, BW) uses BW (a binary image the same size as I) as a mask. roifill fills in the regions in I corresponding to the nonzero pixels in BW. If there are multiple regions, roifill performs the interpolation on each region independently.

[J,BW] = roifill(...) returns the binary mask used to determine which pixels in I get filled. BW is a binary image the same size as I with 1's for pixels corresponding to the interpolated region of I and 0's elsewhere.

J = roifill(x, y, I, xi, yi) uses the vectors x and y to establish a nondefault spatial coordinate system. xi and yi are equal-length vectors that specify polygon vertices as locations in this coordinate system.

[x, y, J, BW, xi, yi] = roifill(...) returns the XData and YData in x and y, the output image in J, the mask image in BW, and the polygon coordinates in xi and yi. xi and yi are empty if the roifill(I,BW) form is used.

Interactive Behavior

When you call roifill with an interactive syntax, the pointer changes to a cross hairs shape when you move it over the target image. Using the mouse, you specify a region-of-interest by selecting vertices of a polygon. You can change the size or shape of the polygon using the mouse. The following figure illustrates a polygon defined by multiple vertices. For more information about all the interactive capabilities of roifill, see the table that follows.

Interactive BehaviorDescription
Closing the polygon. (Completing the region-of-interest.)

Use any of the following mechanisms:

  • Move the pointer over the initial vertex of the polygon that you selected. The shape changes to a circle . Click either mouse button.

  • Double-click the left mouse button. This action creates a vertex at the point under the mouse and draws a straight line connecting this vertex with the initial vertex.

  • Click the right mouse button. This action draws a line connecting the last vertex selected with the initial vertex; it does not create a new vertex.

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 polygonMove 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 polygonMove 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.

Class Support

The input image I can of class uint8, uint16, int16, single, or double. The input binary mask BW can be any numeric class or logical. The output binary mask BW is always logical. The output image J is of the same class as I. All other inputs and outputs are of class double.


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);
figure, imshow(J)

See Also

| |

Introduced before R2006a

Was this topic helpful?