OPencv基本图形的绘制

摘自毛星云 opencv3 chapter 4.3


#include <opencv2/opencv.hpp>
#include <iostream>
#include <string>
#define WINDOW_WIDTH 600
#define WINDOW_NAME1 "【绘制图1】"
#define WINDOW_NAME2 "【绘制图2】"
using namespace std;
using namespace cv;

void DrawEllipse(Mat img,double angle)
{
	int thickness =2;
	int lineType = 2;
	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;
	circle(img,center,WINDOW_WIDTH/32,Scalar(0,0,255),thickness,lineType);
}
void DrawPolygon(Mat img)
{
	int lineType = 8;
	Point rookPoint[1][20]; //二位数组,因为Point 要保存两个值
	rookPoint[0][0]=Point(WINDOW_WIDTH/4,7*WINDOW_WIDTH/8);
	rookPoint[0][1]=Point(3*WINDOW_WIDTH/4,7*WINDOW_WIDTH/8);
	rookPoint[0][2]=Point(3*WINDOW_WIDTH/4,13*WINDOW_WIDTH/16);
	rookPoint[0][3]=Point(11*WINDOW_WIDTH/16,13*WINDOW_WIDTH/16);
	rookPoint[0][4]=Point(19*WINDOW_WIDTH/32,3*WINDOW_WIDTH/8);
	rookPoint[0][5]=Point(3*WINDOW_WIDTH/4,3*WINDOW_WIDTH/8);
	rookPoint[0][6]=Point(3*WINDOW_WIDTH/4,WINDOW_WIDTH/8);
	rookPoint[0][7]=Point(26*WINDOW_WIDTH/40,WINDOW_WIDTH/8);
	rookPoint[0][8]=Point(26*WINDOW_WIDTH/40,WINDOW_WIDTH/4);
	rookPoint[0][9]=Point(22*WINDOW_WIDTH/40,WINDOW_WIDTH/4);
	rookPoint[0][10]=Point(22*WINDOW_WIDTH/40,WINDOW_WIDTH/8);
	rookPoint[0][11]=Point(18*WINDOW_WIDTH/40,WINDOW_WIDTH/8);
	rookPoint[0][12]=Point(18*WINDOW_WIDTH/40,WINDOW_WIDTH/4);
	rookPoint[0][13]=Point(14*WINDOW_WIDTH/40,WINDOW_WIDTH/4);
	rookPoint[0][14]=Point(14*WINDOW_WIDTH/40,WINDOW_WIDTH/8);
	rookPoint[0][15]=Point(WINDOW_WIDTH/4,WINDOW_WIDTH/8);
	rookPoint[0][16]=Point(WINDOW_WIDTH/4,3*WINDOW_WIDTH/8);
	rookPoint[0][17]=Point(13*WINDOW_WIDTH/32,3*WINDOW_WIDTH/8);
	rookPoint[0][18]=Point(5*WINDOW_WIDTH/16,13*WINDOW_WIDTH/16);
	rookPoint[0][19]=Point(WINDOW_WIDTH/4,13*WINDOW_WIDTH/16);

	const Point* ppt[1] = {rookPoint[0]};
	int npt[] = {20};
	fillPoly(img,ppt,npt,1,Scalar(255,255,255),lineType);
}
void DrawLine(Mat img,Point start,Point end)
{
	int thickness =2 ;
	int lineType = 8;
	line(img,start,end,Scalar(0,0,0),thickness,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));
	
	DrawPolygon(rookImage);
	rectangle(rookImage,Point(0,7*WINDOW_WIDTH/8),Point(WINDOW_WIDTH,WINDOW_WIDTH),Scalar(0,255,255),-1,8);

	DrawLine(rookImage,Point(0,15*WINDOW_WIDTH/16),Point(WINDOW_WIDTH,15*WINDOW_WIDTH/16));
	DrawLine(rookImage,Point(WINDOW_WIDTH/4,7*WINDOW_WIDTH/8),Point(WINDOW_WIDTH/4,WINDOW_WIDTH));
	DrawLine(rookImage,Point(WINDOW_WIDTH/2,7*WINDOW_WIDTH/8),Point(WINDOW_WIDTH/2,WINDOW_WIDTH));
	DrawLine(rookImage,Point(3*WINDOW_WIDTH/4,7*WINDOW_WIDTH/8),Point(3*WINDOW_WIDTH,WINDOW_WIDTH/4));

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

	imshow(WINDOW_NAME2,rookImage);
	moveWindow(WINDOW_NAME2,WINDOW_WIDTH,200);

	waitKey();
	system("pause");
	return 0 ; 
}


发布了33 篇原创文章 · 获赞 28 · 访问量 13万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览