Main Content

createMask

Create binary mask image from ROI

Description

bw = createMask(ROI) returns a binary mask image with pixels inside the ROI set to true and pixels outside the ROI set to false. The mask is the same size as the image associated with the ROI.

example

bw = createMask(ROI,m,n) returns a binary mask image that is size [m,n].

bw = createMask(ROI,I) returns a binary mask image that is the size of the image I.

bw = createMask(ROI,hImage) returns a binary mask image that is the size of the Image object hImage.

Examples

collapse all

Read an image into the workspace and display it.

I = imread("pears.png");
imshow(I)

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

Draw an elliptical ROI on the image, using the Center name-value argument to specify the location of the ellipse and theSemiAxes name-value argument to specify the shape of the ellipse. Specify that the edge of the ellipse is a striped line.

h = drawellipse(Center=[447 204],SemiAxes=[78 72], ...
    RotationAngle=287,StripeColor="m");

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

Get a binary mask from the ROI. Pixels inside the ROI are true and pixels outside the ROI are false. Display the mask.

mask = createMask(h);
imshow(mask)

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

Read an image into the workspace and display it by using the imageshow function.

I = imread("peppers.png");
imObj = imageshow(I);

Get a handle to the viewer window that contains the image.

viewer = imObj.Parent;

Draw a freehand ROI around the onion.

freehand = uidraw(viewer,"freehand");

Create a binary mask from the ROI. Pixels inside the ROI are true and pixels outside the ROI are false. Display the mask.

mask = createMask(freehand);
imageshow(mask)

Input Arguments

collapse all

Region of interest, specified as an ROI object associated with an imshow or imageshow display.

Row dimension of the mask image, specified as a positive integer.

Column dimension of the mask image, specified as a positive integer.

Input image array, specified as a numeric array.

Input image object, specified as an Image object created by the imshow or imageshow function. For more details, see Image Properties for imshow displays or Image Properties for imageshow displays. If hImage corresponds to a multilevel blocked image, the finest resolution level determines the size of the mask image.

Output Arguments

collapse all

Binary mask image, returned as a logical array.

Data Types: logical

Tips

  • For more information about classifying pixels on the ROI boundary, see Classify Pixels That Are Partially Enclosed by ROI.

  • If you specify a mask size that does not match the size of the image associated with the ROI object, then createMask crops or zero-pads the mask to the specified size. For imshow displays, the image associated with the ROI object is ROI.Parent.Children(2,1).CData. For imageshow displays, the image associated with the ROI object is ROI.Parent.Children.

Version History

Introduced in R2018b

expand all