如何在 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;
}

輸出

更新於: 10-Mar-2021

4K+ 瀏覽

開啟你的 職業生涯

完成課程以獲得認證

開始學習
廣告
© . All rights reserved.