OpenCV学习笔记4——基本函数和绘图

OpenCV学习笔记4——基本函数和绘图_第1张图片

OpenCV学习笔记4——基本函数和绘图_第2张图片

OpenCV学习笔记4——基本函数和绘图_第3张图片

#include
#include
#include
#include
using namespace cv;
#define WINDOW_NAME1 "【绘制图1】"
#define WINDOW_NAME2 "【绘制图2】"
#define WINDOW_WIDTH 600

//画椭圆
void DrawEllipse(Mat img, double angle)
{
	int thickness = 2;
	int lineType = 8;
	//将椭圆画到img,中心为point,
	//外接矩形为size,旋转角度为angle
	//扩展弧度0-360,颜色为scalar()
	//线宽thickness,线型linetype
	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 DrawFilledCircle(Mat img, Point center)
{
	int thickness = -1;
	int lineType = 8;
	//将圆画到img,圆心为center点
	//半径为WINDOW_WIDTH,颜色为scalar()
	//线宽线型同上
	circle(img, center, WINDOW_WIDTH/32, Scalar(0, 0, 255), thickness, lineType);
}

//画线
void DrawLine(Mat img, Point start, Point end)
{
	int thickness = 2;
	int lineType = 8;
	//start起点,end终点,scalar()颜色
	line(img, start, end, Scalar(0, 0, 0), thickness, lineType);
}

//画凹多边形
void DrawPolygon(Mat img)
{
	int lineType = 8;
	Point rookPoints[1][20];
	/*
	给这些点定位置
	*/
	const Point*ppt[1] = { rookPoints[0] };
	int npt[] = { 20 };
	//多边形顶点集合为ppt
	//顶点数目为npt
	//要绘制的多边形数量为1
	//颜色scalar()
	fillPoly(img, ppt, npt, 1, Scalar(255, 255, 255), lineType);
}
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);

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

	imshow(WINDOW_NAME1, atomImage);
	moveWindow(WINDOW_NAME1, 0, 200);

	DrawPolygon(rookImage);
	//对角线两点画矩形或者用一个Rect代替两个point
	rectangle(rookImage, Point(0, 7 * WINDOW_WIDTH / 8), Point(WINDOW_WIDTH, WINDOW_WIDTH), Scalar(0, 255, 255), -1, 8);
	rectangle(rookImage,Rect(WINDOW_WIDTH/4, WINDOW_WIDTH / 4, WINDOW_WIDTH / 4, WINDOW_WIDTH / 4), Scalar(255, 0, 255), -1, 8);
	imshow(WINDOW_NAME2, rookImage);
	waitKey(0);
	return 0;
}

效果如下:

OpenCV学习笔记4——基本函数和绘图_第4张图片

OpenCV学习笔记4——基本函数和绘图_第5张图片

真是一个神奇的东西!

你可能感兴趣的:(OpenCV)