【OpenCV】文字绘制 putText

Backto OpenCV Index


在图像上绘制文字,

void putText( InputOutputArray img, const String& text, Point org,
                         int fontFace, double fontScale, Scalar color,
                         int thickness = 1, int lineType = LINE_8,
                         bool bottomLeftOrigin = false );

fontFace 字体

其中, fontFace 指的是字体. OpenCV 采用的是适合屏幕显示的 HersheyFonts, 采用了其中的9个基本款. 分别是

FONT_HERSHEY_SIMPLEX = 0
FONT_HERSHEY_PLAIN = 1
FONT_HERSHEY_DUPLEX = 2
FONT_HERSHEY_COMPLEX = 3
FONT_HERSHEY_TRIPLEX = 4
FONT_HERSHEY_COMPLEX_SMALL = 5
FONT_HERSHEY_SCRIPT_SIMPLEX = 6
FONT_HERSHEY_SCRIPT_COMPLEX = 7
FONT_ITALIC = 16

显示效果如下

【OpenCV】文字绘制 putText_第1张图片

图像生成代码,

#include 

int main() {
	const cv::String text = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789";
	cv::Mat frame = cv::Mat::zeros(1080, 1920, CV_8UC3);//black background
	const int unit_height = 80;
	cv::putText(frame, "FONT_HERSHEY_COMPLEX:" + text, cv::Point(20, unit_height * 1), cv::HersheyFonts::FONT_HERSHEY_COMPLEX, 1.0, cv::Scalar(255, 255, 255));
	cv::putText(frame, "FONT_HERSHEY_COMPLEX_SMALL:" + text, cv::Point(20, unit_height * 2), cv::HersheyFonts::FONT_HERSHEY_COMPLEX_SMALL, 1.0, cv::Scalar(255, 255, 255));
	cv::putText(frame, "FONT_HERSHEY_DUPLEX:" + text, cv::Point(20, unit_height * 3), cv::HersheyFonts::FONT_HERSHEY_DUPLEX, 1.0, cv::Scalar(255, 255, 255));
	cv::putText(frame, "FONT_HERSHEY_PLAIN:" + text, cv::Point(20, unit_height * 4), cv::HersheyFonts::FONT_HERSHEY_PLAIN, 1.0, cv::Scalar(255, 255, 255));
	cv::putText(frame, "FONT_HERSHEY_SCRIPT_COMPLEX:" + text, cv::Point(20, unit_height * 5), cv::HersheyFonts::FONT_HERSHEY_SCRIPT_COMPLEX, 1.0, cv::Scalar(255, 255, 255));
	cv::putText(frame, "FONT_HERSHEY_SCRIPT_SIMPLEX:" + text, cv::Point(20, unit_height * 6), cv::HersheyFonts::FONT_HERSHEY_SCRIPT_SIMPLEX, 1.0, cv::Scalar(255, 255, 255));
	cv::putText(frame, "FONT_HERSHEY_SIMPLEX:" + text, cv::Point(20, unit_height * 7), cv::HersheyFonts::FONT_HERSHEY_SIMPLEX, 1.0, cv::Scalar(255, 255, 255));
	cv::putText(frame, "FONT_HERSHEY_TRIPLEX:" + text, cv::Point(20, unit_height * 8), cv::HersheyFonts::FONT_HERSHEY_TRIPLEX, 1.0, cv::Scalar(255, 255, 255));
	cv::putText(frame, "FONT_ITALIC:" + text, cv::Point(20, unit_height* 9), cv::HersheyFonts::FONT_ITALIC, 1.0, cv::Scalar(255, 255, 255));
	cv::imwrite("fonts.png", frame);
	return 0;
}

org: 文本框左下角的起始点

可以指定, 需要特别精确控制的话, 可以用 cv::getTextSize() 得到文本框大小, 在相对图像放置.具体参考 Ref1

Ref

  • 文字绘制——cv::putText详解

你可能感兴趣的:(OpenCV)