insertText

Insert text in image or video

Syntax

  • RGB = insertText(I,position,textstring) example
  • RGB = insertText(I,position,numericvalue) example
  • RGB = insertText(___,Name,Value) example

Description

example

RGB = insertText(I,position,textstring) returns a truecolor image with text strings inserted. The input image, I, can be either a truecolor or grayscale image. The text strings are drawn by overwriting pixel values.

example

RGB = insertText(I,position,numericvalue) returns a truecolor image with numeric values inserted.

example

RGB = insertText(___,Name,Value) uses additional options specified by one or more Name,Value pair arguments.

Examples

expand all

Insert Numeric Values on an Image

Read the image.

I = imread('peppers.png');

Define position for the text.

position =  [1 50; 100 50]; % [x y]
value = [555 pi];

Insert text.

RGB = insertText(I, position, value, 'AnchorPoint', 'LeftBottom'); 

Display the image.

figure, imshow(RGB), title('Numeric values');

Insert Numbers and Strings on an Image

Read the image.

I = imread('board.tif');

Create texts that contain fractions.

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

Define positions and colors.

   position = [23 373; 35 185; 77 107]; % [x y]
   box_color = {'red','green','yellow'};

Insert text.

   RGB = insertText(I, position, text_str, 'FontSize', 18, 'BoxColor', box_color, 'BoxOpacity', 0.4);

Display the image.

figure, imshow(RGB), title('Board');

Input Arguments

expand all

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

Input image, specified in truecolor or 2-D grayscale.

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

textstring — Text stringASCII text string | cell array of ASCII text strings

Text string, specified as a single ASCII text string or a cell array of ASCII strings. The length of the cell array must equal the number of rows in the position matrix. If a single text string is provided, it is used for all positions.

Data Types: char

numericvalue — Numeric value textscalar | vector

Numeric value text, specified as a scalar or a vector. If a scalar value is provided, it is used for all positions. The vector length must equal the number of rows in the position matrix. Numeric values are converted to a string using format '%0.5g', as used by sprintf.

Data Types: char

position — Position of textvector | matrix

Position of inserted text, specified as an M-by-2 matrix of [x y] coordinates. Each row represents the [x y] coordinate for the AnchorPoint of the text bounding box.

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

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: 'AnchorPoint','LeftTop' specifies the upper-left anchor point of the text bounding box.

'FontSize' — Font size12 (default) | integer value

Font size, specified as the comma-separated pair consisting of 'FontSize' and an integer value in the range [8 72].

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

'TextColor' — Text color'black' (default) | character string | cell array of character strings | [R G B] vector | M-by-3 matrix

Text color, specified as the comma-separated pair consisting of 'TextColor' and either a string, cell array of strings, vector, or matrix. You can specify a different color for each string or one color for all strings.

To specify a color for each text string, set TextColor to a cell array of M number of color strings or an M-by-3 matrix of M number of RGB (red, green, and blue) string color values.

To specify one color for all text strings, set TextColor to either a color string or an [R G B] vector. The [R G B] vector contains the red, green, and blue values.

RGB values must be in the range of the image data type. Supported color strings are: 'blue', 'green', 'red', 'cyan', 'magenta', 'yellow','black', and 'white'.

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

'BoxColor' — Text box color'yellow' (default) | character string | cell array of character strings | [R G B] vector | M-by-3 matrix

Text box color, specified as the comma-separated pair consisting of 'BoxColor' and either a string, cell array of strings, vector, or matrix. You can specify a different color for each text box, or one color for all of the boxes.

To specify a color for each text box, set BoxColor to a cell array of M number of color strings or an M-by-3 matrix of M number of RGB (red, green, and blue) string color values.

To specify one color for all of the text boxes, set BoxColor to either a color string or an [R G B] vector. The [R G B] vector contains the red, green, and blue values.

RGB values must be in the range of the image data type. Supported color strings are: 'blue', 'green', 'red', 'cyan', 'magenta', 'yellow','black', and 'white'.

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

'BoxOpacity' — Filled shape opacity0.6 (default) | range of [0 1]

Filled shape opacity of text box, specified as the comma-separated pair consisting of 'BoxOpacity' and a scalar value in the range [0 1]. A value of 0 corresponds to a fully transparent text box, and a value of 1 corresponds to a fully opaque text box. When you set the BoxOpacity to 0, no text box appears.

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

'AnchorPoint' — Text box reference point'LeftTop' (default) | 'LeftCenter' | 'CenterTop' | 'Center' | 'CenterBottom' | 'RightTop' | 'RightCenter' | 'RightBottom' |

Text box reference point, specified as the comma-separated pair consisting of 'AnchorPoint' and a string value. The anchor point defines a relative location on the text box. You can position the text box by placing the anchor point for it at the [x y] coordinate defined by the corresponding position for the text. For example, if you want the center of the text box to be at the [x y] coordinate you specifed with the position input, then set AnchorPoint to Center.

Supported positions are LeftTop, LeftCenter, LeftBottom, CenterTop, Center, CenterBottom, RightTop, RightCenter, and RightBottom.

Data Types: char

Output Arguments

expand all

RGB — Output imageM-by-N-by-3 truecolor | M-by-N 2-D grayscale image

Output image, returned as a truecolor image.

Was this topic helpful?