//基本图形的绘制
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#define WINDOW_NAME1 "【绘制图1】"//为窗口标题定义的宏
#define WINDOW_NAME2 "【绘制图2】"//为窗口标题定义的宏
#define WINDOW_WIDTH 600 //定义窗口大小的宏
using namespace cv;
//DrawEllipse()函数的写法
void DrawEllipse(Mat img, double angle) {
int thickness = 2;//线宽
int lineType = 8;//线型
//调用OpenCV中的ellipse()函数,将椭圆画到图像img上
ellipse(img,//原图像
Point(WINDOW_WIDTH / 2, WINDOW_WIDTH / 2),//椭圆中心
Size(WINDOW_WIDTH / 4, WINDOW_WIDTH / 16),//椭圆的长和宽
angle,//椭圆的转动角度为0~360°
0,
360,
Scalar(255, 129, 0),//蓝色
thickness,//线宽
lineType);//线型
}
//DrawFilledCircle()函数的写法
void DrawFilledCircle(Mat img,Point center) {//传入图像和圆心位置
int thickness = -1;//表示绘制的圆是实心的
int lineType = 8;
//调用opencv中的circle()函数,将圆画到图像img上
circle(img,
center,//圆心由二维点center定义
WINDOW_WIDTH / 32,//半径
Scalar(0, 0, 255),//红色
thickness,//线宽
lineType);//线型
}
//DrawPolygon()函数的写法
void DrawPolygon(Mat img) {//多边形的绘制函数
int lineType = 8;
Point rootPoints[1][20];//创建一些点
rootPoints[0][0] = Point(WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8);
rootPoints[0][1] = Point(3*WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8);
rootPoints[0][2] = Point(3*WINDOW_WIDTH / 4, 13 * WINDOW_WIDTH / 16);
rootPoints[0][3] = Point(11*WINDOW_WIDTH / 16, 13 * WINDOW_WIDTH / 16);
rootPoints[0][4] = Point(19*WINDOW_WIDTH / 32, 3 * WINDOW_WIDTH / 8);
rootPoints[0][5] = Point(3*WINDOW_WIDTH / 4, 3 * WINDOW_WIDTH / 8);
rootPoints[0][6] = Point(3*WINDOW_WIDTH / 4, WINDOW_WIDTH / 8);
rootPoints[0][7] = Point(26*WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
rootPoints[0][8] = Point(26*WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
rootPoints[0][9] = Point(22*WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
rootPoints[0][10] = Point(22*WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
rootPoints[0][11] = Point(18*WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
rootPoints[0][12] = Point(18*WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
rootPoints[0][13] = Point(14*WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
rootPoints[0][14] = Point(14*WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
rootPoints[0][15] = Point(WINDOW_WIDTH / 4, WINDOW_WIDTH / 8);
rootPoints[0][16] = Point(WINDOW_WIDTH / 4, 3 * WINDOW_WIDTH / 8);
rootPoints[0][17] = Point(13*WINDOW_WIDTH / 32, 3 * WINDOW_WIDTH / 8);
rootPoints[0][18] = Point(5*WINDOW_WIDTH / 16, 13 * WINDOW_WIDTH / 16);
rootPoints[0][19] = Point(WINDOW_WIDTH / 4, 13 * WINDOW_WIDTH / 16);
const Point* ppt[1] = { rootPoints[0] };
int npt[] = { 20 };
//调用了OpenCV中的fillPoly()函数,用于将多边形画到图像img上
fillPoly(img,
ppt,//多边形的顶点集
npt,//多边形的顶点数目
1,//要绘制的多边形数目为1
Scalar(255,255,255),//白色
lineType);
}
//DrawLine()函数的写法
void DrawLine(Mat img, Point start, Point end) {
int thickness = 2;
int lineType = 8;
//调用了OpenCV中的line()函数,用于在图像img上画一条从start到end的直线段
line(img,
start,
end,
Scalar(0,0,0),//黑色
thickness,//线的粗细为2
lineType);
}
//以上是四个自定义函数的写法和分析,接下来我们看main函数的写法
int main(void) {
double time0 = static_cast<double>(getTickCount());//记录运行起始时间,以下开始运行
//创建空白的Mat图像
Mat atomImage = Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);
Mat rootImage = Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);
//----------------------<1>绘制化学中的原子示例图------------------------
//【1.1】先绘制出椭圆
DrawEllipse(atomImage, 90);
DrawEllipse(atomImage, 0);
DrawEllipse(atomImage, 45);
DrawEllipse(atomImage, -45);
//【1.2】再绘制圆心
DrawFilledCircle(atomImage, Point(WINDOW_WIDTH / 2, WINDOW_WIDTH / 2));
//----------------------<2>绘制组合图------------------------------------
//【2.1】先绘出多边形
DrawPolygon(rootImage);
//【2.2】绘制矩形
rectangle(rootImage,//操作对象
Point(0,7*WINDOW_WIDTH/8),//左上角点坐标
Point(WINDOW_WIDTH,WINDOW_WIDTH),//右下角点坐标
Scalar(0,255,255),//矩形颜色
-1,//实心矩形
8);
//【2.3】绘制一些线段
DrawLine(rootImage, Point(0, 15 * WINDOW_WIDTH / 16),Point(WINDOW_WIDTH,15*WINDOW_WIDTH/16));
DrawLine(rootImage, Point(WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8), Point(WINDOW_WIDTH / 4, WINDOW_WIDTH));
DrawLine(rootImage, Point(WINDOW_WIDTH / 2, 7 * WINDOW_WIDTH / 8), Point(WINDOW_WIDTH / 2, WINDOW_WIDTH));
DrawLine(rootImage, Point(3 * WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8), Point(3 * WINDOW_WIDTH / 4, WINDOW_WIDTH));
//--------------------------<3>显示绘制的图像---------------------------------
imshow(WINDOW_NAME1, atomImage);
moveWindow(WINDOW_NAME1, 50, 100);
imshow(WINDOW_NAME2, rootImage);
moveWindow(WINDOW_NAME2, WINDOW_WIDTH+50, 100);
time0 = ((double)getTickCount() - time0) / getTickFrequency();
std::cout << "绘图运行时间为:" << time0 << "秒" << std::endl;//输出运行时间
waitKey(0);
return(0);
}
运行结果如下: