Main Content

createMask

(Not recommended) Create mask within image

createMask is not recommended. Use the createMask object function associated with the new ROI objects instead, described in Version History.

Description

BW = createMask(h) returns a mask, or binary image, with 1s inside the ROI object h and 0s everywhere else. The input image must be contained within the same axes as the ROI object.

example

BW = createMask(h,himage) returns a mask the same size as the image himage, with 1s inside the ROI object h and 0s everywhere else. This syntax is required when the axes that contains the ROI holds more than one image.

Examples

collapse all

Create an ellipse ROI.

imshow("coins.png")
e = imellipse;

Use the mouse to reshape and reposition the ellipse. Then, create a binary mask from the ROI. Pixels inside the ROI have the value 1, and pixels outside the ROI have the value 0. Display the mask in a new figure.

BW = createMask(e);
figure
imshow(BW)

Input Arguments

collapse all

ROI object, specified as an imellipse, imline, impoint, impoly, or imrect object.

Handle to one image, specified as a handle.

Output Arguments

collapse all

Mask, returned as a binary matrix. The mask is the same size as the input image contained in the same axes as h, or the image himage.

Version History

Introduced in R2008a

collapse all

R2018b: createMask is not recommended

Starting in R2018b, a new set of ROI objects replaces the existing set of ROI objects. The new objects provide more functional capabilities, such as face color transparency. The new classes also support events that you can use to respond to changes in your ROI such as moving or being clicked. Although there are no plans to remove the old ROI objects at this time, switch to the new ROIs to take advantage of the additional capabilities and flexibility. For more information on creating ROIs using the new ROI functions, see Create ROI Shapes.

To create a binary mask image using the new ROIs, use the createMask object function associated with the new ROIs.

Update Code

Update all instances of createMask.

Discouraged UsageRecommended Replacement

This example uses the createMask method to create a binary mask image from an ROI.

imshow("cameraman.tif")
h = imrect(gca, [10 10 100 100]);
bw = createMask(h);
imshow(bw);

Here is equivalent code, creating a binary mask image using one of the new ROI objects. Call the createMask object function associated with the new ROIs as you did with the previous ROIs.

imshow("cameraman.tif")
h = drawrectangle(gca,"Position",[10 10 100 100]);
bw = createMask(h);
imshow(bw);