如何使用 C++ 在 OpenCV 中将文本添加到图像中?
opencvc++server side programmingprogramming
在 OpenCV 中,我们可以使用 puttext() 函数将文本添加到图像中。该函数定义在 <imgproc.hpp> 头文件中。要将文本添加到图像中,我们首先需要声明用于加载图像的矩阵。
我们的程序中没有加载图像,而是先用白色填充矩阵,然后将文本添加到该矩阵中。我们需要定义文本在矩阵中的起始点、文本的字体、字体的颜色和字体的粗细。
此方法的基本语法如下:−
语法
putText(image, "Text in Images", text_position,FONT_HERSHEY_COMPLEX, font_size,font_Color, font_weight);
以下程序展示了如何在 OpenCV 中将文本放入图像中。
示例
#include<iostream> #include<opencv2/highgui/highgui.hpp> #include<opencv2/imgproc/imgproc.hpp> #include<string> using namespace cv; using namespace std; int main() { Mat image=Mat(400, 400, CV_8UC3, Scalar(255, 255, 255));//创建一个用白色填充的空矩阵// Point text_position(80, 80);//声明文本位置// int font_size = 1;//声明字体大小// Scalar font_Color(0, 0, 0);//声明字体颜色// int font_weight = 2;//声明字体粗细// putText(image, "Text in Images", text_position,FONT_HERSHEY_COMPLEX, font_size,font_Color, font_weight);//将文本放入矩阵// imshow("Image", image);//显示图像// waitKey(0);//等待按键// return 0; }