insertText
Insert text in image or video
Syntax
Description
returns
a truecolor image with numeric values inserted.RGB
= insertText(I
,position
,numericValue
)
specifies options using one or more name-value arguments in addition to any
combination of arguments from previous syntaxes. For example,
RGB
= insertText(___,Name=Value
)Color="red"
.
Examples
Insert Numbers and Text on 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 the positions and colors of the text boxes.
position = [23 373;35 185;77 107]; box_color = {"red","green","yellow"};
Insert the text with new font size, box color, opacity, and text color.
RGB = insertText(I,position,text_str,FontSize=18,BoxColor=box_color,... BoxOpacity=0.4,TextColor="white");
Display the image.
figure
imshow(RGB)
title("Board");
Insert Numeric Text on Image
Read the image.
I = imread('peppers.png');
Define the ( x,_y_ ) position for the text and the value.
position = [1 50; 100 50]; value = [555 pi];
Insert text using the bottom-left as the anchor point.
RGB = insertText(I,position,value,'AnchorPoint','LeftBottom');
Display the image with the numeric text inserted.
figure
imshow(RGB),title('Numeric values');
Display non-ASCII character (U+014C)
OWithMacron=native2unicode([hex2dec('C5') hex2dec('8C')],'UTF-8'); RGB = insertText(RGB,[256 50],OWithMacron,'Font','LucidaSansRegular','BoxColor','w');
Display the image with the numeric text inserted.
figure
imshow(RGB),title('Numeric values');
Input Arguments
I
— Input image
M-by-N-by-3 truecolor | M-by-N 2-D grayscale
image
Input image, specified as an M-by-N-by-3 truecolor image or an M-by-N 2-D grayscale image.
Data Types: single
| double
| int16
| uint8
| uint16
text
— Unicode text character vector
character vector | string scalar | cell array of character vectors | string array
Unicode text, specified as a single UNICODE text string or a cell array of UNICODE strings of
length M, where M is the number of
rows in position
. The function overwrites pixels with
the value of text
. The length of the cell array must
equal the number of rows in the position
matrix. If you
specify a single string, the function uses it for all positions in the
position
matrix. Most unicode fonts contain ASCII
characters. You can display non-English and English characters, including
English numeric values, with a single font.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
numericValue
— Numeric value text
scalar | vector
position
— Position of inserted text
vector | matrix
Position of inserted text, specified as a vector or 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 Arguments
Specify optional pairs of arguments as
Name1=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: Font="Calibri"
sets the font face to
Calibri
.
Font
— Font face of text
"LucidaSansRegular"
(default) | character vector
Font face of text, specified as a character vector. The font face must be one of the available
truetype fonts installed on your system. To get a list of available
fonts on your system, type listTrueTypeFonts
at
the MATLAB® command prompt.
Data Types: char
FontSize
— Font size
12
(default) | positive integer in the range [1,200]
Font size, specified as a positive integer in the range [1,200].
Data Types: double
| single
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
TextColor
— Text color
"green"
(default) | character vector | cell array of character vectors | string scalar | string array | M-by-3 matrix
Text color, specified as a character vector, cell array of character
vectors, vector, or M-by-3 matrix. You can specify a
different color for each text string or one color for all text strings.
Color values must be specified in the range [0,255]
.
Values that have a range of [0,1]
must be scaled by a
value of 255 before using it with this function. For example,
[255 255 255].*colorvalue
Supported colors are: "blue"
,
"green"
, "red"
,
"cyan"
, "magenta"
,
"yellow"
,"black"
, and
"white"
.
Color | Format | Example |
---|---|---|
Specify one color for all text strings | String or character color name |
|
1-by-3 vector (RGB triplet) |
| |
Specify a color for each text string | M-element vector |
|
M-by-3 matrix, as a list of RGB values | 255 0 0 255 0 0 0 255 255 ![]() |
Data Types: cell
| char
| uint8
| uint16
| int16
| double
| single
BoxColor
— Box color
"green"
(default) | character vector | cell array of character vectors | string scalar | string array | M-by-3 matrix
Box color, specified as a character vector, cell array of character
vectors, vector, or an M-by-3 matrix. You can specify
a different color for each box or one color for all boxes. Color values
must be specified in the range [0,255]
. Values that
have a range of [0,1]
must be scaled by a value of
255 before using it with this function. For example, [255 255
255].*colorvalue
.
Supported colors are: "blue"
,
"green"
, "red"
,
"cyan"
, "magenta"
,
"yellow"
,"black"
, and
"white"
.
Color | Format | Example |
---|---|---|
Specify one color for all boxes | String or character color name |
|
1-by-3 vector (RGB triplet) |
| |
Specify a color for each box | M-element vector |
|
M-by-3 matrix, as a list of RGB values | 255 0 0 255 0 0 0 255 255 ![]() |
Data Types: cell
| char
| uint8
| uint16
| int16
| double
| single
BoxOpacity
— Opacity of text box
0.6 (default) | scalar value in the range of [0
1
]
Opacity of text box, specified as a scalar value in the range [0,1]. A value of
0
corresponds to a fully transparent text box, or
no box. A value of 1
corresponds to a fully opaque
text box.
Data Types: double
| single
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
AnchorPoint
— Text box reference point
"LeftTop"
(default) | "LeftCenter"
| "LeftBottom"
| "CenterTop"
| "Center"
| "CenterBottom"
| "RightTop"
| "RightCenter"
| "RightBottom"
Text box reference point, specified as a character
vector value. The anchor point defines a relative location on the text
box. You can position the text box by placing its anchor point at the
[x,y] coordinate defined by
the corresponding position
for the text. For
example, to place the center of the text box to be at the
[x,y] coordinate you specified
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
RGB
— Output image
M-by-N-by-3 truecolor
image
Output image, returned as an M-by-N-by-3 truecolor image with the specified text inserted.
Limitations
If you do not see characters in the output image, it means that the font did not contain the character. Select a different font. To get a list of available fonts on your system, at the MATLAB prompt, type
listTrueTypeFonts
.Increasing the font size also increases the preprocessing time and memory usage.
The
insertText
function does not work for certain composite characters. For example, you cannot insert text when the rendering of one glyph corresponding to a character code influences the position, shape, or size of the adjacent glyph.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
Font
,FontSize
must be compile-time constants.Non-ASCII characters are not supported.
Version History
Introduced in R2013a
MATLAB 명령
다음 MATLAB 명령에 해당하는 링크를 클릭했습니다.
명령을 실행하려면 MATLAB 명령 창에 입력하십시오. 웹 브라우저는 MATLAB 명령을 지원하지 않습니다.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)