Main Content

Create Binary Mask Using an ROI Function

R2026b

This example shows how to create a binary mask using one of the ROI creation functions, such as drawcircle, with the mask creation function createMask. Image Processing Toolbox™ provides multiple types of ROI objects for different use cases, including axes-based ROIs for imshow displays and interactive and static annotations for imageshow and volshow displays. To learn more, see Choose Approach to Annotate ROIs in Images.

Read an image into the workspace and display it.

img = imread("pout.tif");
h_im = imshow(img);

Figure contains an axes object. The hidden axes object contains an object of type image.

Create an ROI on the image using one of the ROI creation functions.

circ = drawcircle(Center=[113,66],Radius=60);

Figure contains an axes object. The hidden axes object contains 2 objects of type image, images.roi.circle.

Create a binary mask from the ROI using createMask. The createMask function returns a binary image the same size as the input image. The pixels inside the ROI are set to 1 and the pixel values everywhere else are set to 0.

BW = createMask(circ);
imshow(BW)

Figure contains an axes object. The hidden axes object contains an object of type image.

See Also

Topics