opencv draws various geometric shapes (lines, rectangles, circles, ellipses, etc.), text and curves on images


Use OpenCV to create a transparent image, and draw a line, a rectangle, a circle, an ellipse, and a piece of text in the image;

Some applications of OpenCV drawing images:

  • Image annotation: Add annotations or annotations to images, for example, in object detection or image classification tasks, mark detected objects by drawing boxes, labels and other information on the image;
  • Post-processing display: For example, during image processing, a comparison chart can be drawn on the pre-processing and post-processing images to intuitively display the effect of image processing;
  • Real-time display: Realized by continuously drawing on the screen, displaying the effect in real time, such as outputting the processed video stream in video processing and rendering it on the screen in real time;

cv::Mat is a data structure representing images in OpenCV, which can store multi-channel images;

cv::Scalar is a four-channel real vector used to represent the color and alpha value of the pixel;

cv::Point is a two-dimensional integer vector used to represent the coordinates of pixel points;

cv::Size(width, height) The two data members in the size class are called width and height;

cv::ones(): equivalent to the first channel of each pixel is 1, and the remaining two channels are 0, Scalar(1,0,0);

cv::zeros(): equivalent to creating a black image, each channel of each pixel is 0, Scalar(0,0,0);

The cv::line function is used to draw a straight line on the picture;

The cv::rectangle function is used to draw a rectangle on the image

The cv::circle function is used to draw a circle on the image

The cv::ellipse function is used to draw an ellipse on an image

The cv::putText function is used to draw text on the image

Function introduction:

1. Draw a straight line:

1. line() function: used to draw a straight line on the picture;
(1) Function prototype:
void cv::line( InputOutputArray img, 
	       Point pt1, 
	       Point pt2, 
               const Scalar& color,
               int thickness = 1, 
               LineTypes lineType = LINE_8, 
               int shift = 0 );
参数解释:
img:输入输出参数,表示待绘制的目标图像;
pt1: 输入参数,表示的是直线的起点坐标(是一个 cv::Point 类型的对象);
pt2: 输入参数,表示的是直线的终点坐标(是一个 cv::Point 类型的对象);
color: 输入参数,表示绘制直线的颜色以及透明度,是一个 cv::Scalar 类型的对象,常见的颜色有(
	红色(0, 0, 255);
	绿色(0, 255, 0);
	蓝色(255, 0, 0)等;)
thickness: 可选参数,表示绘制直线线条的宽度:(
	默认值为1,表示绘制一个线条宽度为1像素的直线;
	如果设置为负值,则表示绘制一条填充直线;)
lineType:可选参数,表示绘制直线的类型,可以取以下几个值:( 
	cv::LINE_4,默认值,表示绘制一条 4 连通的直线;
	cv::LINE_8: 表示绘制一条 8 连通的直线;
	cv::LINE_AA: 表示绘制一条抗锯齿的直线;)
shift:可选参数,坐标点的小数点位数,默认为 0
(2) Sample code:
#include <opencv2\opencv.hpp>
#include <iostream>
#include <demo.h>

using namespace cv;
using namespace std;

int main() {
    
    

	// 创建一张大小为 512x512,具有 alpha 通道的透明图片
	cv::Mat img( 512, 512, CV_8UC4, cv::Scalar(0, 0, 0, 0) );
	// 在图片上绘制一条直线
	cv::line( img, cv::Point(300, 100), cv::Point(450, 100), cv::Scalar(0, 255, 0, 255), 3 );
	// 显示图片
	imshow("img", img);

	waitKey();
	destroyAllWindows();
	return 0;
}

2. To draw a rectangle, you need to use the Rect() class to initialize a rectangle first, and then use the rectangle() function to draw the rectangle;

1. Rect() rectangle class

Rect() rectangle class, the main function is to initialize a rectangle, and then use the rectangle() function to draw the rectangle;

Constructor:
Rect rect(int x, int y, int width, int height);
// 初始化Rect()矩形类
Rect rect(100, 50, 50, 100);

创建矩形对象的构造函数有四个参数,左上角的坐标(x,y)和矩阵的宽度和高度
Member functions of the Rect class:

