Main Content

Fill Region of Interest in an Image

This example shows how to use regionfill to fill a region of interest (ROI) in an image. The example uses the roipoly function to define the region of interest interactively with the mouse. regionfill smoothly interpolates inward into the region from the pixel values on the boundary of the polygon. You can use this function for image editing, including removal of extraneous details or artifacts. The filling process replaces values in the region with values that blend with the background.

Read and display an image of four coins on a table.

I = imread('eight.tif');
imshow(I)

Create a mask image to specify the ROI that you want to fill. Use the roipoly function to specify the region interactively. Call roipoly and move the pointer over the image. The pointer shape changes to cross hairs . Define the ROI by clicking the mouse to specify the vertices of a polygon. You can use the mouse to adjust the size and position of the ROI.

mask = roipoly(I);

Blue polygon ROI with several vertices that enclose one coin.

Double-click to finish defining the region. roipoly creates a binary image with the region filled with 1-valued pixels.

Display the mask image.

figure
imshow(mask)

Binary mask image that is white inside the ROI defined by the polygon and black outside the ROI.

Fill the region, using regionfill, specifying the image to be filled and the mask image as inputs. Display the result. Note the image contains one less coin.

J = regionfill(I,mask);
figure
imshow(J)

Three coins remain in the image. The area within the ROI is approximately the same color and texture as the background.

See Also

| | |