Documentation

Contents

insertObjectAnnotation

Annotate truecolor or grayscale image or video stream

Syntax

  • RGB = insertObjectAnnotation(I,Shape,Position,Label) example
  • RGB = insertObjectAnnotation(I,Shape,Position,Label, Name,Value) example
  • insertObjectAnnotation(I,'rectangle',Position,Label) example
  • insertObjectAnnotation(I,'circle',Position,Label) example

Description

example

RGB = insertObjectAnnotation(I,Shape,Position,Label) returns a truecolor image annotated with Shape and Label . The input image, I, can be either a truecolor or grayscale image. You can set the shape to ‘rectangle' or ‘circle', and insert it at the location specified by the matrix, Position. The input, Label, can be a numeric vector of length M. It can also be a cell array of ASCII strings of length M, In this array, M is the number of shape positions. You can also specify a single label for all shapes as a numeric scalar or string.

example

RGB = insertObjectAnnotation(I,Shape,Position,Label, Name,Value) uses additional options specified by one or more Name,Value pair arguments.

example

insertObjectAnnotation(I,'rectangle',Position,Label) inserts rectangles and corresponding labels at the location indicated by the position matrix. The Position input must be an M-by-4 matrix, where each row, M, specifies a rectangle as a four-element vector, [x y width height]. The elements x and y indicate the upper-left corner of the rectangle, and the width and height specify the size.

example

insertObjectAnnotation(I,'circle',Position,Label) inserts circles and corresponding labels at the location indicated by the position matrix. The Position input must be an M-by-3 matrix, where each row, M, specifies a circle as a three-element vector, [x y r]. The elements x and y indicate the center of the circle, and r specifies the radius.

Examples

expand all

Object Annotation with Rectangles, Numbers, and Strings

Read and display an image.

I = imread('board.tif');
figure, imshow(I);

Create labels with floating point numbers that represent detection confidence for each shape.

label_str = cell(3,1);
conf_val = [85.212 98.76 78.342];   
for ii=1:3
    label_str{ii} = ['Confidence: ' num2str(conf_val(ii),'%0.2f') '%'];
end

Set the position and size of the three rectangles in the format [x y width height].

position = [23 373 60 66;35 185 77 81;77 107 59 26];

Insert the annotation.

RGB = insertObjectAnnotation(I, 'rectangle', position, label_str,'TextBoxOpacity', 0.9, 'FontSize', 18);

Display the annotated image.

figure, imshow(RGB), title('Annotated chips');

Object Annotation with Circles and Integer Numbers

Read and display an image.

I = imread('coins.png');
figure, imshow(I);

Set up the circle annotations with labels. The first two arguments are the (x,y) coordinates for the center of the circle, the third argument is the radius.

position = [96 146 31;236 173 26];
label = [5 10];

Insert the annotation.

RGB = insertObjectAnnotation(I, 'circle', position, label,'Color', {'cyan', 'yellow'}, 'TextColor', 'black');

Display the annotated image.

figure, imshow(RGB), title('Annotated coins');

Input Arguments

expand all

I — Truecolor or grayscale imageM-by-N-by-3 truecolor | M-by-N 2-D grayscale image

Truecolor or grayscale image, specified as an image or video stream.

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

Shape — Rectangle or circle annotationrectangle' | ‘circle'

Rectangle or circle annotation, specified as a string indicating the annotation shape.

Data Types: char

Position — Location and size of the annotation shapeM-by-3 matrix | M-by-4 matrix

Location and size of the annotation shape, specified as an M-by-3 or M-by-4 matrix. When you specify a rectangle, the position input matrix must be an M-by-4 matrix. Each row, M, specifies a rectangle as a four-element vector, [x y width height]. The elements, x and y, indicate the upper-left corner of the rectangle, and the width and height specify the size.

When you specify a circle, the position input matrix must be an M-by-3 matrix, where each row, M, specifies a three-element vector [x y r]. The elements, x and y, indicate the center of the circle and r specifies the radius.

Example: position = [50 120 75 75]

A rectangle with top-left corner located at x=50, y=120, with a width and height of 75 pixels.

Example: position = [96 146 31]

A circle with center located at x=96, y=146 and a radius of 31 pixels.

Example: position = [23 373 60 66;35 185 77 81;77 107 59 26]

Location and size for three rectangles.

Label — A string label to associate with a shapenumeric scalar | numeric vector | ASCII string | cell array of ASCII strings

A string label to associate with a shape, specified as a numeric vector or a cell array. The input can be a numeric vector of length M. It can also be a cell array of ASCII strings of length M, where M is the number of shape positions. You can also specify a single numeric scalar or string label for all shapes.

Example: label = [5 10], where the function marks the first shape with the label, 5, and the second shape with the label, 10.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: 'Color', 'white' sets the color for the label text box to white.

'Color' — Color for shape and corresponding label text box'yellow' (default) | color string | [R G B] vector | cell array | M-by-3 matrix

Color for shape and for corresponding label text box. You can specify this value as the comma-separated pair consisting of 'Color' and either a string, an [R G B] vector, a cell array, or an M-by-3 matrix. To specify one color for all shapes, set this parameter to either a string or an [R G B] vector. To specify a color for each of the M shapes, set this parameter to a cell array of M strings. Alternatively, you can specify an M-by-3 matrix of RGB values for each annotation. RGB values must be in the range of the input image data type.

Supported color strings are: 'blue', 'green', 'cyan', 'red', 'magenta', 'black', and 'white'.

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

'TextColor' — Color of text in text label'black' (default) | color string | [R G B] vector | cell array | M-by-3 matrix

Color of text in text label. You can specify this value as the comma-separated pair consisting of 'TextColor' and either a string, an [R G B] vector, a cell array, or an M-by-3 matrix. To specify one color for all text, set this parameter to either a string or an [R G B] vector. To specify a color for each of the M text labels, set this parameter to a cell array of M strings. Alternatively, you can specify an M-by-3 matrix of RGB values for each annotation. RGB values must be in the range of the input image data type.

Supported color strings are: 'blue', 'green', 'cyan', 'red', 'magenta', 'yellow', and 'white'.

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

'TextBoxOpacity' — Opacity of text label box background0.6 (default) | range of [0 1]

Opacity of text label box background. You can specify this value as the comma-separated pair consisting of 'TextBoxOpacity' and a scalar defining the opacity of the background of the label text box. Specify this value in the range of 0 to 1.

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

'FontSize' — Label text font size12 (default) | integer in the range of [8 72]

Label text font size, specified as the comma-separated pair consisting of 'FontSize' and an integer corresponding to points in the range of [8 72].

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

Output Arguments

expand all

RGB — Truecolor or grayscale image with annotationM-by-N-by-3 truecolor | M-by-N 2-D grayscale image

Truecolor or grayscale image with annotation, returned as an image or video stream.

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

Was this topic helpful?