(1) rect.size() and rect.area() and rect.width() and rect.height() are used to describe the width, height, size and area of ​​a rectangle;

  • rect.width() // returns the width of rect 50
  • rect.height() // returns the height of rect 100
  • rect.size() // returns the size of rect [50 × 100]
  • rect.area() // returns the area of ​​rect 5000

(2) br() and tl(), used to describe the rectangular coordinate points

  • rect.br() // returns the coordinates of the lower right corner of the rect [150, 150]
  • rect.tl() // returns the coordinates of the upper left corner of the rect [100, 50]

(3) rect.contains(Point(x, y)) and rect.inside(Point(x, y)), used to determine whether the point is in the rectangle, the return value is bool type

  • rect.contains(Point(x, y)) // Determine whether the rect contains Point(x, y), the usage of the two functions is the same
  • rect.inside(Point(x, y)) // Determine whether the rect contains Point(x, y), the usage of the two functions is the same

(4) Compare the rectangles and return a Boolean variable

  • rect1 == rect2
  • rect1 != rect2

(5) Translate and scale the rectangle:

The Rect class can achieve rectangle translation by simply adding to a Point type variable, and adding to a Size type variable to realize rectangle scaling

  • rect = rect + Point(-100, 100); // translation, that is, the x coordinate of the upper left vertex -100, the y coordinate +100
  • rect = rect + Size(-100, 100); // scaling, the upper left vertex remains unchanged, width -100, height +100

(6) Extension

1、OpenCV里没有判断rect1是否在rect2里面的功能,所以自己写一个 
bool isInside(Rect rect1, Rect rect2)  
{
    
      
    return (rect1 == (rect1&rect2));  
}  

2、OpenCV没有获取矩形中心点的功能,自己写一个  
Point getCenterPoint(Rect rect)  
{
    
      
    Point cpt;  
    cpt.x = rect.x + cvRound(rect.width/2.0);  
    cpt.y = rect.y + cvRound(rect.height/2.0);  
    return cpt;  
}  
  
3、围绕矩形中心缩放函数  
Rect rectCenterScale(Rect rect, Size size)  
{
    
      
    rect = rect + size;   
    Point pt;  
    pt.x = cvRound(size.width/2.0);  
    pt.y = cvRound(size.height/2.0);  
    return (rect-pt);  
}  

(7) Rectangle operation, find the intersection and union of two rectangles

  • rect = rect1 & rect2
  • rect = rect1 | rect2
Mat m1 = imread("1.jpg");
Mat m2 = imread("1.jpg");
Rect rect1(0,0,300,300);
Rect rect2(0, 0, 100, 100);
Scalar s(255, 255, 0);

Rect rt1 = rect1 & rect2; //取并集
rectangle(m1, rt1, s);
Rect rt2 = rect1 | rect2; //取交集
rectangle(m2, rt2, s);
imshow("交集", m1);
imshow("并集", m2);
2. The rectangle() function

The main function of the rectangle() function is to add a rectangular border to the specified area of ​​the image, which is usually used on the mark of the image to visualize the target area of ​​image processing, which is convenient for us to process the target area;

(1) Function prototype 1:
void rectangle( InputOutputArray img,
	 	Rect 	rect,
		const 	Scalar &color,
		int 	thickness = 1,
		int 	lineType = LINE_8,
		int 	shift = 0 )

参数解释:
img:输入输出参数,表示待绘制的目标图像;
rect:输入参数,使用Rect()定义的矩形;
color: 输入参数,表示绘制矩形的颜色以及透明度,是一个 cv::Scalar 类型的对象,常见的颜色有(
	红色(0, 0, 255);
	绿色(0, 255, 0);
	蓝色(255, 0, 0)等;)
thickness:可选参数,表示矩形边框的宽度:(
	默认值为1,表示绘制矩形的边框线条宽度为1像素;;
	thickness小于0表示绘制一个填充矩形,即一个实心矩形;
	thickness小于0表示绘制一个矩形框,矩形边框的宽度为thickness的值;)
lineType:可选参数,表示矩形边框的类型,可以取以下几个值:(
	cv::LINE_4: 表示绘制四个相邻的点的矩形边框,默认值;
	cv::LINE_8: 表示绘制八个相邻的点的矩形边框;
	cv::LINE_AA: 表示绘制抗锯齿的矩形边框;)
shift:可选参数,坐标点的小数点位数,默认为 0

Sample code:

