学习OpenCV2——绘制基本图形及文字

    OpenCV中可以绘制的图形有直线、矩形、多边形、圆、椭圆。以及一个写文本的函数puttext

1. 基本函数

Line

C++: void line(Mat& img, Point pt1,Point pt2, const Scalar& color, int thickness=1, int lineType=8,int shift=0)
Parameters:

img – 图像.

pt1 – 线条起点.

pt2 – 线条终点.

color – 线条颜色.

thickness – 线条宽度.

lineType – 线型

Type of the line:  8 (or omitted) - 8-连接线.

                           4 - 4-连接线.

                           CV_AA - 反走样线条.

shift – 坐标点小数点位数.


Rectangle

C++: void rectangle(Mat& img,Point pt1, Pointpt2, const Scalar&color, intthickness=1,intlineType=8, intshift=0)
C++: void rectangle(Mat& img,Rect rec, const Scalar&color, intthickness=1, intlineType=8,intshift=0 )

Parameters:

img – 画矩形的对象

pt1 – 矩形的一个顶点,左上角的.

pt2 – 另一个顶点,右下角的.

rec – 确定矩形的另一种方式,给左上角坐标和长宽

color – 指定矩形的颜色或亮度(灰度图像),scalar(255,0,255)既可指定.

thickness – 矩形边框的粗细. 负值(like CV_FILLED)表示要画一个填充的矩形

lineType – 边框线型. ( 8 (or 0) - 8-connected line(8邻接)连接线。

                                      4 - 4-connected line(4邻接)连接线。

                                      CV_AA - antialiased 线条。)

shift –坐标点的小数点位数


PolyLine

C++: void polylines(Mat& img, const Point** pts, const int* npts, int ncontours, bool isClosed, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )
C++: void polylines(InputOutputArray img, InputArrayOfArrays pts, bool isClosed, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )

Parameters:

img – 折线所在图像.

pts – 折线中拐点坐标指针.

npts – 折线拐点个数指针.

ncontours – 折线线段数量.

isClosed – 折线是否闭合.

color – 折线颜色.

thickness – 折线宽度.

lineType – 线型.

shift – 顶点坐标小数点位数.


Circle

C++: void circle(Mat&img, Point center, intradius, const Scalar&color,intthickness=1, intlineType=8, intshift=0)
Parameters:

img – 要画圆的那个矩形.

center – 圆心坐标.

radius – 半径.

color – 圆边框颜色,scalar类型的

thickness – 正值表示圆边框宽度. 负值表示画一个填充圆形

lineType – 圆边框线型

shift – 圆心坐标和半径的小数点位数


Ellipse

C++: 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)
C++: void ellipse(Mat& img, constRotatedRect& box, const Scalar& color, int thickness=1, int lineType=8)

Parameters:

img – 椭圆所在图像.

center – 椭圆中心.

axes – 椭圆主轴一半的长度

angle – 椭圆旋转角度

startAngle – 椭圆弧起始角度

endAngle –椭圆弧终止角度

box – 指定椭圆中心和旋转角度的信息,通过 RotatedRect 或 CvBox2D. 这表示椭圆画在旋转矩形上(矩形是不可见的,只是指定了一个框而已)

color – 椭圆边框颜色.

thickness – 正值代表椭圆边框宽度,负值代表填充的椭圆

lineType – 线型

shift – 椭圆中心坐标和坐标轴的小数点位数


PutText

C++: void putText(Mat& img, const string& text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=8, bool bottomLeftOrigin=false )
Parameters:

img – 显示文字所在图像.

text – 待显示的文字.

org – 文字在图像中的左下角 坐标.

font – 字体结构体.

fontFace – 字体类型, 可选择字体:

FONT_HERSHEY_SIMPLEX,                    FONT_HERSHEY_PLAIN,                             

          FONT_HERSHEY_DUPLEX,                     FONT_HERSHEY_COMPLEX, 
          FONT_HERSHEY_TRIPLEX,                     FONT_HERSHEY_COMPLEX_SMALL, 
          FONT_HERSHEY_SCRIPT_SIMPLEX, or FONT_HERSHEY_SCRIPT_COMPLEX,

以上所有类型都可以配合 FONT_HERSHEY_ITALIC使用,产生斜体效果。

fontScale – 字体大小,该值和字体内置大小相乘得到字体大小

color – 文本颜色

thickness –  写字的线的粗细

lineType – 线型.

bottomLeftOrigin – true, 图像数据原点在左下角. Otherwise, 图像数据原点在左上角.


2. 一些参数取值情况

lineType:    8或0  8-连接线

                    4   4-连接线

                    CV_AA 反走样线条

