【小白】Open-CV 学习笔记- 4.3基本图形绘制

4.3.1 DrawEllipse()函数的写法(椭圆)

void 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);

ellipse函数将椭圆画到图像 lmg 上, 椭圆中心为点center,并且大小位于矩形 axes 内,椭圆旋转角度为 angle, 扩展的弧度从 0 度到 360 度,图形颜色为 Scalar(x, y,z)线宽 (thickness)为 1,线型(lineType)为 8 (8 联通线型)。

4.3.2 DrawFilledCircle()函数的写法(圆)

void circle(InputOutputArray img,  Point center,  int radius, 
 const Scalar& color,   int thickness = 1,   
int lineType = LINE_8,  int shift = 0);

img :表示输入的图像

center: 圆心坐标

radius: 圆的半径

color:Scalar类型,表示圆的颜色,例如蓝色为Scalar(255,0,0)

thickness:线的宽度

lineType:线的类型,(默认为8联通型)

代码演示

#include<iostream>
#include<opencv2/opencv.hpp>
using namespace cv;
using namespace std;

#define WINDOW_NAME1 "绘制图1"
#define WINDOW_NAME2 "绘制图2"
#define WINDOW_WIDTH 600    //定义窗口大小
string image = "C:\\Users\\asus\\Pictures\\Saved Pictures\\123.jpg";

void DrawEllipse(Mat img, double angle);
void DrawFi1ledCirc1e(Mat img, Point center);
int main()
{  
    Mat atomImage = Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);
    Mat rookImage = Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);
    //绘制椭圆
    DrawEllipse(atomImage, 90);
    DrawEllipse(atomImage, 0);
    DrawEllipse(atomImage, 45);
    DrawEllipse(atomImage, -45);

    //绘制圆心
    DrawFi1ledCirc1e(atomImage, Point(WINDOW_WIDTH / 2,WINDOW_WIDTH / 2));

    imshow(WINDOW_NAME1, atomImage);
    waitKey(0);
    return 0;
}
void DrawEllipse(Mat img, double angle) {
    int thickness = 2;
    int lineType = 8;
    ellipse(img, Point(WINDOW_WIDTH / 2, WINDOW_WIDTH / 2),
        Size(WINDOW_WIDTH / 4, WINDOW_WIDTH / 16), angle, 0, 360, Scalar(255, 129, 0),
        thickness, lineType);
}
void DrawFi1ledCirc1e(Mat img, Point center) {
    int thickness = -1;
    int lineType = 8;
    circle(img, center, WINDOW_WIDTH / 32, Scalar(0, 0, 255), thickness, lineType);
}

效果图:
在这里插入图片描述

4.3.3 DrawPolygon()函数写法

void DrawPolygon(Mat img) {
	int lineType = 8;
	//创建一些点
	Point rookPoints[1][20];
	rookPoints[0][0] = Point(WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8);
	rookPoints[0][1] = Point(3 * WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8);
	rookPoints[0][2] = Point(3 * WINDOW_WIDTH / 4, 13 * WINDOW_WIDTH / 16);
	rookPoints[0][3] = Point(11 * WINDOW_WIDTH / 16, 13 * WINDOW_WIDTH / 16);
	rookPoints[0][4] = Point(19 * WINDOW_WIDTH / 32, 3 * WINDOW_WIDTH / 8);
	rookPoints[0][5] = Point(3 * WINDOW_WIDTH / 4, 3*WINDOW_WIDTH / 8);
	rookPoints[0][6] = Point(3 * WINDOW_WIDTH / 4, WINDOW_WIDTH / 8);
	rookPoints[0][7] = Point(26 * WINDOW_WIDTH / 40,  WINDOW_WIDTH / 8);
	rookPoints[0][8] = Point(26 * WINDOW_WIDTH / 40,  WINDOW_WIDTH /4);
	rookPoints[0][9] = Point(22 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
	rookPoints[0][10] = Point(22 * WINDOW_WIDTH / 40,  WINDOW_WIDTH / 8);
	rookPoints[0][11] = Point(18*WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
	rookPoints[0][12] = Point(18*WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
	rookPoints[0][13] = Point(14*WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
	rookPoints[0][14] = Point(14*WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
	rookPoints[0][15] = Point(WINDOW_WIDTH / 4, WINDOW_WIDTH / 8);
	rookPoints[0][16] = Point(WINDOW_WIDTH / 4, 3 * WINDOW_WIDTH / 8);
	rookPoints[0][17] = Point(13*WINDOW_WIDTH / 32, 3 * WINDOW_WIDTH / 8);
	rookPoints[0][18] = Point(5*WINDOW_WIDTH / 16, 13 * WINDOW_WIDTH / 16);
	rookPoints[0][19] = Point(WINDOW_WIDTH / 4, 13 * WINDOW_WIDTH / 16);
	const Point*ppt[1] = {rookPoints[0]};
	int npt[] = {20};
	fillPoly(img,
			ppt,//多边形定点集
			npt,//要绘制的多边形的定点数
			1,//绘制多边形的个数
			Scalar(255,255,255),//白色
			lineType);//线型为8(8联通线型)
}

4.3.4 DrawLine()函数的写法

void DrawLine(Mat img,Point start,Point end) {
		int thickness = 2;
		int lineType = 8;
		line(img,
		start,
		end,//直线段的起始点
		Scalar(0,0,0),//黑色
		thickness,
		lineType);
}

4.3.5 DrawRectangle()函数的写法

//利用对角线两点来画矩形

void rectangle(InputOutputArray img, Point pt1, Point pt2, const Scalar& color, 
				int thickness = 1,int lineType = LINE_8, int shift = 0)

//传入矩形参数来画矩形

void rectangle(Mat& img, Rect rec, const Scalar& color, int thickness=1, 
int lineType=8, int shift=0 )
发布了34 篇原创文章 · 获赞 8 · 访问量 1900

猜你喜欢

转载自blog.csdn.net/weixin_43583163/article/details/96465978