OpenCV ————基本绘图

函数说明:

绘制矩形函数:rectangle(Mat& img, Point pt1, const Scalar& color, int tickness = 1, int lineType = 8, int shift = 0)
img: 所要绘制的矩形所在的图像
pt1: 矩形的顶点
pt2: 矩形pt2对角的顶点
color: 矩形的颜色
thickness: 矩形线的厚度
lineType: 矩形线的格式
shift:点坐标内的部分位数
rec: 所要绘制的矩形内容
绘制椭圆函数: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 )

img : 要绘制椭圆的图像
center : 椭圆圆心
axes: 椭圆主轴
angle:椭圆旋转角度度数
startAngle: 椭圆角度起始弧度
endAngle: 椭圆角度结束弧度
color: 椭圆颜色
thickness: 椭圆弧线的厚度
lineType: 绘制椭圆线的形态
8 :8的连接线 (8-connected line)
4 : 4的连接线(4-connected line)
CV_AA:非别名线(antialiased line)
shift: 椭圆心与轴值(values of axes)坐标的部分位数
box: 椭圆外围矩形
绘制圆: circle(Mat& img, Point center, int radius, const Scalar& color, int thickness = 1, lineType = 8, int shift = 0)
img : 要绘制圆的图像
center:圆的圆心
radius:圆的半径
color:圆的颜色
thickness:圆的厚度
lineType:绘制圆的形态
shift:圆心与轴值(values of axes)坐标的部分位数
将多边形围绕区域填满:fillPoly(Mat& img, const Point** npts, int ncontours, const Scalar& color, int lineType = 8, int shift =
0, Point offset = Point() )

img:要填满区域所在的图像
pst:多边形数组,数组内容是多边形点的数组
npst: 多边形顶点数数组
ncontours: 要填满区域的轮廓数
color:多边形的颜色
lineType:多边形的形态
shift:顶点坐标(vertex coordinate)的部分位数
offset:轮廓的位移点
绘制直线:line(Mat& img, Point pt1, Point pt2, const Scalar& color, int
thickness = 1, int lineType = 8, int shift = 0)

img:要绘制直线的图像
pt1:直线的起点
pt2:直线的终点
color:直线的颜色
thickness:直线的厚度
lineType:直线的形态
shift:点坐标(point coordinate)内的部分位数

//基本绘图
#include<opencv2/core/core.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>

#define w 400

using namespace cv;

//函数声明
void MyEllipse(Mat img, double angle);
void MyFilledCircle(Mat img,Point center);
void MyPolygon(Mat img);
void MyLine(Mat img, Point start, Point end);

int main(void)
{
	//窗口名称
	char atom_window[] = "Drawing 1: Atom";
	char rook_window[] = "Drawing 2: Rook";

	//建立空的黑图
	Mat atom_image = Mat::zeros(w, w, CV_8UC3);
	Mat rook_image = Mat::zeros(w, w, CV_8UC3);

	//1.简单绘图

	//1.a.绘制椭圆
	MyEllipse(atom_image, 90);
	MyEllipse(atom_image, 0);
	MyEllipse(atom_image, 45);
	MyEllipse(atom_image,-45);


	//1.b. 绘制图


	//2.绘制城堡

	//2.a 绘制凸面多边形
	MyPolygon(rook_image);

	//2.b 绘制长方形
	rectangle(rook_image, Point(0, 7 * w/8), Point(w, w), 
		Scalar(0, 255, 255), -1, 8);	

	//3.c. 绘制直线
	MyLine(rook_image, Point(0, 15 * w / 16), Point(w, 15 * w / 16));
	MyLine(rook_image, Point(w / 4, 7 * w / 8), Point(w / 4, w));
	MyLine(rook_image, Point(w / 4, 7 * 2 / 8), Point(w / 4, w));
	MyLine(rook_image, Point(3 * w / 4, 7 * w / 8), Point(3 * w / 4, w));

	//显示窗口
	imshow(atom_window,atom_image);

	//将窗口移动到指定位置
	moveWindow(atom_window, 0, 200);

	imshow(rook_window, rook_image);
	moveWindow(rook_window, 0, 200);

	waitKey();
	return 0;
}

//函数定义
//绘制椭圆
void MyEllipse(Mat img, double angle)
{
	int thickness = 2;
	int lineType = 8;

	ellipse(img, Point(w / 2), Size(w / 4, w / 16), 
		angle, 0, 360, Scalar(255, 0, 0), 
		thickness, lineType);
}

//绘制圆
void MyFilledCircle(Mat img, Point center)
{
	int thickness = -1;
	int lineType = 8;

	circle(img, center, w / 32, Scalar(0, 0, 255), thickness, lineType);
}


//绘制凸面多边形
void MyPolygon(Mat img)
{
	int lineType = 8;

	//先建立点
	Point rook_points[1][20];
	rook_points[0][0] = Point(w / 4, 7 * w / 8);
	rook_points[0][1] = Point(3 * w / 4, 7 * w / 8);
	rook_points[0][2] = Point(3 * w / 4, 13 * w / 16);
	rook_points[0][3] = Point(11 * w / 16, 13 * w / 16);
	rook_points[0][4] = Point(19 * w / 32, 3 * w / 8);
	rook_points[0][5] = Point(3 * w / 4, 3 * w / 8);
	rook_points[0][6] = Point(3 * w / 4, w / 8);
	rook_points[0][7] = Point(26 * w / 40, w / 8);
	rook_points[0][8] = Point(26 * w / 40, w / 4);
	rook_points[0][9] = Point(22 * w / 40, w / 4);
	rook_points[0][10] = Point(22 * w / 40, w / 8);
	rook_points[0][11] = Point(18 * w / 40, w / 8);
	rook_points[0][12] = Point(18 * w / 40, w / 4);
	rook_points[0][13] = Point(14 * w / 40, w / 4);
	rook_points[0][14] = Point(14 * w / 40, w / 8);
	rook_points[0][15] = Point(w / 4, w / 8);
	rook_points[0][16] = Point(w / 4, 3 * w / 8);
	rook_points[0][17] = Point(13 * w / 32, 3 * w / 8);
	rook_points[0][18] = Point(5 * w / 16, 13 * w / 8);
	rook_points[0][19] = Point(w / 4, 13 * w / 8);

	const Point* ppt[1] = { rook_points[10] };
	int npt[] = { 20 };

	fillPoly(img, ppt, npt, 1, Scalar(255, 255, 255), lineType);

}

//绘制直线
void MyLine(Mat img, Point start, Point end)
{
	int thickness = 2;
	int lineType = 8;

	line(img, start, end, Scalar(0, 0, 0), thickness, lineType);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值