#include <opencv2\opencv.hpp>
#include <iostream>
#include <demo.h>

using namespace cv;
using namespace std;

int main() {
    
    

	// 创建一张大小为 512x512,具有 alpha 通道的透明图片
	cv::Mat img(512, 512, CV_8UC4, cv::Scalar(0, 0, 0, 0));
	// 初始化一个矩形A
	cv::Rect rectA;
	rectA.x = 100;
	rectA.y = 100;
	rectA.width = 100;
	rectA.height = 100;
	// thickness小于0表示绘制一个填充矩形,即一个实心矩形
	cv::rectangle(img, rectA, Scalar(0, 255, 0), -1);

	// 初始化一个矩形B
	cv::Rect rectB;
	rectB.x = 300;
	rectB.y = 300;
	rectB.width = 100;
	rectB.height = 100;
	// thickness大于0表示绘制一个矩形框
	cv::rectangle(img, rectB, Scalar(0, 0, 255), 2);


	// 显示图片
	imshow("img", img);

	waitKey();
	destroyAllWindows();
	return 0;
}

(2) Function prototype 2:
void rectangle( InputOutputArray img,
		Point(i, j) pt1, 
		Point(i+img.cols, j+img.rows) pt2,
                const Scalar &color, 
		int thickness = 1,
                int lineType = LINE_8, 
		int shift = 0 );

参数解释:
img:输入输出参数,表示待绘制的目标图像;
pt1:矩形左上角顶点的坐标;
pt2:矩形对角线上,右下角顶点的坐标;
color: 输入参数,表示绘制矩形的颜色以及透明度,是一个 cv::Scalar 类型的对象,常见的颜色有(
	红色(0, 0, 255);
	绿色(0, 255, 0);
	蓝色(255, 0, 0)等;)
thickness:可选参数,表示矩形边框的宽度:(
	默认值为1,表示绘制矩形的边框线条宽度为1像素;
	thickness小于0表示绘制一个填充矩形,即一个实心矩形;
	thickness大于0表示绘制一个矩形框,矩形边框的宽度为thickness的值;)
lineType:可选参数,表示矩形边框的类型,可以取以下几个值:(
	cv::LINE_4: 表示绘制四个相邻的点的矩形边框,默认值;
	cv::LINE_8: 表示绘制八个相邻的点的矩形边框;
	cv::LINE_AA: 表示绘制抗锯齿的矩形边框;)
shift:可选参数,坐标点的小数点位数,默认为 0

Sample code:

#include <opencv2\opencv.hpp>
#include <iostream>
#include <demo.h>

using namespace cv;
using namespace std;

int main() {
    
    

	// 创建一张大小为 512x512,具有 alpha 通道的透明图片
	cv::Mat img(512, 512, CV_8UC4, cv::Scalar(0, 0, 0, 0));
	// thickness小于0表示绘制一个填充矩形,即一个实心矩形
	cv::rectangle(img, cv::Point(100, 100), cv::Point(200, 200), cv::Scalar(0, 255, 0, 255), -1);
	// thickness大于0表示绘制一个矩形框
	cv::rectangle(img, cv::Point(300, 300), cv::Point(400, 400), cv::Scalar(0, 0, 255, 255), 2);
	// 显示图片
	imshow("img", img);

	waitKey();
	destroyAllWindows();
	return 0;
}

insert image description here

3. Draw a circle

1. circle() function: used to draw a circle on the picture;
(1) Function prototype:
void cv::circle( InputOutputArray img, 
		 Point center, 
		 int radius, 
		 const Scalar &color,
                 int thickness = 1, 
		 LineTypes lineType = LINE_8, 
                 int shift = 0 );

参数解释:
img:输入输出参数,表示待绘制的目标图像;
center:输入参数,表示圆心坐标(是一个 cv::Point 类型的对象);
radius:输入参数,表示圆的半径;
color: 输入参数,表示绘制圆的颜色以及透明度,是一个 cv::Scalar 类型的对象,常见的颜色有(
	红色(0, 0, 255);
	绿色(0, 255, 0);
	蓝色(255, 0, 0)等;)
thickness:可选参数,表示圆形边框的宽度:(
	默认值为1,表示绘制圆的边框线条宽度为1像素;
	thickness小于0表示绘制一个填充圆,即一个实心圆;
	thickness大于0表示绘制一个圆形框,圆形边框的宽度为thickness的值;)
