內容目錄
前言
此文為 C ++ 在 OpenCV 的寫上文字的用法
sample code
#include <opencv2/opencv.hpp>
// create 640*480 Mat
cv::Mat image = cv::Mat::zeros(cv::Size(640, 480), CV_8UC3);
// set to blue color
image.setTo(cv::Scalar(100, 0, 0));
// set string and variables
std::string text = "Hello World!";
int font_face = cv::FONT_HERSHEY_COMPLEX;
double font_scale = 2;
int thickness = 2;
int baseline;
// get text size
cv::Size text_size = cv::getTextSize(text, font_face, font_scale, thickness, &baseline);
// decide text position
cv::Point origin;
origin.x = image.cols / 2 - text_size.width / 2;
origin.y = image.rows / 2 + text_size.height / 2;
cv::putText(image, text, origin, font_face, font_scale, cv::Scalar(0, 255, 255), thickness, 8, 0);
// show result
cv::imshow("image", image);
cv::waitKey(0);
return 0;
C++ compile
g++ test.cpp -o test.out -std=c++11 `pkg-config --cflags --libs opencv`
./test.out
- test.cpp:為要編譯的程式碼
- test.out:為編譯結果的 binary (執行也是用此執行檔執行)
執行結果
Reference
https://blog.csdn.net/guduruyu/article/details/68491211
★留個言吧!內容有誤或想要補充也歡迎與我討論!