简单图形绘制

前言

看毛星云大大的教程的测试代码就把我看晕了……所以没有前言。

测试代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <time.h>
#include <string>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;

#define WINDOW_WIDTH 600
#define WINDOW_NAME1 "Picture1"
#define WINDOW_NAME2 "Picture2"

void DrawEllipse(Mat img, double angle);
void DrawFilledCircle(Mat img,Point center);
void DrawLine(Mat img,Point start,Point end);
void DrawPolygon(Mat img);
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);
9)
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/4,WINDOW_WIDTH));

imshow(WINDOW_NAME1,atomImage);
moveWindow(WINDOW_NAME1,0,200);
imshow(WINDOW_NAME2,rookImage);
moveWindow(WINDOW_NAME2,WINDOW_WIDTH,200);

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 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 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);
}
void DrawLine(Mat img,Point start,Point end){
int thinkness = 2;
int linetype = 8;
line(img,
start,
end,
Scalar(0,0,0),
thinkness,
linetype);
}

效果图:

一、void DrawEllipse(Mat img, double angle)

该函数调用了OpenCV中的ellipse函数用以绘制椭圆。
函数原型:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void 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
)

void ellipse(
Mat& img,
const RotatedRect& box,
const Scalar& color,
int thickness=1,
int lineType=8
)

二、void DrawFilledCircle(Mat img,Point center)

该函数调用circle函数用以绘制圆形。
函数原型:

1
2
3
4
5
6
7
8
9
void circle(
InputOutputArray img,
Point center,
int radius,
const Scalar &color,
int thickness = 1,
int lineType = 8,
int shift = 0
)

在测试程序中,由于线粗设置为-1,因此圆形是实心的。

三、void DrawPolygon(Mat img)

该函数调用了fillPoly函数用以绘制自定义的多边形。
函数原型:

1
2
3
4
5
6
7
8
9
10
void fillPoly(
InputOutputArray img,
const Point **pts,
const int *npts,
int ncontours,
const Scalar &color,
int lineType = 8,
int shift = 0,
Point offset = Point()
)

在测试程序中,该函数的多边形的顶点集为ppt,需绘制的多边形顶点数目为npt,绘制图形数量为1。

四、void DrawLine(Mat img,Point start,Point end)

函数原型:

1
2
3
4
5
6
7
8
9
void line(
InputOutputArray img,
Point pt1,
Point pt2,
const Scalar &color,
int thickness = 1,
int lineType = 8,
int shift = 0
)

无甚可讲。