lineType:可选参数,表示圆形边框的类型,可以取以下几个值:(
	cv::LINE_4: 表示绘制四个相邻的点的圆边界,默认值;
	cv::LINE_8: 表示绘制八个相邻的点的圆边界;
	cv::LINE_AA: 表示绘制抗锯齿的圆边界;)
shift:可选参数,坐标点的小数点位数,默认为 0
(2) Sample code:
#include <opencv2\opencv.hpp>
#include <iostream>
#include <demo.h>

using namespace cv;
using namespace std;


int main() {
    
    

	// 创建一张大小为 512x512,具有 alpha 通道的透明图片
	cv::Mat img(512, 512, CV_8UC4, cv::Scalar(0, 0, 0, 0));

	// thickness小于0表示绘制一个填充圆,即一个实心圆
	cv::circle(img, cv::Point(100, 100), 50, cv::Scalar(0, 255, 0, 255), -1);

	// thickness大于0表示绘制一个圆形框
	cv::circle(img, cv::Point(200, 200), 50, cv::Scalar(0, 0, 255, 255), 1);
	// 显示图片
	imshow("img", img);

	waitKey();
	destroyAllWindows();
	return 0;
}

4. Draw an ellipse

1. ellipse() function: used to draw an ellipse on the picture;
(1) Function prototype:
void cv::ellipse(
	InputOutputArray img,
	Point 		 center,
	Size 		 axes,
	double 		 angle,
	double 		 startAngle,
	double 		 endAngle,
	const Scalar 	 &color,
	int 		 thickness = 1,
	int 		 lineType = LINE_8,
	int 		 shift = 0
)

参数解释:
img:输入输出参数,表示待绘制的目标图像;
center:输入参数,表示椭圆中心点坐标(是一个 cv::Point 类型的对象);
axes:输入参数,椭圆的长轴和短轴,是一个 cv::Size(width, height) 类型的对象;
angle:输入参数,绘制出的椭圆沿水平方向旋转的角度,举个例子(
	0   -> 水平,不旋转
	60  -> 水平防线旋转60)
startAngle:输入参数,开始绘制角度,与endAngle搭配使用,可以决定椭圆绘制的方向,举个例子(
	0   ->0度开始绘制,到endAngle结束绘制;
	360  ->360度开始绘制,到endAngle结束绘制;
)
endAngle:输入参数,结束绘制角度,与startAngle搭配使用,可以决定椭圆绘制的方向,举个例子(
	0   -> 从startAngle开始绘制,绘制到0度结束绘制;
	360  -> 从startAngle开始绘制,绘制到360度结束绘制;
)
color: 输入参数,表示绘制椭圆的颜色以及透明度,是一个 cv::Scalar 类型的对象,常见的颜色有(
	红色(0, 0, 255);
	绿色(0, 255, 0);
	蓝色(255, 0, 0)等;)
thickness:可选参数,表示椭圆形边框的宽度:(
	默认值为1,表示绘制椭圆形的边框线条宽度为1像素;
	thickness小于0表示绘制一个填充椭圆,即一个实心椭圆;
	thickness大于0表示绘制一个椭圆形框,圆形边框的宽度为thickness的值;)
lineType:可选参数,表示椭圆形边框的类型,可以取以下几个值:(
	cv::LINE_4: 表示绘制四个相邻的点的椭圆边界,默认值;
	cv::LINE_8: 表示绘制八个相邻的点的椭圆边界;
	cv::LINE_AA: 表示绘制抗锯齿的椭圆边界;)
shift:可选参数,坐标点的小数点位数,默认为 0
(2) Sample code:
#include <opencv2\opencv.hpp>
#include <iostream>
#include <demo.h>

using namespace cv;
using namespace std;