thickness: >0,线条粗细   <0,填充

fontFace : 

        CV_FONT_HERSHEY_SIMPLEX                       正常尺寸sanserif字体
        CV_FONT_HERSHEY_PLAIN                            小尺寸sanserif字体
        CV_FONT_HERSHEY_DUPLEX                         正常尺寸sanserif, 比CV_FONT_HERSHEY_SIMPLEX更复杂
        CV_FONT_HERSHEY_COMPLEX                      正常尺寸serif, 比CV_FONT_HERSHEY_DUPLEX更复杂
        CV_FONT_HERSHEY_TRIPLEX                         正常尺寸serif, 比CV_FONT_HERSHEY_COMPLEX更复杂
        CV_FONT_HERSHEY_COMPLEX_SMALL        小尺寸的CV_FONT_HERSHEY_COMPLEX
        CV_FONT_HERSHEY_SCRIPT_SIMPLEX        手写风格
        CV_FONT_HERSHEY_SCRIPT_COMPLEX       比CV_FONT_HERSHEY_SCRIPT_SIMPLEX更复杂的风格


常见颜色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

 

 

 

 

 


更全的RGB颜色对照表参见 :http://tool.oschina.net/commons?type=3

3. 示例代码

例1.

#include <iostream>
#include "cv.h"
#include "highgui.h"

using namespace std;
using namespace cv;

static void help()
{
	cout << "This program demonstrates OpenCV drawing and text output functions" << endl 
		<< "Usage:" << endl
		<<"./drawing" << endl;
}

static Scalar randomColor(RNG& rng)
{
	int iColor = unsigned(rng);
	//255 = 0xFF
	return Scalar(iColor & 255,(iColor >> 8) & 255,(iColor >> 16) & 255);    //产生一种颜色
}

