[转] OpenCV 文字绘制cv::putText详解

ref:【OpenCV3】文字绘制——cv::putText详解_cv.puttext_PHILOS_THU的博客-CSDN博客

opencv学习(十三)之文本文字插入_opencv视频添加文本并保存_梧桐栖鸦的博客-CSDN博客

opencv中除了提供绘制各种图形的函数外,还提供了一个特殊的绘制函数——在图像上绘制文字。这个函数即是cv::putText()。

具体形式如下:

	void cv::putText(
		cv::Mat& img, // 待绘制的图像
		const string& text, // 待绘制的文字
		cv::Point origin, // 文本框的左下角
		int fontFace, // 字体 (如cv::FONT_HERSHEY_PLAIN)
		double fontScale, // 尺寸因子,值越大文字越大
		cv::Scalar color, // 线条的颜色(RGB)
		int thickness = 1, // 线条宽度
		int lineType = 8, // 线型(4邻域或8邻域,默认8邻域)
		bool bottomLeftOrigin = false // true='origin at lower left'
	);

参数解释:
. 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则指定点为插入文字的左下角位置.

对于字体的解释,请查看官方文档cvInitFont()

另外,我们在实际绘制文字之前,还可以使用cv::getTextSize()接口先获取待绘制文本框的大小,以方便放置文本框。具体调用形式如下:

	cv::Size cv::getTextSize(
		const string& text,
		cv::Point origin,
		int fontFace,
		double fontScale,
		int thickness,
		int* baseLine
	);

参数解释:
. const string& text: 输入的文本文字
. int fontFace: 文字字体类型
. double fontScale: 字体缩放系数
. int thickness: 字体笔画线宽
. CV_OUT int* baseLine: 文字最底部y坐标

下面就通过一个示例,来看看cv::getTextSize()与cv::putText()相结合的妙用:

	//创建空白图用于绘制文字
	cv::Mat image = cv::Mat::zeros(cv::Size(640, 480), CV_8UC3);
	//设置蓝色背景
	image.setTo(cv::Scalar(100, 0, 0));
 
	//设置绘制文本的相关参数
	std::string text = "Hello World!";
	int font_face = cv::FONT_HERSHEY_COMPLEX; 
	double font_scale = 2;
	int thickness = 2;
	int baseline;
	//获取文本框的长宽
	cv::Size text_size = cv::getTextSize(text, font_face, font_scale, thickness, &baseline);
 
	//将文本框居中绘制
	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);
 
	//显示绘制解果
	cv::imshow("image", image);
	cv::waitKey(0);
	return 0;

绘制解果如下:

the other示例程序:

#include 
#include 
#include 
 
using namespace std;
using namespace cv;
 
int main()
{
    string text = "Funny text inside the box";
    //int fontFace = FONT_HERSHEY_SCRIPT_SIMPLEX;     //手写风格字体
    int fontFace = FONT_HERSHEY_SCRIPT_COMPLEX;
    double fontScale = 2;       //字体缩放比
    int thickness = 3;
 
    Mat img(600,800,CV_8UC3, Scalar::all(0));
 
    int baseline = 0;
 
    Size textSize = getTextSize(text, fontFace, fontScale, thickness, &baseline);
    baseline += thickness;
 
    //center the text
    Point textOrg((img.cols - textSize.width)/2,(img.rows + textSize.height)/2);
 
    //draw the box
    rectangle(img,textOrg + Point(0,baseline),textOrg + Point(textSize.width, -textSize.height),Scalar(0,0,255));
 
    line(img,textOrg + Point(0,thickness),textOrg + Point(textSize.width,thickness),Scalar(0,0,255));
 
    putText(img,text,textOrg,fontFace,fontScale,Scalar::all(255),thickness,8);
    imshow("text",img);
 
    waitKey(0);
 
    return 0;
}