int main() {
    
    

	// 创建一张大小为 512x512,具有 alpha 通道的透明图片
	cv::Mat img(512, 512, CV_8UC4, cv::Scalar(0, 0, 0, 0));

	// thickness小于0表示绘制一个填充圆,即一个实心圆
	ellipse(img, Point(100, 100), Size(60, 30), 0, 0, 360, Scalar(0, 0, 255), -1, LINE_8);

	// thickness大于0表示绘制一个圆形框
	ellipse(img, Point(200, 200), Size(60, 30), 0, 0, 360, Scalar(0, 0, 255), 1, LINE_8);

	// angle=90,绘制出的椭圆沿水平方向旋转90度
	ellipse(img, Point(300, 300), Size(60, 30), 90, 0, 360, Scalar(0, 0, 255), -1, LINE_8);

	// startAngle=0,endAngle=180,逆时针开始绘制椭圆,绘制180度,结束绘制
	ellipse(img, Point(400, 400), Size(60, 30), 0, 0, 180, Scalar(0, 0, 255), -1, LINE_8);

	// startAngle=360,endAngle=180,顺时针开始绘制椭圆,绘制180度,结束绘制
	ellipse(img, Point(400, 500), Size(60, 30), 0, 360, 180, Scalar(0, 0, 255), -1, LINE_8);

	// 显示图片
	imshow("img", img);

	waitKey();
	destroyAllWindows();
	return 0;
}

5. Drawing and filling polygons

1. polylines(): Only one polygonal frame can be drawn

Opencv supports the drawing and filling of common points, lines, circles, ellipses and rectangles by setting the thickness parameter to achieve drawing and filling. When the thickness is positive, it will be drawn; when the thickness is non-positive, it will be filling;

But for polygons, if the polylines() function sets the thickness to a non-positive number, an error will be reported directly, and the filling cannot be completed by modifying the thickness. This function can only draw a polygonal frame;

(1) Function prototype:
void cv::polylines(
         InputOutputArray      img,
         InputArrayOfArrays    pts,
         bool                  isClosed,
         const Scalar          &color,
         int                   thickness = 1,
         int                   lineType = LINE_8,
         int                   shift = 0
)

参数解释:
img:输入输出参数,表示待绘制的目标图像;
pts:输入参数,表示多边形的顶点坐标集合(是一个 std::vector<Point> pts 类型的对象);
isClosed:表示是否闭合,默认闭合;
color: 输入参数,表示绘制多边形的颜色以及透明度,是一个 cv::Scalar 类型的对象,常见的颜色有(
	红色(0, 0, 255);
	绿色(0, 255, 0);
	蓝色(255, 0, 0)等;)
thickness:可选参数,表示多边形边框的宽度,polylines()函数的thickness必须是正数;
lineType:可选参数,表示多边形边框的类型,可以取以下几个值:(
	cv::LINE_4: 表示绘制四个相邻的点的多边形边界,默认值;
	cv::LINE_8: 表示绘制八个相邻的点的多边形边界;
	cv::LINE_AA: 表示绘制抗锯齿的多边形边界;)
shift:可选参数,坐标点的小数点位数,默认为 0
(2) Sample code:
#include <opencv2\opencv.hpp>
#include <iostream>
#include <demo.h>

using namespace cv;
using namespace std;


int main() {
    
    

	// 创建一张大小为 512x512,3通道的图片
	Mat canvas = Mat::zeros(Size(512, 512), CV_8UC3);
	int w = canvas.cols;
	int h = canvas.rows;
	Point p1(100, 100);
	Point p2(300, 150);
	Point p3(300, 350);
	Point p4(250, 450);
	Point p5(50, 450);
	std::vector<Point> pts;
	pts.push_back(p1);
	pts.push_back(p2);
	pts.push_back(p3);
	pts.push_back(p3);
	pts.push_back(p4);
	pts.push_back(p5);
	// 绘制多边形
	polylines(canvas, pts, true, Scalar(0, 255, 0), 1, 8, 0);

	// 显示图片
	imshow("canvas", canvas);

	waitKey();
	destroyAllWindows();
	return 0;
}

2. fillPoly(): Only one filled polygon can be drawn, that is, a solid polygon;

fillPoly() function: there is no thickness and isClose parameters, this function can only draw a filled polygon, that is, a solid polygon;

(1) Function prototype:
void cv::fillPoly(   
         InputOutputArray      img,
         InputArrayOfArrays    pts,
         const Scalar 	       &color,
         int                   lineType = LINE_8,
         int                   shift = 0,
)

