opencv提供的一些绘图函数,来简单的绘制一个简易的图形,包括线段、矩形、椭圆、多边形等。
opencv提供了line()函数来对直线的绘制。其原型如下:
CV_EXPORTS_W void line(CV_IN_OUT Mat& img, Point pt1, Point pt2, const Scalar& color,
int thickness=1, int lineType=8, int shift=0);
img: 要绘制线段的图像。opencv提供了ellipse()函数来绘制椭圆。其原型如下:
CV_EXPORTS_W void ellipse(CV_IN_OUT 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: 要绘制椭圆的图像。opencv提供了rectangle()函数来绘制矩形。其原型如下:
CV_EXPORTS_W void rectangle(CV_IN_OUT Mat& img, Point pt1, Point pt2,
const Scalar& color, int thickness=1,int lineType=8, int shift=0);
pt1: 矩形的左上角坐标。opencv提供了circle()函数来绘制圆。其原型如下:
CV_EXPORTS_W void circle(CV_IN_OUT Mat& img, Point center, int radius,
const Scalar& color, int thickness=1,int lineType=8, int shift=0);
center: 圆心坐标。opencv为我们提供了fillPoly()函数来绘制填充多边形。其原型如下:
CV_EXPORTS void fillPoly(Mat& img, const Point** pts,const int* npts, int ncontours,
const Scalar& color, int lineType=8, int shift=0,Point offset=Point() );
pts: 多边形定点集。
void putText(Mat& img, const string& text, Point org, int fontFace, double fontScale, Scalar color,
int thickness=1, int lineType=8, bool bottomLeftOrigin=false )
利用上述函数,实例1:
#include
#include
using namespace cv;
#define WINDOW_SIZE 400
void DrawEllipse(Mat img,double angle);
void DrawFilledCircle(Mat img,Point center);
void DrawPolygon(Mat img);
void DrawLine(Mat img,Point start,Point end);
int main(int argc,char** argv)
{
//创建空白的图像
Mat atomImg=Mat::zeros(WINDOW_SIZE,WINDOW_SIZE,CV_8UC3);
Mat rookImg=Mat::zeros(WINDOW_SIZE,WINDOW_SIZE,CV_8UC3);
//------------------------------------绘制原子的示例图------------------------------------
//绘制出椭圆
DrawEllipse(atomImg,90);
DrawEllipse(atomImg,0);
DrawEllipse(atomImg,45);
DrawEllipse(atomImg,-45);
namedWindow("ImageEll",CV_WINDOW_NORMAL);
imshow("ImageEll",atomImg);
//再绘制出圆心
DrawFilledCircle(atomImg,Point(WINDOW_SIZE/2,WINDOW_SIZE/2));
namedWindow("ImageEll+Circle",CV_WINDOW_NORMAL);
imshow("ImageEll+Circle",atomImg);
//绘制会多边形
DrawPolygon(rookImg);
//显示多边形
namedWindow("polygon",CV_WINDOW_NORMAL);
imshow("polygon",rookImg);
//绘制矩形
rectangle(rookImg,Point(0,7*WINDOW_SIZE/8), //两个对角定点
Point(WINDOW_SIZE,WINDOW_SIZE),
Scalar(0,255,255), //矩阵颜色图像
-1, //由于线粗为-1,,此举真将被填充
8);
//显示多边形+矩形
namedWindow("ImageRook",CV_WINDOW_NORMAL);
imshow("ImageRook",rookImg);
waitKey(0);
return 0;
}
/***************************************************************************************************
函数功能:
绘制一条直线
函数参数:
1---画一条从点start到点end的直线段
2---此线段将被滑到图像img上
3---线的颜色有Scalar(0,0,0)来定义
4---线的粗细由thickness设定
5---线的类型由lineType决定,此处为8连通
***************************************************************************************************/
void DrawLine(Mat img,Point start,Point end)
{
int thickness=2;
int lineType=8;
line(img,start,end,Scalar(0,0,0),thickness,lineType);
}
/***************************************************************************************************
函数功能:
绘制一个椭圆
函数参数:
1---椭圆将被画到img图像上
2---椭圆的中心点为Point(WINDOW_SIZE/2.0,WINDOW_SIZE/2.0),并且大小位于矩阵
Size(WINDOW_SIZE/4.0,WINDOW_SIZE/16.0),
3---椭圆的旋转角度为angle
4---椭圆拓展的弧度为0到360度
5--图形颜色为Scalar(255,0,255)
6--椭圆的粗度为thickness
7--线条的类型为lineType---8连通域
***************************************************************************************************/
void DrawEllipse(Mat img,double angle)
{
int thickness=2;
int lineType=8;
ellipse(img,
Point(WINDOW_SIZE/2.0,WINDOW_SIZE/2.0),
Size(WINDOW_SIZE/4.0,WINDOW_SIZE/16.0),
angle,
0,
360,
Scalar(255,0,255),
thickness,
lineType);
}
/***************************************************************************************************
函数功能:
绘制一个圆
函数参数:
1---圆将被画到img图像上
2---圆心由center定义
3---圆的半径为:WINDOW_SIZE/32.0
4---圆的颜色Scalar(0,0,255)
5---线的粗细为thickness=-1,因此,圆将被填充
***************************************************************************************************/
void DrawFilledCircle(Mat img,Point center)
{
int thickness=-1;
int lineType=8;
circle(img,
center,
WINDOW_SIZE/32.0,
Scalar(0,0,255),
thickness,
lineType);
}
/***************************************************************************************************
函数功能:
绘制多边形
函数参数:
1---多边形将被画到img上
2---多边形的顶点集为ppt
3---要绘制的多边形定点数目为npt
4---要绘制的多边形数量为1
5---多边形的颜色定义为Scalar(255,255,255),即BGR的值为白色
***************************************************************************************************/
void DrawPolygon(Mat img)
{
int lineType=0;
//创建一些点
Point rookPoints[1][20];
rookPoints[0][0] =Point(WINDOW_SIZE/4,7*WINDOW_SIZE/8);
rookPoints[0][1] =Point(3*WINDOW_SIZE/4,7*WINDOW_SIZE/8);
rookPoints[0][2] = Point(3*WINDOW_SIZE/4,13*WINDOW_SIZE/16 );
rookPoints[0][3] = Point(11*WINDOW_SIZE/16,13*WINDOW_SIZE/16 );
rookPoints[0][4] = Point(19*WINDOW_SIZE/32,3*WINDOW_SIZE/8 );
rookPoints[0][5] = Point(3*WINDOW_SIZE/4,3*WINDOW_SIZE/8 );
rookPoints[0][6] = Point(3*WINDOW_SIZE/4,WINDOW_SIZE/8 );
rookPoints[0][7] = Point(26*WINDOW_SIZE/40,WINDOW_SIZE/8 );
rookPoints[0][8] = Point(26*WINDOW_SIZE/40,WINDOW_SIZE/4 );
rookPoints[0][9] = Point(22*WINDOW_SIZE/40,WINDOW_SIZE/4 );
rookPoints[0][10] = Point(22*WINDOW_SIZE/40,WINDOW_SIZE/8 );
rookPoints[0][11] = Point(18*WINDOW_SIZE/40,WINDOW_SIZE/8 );
rookPoints[0][12] = Point(18*WINDOW_SIZE/40,WINDOW_SIZE/4 );
rookPoints[0][13] = Point(14*WINDOW_SIZE/40,WINDOW_SIZE/4 );
rookPoints[0][14] = Point(14*WINDOW_SIZE/40,WINDOW_SIZE/8 );
rookPoints[0][15] = Point(WINDOW_SIZE/4,WINDOW_SIZE/8 );
rookPoints[0][16] = Point(WINDOW_SIZE/4,3*WINDOW_SIZE/8 );
rookPoints[0][17] = Point(13*WINDOW_SIZE/32,3*WINDOW_SIZE/8 );
rookPoints[0][18] = Point(5*WINDOW_SIZE/16,13*WINDOW_SIZE/16 );
rookPoints[0][19] = Point(WINDOW_SIZE/4,13*WINDOW_SIZE/16 );
const Point* ppt[1]={rookPoints[0]};
int npt[]={20};
fillPoly(img,
ppt,
npt,
1,
Scalar(255,255,255),
lineType);
}
运行结果如下:
附:常见颜色RGB值对照表
|
R |
G |
B |
值 |
|
R |
G |
B |
值 |
|
R |
G |
B |
值 |
黑色 |
0 |
0 |
0 |
#000000 |
黄色 |
255 |
255 |
0 |
#FFFF00 |
浅灰蓝色 |
176 |
224 |
230 |
#B0E0E6 |
象牙黑 |
41 |
36 |
33 |
#292421 |
香蕉色 |
227 |
207 |
87 |
#E3CF57 |
品蓝 |
65 |
105 |
225 |
#4169E1 |
灰色 |
192 |
192 |
192 |
#C0C0C0 |
镉黄 |
255 |
153 |
18 |
#FF9912 |
石板蓝 |
106 |
90 |
205 |
#6A5ACD |
冷灰 |
128 |
138 |
135 |
#808A87 |
dougello |
235 |
142 |
85 |
#EB8E55 |
天蓝 |
135 |
206 |
235 |
#87CEEB |
石板灰 |
112 |
128 |
105 |
#708069 |
forum gold |
255 |
227 |
132 |
#FFE384 |
|
|
|
|
|
暖灰色 |
128 |
128 |
105 |
#808069 |
金黄色 |
255 |
215 |
0 |
#FFD700 |
青色 |
0 |
255 |
255 |
#00FFFF |
|
|
|
|
|
黄花色 |
218 |
165 |
105 |
#DAA569 |
绿土 |
56 |
94 |
15 |
#385E0F |
白色 |
225 |
225 |
225 |
#FFFFFF |
瓜色 |
227 |
168 |
105 |
#E3A869 |
靛青 |
8 |
46 |
84 |
#082E54 |
古董白 |
250 |
235 |
215 |
#FAEBD7 |
橙色 |
255 |
97 |
0 |
#FF6100 |
碧绿色 |
127 |
255 |
212 |
#7FFFD4 |
天蓝色 |
240 |
255 |
255 |
#F0FFFF |
镉橙 |
255 |
97 |
3 |
#FF6103 |
青绿色 |
64 |
224 |
208 |
#40E0D0 |
白烟 |
245 |
245 |
245 |
#F5F5F5 |
胡萝卜色 |
237 |
145 |
33 |
#ED9121 |
绿色 |
0 |
255 |
0 |
#00FF00 |
白杏仁 |
255 |
235 |
205 |
#FFFFCD |
桔黄 |
255 |
128 |
0 |
#FF8000 |
黄绿色 |
127 |
255 |
0 |
#7FFF00 |
cornsilk |
255 |
248 |
220 |
#FFF8DC |
淡黄色 |
245 |
222 |
179 |
#F5DEB3 |
钴绿色 |
61 |
145 |
64 |
#3D9140 |
蛋壳色 |
252 |
230 |
201 |
#FCE6C9 |
|
|
|
|
|
翠绿色 |
0 |
201 |
87 |
#00C957 |
花白 |
255 |
250 |
240 |
#FFFAF0 |
棕色 |
128 |
42 |
42 |
#802A2A |
森林绿 |
34 |
139 |
34 |
#228B22 |
gainsboro |
220 |
220 |
220 |
#DCDCDC |
米色 |
163 |
148 |
128 |
#A39480 |
草地绿 |
124 |
252 |
0 |
#7CFC00 |
ghostWhite |
248 |
248 |
255 |
#F8F8FF |
锻浓黄土色 |
138 |
54 |
15 |
#8A360F |
酸橙绿 |
50 |
205 |
50 |
#32CD32 |
蜜露橙 |
240 |
255 |
240 |
#F0FFF0 |
锻棕土色 |
135 |
51 |
36 |
#873324 |
薄荷色 |
189 |
252 |
201 |
#BDFCC9 |
象牙白 |
250 |
255 |
240 |
#FAFFF0 |
巧克力色 |
210 |
105 |
30 |
#D2691E |
草绿色 |
107 |
142 |
35 |
#6B8E23 |
亚麻色 |
250 |
240 |
230 |
#FAF0E6 |
肉色 |
255 |
125 |
64 |
#FF7D40 |
暗绿色 |
48 |
128 |
20 |
#308014 |
navajoWhite |
255 |
222 |
173 |
#FFDEAD |
黄褐色 |
240 |
230 |
140 |
#F0E68C |
海绿色 |
46 |
139 |
87 |
#2E8B57 |
old lace |
253 |
245 |
230 |
#FDF5E6 |
玫瑰红 |
188 |
143 |
143 |
#BC8F8F |
嫩绿色 |
0 |
255 |
127 |
#00FF7F |
海贝壳色 |
255 |
245 |
238 |
#FFF5EE |
肖贡土色 |
199 |
97 |
20 |
#C76114 |
|
|
|
|
|
雪白 |
255 |
250 |
250 |
#FFFAFA |
标土棕 |
115 |
74 |
18 |
#734A12 |
紫色 |
160 |
32 |
240 |
#A020F0 |
|
|
|
|
|
乌贼墨棕 |
94 |
38 |
18 |
#5E2612 |
紫罗蓝色 |
138 |
43 |
226 |
#8A2BE2 |
红色 |
255 |
0 |
0 |
#FF0000 |
赫色 |
160 |
82 |
45 |
#A0522D |
jasoa |
160 |
102 |
211 |
#A066D3 |
砖红 |
156 |
102 |
31 |
#9C661F |
马棕色 |
139 |
69 |
19 |
#8B4513 |
湖紫色 |
153 |
51 |
250 |
#9933FA |
镉红 |
227 |
23 |
13 |
#E3170D |
沙棕色 |
244 |
164 |
96 |
#F4A460 |
淡紫色 |
218 |
112 |
214 |
#DA70D6 |
珊瑚色 |
255 |
127 |
80 |
#FF7F50 |
棕褐色 |
210 |
180 |
140 |
#D2B48C |
梅红色 |
221 |
160 |
221 |
#DDA0DD |
耐火砖红 |
178 |
34 |
34 |
#B22222 |
|
|
|
|
|
|
|
|
|
|
印度红 |
176 |
23 |
31 |
#B0171F |
蓝色 |
0 |
0 |
255 |
#0000FF |
|
|
|
|
|
栗色 |
176 |
48 |
96 |
#B03060 |
钴色 |
61 |
89 |
171 |
#3D59AB |
|
|
|
|
|
粉红 |
255 |
192 |
203 |
#FFC0CB |
dodger blue |
30 |
144 |
255 |
#1E90FF |
|
|
|
|
|
草莓色 |
135 |
38 |
87 |
#872657 |
jackie blue |
11 |
23 |
70 |
#0B1746 |
|
|
|
|
|
橙红色 |
250 |
128 |
114 |
#FA8072 |
锰蓝 |
3 |
168 |
158 |
#03A89E |
|
|
|
|
|
蕃茄红 |
255 |
99 |
71 |
#FF6347 |
深蓝色 |
25 |
25 |
112 |
#191970 |
|
|
|
|
|
桔红 |
255 |
69 |
0 |
#FF4500 |
孔雀蓝 |
51 |
161 |
201 |
#33A1C9 |
|
|
|
|
|
深红色 |
255 |
0 |
255 |
#FF00FF |
土耳其玉色 |
0 |
199 |
140 |
#00C78C |
|
|
|
|
|
参考:
http://blog.csdn.NET/ubunfans/article/details/24421981
http://lib.csdn.Net/article/opencv/29052
http://blog.csdn.net/gdfsg/article/details/50867809