ใน OpenCV เราสามารถใส่ข้อความในภาพโดยใช้ฟังก์ชัน puttext() ฟังก์ชันนี้กำหนดไว้ใน
แทนที่จะโหลดรูปภาพในโปรแกรมของเรา เราได้เติมเมทริกซ์ด้วยสีขาว แล้วเราก็ใส่ข้อความในเมทริกซ์นั้น เราจำเป็นต้องกำหนดจุดเริ่มต้นของข้อความในเมทริกซ์ แบบอักษรของข้อความ สีของแบบอักษร และน้ำหนักของแบบอักษร
ไวยากรณ์พื้นฐานของวิธีนี้มีดังนี้ −
ไวยากรณ์
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;
} ผลลัพธ์