参数解释:
img:输入输出参数,表示待绘制的目标图像;
pts:输入参数,表示多边形的顶点坐标集合(是一个 std::vector<Point> pts 类型的对象);
color: 输入参数,表示绘制多边形的颜色以及透明度,是一个 cv::Scalar 类型的对象,常见的颜色有(
	红色(0, 0, 255);
	绿色(0, 255, 0);
	蓝色(255, 0, 0)等;)
lineType:可选参数,表示多边形边框的类型,可以取以下几个值:(
	cv::LINE_4: 表示绘制四个相邻的点的多边形边界,默认值;
	cv::LINE_8: 表示绘制八个相邻的点的多边形边界;
	cv::LINE_AA: 表示绘制抗锯齿的多边形边界;)
shift:可选参数,坐标点的小数点位数,默认为 0
(2) Sample code:
#include <opencv2\opencv.hpp>
#include <iostream>
#include <demo.h>

using namespace cv;
using namespace std;

int main() {
    
    

	// 创建一张大小为 512x512,3通道的图片
	Mat canvas = Mat::zeros(Size(512, 512), CV_8UC3);
	int w = canvas.cols;
	int h = canvas.rows;
	Point p1(100, 100);
	Point p2(300, 150);
	Point p3(300, 350);
	Point p4(250, 450);
	Point p5(50, 450);
	std::vector<Point> pts;
	pts.push_back(p1);
	pts.push_back(p2);
	pts.push_back(p3);
	pts.push_back(p3);
	pts.push_back(p4);
	pts.push_back(p5);

	// 绘制一个多边形框
	//polylines(canvas, pts, true, Scalar(0, 255, 255), 1, LINE_8, 0);
	// 绘制一个实心多边形
	fillPoly(canvas, pts, Scalar(255, 0, 255), LINE_8, 0);

	// 显示图片
	imshow("canvas", canvas);

	waitKey();
	destroyAllWindows();
	return 0;
}

3. drawContours() function: it can draw both polygonal borders and solid polygons;
  • Any number of polygon borders or solid polygons can be drawn at a time;
  • Like the drawing and filling of points, lines, circles, ellipses and rectangles, the drawing and filling are realized by setting the thickness parameter. When the thickness is a positive number, it will be drawn; when the thickness is a non-positive number, it will be filled;
(1) Function prototype:
 void cv::drawContours (  
     InputOutputArray      image,
     InputArrayOfArrays    contours,
     int                   contourIdx,
     const Scalar          &color,
     int                   thickness = 1,
     int                   lineType = LINE_8,
     InputArray            hierarchy = noArray(),
     int                   maxLevel = INT_MAX
)

参数解释:
image:输入输出参数,表示待绘制的目标图像;
contours:输入参数,contours是个二维嵌套数组,可以存储多个多边形,数组中的元素,存储的是一个个多边形顶点坐标的集合(contours是一个 std::vector<std::vector<Point>> contours 类型的对象);
contourIdx:输入参数,表示绘制指定索引的多边形(contours里是多个集合,绘制哪一个集合,用contourIdx指定;contourIdx大于0表示绘制指定索引的集合,小于0表示绘制全部集合)  
color: 输入参数,表示绘制多边形的颜色以及透明度,是一个 cv::Scalar 类型的对象,常见的颜色有(
	红色(0, 0, 255);
	绿色(0, 255, 0);
	蓝色(255, 0, 0)等;)
thickness:可选参数,表示多边形边框的宽度:(
	默认值为1,表示绘制多边形的边框线条宽度为1像素;
	thickness小于0表示绘制一个填充多边形,即一个实心多边形;
	thickness大于0表示绘制一个多边形边框,多边形边框的宽度为thickness的值;)
lineType:可选参数,表示多边形边框的类型,可以取以下几个值:(
	cv::LINE_4: 表示绘制四个相邻的点的多边形边界,默认值;
	cv::LINE_8: 表示绘制八个相邻的点的多边形边界;
	cv::LINE_AA: 表示绘制抗锯齿的多边形边界;)
hierarchy:可选参数,
maxLevel:可选参数,

(2) Sample code:
#include <opencv2\opencv.hpp>
#include <iostream>
#include <demo.h>

using namespace cv;
using namespace std;

