如何在 OpenCV 中使用 C++ 在图像中放置文字?


在 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));//Creating an empty matrix filled with white color//
   Point text_position(80, 80);//Declaring the text position//
   int font_size = 1;//Declaring the font size//
   Scalar font_Color(0, 0, 0);//Declaring the color of the font//
   int font_weight = 2;//Declaring the font weight//
   putText(image, "Text in Images", text_position,FONT_HERSHEY_COMPLEX, font_size,font_Color, font_weight);//Putting the text in the matrix//
   imshow("Image", image);//Showing the image//
   waitKey(0);//Wait for Keystroke//
   return 0;
}

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

输出

更新于: 10-Mar-2021

4K+ 浏览

开启你的 职业生涯

完成课程以获得认证

开始学习
广告