int main(int argc,char** argv)
{
	help();
	char wndName[] = "Drawing Demo";
	const int randomNumber = 100;
	const int DELAY = 10;
	int lineType = CV_AA;
	int height = 700;
	int width = 1000;
	int x1 = - width/2;      //-500
	int x2 = 3 * width/2;    //1500
	int y1 = - height/2;     //-350
	int y2 = 3 * height/2;   //1050
	RNG rng(0xFFFFFFFF);

	
	Mat image = Mat::zeros(height,width,CV_8UC3);
	imshow(wndName,image);
	waitKey(DELAY);

	//draw line
	for(int i = 0;i < randomNumber;i++)
	{
		Point pt1,pt2;
		pt1.x = rng.uniform(x1,x2);
		pt1.y = rng.uniform(y1,y2);
		pt2.x = rng.uniform(x1,x2);
		pt2.y = rng.uniform(y1,y2);
		line(image,pt1,pt2,randomColor(rng),rng.uniform(1,10),lineType);
		imshow(wndName,image);
		if(waitKey(DELAY) >= 0)
			return 0;
	}

	//draw rectangle
	for(int i = 0;i < randomNumber;i++)
	{
		Point pt1,pt2;
		pt1.x = rng.uniform(x1,x2);
		pt1.y = rng.uniform(y1,y2);
		pt2.x = rng.uniform(x1,x2);
		pt2.y = rng.uniform(y1,y2);
		int thickness = rng.uniform(-3,10);

		/*----------------------draws a simple, thick, or filled up-right rectangle-----------
		*	C++: void rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, 
								int thickness=1, int lineType=8,int shift=0)
		*	C++: void rectangle(Mat& img, Rect rec, const Scalar& color, int thickness=1, 
								int lineType=8, int shift=0)
		*	img – image
		*	pt1 – Vertex of the rectangle                    矩形的一个顶点p1
		*	pt2 – Vertex of the rectangle opposite to pt1    矩形的另一个顶点,与p1相对
		*	rec – Alternative specification of the drawn rectangle
		*	color – Rectangle color or brightness (grayscale image)      线条颜色,对于灰度图是亮度
		*	thickness – Thickness of lines that make up the rectangle. Negative values,   线条粗细
			like CV_FILLED, mean that the function has to draw a filled rectangle
		*	lineType – Type of the line. See the line() description       线条类型
		*	shift – Number of fractional bits in the point coordinates    
		-----------------------------------------------------------------------------*/
		rectangle(image,pt1,pt2,randomColor(rng),MAX(thickness,-1),lineType);
		imshow(wndName,image);
		if(waitKey(DELAY) >= 0)
			return 0;
	}




	//draw ellipse
	for(int i = 0; i < randomNumber;i++)
	{
		Point center;
		center.x = rng.uniform(x1,x2);
		center.y = rng.uniform(y1,y2);
		Size axes;
		axes.width = rng.uniform(0,200);
		axes.height = rng.uniform(0,200);
		double angle = rng.uniform(0,180);

		/*---------draws a simple or thick elliptic arc or fills an ellipse sector---------
		*	C++: 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)
		*	C++: void ellipse(Mat& img, const RotatedRect& box, const Scalar& color, 
								int thickness=1, int lineType=8)
		*	img – image 
		*	center – Center of the ellipse 椭圆中心
		*	axes – Half of the size of the ellipse main axes 椭圆长轴的一半
		*	angle – Ellipse rotation angle in degrees 椭圆旋转的角度
		*	startAngle – Starting angle of the elliptic arc in degrees 弧度开始的角度
		*	endAngle – Ending angle of the elliptic arc in degrees 弧度结束的角度
		*	box – Alternative ellipse representation via RotatedRect or CvBox2D
			This means that the function draws an ellipse inscribed in the rotated rectangle
		*	color – Ellipse color
		*	thickness – Thickness of the ellipse arc outline, if positive. Otherwise, this indicates that a
			filled ellipse sector is to be drawn
		*	lineType – Type of the ellipse boundary. See the line() description
		*	shift – Number of fractional bits in the coordinates of the center and values of axes
		-----------------------------------------------------------------------------*/
		ellipse(image,center,axes,angle,angle - 100,angle + 200,randomColor(rng),rng.uniform(1,8),lineType);
		imshow(wndName,image);
		if(waitKey(DELAY) >= 0)
			return 0;
	}

	//draw polylines
	for(int i = 0;i < randomNumber;i++)
	{
		Point pt[2][3];
		pt[0][0].x = rng.uniform(x1,x2);
		pt[0][0].y = rng.uniform(y1,y2);
		pt[0][1].x = rng.uniform(x1,x2);
		pt[0][1].y = rng.uniform(y1,y2);
		pt[0][2].x = rng.uniform(x1,x2);
		pt[0][2].y = rng.uniform(y1,y2);
		pt[1][0].x = rng.uniform(x1,x2);
		pt[1][0].y = rng.uniform(y1,y2);
		pt[1][1].x = rng.uniform(x1,x2);
		pt[1][1].y = rng.uniform(y1,y2);
		pt[1][2].x = rng.uniform(x1,x2);
		pt[1][2].y = rng.uniform(y1,y2);
		const Point* ppt[2] = {pt[0],pt[1]};
		int npt[] = {3,3};

		/*-------------------draws several polygonal curves----------------------------
		*	C++: void polylines(Mat& img, const Point** pts, const int* npts, int ncontours, 
									bool isClosed, const Scalar& color, int thickness=1,
										int lineType=8, int shift=0 )
		*	C++: void polylines(InputOutputArray img, InputArrayOfArrays pts, bool isClosed, 
							const Scalar& color,int thickness=1, int lineType=8, int shift=0 )
		*	img – image
		*	pts – Array of polygonal curves 多边形曲线数组
		*	npts – Array of polygon vertex counters 顶点数组
		*	ncontours – Number of curves 曲线数量
		*	isClosed – Flag indicating whether the drawn polylines are closed or not
						If they are closed,the function draws a line from the last vertex 
						of each curve to its first vertex 标志曲线是否闭合
		*	color – Polyline color
		*	thickness – Thickness of the polyline edges
		*	lineType – Type of the line segments. See the line() description
		*	shift – Number of fractional bits in the vertex coordinates 
		-----------------------------------------------------------------------------*/
		polylines(image,ppt,npt,2,TRUE,randomColor(rng),rng.uniform(1,10),lineType);
		imshow(wndName,image);
		if(waitKey(DELAY) >= 0)
			return 0;
	}

	//draw polygons with filled area
	for(int i = 0;i < randomNumber;i++)
	{
		Point pt[2][3];
		pt[0][0].x = rng.uniform(x1, x2);
		pt[0][0].y = rng.uniform(y1, y2);
		pt[0][1].x = rng.uniform(x1, x2);
		pt[0][1].y = rng.uniform(y1, y2);
		pt[0][2].x = rng.uniform(x1, x2);
		pt[0][2].y = rng.uniform(y1, y2);
		pt[1][0].x = rng.uniform(x1, x2);
		pt[1][0].y = rng.uniform(y1, y2);
		pt[1][1].x = rng.uniform(x1, x2);
		pt[1][1].y = rng.uniform(y1, y2);
		pt[1][2].x = rng.uniform(x1, x2);
		pt[1][2].y = rng.uniform(y1, y2);
		const Point* ppt[2] = {pt[0], pt[1]};
		int npt[] = {3, 3};

		/*--------------fills the area bounded by one or more polygons---------------
		*	C++: void fillPoly( Mat& img, const Point** pts, const int* npts, int ncontours,
						const Scalar& color, int lineType=8, int shift=0, Point offset=Point() )
		*	img – image
		*	pts – Array of polygons where each polygon is represented as an array of points
		*	npts – Array of polygon vertex counters
		*	ncontours – Number of contours that bind the filled region
		*	color – Polygon color
		*	lineType – Type of the polygon boundaries. See the line() description
		*	shift – Number of fractional bits in the vertex coordinates
		*	offset – Optional offset of all points of the contours
		-----------------------------------------------------------------------------*/
		fillPoly(image, ppt, npt, 2, randomColor(rng), lineType);
		imshow(wndName, image);
		if(waitKey(DELAY) >= 0)
			return 0;
	}

	//draw circle
	for(int i = 0;i < randomNumber;i++)
	{
		Point center;
		center.x = rng.uniform(x1,x2);
		center.y = rng.uniform(y1,y2);

		/*-----------------------------draw a circle----------------------------------
		*	C++: void circle(Mat& img, Point center, int radius, const Scalar& color,
								int thickness=1, int lineType=8,int shift=0)
		*	img – Image where the circle is drawn
		*	center – Center of the circle
		*	radius – Radius of the circle
		*	color – Circle color
		*	thickness – Thickness of the circle outline, if positive.
						Negative thickness means that a
		*	filled circle is to be drawn
		*	lineType – Type of the circle boundary. See the line() description
		*	shift – Number of fractional bits in the coordinates of the center and 
			in the radius value
		-----------------------------------------------------------------------------*/
		circle(image,center,rng.uniform(0,300),randomColor(rng),rng.uniform(-1,9),lineType);
		imshow(wndName,image);
		if(waitKey(DELAY) >= 0)
			return 0;
	}

	//put text on the image
	for(int i = 0;i < randomNumber;i++)
	{
		Point org;
		org.x = rng.uniform(x1,x2);
		org.y = rng.uniform(y1,y2);
		putText(image,"Testing text rendering",org,rng.uniform(0,8)/*font type*/,
				rng.uniform(0,100)*0.05 + 0.1/*font scale*/,
					randomColor(rng),rng.uniform(1,10)/*thickness*/,lineType);
		imshow(wndName,image);
		if(waitKey(DELAY) >= 0)
			return 0;
	}
	
	/*------------------calculates the width and height of a text string--------------
	*	C++: Size getTextSize( const string& text, int fontFace, double fontScale,
								int thickness, int* baseLine)
	*	text – Input text string
	*	fontFace – Font to use. See the putText() for details
	*	fontScale – Font scale. See the putText() for details
	*	thickness – Thickness of lines used to render the text
	*	baseLine – Output parameter - y-coordinate of the baseline relative 
					to the bottom-most text	point.
	--------------------------------------------------------------------------------------*/
	//string text = " OpenCV Forever!" ;
	//int fontFace = FONT_HERSHEY_COMPLEX;
	//double fontScale = 2;
	//int thickness = 3;
	//int baseline=0;
	//baseline += thickness;
	//Size textSize = getTextSize(text, fontFace,
	//						fontScale, thickness, &baseline);

	Size textSize = getTextSize("OpenCV Forever!",FONT_HERSHEY_COMPLEX,3,5,0);
	Point org((width - textSize.width)/2,(height - textSize.height)/2);

	Mat image2;
	for(int i = 0;i < 255;i += 2)
	{
		image2 = image - Scalar::all(i);
		putText(image2,"OpenCV Forever!",org,FONT_HERSHEY_COMPLEX,
			3,Scalar(i,i,255),5,lineType);

		imshow(wndName,image2);
		if(waitKey(DELAY) >= 0)
			return 0;
	}

	waitKey();
	return 0;
}
结果