程序解释:
. int fontFace = FONT_HERSHEY_SCRIPT_COMPLEX;字体为手写风格
. double fontScale = 2: 字体缩放比,在此处表示是默认字体大小的两倍
. Point textOrg((img.cols - textSize.width)/2,(img.rows + textSize.height)/2);这句代码可以求出字符串左下角位置坐标
. rectangle中第一个点:textOrg+Point(0, baseline)矩形框左下角坐标,第二个点:textOrg+Point(textSize.width,-textSize.height)是矩形的右上角
.Scalar::all(255)即三通道分量BGR均为255,字体为白色

程序运行结果:
[转] OpenCV 文字绘制cv::putText详解_第1张图片

OpenCV几个绘图函数

ref:OpenCV几个绘图函数_西伯利亚大橘猫的博客-CSDN博客

矩形

rectangle(Mat& img,Point pt1, Point pt2, const Scalar&color, int thickness=1,int lineType=8, int shift=0)

rectangle(Mat& img,Rect rec, const Scalar&color, int thickness=1, int lineType=8,int shift=0)

intthickness为-1 则填充矩形   lineType线形

线

line(Mat& img, Point pt1,Point pt2, const Scalar& color, int thickness=1, int lineType=8,int shift=0)

circle(Mat&img, Point center, int radius, const Scalar&color,int thickness=1, intl ineType=8, int shift=0)

center圆心  radius半径 

椭圆

ellipse(Mat& img, Point center,Size axes, double angle, double startAngle, double endAngle, const Scalar& color,int thickness=1, int lineType=8, int shift=0)

axes:横竖大小, angle旋转角度

折线

polylines(Mat& img, const Point** pts, const int* npts, int ncontours, bool isClosed, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )

文字

void putText(Mat& img, const string& text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=8, bool bottomLeftOrigin=false )

test文字 org左下坐标 

例子

/*
绘图
*/
#include 
#include 
#include 
 
using namespace cv;
using namespace std;
 
int main(int argc, char **argv)
{
    Mat canvas(400,400,CV_8UC3,Scalar(255,255,255));
    //线
    line(canvas, Point(10,10),Point(100,10),Scalar(0), 1, 8);
    line(canvas, Point(10,20),Point(200,20),Scalar(50), 2, 4);
    line(canvas, Point(10,40),Point(300,30),Scalar(100,200,255), 3, 2);
    line(canvas, Point(10,80),Point(400,40),Scalar(200), 4, 1);
    
    //矩形
    rectangle(canvas, Rect(Point(0,200),Point(40,280)),Scalar(0,0,255),-1,8);
    rectangle(canvas, Rect(Point(40,200),Point(80,280)),Scalar(0,255,0),4,8);
    rectangle(canvas, Rect(Point(80,200),Point(120,280)),Scalar(255,255,0),8,8);
    
    //○
    circle(canvas, Point(50,50),10, Scalar(0,0,255),2);
    circle(canvas, Point(300,100),5, Scalar(0,255,0),3);
    circle(canvas, Point(250,350),15, Scalar(255,0,0),4);        
    ellipse(canvas, Point(50,300),Size(20,10),0,0,300,Scalar(255,0,255),5);
    
    //文字
    putText(canvas,"(50,50)",Point(50,50),1,1,Scalar(0));
    putText(canvas,"(300,100)",Point(300,100),1,1,Scalar(0));
    putText(canvas,"(250,350)",Point(250,350),1,1,Scalar(0));
    putText(canvas,"(50,300)",Point(50,300),1,1,Scalar(0));
    
    //折线
    Point ps[1][5];
    ps[0][0] = Point(50,50);
    ps[0][1] = Point(300,100);
    ps[0][2] = Point(250,350);
    ps[0][3] = Point(50,300);            
    const Point* pts[1]={ps[0]};
    int pn[1]={4};
    polylines(canvas,pts,pn,1,true,Scalar(0));
    
    namedWindow("canvas", CV_WINDOW_AUTOSIZE);
    imshow("canvas", canvas);
    waitKey(0);
    
    return 0; 
}

[转] OpenCV 文字绘制cv::putText详解_第2张图片

你可能感兴趣的:(opencv,opencv,计算机视觉,人工智能)