Main Content

Specify ROI as Binary Mask

A binary mask defines a region of interest (ROI) of an image. Mask pixel values of 1 indicate image pixels that belong to the ROI. Mask pixel values of 0 indicate image pixels that are part of the background.

Depending on the application, an ROI can consist of contiguous or discontiguous groups of pixels. A contiguous region is a single group of connected pixels. A contiguous ROI could represent a single object in an image, such as one car in an image of a street scene, or the body tissue in a medical image. For example, a discontiguous ROI could represent all pixels corresponding to water in an aerial photograph, or all tumorous cells in a medical image.

Image Processing Toolbox™ supports many options to create a binary mask. Here are some common approaches, although this selection is not exhaustive.

Create Mask Using Thresholding

A common way to create a mask from an image is to classify each pixel based on the intensity value of the pixel. For example, pixels in a region of interest can appear brighter than the background or have a different color than the background. Various functions and apps enable you to apply thresholding to grayscale, indexed, and color images.

OperationDescriptionSample Output (Input Image, Binary Mask, and Masked ROI)
Single threshold

The ROI consists of grayscale pixels whose intensity is above (or below) a specified threshold. You can create a binary mask representing the ROI using mathematical operations or functions such as imbinarize.

For an example, see Correct Nonuniform Illumination and Analyze Foreground Objects.

Grayscale image of a flower and binary mask using the imbinarize function. Bright pixels such as the flower petals belong to the ROI.

Range of intensity or index valuesThe ROI consists of grayscale pixels whose intensity is within a range of values. You can create a binary mask representing the ROI using mathematical operations or by using the roicolor function. This function also supports indexed images, in which case the mask specifies pixels with specific index values corresponding to colors in a colormap.

Grayscale image of a flower and binary mask using the roicolor function. Pixels with midrange intensity values, such as the leaves, belong to the ROI.

Range of color valuesThe ROI consists of the pixels in a color image whose color channels are within a range of values. You can create a binary mask from an RGB image using the Color Thresholder app. This app enables you to interactively select the range of values for the three color channels based on different color spaces.

Color Thresholder app with interactive controls for selecting ranges of color values in the RGB color space using sliders and a 3-D color cloud.

Grayscale flood fill

The ROI consists of connected pixels of similar intensity value. You specify a seed point and tolerance. Perform a flood fill operation of a grayscale image using the grayconnected function or the Image Segmenter app. Perform a flood fill operation on a color image using the Image Segmenter app.

Flood fill mask showing all pixels connected to a seed pixel in the center of a bright petal.

Color flood fill

The ROI consists of connected pixels of similar color value. You specify a seed point and tolerance. Perform a flood fill operation on a color image using the Image Segmenter app.

Image Segmenter app showing a preview of the flood fill region over the original image, with interactive controls for the seed point and tolerance.

Create Mask Based on Position

You can specify an ROI based on the position of pixels in an image. For example, you can define an ROI consisting of pixels within an ROI shape that you draw, or within a rectangular patch whose location is specified using array indexing.

OperationDescriptionSample Output (Input Image, Binary Mask, and Masked ROI)
Create geometrical or freehand shape using ROI objects

The ROI consists of all pixels whose position is within a geometrical or a hand drawn shape. First, you create an ROI object, then you create a binary mask using the createMask function. You can draw shapes interactively. You can also create shape programmatically from known positional constraints, such as vertices for polygons, waypoints for freehands, and radius and center for circles. For more information, see Create ROI Shapes.

For an example, see Create Binary Mask Using an ROI Function.

Binary mask created using an AssistedFreehand object following the boundaries of an ROI

Create polygonal shape using polygon tool

The ROI consists of all pixels within a polygonal shape. You can draw the shape interactively in the polygon tool by using the roipoly function. This function returns a binary mask directly.

Binary mask created by drawing a polygon around the ROI

Create mask from polygon coordinates

The ROI consists of all pixels whose position is within a polygon. You can create this binary mask by specifying the vertices of the polygon using the roipoly function, or by specifying the vertices and the target size of the mask using the poly2mask function. poly2mask does not require an input image.

Binary mask created by specifying the (x,y) coordinates of six vertices surrounding the ROI

Create Mask Using Automated and Semi-Automated Segmentation Algorithms

Some image segmentation algorithms predict an ROI based on features in the image and a coarse estimate of the location of the ROI. For example, the active contours technique iteratively refines a mask that you provide towards object boundaries. A benefit of automated and semi-automated segmentation algorithms is that they can detect intricate ROI boundaries with high fidelity given an imprecise initial location estimate.

The Image Segmenter and Volume Segmenter apps enable automated and semi-automated segmentation techniques. You can also segment images into ROI and background using a variety of functions in the toolbox. For more information about available segmentation techniques, see Image Segmentation.

OperationDescriptionSample Output (Input Image, Binary Mask, and Masked ROI)
Graph cutThe graph cut algorithm estimates an ROI using iterative graph-based segmentation. You specify and refine the ROI using seed pixels for both the ROI and the background. Graph cut functionality is enabled by the grabcut function and the Image Segmenter app.

Binary mask using graph cut. Pixels with colors similar to selected seed pixels, such as petals and leaves, belong to the ROI.

Lazy snappingThe lazy snapping algorithm estimates an ROI using graph-based segmentation. You specify an initial mask or pixel coordinates for both the ROI and the background. Lazy snapping functionality is enabled by the lazysnapping function.

Binary mask created by specifying one rectangular foreground mask and one rectangular background mask.

Active contoursThe active contours (snakes) algorithm estimates an ROI using a region growing technique. You specify an initial mask around the object boundaries. Active contour functionality is enabled by the activecontour function and the Image Segmenter app.

Initial mask and intermediate states of the evolution towards the ROI boundary

See Also

| |

Related Topics