例2.

/**
* @file Drawing_1.cpp
* @brief Simple sample code
*/

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

#define w 400

using namespace cv;

/// Function headers
void MyEllipse( Mat img, double angle );
void MyFilledCircle( Mat img, Point center );
void MyPolygon( Mat img );
void MyLine( Mat img, Point start, Point end );

/**
* @function main
* @brief Main function
*/
int main( void ){

	/// Windows names
	char atom_window[] = "Drawing 1: Atom";
	char rook_window[] = "Drawing 2: Rook";

	/// Create black empty images
	Mat atom_image = Mat::zeros( w, w, CV_8UC3 );
	Mat rook_image = Mat::zeros( w, w, CV_8UC3 );

	/// 1. Draw a simple atom:
	/// -----------------------

	/// 1.a. Creating ellipses
	MyEllipse( atom_image, 90 );
	MyEllipse( atom_image, 0 );
	MyEllipse( atom_image, 45 );
	MyEllipse( atom_image, -45 );

	/// 1.b. Creating circles
	MyFilledCircle( atom_image, Point( w/2, w/2) );

	/// 2. Draw a rook
	/// ------------------

	/// 2.a. Create a convex polygon
	MyPolygon( rook_image );

	/// 2.b. Creating rectangles
	rectangle( rook_image,
		Point( 0, 7*w/8 ),
		Point( w, w),
		Scalar( 0, 255, 255 ),
		-1,
		8 );

	RotatedRect rRect = RotatedRect(Point2f(100,100), Size2f(100,50), 30);
	ellipse(rook_image, rRect, Scalar(255,255,0));

	/// 2.c. Create a few lines
	MyLine( rook_image, Point( 0, 15*w/16 ), Point( w, 15*w/16 ) );
	MyLine( rook_image, Point( w/4, 7*w/8 ), Point( w/4, w ) );
	MyLine( rook_image, Point( w/2, 7*w/8 ), Point( w/2, w ) );
	MyLine( rook_image, Point( 3*w/4, 7*w/8 ), Point( 3*w/4, w ) );

	/// 3. Display your stuff!
	imshow( atom_window, atom_image );
	moveWindow( atom_window, 0, 200 );
	imshow( rook_window, rook_image );
	moveWindow( rook_window, w, 200 );

	waitKey( 0 );
	return(0);
}

