insertObjectMask - Insert masks in image or video stream - MATLAB (original) (raw)

Insert masks in image or video stream

Since R2020b

Syntax

Description

[RGB](#mw%5F9a0e8e27-20b7-4301-b9a2-a7ed4ad42f82) = insertObjectMask([I](#mw%5F505a447e-6010-439e-b301-43783ea99b5d),[BW](#mw%5F037c6894-f6bf-4bdc-b6a8-d0ffbea1c3b6)) inserts a mask BW into the specified image I and returns the result as a truecolor image RGB.

example

[RGB](#mw%5F9a0e8e27-20b7-4301-b9a2-a7ed4ad42f82) = insertObjectMask([I](#mw%5F505a447e-6010-439e-b301-43783ea99b5d),[maskstack](#mw%5F707ceb40-ded6-48e5-89e1-09ea4b6f7e98)) inserts a set of masks maskstack into the specified imageI and returns the result as a truecolor imageRGB.

example

[RGB](#mw%5F9a0e8e27-20b7-4301-b9a2-a7ed4ad42f82) = insertObjectMask(___,[Name=Value](#namevaluepairarguments)) specifies options using one or more name-value arguments in addition to any combination of arguments from previous syntaxes. For example, (MaskColor="red") sets the color of the mask to red.

Examples

collapse all

Read an image into the workspace.

I = imread("visionteam1.jpg");

Load a stack of binary masks into the workspace.

load("visionteam1Maskstack.mat")

Insert the masks with white outlines into the image.

RGB = insertObjectMask(I,maskstack,LineColor="white",LineWidth=2);

Display the image with the masks inserted.

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

Read an image into the workspace.

I = imread("visionteam1.jpg");

Load a stack of binary mask images.

load("visionteam1Maskstack.mat");

Insert the masks into the image and specify a unique color for each mask.

numMasks = size(maskstack,3); RGB = insertObjectMask(I,maskstack,MaskColor=lines(numMasks));

Display the image with the inserted masks.

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

Input Arguments

collapse all

Input image, specified as a truecolor (RGB) image,_m_-by-_n_-by-3 array, or a grayscale image,_m_-by-n array.

Data Types: single | double | int16 | uint8 | uint16

Input mask image, specified as an _m_-by-n logical matrix.

Data Types: logical

Stack of mask images, specified as an_m_-by-_n_-by-P logical array, where P is the total number of masks in the stack.

maskstack has the same width and height asI.

Data Types: logical

Name-Value Arguments

collapse all

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: LineColor="white" set the inserted line color to white.

Mask color, specified as a character vector, cell array of character vectors, vector, or _M_-by-3 matrix of RGB values.

An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range [0,1], for example, [0.4 0.6 0.7]. When using a data type of uint8, which is defined within the range of [0,255], conversions are necessary to ensure that the expected range is met. For example, to specify the previous vector, you must convert it by using uint8(255*[0.4 0.6 0.7]).

You can specify a different color for each mask or one color for all masks. To specify one color for all masks, set MaskColor to a color string or an [R G _B_] vector.

Specification Format Example
Specify one color for all shapes (or markers) Short color name or color name "r""red"
RGB triplet [1 0 0]1-by-3 grid, with columns labeled r,g,b respectively.
Specify a color for each shape (or marker) Vector of color names ["red","yellow","blue"]
Three-column matrix of RGB triplets [1 0 0 0 1 1 1 0 1 1 1 1]M-by-3 grid, with columns labeled r,g,b respectively.

Supported colors are listed in the table.

Color Name Short Name RGB Triplet Appearance
"red" "r" [1 0 0] Sample of the color red
"green" "g" [0 1 0] Sample of the color green
"blue" "b" [0 0 1] Sample of the color blue
"cyan" "c" [0 1 1] Sample of the color cyan
"magenta" "m" [1 0 1] Sample of the color magenta
"yellow" "y" [1 1 0] Sample of the color yellow
"black" "k" [0 0 0] Sample of the color black
"white" "w" [1 1 1] Sample of the color white

Color values must be specified in the range of the data type support that you use to specify them. For example, if you specify an input of double datatype, then values must be in the range [0,1]. If you specify an input of uint8 datatype, then values must be in the range[0 255]. For example, [0,0,1] anduint8([0,0,255]) are two formats for the same color blue.

Data Types: logical | uint8 | uint16 | int16 | double | single

Opacity of mask, specified as a scalar value in the range [0 1]. The value1 makes the mask completely opaque and the value0 makes the mask completely transparent.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Color of mask borders, specified as one of these values:

Data Types: single | double | int16 | uint8 | uint16 | logical | char | string

Opacity of the mask borders, specified as a scalar value in the range of [0 1].

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Width of mask borders, specified as a positive scalar in pixels.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Output Arguments

collapse all

Output image, returned as a truecolor image of same datatype as the input image, with the same _m_-by-n dimensions asI.

Tips

Extended Capabilities

expand all

Usage notes and limitations:

Version History

Introduced in R2020b

expand all

Starting in R2023b, you can specify color with RGB values in the range [0,1].