Skip to content

OSD (Text Overlay)

1.1. OSD Overview

In some application scenarios (for example, IP cameras), there is a requirement to add watermarks to images, especially to draw watermarks using multibyte characters such as Japanese. However, OpenCV itself does not support drawing these Asian-language characters by default. To address this situation, the EASY EAI API provides an encapsulated font engine dedicated to drawing text on images.

1.1. OSD Overview Figure 10

1.1. OSD Overview Figure 10

1.2. Quick Start

1.2.1 Preparing the Development Environment

Run the run script on the Ubuntu system on the PC to enter the EASY-EAI compilation environment. The details are as follows.

Terminal window
cd ~/develop_environment
./run.sh 2204

1.2.2 Downloading the Source Code and Compiling the Sample

In the EASY-EAI compilation environment, create a directory for managing the source code repository.

Terminal window
cd /opt
mkdir EASY-EAI-Toolkit
cd EASY-EAI-Toolkit

https://dl.dragonwake.com/download/rv1126b/embedded/EASY-EAI-Toolkit-1126B/Demos.zip

Download the Demos.zip archive from the link above, upload it to the /opt/EASY-EAI-Toolkit directory in the virtual machine, and extract it. Go to the corresponding sample directory and perform the compilation operation. The specific commands are as follows.

Terminal window
cd EASY-EAI-Toolkit-1126B/Demos/common-font_engine/
./build.sh cpres

💡 Note: Because the dependency libraries are placed on the board, be sure to keep /mnt mounted during cross-compilation.

1.2.2 Downloading the Source Code and Compiling the Sample Figure 12

1.2.2 Downloading the Source Code and Compiling the Sample Figure 12

1.2.3 Running the Sample

Enter the board backend through serial debugging or SSH debugging, and go to the location where the sample is placed, as shown below.

Terminal window
cd /userdata/Demo/common-font_engine

1.2.3 Running the Sample Figure 13

1.2.3 Running the Sample Figure 13

The command for running the sample is as follows.

Terminal window
./test-font_engine

1.2.4 Execution Result

The execution result is as follows.

After running the test program, an image named “result.jpg” will be generated.

1.2.4 Execution Result Figure 14

1.2.4 Execution Result Figure 14

Return to the [compilation window] and use the eog command to open the image directly.

Terminal window
eog /mnt/userdata/Demo/common-font_engine/result.jpg &
eog /mnt/userdata/Demo/common-font_engine/testPic.png &

1.2.4 Execution Result Figure 15

1.2.4 Execution Result Figure 15

This allows you to confirm that the text has been overlaid on the original image. If the EASY-EAI compilation environment prompts that the eog command cannot be found, install it using the following operations.

Terminal window
sudo apt-get update
sudo apt-get install eog

Alternatively, you can copy the image from the board to the PC.

Terminal window
cp /mnt/userdata/Demo/common-font_engine/result.jpg ./Release

For detailed API descriptions and API calls (the source code of this sample), refer to the following explanation.

1.3. String Operation API Description

1.3.1 Include/Reference Method

The EASY EAI API library is located in the easyeai-api directory of this repository. To allow users to directly call the EASY EAI API library in a local project, the libraries, header files, and related items that need to be linked in the project are listed below.

DescriptionCMake DescriptionMakefile Description
api.cmake${common_root}/font_engine/api.cmakeNone
Header File Directory${FONT_ENGINE_INCLUDE_DIRS}-I ../../easyeai-api/common/font_engine
Source File Directory${FONT_ENGINE_SOURCE_DIRS}../../easyeai-api/common/font_engine
Library File DirectoryNoneNone
Library Link Parameters${FONT_ENGINE_LIBS}None

The API source code path is EASY-EAI-Toolkit-1126B/easyeai-api/common/font_engine/. Users can check the interface implementation through the source code and can also modify the source code directly.

1.3.2 Creating the Global Font Object

The function prototype is as follows.

