Documentation Center

  • Trial Software
  • Product Updates

vision.TextInserter System object

Package: vision

Draw text on image or video stream

Description

The text inserter System object draws text in an image. The output image can then be displayed or saved to a file. It can draw one or more arbitrary ASCII strings at multiple locations in an image. The object uses the FreeType 2.3.5 library, an open-source font engine, to produce stylized text bitmaps. To learn more about the FreeType Project, visit http://www.freetype.org/. The text inserter object does not support character sets other than ASCII.

Construction

txtInserter = vision.TextInserter('string') returns a text inserter object, txtInserter with the input text provided in quotes added to the image. You can specify the location for the text, font style, and other text characteristics with the properties described below.

txtInserter = vision.TextInserter(...,Name,Value) returns a text inserter object, txtInserter, with each specified property set to the specified value. You can specify additional name-value pair arguments in any order as (Name1, Value1,...,NameN,ValueN).

Code Generation Support
Supports MATLAB® Function block: Yes
System Objects in MATLAB Code Generation
Code Generation Support, Usage Notes, and Limitations.

To insert text:

  1. Define and set up your text inserter using the constructor.

  2. Call the step method with the input image, I, the text inserter object, txtInserter, and any optional properties. See the syntax below for using the step method.

J = step(txtInserter,I) draws the text specified by the Text property onto input image I and returns the modified image J. The input image I, can be a 2-D grayscale image or a truecolor RGB image.

J = step(txtInserter,I,LOC) places the text at the [x y] coordinates specified by the LOC input. This applies when you set the LocationSource property to 'Input port'.

J = step(txtInserter,I,VARS) uses the data in the input VARS for variable substitution. This applies when the Text property contains ANSI C printf-style format specifications (%d, %.2f, etc.). The input VARS can be a scalar or a vector having length equal to the number of format specifiers in each element in the specified text string.

J = step(txtInserter,I,CELLIDX) draws the text string selected by the index CELLIDX input. This applies when you set the Text property to a cell array of strings. For example, if you set the Text property to {'str1','str2'}, and the CELLIDX property to 1, the object inserts the string 'str1' in the image. Values of CELLIDX outside of a valid range result in no text drawn on the image.

J = step(txtInserter,I,COLOR) sets the text intensity or color, specified by the Color property. The Color property can be a scalar or 3-element vector. This property applies when you set the ColorSource property to Input port.

J = step(txtInserter,I,OPAC) sets the text opacity, specified by the Opacity property. This property applies when you set the OpacitySource property to 'Input port'.

J = step(txtInserter,I,CELLIDX,VARS,COLOR,LOC,OPAC) draws the text specified by the Text property onto input image I, using the index CELLIDX. It also uses the text variable substitution data VARS, and color value COLOR. The object draws the text at location specified by LOC with opacity OPAC. You can use any combination or all possible inputs. Properties must be set appropriately.

Properties

Text

Text string to draw on image or video stream

Specify the text string to be drawn on image or video stream as a single text string or a cell array of strings. The string(s) can include ANSI C printf-style format specifications, such as %d, %f, or %s.

ColorSource

Source of intensity or color of text

Specify the intensity or color value of the text as Property or Input port. The default is Property.

Color

Text color or intensity

Specify the intensity or color of the text as a scalar integer value or a 3-element vector respectively. Alternatively, if the Text property is a cell array of N number of strings, specify a M-by-1 vector of intensity values or M-by-3 matrix of color values that correspond to each string. The default is [0 0 0]. This property applies when you set the ColorSource property to Property. This property is tunable.

LocationSource

Source of text location

Specify the location of the text as Property or Input port. The default is Property.

Location

Top-left corner of text bounding box

Specify the top-left corner of the text bounding box as a 2-element vector of integers, [x y]. The default is [1 1]. This property applies when you set the LocationSource property to Property. This property is tunable.

OpacitySource

Source of opacity of text

Specify the opacity of the text as Property or Input port. The default is Property.

Opacity

Opacity of text

Specify the opacity of the text as numeric scalar between 0 and 1. This property applies when you set the OpacitySource property to Property. The default is 1. This property is tunable.

TransposedInput

Specifies if input image data order is row major

Set this property to true to indicate that the input image data order is row major.

The default is false.

Font

Font face of text

Specify the font of the text as the available fonts installed on the system.

FontSize

Font size in points

Specify the font size as any positive integer value. The default is 12.

Antialiasing

Perform smoothing algorithm on text edges

Set this property to true to smooth the edges of the text. The default is true.

Methods

cloneCreate text inserter object with same property values
getNumInputsNumber of expected inputs to step method
getNumOutputsNumber of outputs from step method
isLockedLocked status for input attributes and non-tunable properties
release Allow property value and input characteristics changes
stepDraws the specified text onto input image

Examples

expand all

Display a Text String in an Image

  1. Set up text [red, green, blue] formatted color and [x y] location.

    textColor    = [255, 255, 255];
    textLocation = [100 315];
    
  2. Insert the text using the text inserter object.

    textInserter = vision.TextInserter('Peppers are good for you!','Color', textColor, 'FontSize', 24, 'Location', textLocation);
    
  3. Display the original image and the image with text.

    I = imread('peppers.png');
    J = step(textInserter, I);
    imshow(J);

Display Four Numeric Values at Different Locations

  1. Set up the text using the text inserter object.

    textInserter = vision.TextInserter('%d','LocationSource','Input port','Color', [255, 255, 255],'FontSize', 24);
  2. Read the image.

    I = imread('peppers.png');
    
  3. Insert the text.

    J = step(textInserter, I, int32([1 2 3 4]),int32([1 1; 500 1; 1 350; 500 350]));
    
  4. Display the image with the numeric values.

    imshow(J);

Display Two Strings Inserted at Different Locations on an Image

  1. Set up the text inserter.

    textInserter = vision.TextInserter('%s','LocationSource','Input port','Color',[255, 255, 255],'FontSize',24);
    
  2. Read the image.

    I = imread('peppers.png');
    
  3. Create null separated strings.

    strings = uint8(['left' 0 'right']);
  4. Insert the text.

    J = step(textInserter, I, strings, int32([10 1; 450 1]));
    
  5. Display the image with the numeric values.

    imshow(J);

See Also

| |

Was this topic helpful?