使用C++进行文本文字插入(opencv)



#include 
#include 
#include 

using namespace std;
using namespace cv;


int main()
{
	string text = "I Love You Baby ! ";
	int fontface = FONT_HERSHEY_COMPLEX_SMALL;
	double fontscale = 2;
	int thickness = 3;

	Mat image(700, 900, CV_8UC3, Scalar::all(0));
	int baseline = 0;

	Size textsize = getTextSize(text, fontface, fontscale, thickness, &baseline);
	baseline += thickness;

	Point textorg((image.cols - textsize.width) / 2, 
		(image.rows + textsize.height) / 2);
	rectangle(image, textorg + Point(0, baseline), textorg + Point(textsize.width,
		-textsize.height), Scalar(0, 0, 255));
	line(image, textorg + Point(0, thickness), textorg + Point(textsize.width, thickness),
		Scalar(0, 0, 255));
	putText(image, text, textorg, fontface, fontscale, Scalar::all(255), thickness, 8);
	imshow("添加文档:", image);


	waitKey(0);
    return 0;
}


//1. putText()函数
//
//putText()是字符串绘制函数,其定义如下:
//
//CV_EXPORTS_W void putText(Mat& img, const string& text, Point org,
//	int fontFace, double fontScale, Scalar color,
//	int thickness = 1, int lineType = 8,
//	bool bottomLeftOrigin = false);
//
//参数解释:
//.Mat& img: 要添加备注的图片
//. const string& text : 要添加的文字内容
//.Point org : 要添加的文字基准点或原点坐标,左上角还是左下角取决于最后
//一个参数bottomLeftOrigin的取值
//. int fontFace : 文字的字体类型(Hershey字体集),可供选择的有
//FONT_HERSHEY_SIMPLEX:正常大小无衬线字体
//FONT_HERSHEY_PLAIN:小号无衬线字体
//FONT_HERSHEY_DUPLEX:正常大小无衬线字体,比FONT_HERSHEY_SIMPLEX更复杂
//FONT_HERSHEY_COMPLEX:正常大小有衬线字体
//FONT_HERSHEY_TRIPLEX:正常大小有衬线字体,比FONT_HERSHEY_COMPLEX更复杂
//FONT_HERSHEY_COMPLEX_SMALL:FONT_HERSHEY_COMPLEX的小译本
//FONT_HERSHEY_SCRIPT_SIMPLEX:手写风格字体
//FONT_HERSHEY_SCRIPT_COMPLEX:手写风格字体,比FONT_HERSHEY_SCRIPT_SIMPLEX更复杂
//这些参数和FONT_ITALIC同时使用就会得到相应的斜体字
//. double fontScale : 字体相较于最初尺寸的缩放系数。若为1.0f,则字符宽度是最初字符宽度,
//若为0.5f则为默认字体宽度的一半
//.Scalar color : 很熟悉了,字体颜色
//. int thickness = 1 : 字体笔画的粗细程度,有默认值1
//. int lineType = 8 : 字体笔画线条类型,有默认值8
//. bool bottomLeftOrigin = false : 如果取值为TRUE,则Point org指定的点为插入文字的
//左上角位置,如果取值为默认值false则指定点为插入文字的左下角位置.
//
//
//2. getTextSize()函数
//
//计算插入文本文字的宽和高,其定义如下
//
////! returns bounding box of the text string
//CV_EXPORTS_W Size getTextSize(const string& text, int fontFace,
//	double fontScale, int thickness,
//	CV_OUT int* baseLine);
//参数解释:
//. const string& text: 输入的文本文字
//. int fontFace : 文字字体类型
//. double fontScale : 字体缩放系数
//. int thickness : 字体笔画线宽
//.CV_OUT int* baseLine : 文字最底部y坐标

参考博客:https://blog.csdn.net/keith_bb/article/details/53366674

你可能感兴趣的:(OpenCv学习)