int main() {
    
    

	// 创建一张大小为 512x512,3通道的图片
	Mat canvas = Mat::zeros(Size(512, 512), CV_8UC3);
	int w = canvas.cols;
	int h = canvas.rows;
	// 第1个多边形
	Point p1(100, 100);
	Point p2(300, 150);
	Point p3(300, 350);
	Point p4(250, 450);
	Point p5(50, 450);
	std::vector<Point> pts1;
	pts1.push_back(p1);
	pts1.push_back(p2);
	pts1.push_back(p3);
	pts1.push_back(p3);
	pts1.push_back(p4);
	pts1.push_back(p5);
	// 第2个多边形
	Point p6(200, 200);
	Point p7(300, 200);
	Point p8(400, 450);
	Point p9(350, 350);
	Point p10(60, 500);
	std::vector<Point> pts2;
	pts2.push_back(p6);
	pts2.push_back(p7);
	pts2.push_back(p8);
	pts2.push_back(p9);
	pts2.push_back(p10);

	std::vector<std::vector<Point>> contours;
	contours.push_back(pts1);
	contours.push_back(pts2);
	// 绘制contours中第1个多边形边框
	drawContours(canvas, contours, 0, Scalar(0, 0, 255), 1, 8);
	// 绘制contours中第2个多边形边框
	drawContours(canvas, contours, 1, Scalar(0, 255, 0), 1, 8);

	// 显示图片
	imshow("canvas", canvas);

	waitKey();
	destroyAllWindows();
	return 0;
}

6. Draw text

1. The putText() function is used to draw text on the image;
(1) Function prototype:
void cv::putText( 
	InputOutputArray img, 
	const String     &text, 
	Point            org, 
        int              fontFace,
        double           fontScale, 
	Scalar           color, 
        int              thickness = 1, 
        int              lineType = LINE_8,
        bool             bottomLeftOrigin = false
)

参数解释:
image:输入输出参数,表示待绘制的目标图像;
text:输入参数,表示要绘制的文本字符串;
org:输入参数,表示文本框左下角的坐标点(是一个 cv::Point 类型的对象);
fontFace:输入参数,表示字体的类型,可以取以下几个值(
	cv::FONT_HERSHEY_COMPLEX:复杂风格字体;
	cv::FONT_HERSHEY_COMPLEX_SMALL:小字号复杂风格字体;
	cv::FONT_HERSHEY_DUPLEX:双线条字体;
	cv::FONT_HERSHEY_PLAIN:单线条字体;
	cv::FONT_HERSHEY_SIMPLEX:正常大小的字体;
	cv::FONT_HERSHEY_TRIPLEX:三线条字体;
)
fontScale:输入参数,表示字体大小缩放比例;
color: 输入参数,表示绘制文本的颜色以及透明度,是一个 cv::Scalar 类型的对象,常见的颜色有(
	红色(0, 0, 255);
	绿色(0, 255, 0);
	蓝色(255, 0, 0)等;)
thickness:可选参数,表示文本轮廓线条的宽度:(
	默认值为1,表示绘制文本轮廓线条宽度为1像素;
	thickness大于0表示绘制文本轮廓线条宽度为thickness的值;小于0表示绘制一个填充文本)
lineType:可选参数,表示文本轮廓线条的类型,可以取以下几个值:(
	cv::LINE_4: 表示绘制四个相邻的点的文本边界,默认值;
	cv::LINE_8: 表示绘制八个相邻的点的文本边界;
	cv::LINE_AA: 表示绘制抗锯齿的文本边界;)
bottomLeftOrigin:可选参数,表示坐标点是否为文本框左下角的坐标点(
	默认值为 false,表示坐标点为文本框左下角的坐标点;
	设置成 true,表示坐标点为文本框左上角的坐标点;
)

(2) Sample code:
#include <opencv2\opencv.hpp>
#include <iostream>
#include <demo.h>

using namespace cv;
using namespace std;

int main() {
    
    

	// 创建一张大小为 512x512,3通道的图片
	Mat canvas = Mat::zeros(Size(512, 512), CV_8UC3);
	// 待绘制的文本
	std::string text = "Hello, OpenCV!";
	// 开始绘制文本
	cv::putText(canvas, text, cv::Point(0, 0), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 0, 255, 255), 2, LINE_8, true);

	// 显示图片
	imshow("canvas", canvas);

	waitKey();
	destroyAllWindows();
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_33867131/article/details/131552307