Terminal window
int32_t global_font_create(const char \*fontLib, const char \*codec);

The detailed description is as follows.

Function name: global_font_create()Details
Header Fileeasyeai-api/common/font_engine/font_engine.h
Input ParametersfontLib: font library file to load (for example: ”./simhei.ttf”)
codec: character encoding format of the text to be written to the image (for example: utf-8 or
gbk, etc.)
Return ValueCreation successful: 0
Creation failed: -1
NotesIf a global font object has already been created in the current process, calling it again will always
return -1.

1.3.3 Setting the Character Encoding of the Global Font Object

The function prototype is as follows.

Terminal window
int32_t global_font_set_textCodec(const char \*codec);

The detailed description is as follows.

Function name: global_font_set_textCodec()Details
Header Fileeasyeai-api/common/font_engine/font_engine.h
Input Parameterscodec: character encoding format of the text to be written to the image (for example: utf-8
or gbk, etc.)
Return ValueSetting successful: 0
Setting failed: -1
NotesA global font object must be created in advance.

1.3.4 Setting the Font Size of the Global Font Object

The function prototype is as follows.

Terminal window
int32_t global_font_set_fontSize(uint32_t fontSize);

The detailed description is as follows.

Function name: global_font_set_fontSize()Details
Header Fileeasyeai-api/common/font_engine/font_engine.h
Input ParametersfontSize: target font size to set
Return ValueSetting successful: 0
Setting failed: -1
NotesA global font object must be created in advance.

1.3.5 Writing Specified Text to an Image

The function prototype is as follows.

Terminal window
int32_t putText(uint8_t \*imgData, uint32_t imgWidth, uint32_t imgHeight, const char \*text, uint32_t posX, uint32_t posY, FontColor color);

The detailed description is as follows.

Function name: putText()Details
Header Fileeasyeai-api/common/font_engine/font_engine.h
Input ParametersimgData: start address of the image data. The data format is BGR888.
imgWidth: image resolution size (width).
imgHeight: image resolution size (height).
text: string to write.
posX: X coordinate of the upper-left corner of the string to write (relative position in the image). The image coordinate origin is the upper-left corner.
posY: Y coordinate of the upper-left corner of the string to write (relative position in the image). The image coordinate origin is the upper-left corner.
color: font color value. The structure is described later.
Return ValueWrite successful: 0
Write failed: -1
NotesA global font object must be created in advance.

FontColor structure:

Terminal window
typedef struct {
uint8_t Alpha; // Color value of the transparency (alpha) channel
uint8_t Red; // Color value of the red channel
uint8_t Green; // Color value of the green channel
uint8_t Bule; // Color value of the blue channel (as in the original text)
} FontColor;

1.3.6 Destroying the Global Font Object

The function prototype is as follows.

Terminal window
int32_t global_font_destory();

The detailed description is as follows.

Function name: global_font_destory()Details
Header Fileeasyeai-api/common/font_engine/font_engine.h
Input ParametersNone
Return ValueDestruction successful: 0
Destruction failed: -1
NotesA global font object must be created in advance.

1.4. API Test Case

The sample code path is EASY-EAI-Toolkit-1126B/Demos/common-font_engine/test-font_engine.cpp.

The following is an actual usage example of each API.

int main(void){
// Initialize font transparency and color
FontColor color = {200, 135, 189, 67}; // {A, R, G, B};
// Create the global font
global_font_create("./simhei.ttf", CODE_UTF8);
// Load the background image
cv::Mat img = cv::imread("./testPic.png");
// Write text
global_font_set_fontSize(80);
putText(img.data, img.cols, img.rows, "欢迎使用", 210, 940,
color);
global_font_set_fontSize(40);
putText(img.data, img.cols, img.rows, "欢迎使用", 290, 1020,
color);
// Save the image after writing text
cv::imwrite("./res.jpg", img);
// Destroy the global font
global_font_destory();
return 0;
}