/// Function Declaration

/**
* @function MyEllipse
* @brief Draw a fixed-size ellipse with different angles
*/
void MyEllipse( Mat img, double angle )
{
	int thickness = 2;
	int lineType = 8;

	ellipse( img,
		Point( w/2, w/2 ),
		Size( w/4, w/16 ),
		angle,
		0,
		360,
		Scalar( 255, 0, 0 ),
		thickness,
		lineType );
}

/**
* @function MyFilledCircle
* @brief Draw a fixed-size filled circle
*/
void MyFilledCircle( Mat img, Point center )
{
	int thickness = -1;
	int lineType = 8;

	circle( img,
		center,
		w/32,
		Scalar( 0, 0, 255 ),
		thickness,
		lineType );
}

/**
* @function MyPolygon
* @function Draw a simple concave polygon (rook)
*/
void MyPolygon( Mat img )
{
	int lineType = 8;

	/** Create some points */
	Point rook_points[1][20];
	rook_points[0][0]  = Point(    w/4,   7*w/8 );
	rook_points[0][1]  = Point(  3*w/4,   7*w/8 );
	rook_points[0][2]  = Point(  3*w/4,  13*w/16 );
	rook_points[0][3]  = Point( 11*w/16, 13*w/16 );
	rook_points[0][4]  = Point( 19*w/32,  3*w/8 );
	rook_points[0][5]  = Point(  3*w/4,   3*w/8 );
	rook_points[0][6]  = Point(  3*w/4,     w/8 );
	rook_points[0][7]  = Point( 26*w/40,    w/8 );
	rook_points[0][8]  = Point( 26*w/40,    w/4 );
	rook_points[0][9]  = Point( 22*w/40,    w/4 );
	rook_points[0][10] = Point( 22*w/40,    w/8 );
	rook_points[0][11] = Point( 18*w/40,    w/8 );
	rook_points[0][12] = Point( 18*w/40,    w/4 );
	rook_points[0][13] = Point( 14*w/40,    w/4 );
	rook_points[0][14] = Point( 14*w/40,    w/8 );
	rook_points[0][15] = Point(    w/4,     w/8 );
	rook_points[0][16] = Point(    w/4,   3*w/8 );
	rook_points[0][17] = Point( 13*w/32,  3*w/8 );
	rook_points[0][18] = Point(  5*w/16, 13*w/16 );
	rook_points[0][19] = Point(    w/4,  13*w/16 );

	const Point* ppt[1] = { rook_points[0] };
	int npt[] = { 20 };

	fillPoly( img,
		ppt,
		npt,
		1,
		Scalar( 255, 255, 255 ),
		lineType );
}

/**
* @function MyLine
* @brief Draw a simple line
*/
void MyLine( Mat img, Point start, Point end )
{
	int thickness = 2;
	int lineType = 8;
	line( img,
		start,
		end,
		Scalar( 0, 0, 0 ),
		thickness,
		lineType );
}
结果
学习OpenCV2——绘制基本图形及文字_第1张图片




你可能感兴趣的:(绘图,opencv2)