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

来源:互联网 发布:和平网络电视频道地址 编辑:程序博客网 时间:2024/05/01 00:31

 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.

[cpp] view plain copy
  1. #include <iostream>  
  2. #include "cv.h"  
  3. #include "highgui.h"  
  4.   
  5. using namespace std;  
  6. using namespace cv;  
  7.   
  8. static void help()  
  9. {  
  10.     cout << "This program demonstrates OpenCV drawing and text output functions" << endl   
  11.         << "Usage:" << endl  
  12.         <<"./drawing" << endl;  
  13. }  
  14.   
  15. static Scalar randomColor(RNG& rng)  
  16. {  
  17.     int iColor = unsigned(rng);  
  18.     //255 = 0xFF  
  19.     return Scalar(iColor & 255,(iColor >> 8) & 255,(iColor >> 16) & 255);    //产生一种颜色  
  20. }  
  21.   
  22. int main(int argc,char** argv)  
  23. {  
  24.     help();  
  25.     char wndName[] = "Drawing Demo";  
  26.     const int randomNumber = 100;  
  27.     const int DELAY = 10;  
  28.     int lineType = CV_AA;  
  29.     int height = 700;  
  30.     int width = 1000;  
  31.     int x1 = - width/2;      //-500  
  32.     int x2 = 3 * width/2;    //1500  
  33.     int y1 = - height/2;     //-350  
  34.     int y2 = 3 * height/2;   //1050  
  35.     RNG rng(0xFFFFFFFF);  
  36.   
  37.       
  38.     Mat image = Mat::zeros(height,width,CV_8UC3);  
  39.     imshow(wndName,image);  
  40.     waitKey(DELAY);  
  41.   
  42.     //draw line  
  43.     for(int i = 0;i < randomNumber;i++)  
  44.     {  
  45.         Point pt1,pt2;  
  46.         pt1.x = rng.uniform(x1,x2);  
  47.         pt1.y = rng.uniform(y1,y2);  
  48.         pt2.x = rng.uniform(x1,x2);  
  49.         pt2.y = rng.uniform(y1,y2);  
  50.         line(image,pt1,pt2,randomColor(rng),rng.uniform(1,10),lineType);  
  51.         imshow(wndName,image);  
  52.         if(waitKey(DELAY) >= 0)  
  53.             return 0;  
  54.     }  
  55.   
  56.     //draw rectangle  
  57.     for(int i = 0;i < randomNumber;i++)  
  58.     {  
  59.         Point pt1,pt2;  
  60.         pt1.x = rng.uniform(x1,x2);  
  61.         pt1.y = rng.uniform(y1,y2);  
  62.         pt2.x = rng.uniform(x1,x2);  
  63.         pt2.y = rng.uniform(y1,y2);  
  64.         int thickness = rng.uniform(-3,10);  
  65.   
  66.         /*----------------------draws a simple, thick, or filled up-right rectangle----------- 
  67.         *   C++: void rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color,  
  68.                                 int thickness=1, int lineType=8,int shift=0) 
  69.         *   C++: void rectangle(Mat& img, Rect rec, const Scalar& color, int thickness=1,  
  70.                                 int lineType=8, int shift=0) 
  71.         *   img – image 
  72.         *   pt1 – Vertex of the rectangle                    矩形的一个顶点p1 
  73.         *   pt2 – Vertex of the rectangle opposite to pt1    矩形的另一个顶点,与p1相对 
  74.         *   rec – Alternative specification of the drawn rectangle 
  75.         *   color – Rectangle color or brightness (grayscale image)      线条颜色,对于灰度图是亮度 
  76.         *   thickness – Thickness of lines that make up the rectangle. Negative values,   线条粗细 
  77.             like CV_FILLED, mean that the function has to draw a filled rectangle 
  78.         *   lineType – Type of the line. See the line() description       线条类型 
  79.         *   shift – Number of fractional bits in the point coordinates     
  80.         -----------------------------------------------------------------------------*/  
  81.         rectangle(image,pt1,pt2,randomColor(rng),MAX(thickness,-1),lineType);  
  82.         imshow(wndName,image);  
  83.         if(waitKey(DELAY) >= 0)  
  84.             return 0;  
  85.     }  
  86.   
  87.   
  88.   
  89.   
  90.     //draw ellipse  
  91.     for(int i = 0; i < randomNumber;i++)  
  92.     {  
  93.         Point center;  
  94.         center.x = rng.uniform(x1,x2);  
  95.         center.y = rng.uniform(y1,y2);  
  96.         Size axes;  
  97.         axes.width = rng.uniform(0,200);  
  98.         axes.height = rng.uniform(0,200);  
  99.         double angle = rng.uniform(0,180);  
  100.   
  101.         /*---------draws a simple or thick elliptic arc or fills an ellipse sector--------- 
  102.         *   C++: void ellipse(Mat& img, Point center, Size axes, double angle,  
  103.                                 double startAngle,double endAngle,const Scalar& color,  
  104.                                 int thickness=1, int lineType=8, int shift=0) 
  105.         *   C++: void ellipse(Mat& img, const RotatedRect& box, const Scalar& color,  
  106.                                 int thickness=1, int lineType=8) 
  107.         *   img – image  
  108.         *   center – Center of the ellipse 椭圆中心 
  109.         *   axes – Half of the size of the ellipse main axes 椭圆长轴的一半 
  110.         *   angle – Ellipse rotation angle in degrees 椭圆旋转的角度 
  111.         *   startAngle – Starting angle of the elliptic arc in degrees 弧度开始的角度 
  112.         *   endAngle – Ending angle of the elliptic arc in degrees 弧度结束的角度 
  113.         *   box – Alternative ellipse representation via RotatedRect or CvBox2D 
  114.             This means that the function draws an ellipse inscribed in the rotated rectangle 
  115.         *   color – Ellipse color 
  116.         *   thickness – Thickness of the ellipse arc outline, if positive. Otherwise, this indicates that a 
  117.             filled ellipse sector is to be drawn 
  118.         *   lineType – Type of the ellipse boundary. See the line() description 
  119.         *   shift – Number of fractional bits in the coordinates of the center and values of axes 
  120.         -----------------------------------------------------------------------------*/  
  121.         ellipse(image,center,axes,angle,angle - 100,angle + 200,randomColor(rng),rng.uniform(1,8),lineType);  
  122.         imshow(wndName,image);  
  123.         if(waitKey(DELAY) >= 0)  
  124.             return 0;  
  125.     }  
  126.   
  127.     //draw polylines  
  128.     for(int i = 0;i < randomNumber;i++)  
  129.     {  
  130.         Point pt[2][3];  
  131.         pt[0][0].x = rng.uniform(x1,x2);  
  132.         pt[0][0].y = rng.uniform(y1,y2);  
  133.         pt[0][1].x = rng.uniform(x1,x2);  
  134.         pt[0][1].y = rng.uniform(y1,y2);  
  135.         pt[0][2].x = rng.uniform(x1,x2);  
  136.         pt[0][2].y = rng.uniform(y1,y2);  
  137.         pt[1][0].x = rng.uniform(x1,x2);  
  138.         pt[1][0].y = rng.uniform(y1,y2);  
  139.         pt[1][1].x = rng.uniform(x1,x2);  
  140.         pt[1][1].y = rng.uniform(y1,y2);  
  141.         pt[1][2].x = rng.uniform(x1,x2);  
  142.         pt[1][2].y = rng.uniform(y1,y2);  
  143.         const Point* ppt[2] = {pt[0],pt[1]};  
  144.         int npt[] = {3,3};  
  145.   
  146.         /*-------------------draws several polygonal curves---------------------------- 
  147.         *   C++: void polylines(Mat& img, const Point** pts, const int* npts, int ncontours,  
  148.                                     bool isClosed, const Scalar& color, int thickness=1, 
  149.                                         int lineType=8, int shift=0 ) 
  150.         *   C++: void polylines(InputOutputArray img, InputArrayOfArrays pts, bool isClosed,  
  151.                             const Scalar& color,int thickness=1, int lineType=8, int shift=0 ) 
  152.         *   img – image 
  153.         *   pts – Array of polygonal curves 多边形曲线数组 
  154.         *   npts – Array of polygon vertex counters 顶点数组 
  155.         *   ncontours – Number of curves 曲线数量 
  156.         *   isClosed – Flag indicating whether the drawn polylines are closed or not 
  157.                         If they are closed,the function draws a line from the last vertex  
  158.                         of each curve to its first vertex 标志曲线是否闭合 
  159.         *   color – Polyline color 
  160.         *   thickness – Thickness of the polyline edges 
  161.         *   lineType – Type of the line segments. See the line() description 
  162.         *   shift – Number of fractional bits in the vertex coordinates  
  163.         -----------------------------------------------------------------------------*/  
  164.         polylines(image,ppt,npt,2,TRUE,randomColor(rng),rng.uniform(1,10),lineType);  
  165.         imshow(wndName,image);  
  166.         if(waitKey(DELAY) >= 0)  
  167.             return 0;  
  168.     }  
  169.   
  170.     //draw polygons with filled area  
  171.     for(int i = 0;i < randomNumber;i++)  
  172.     {  
  173.         Point pt[2][3];  
  174.         pt[0][0].x = rng.uniform(x1, x2);  
  175.         pt[0][0].y = rng.uniform(y1, y2);  
  176.         pt[0][1].x = rng.uniform(x1, x2);  
  177.         pt[0][1].y = rng.uniform(y1, y2);  
  178.         pt[0][2].x = rng.uniform(x1, x2);  
  179.         pt[0][2].y = rng.uniform(y1, y2);  
  180.         pt[1][0].x = rng.uniform(x1, x2);  
  181.         pt[1][0].y = rng.uniform(y1, y2);  
  182.         pt[1][1].x = rng.uniform(x1, x2);  
  183.         pt[1][1].y = rng.uniform(y1, y2);  
  184.         pt[1][2].x = rng.uniform(x1, x2);  
  185.         pt[1][2].y = rng.uniform(y1, y2);  
  186.         const Point* ppt[2] = {pt[0], pt[1]};  
  187.         int npt[] = {3, 3};  
  188.   
  189.         /*--------------fills the area bounded by one or more polygons--------------- 
  190.         *   C++: void fillPoly( Mat& img, const Point** pts, const int* npts, int ncontours, 
  191.                         const Scalar& color, int lineType=8, int shift=0, Point offset=Point() ) 
  192.         *   img – image 
  193.         *   pts – Array of polygons where each polygon is represented as an array of points 
  194.         *   npts – Array of polygon vertex counters 
  195.         *   ncontours – Number of contours that bind the filled region 
  196.         *   color – Polygon color 
  197.         *   lineType – Type of the polygon boundaries. See the line() description 
  198.         *   shift – Number of fractional bits in the vertex coordinates 
  199.         *   offset – Optional offset of all points of the contours 
  200.         -----------------------------------------------------------------------------*/  
  201.         fillPoly(image, ppt, npt, 2, randomColor(rng), lineType);  
  202.         imshow(wndName, image);  
  203.         if(waitKey(DELAY) >= 0)  
  204.             return 0;  
  205.     }  
  206.   
  207.     //draw circle  
  208.     for(int i = 0;i < randomNumber;i++)  
  209.     {  
  210.         Point center;  
  211.         center.x = rng.uniform(x1,x2);  
  212.         center.y = rng.uniform(y1,y2);  
  213.   
  214.         /*-----------------------------draw a circle---------------------------------- 
  215.         *   C++: void circle(Mat& img, Point center, int radius, const Scalar& color, 
  216.                                 int thickness=1, int lineType=8,int shift=0) 
  217.         *   img – Image where the circle is drawn 
  218.         *   center – Center of the circle 
  219.         *   radius – Radius of the circle 
  220.         *   color – Circle color 
  221.         *   thickness – Thickness of the circle outline, if positive. 
  222.                         Negative thickness means that a 
  223.         *   filled circle is to be drawn 
  224.         *   lineType – Type of the circle boundary. See the line() description 
  225.         *   shift – Number of fractional bits in the coordinates of the center and  
  226.             in the radius value 
  227.         -----------------------------------------------------------------------------*/  
  228.         circle(image,center,rng.uniform(0,300),randomColor(rng),rng.uniform(-1,9),lineType);  
  229.         imshow(wndName,image);  
  230.         if(waitKey(DELAY) >= 0)  
  231.             return 0;  
  232.     }  
  233.   
  234.     //put text on the image  
  235.     for(int i = 0;i < randomNumber;i++)  
  236.     {  
  237.         Point org;  
  238.         org.x = rng.uniform(x1,x2);  
  239.         org.y = rng.uniform(y1,y2);  
  240.         putText(image,"Testing text rendering",org,rng.uniform(0,8)/*font type*/,  
  241.                 rng.uniform(0,100)*0.05 + 0.1/*font scale*/,  
  242.                     randomColor(rng),rng.uniform(1,10)/*thickness*/,lineType);  
  243.         imshow(wndName,image);  
  244.         if(waitKey(DELAY) >= 0)  
  245.             return 0;  
  246.     }  
  247.       
  248.     /*------------------calculates the width and height of a text string-------------- 
  249.     *   C++: Size getTextSize( const string& text, int fontFace, double fontScale, 
  250.                                 int thickness, int* baseLine) 
  251.     *   text – Input text string 
  252.     *   fontFace – Font to use. See the putText() for details 
  253.     *   fontScale – Font scale. See the putText() for details 
  254.     *   thickness – Thickness of lines used to render the text 
  255.     *   baseLine – Output parameter - y-coordinate of the baseline relative  
  256.                     to the bottom-most text point. 
  257.     --------------------------------------------------------------------------------------*/  
  258.     //string text = " OpenCV Forever!" ;  
  259.     //int fontFace = FONT_HERSHEY_COMPLEX;  
  260.     //double fontScale = 2;  
  261.     //int thickness = 3;  
  262.     //int baseline=0;  
  263.     //baseline += thickness;  
  264.     //Size textSize = getTextSize(text, fontFace,  
  265.     //                      fontScale, thickness, &baseline);  
  266.   
  267.     Size textSize = getTextSize("OpenCV Forever!",FONT_HERSHEY_COMPLEX,3,5,0);  
  268.     Point org((width - textSize.width)/2,(height - textSize.height)/2);  
  269.   
  270.     Mat image2;  
  271.     for(int i = 0;i < 255;i += 2)  
  272.     {  
  273.         image2 = image - Scalar::all(i);  
  274.         putText(image2,"OpenCV Forever!",org,FONT_HERSHEY_COMPLEX,  
  275.             3,Scalar(i,i,255),5,lineType);  
  276.   
  277.         imshow(wndName,image2);  
  278.         if(waitKey(DELAY) >= 0)  
  279.             return 0;  
  280.     }  
  281.   
  282.     waitKey();  
  283.     return 0;  
  284. }  
结果




例2.

[cpp] view plain copy
  1. /** 
  2. * @file Drawing_1.cpp 
  3. * @brief Simple sample code 
  4. */  
  5.   
  6. #include <opencv2/core/core.hpp>  
  7. #include <opencv2/highgui/highgui.hpp>  
  8.   
  9. #define w 400  
  10.   
  11. using namespace cv;  
  12.   
  13. /// Function headers  
  14. void MyEllipse( Mat img, double angle );  
  15. void MyFilledCircle( Mat img, Point center );  
  16. void MyPolygon( Mat img );  
  17. void MyLine( Mat img, Point start, Point end );  
  18.   
  19. /** 
  20. * @function main 
  21. * @brief Main function 
  22. */  
  23. int main( void ){  
  24.   
  25.     /// Windows names  
  26.     char atom_window[] = "Drawing 1: Atom";  
  27.     char rook_window[] = "Drawing 2: Rook";  
  28.   
  29.     /// Create black empty images  
  30.     Mat atom_image = Mat::zeros( w, w, CV_8UC3 );  
  31.     Mat rook_image = Mat::zeros( w, w, CV_8UC3 );  
  32.   
  33.     /// 1. Draw a simple atom:  
  34.     /// -----------------------  
  35.   
  36.     /// 1.a. Creating ellipses  
  37.     MyEllipse( atom_image, 90 );  
  38.     MyEllipse( atom_image, 0 );  
  39.     MyEllipse( atom_image, 45 );  
  40.     MyEllipse( atom_image, -45 );  
  41.   
  42.     /// 1.b. Creating circles  
  43.     MyFilledCircle( atom_image, Point( w/2, w/2) );  
  44.   
  45.     /// 2. Draw a rook  
  46.     /// ------------------  
  47.   
  48.     /// 2.a. Create a convex polygon  
  49.     MyPolygon( rook_image );  
  50.   
  51.     /// 2.b. Creating rectangles  
  52.     rectangle( rook_image,  
  53.         Point( 0, 7*w/8 ),  
  54.         Point( w, w),  
  55.         Scalar( 0, 255, 255 ),  
  56.         -1,  
  57.         8 );  
  58.   
  59.     RotatedRect rRect = RotatedRect(Point2f(100,100), Size2f(100,50), 30);  
  60.     ellipse(rook_image, rRect, Scalar(255,255,0));  
  61.   
  62.     /// 2.c. Create a few lines  
  63.     MyLine( rook_image, Point( 0, 15*w/16 ), Point( w, 15*w/16 ) );  
  64.     MyLine( rook_image, Point( w/4, 7*w/8 ), Point( w/4, w ) );  
  65.     MyLine( rook_image, Point( w/2, 7*w/8 ), Point( w/2, w ) );  
  66.     MyLine( rook_image, Point( 3*w/4, 7*w/8 ), Point( 3*w/4, w ) );  
  67.   
  68.     /// 3. Display your stuff!  
  69.     imshow( atom_window, atom_image );  
  70.     moveWindow( atom_window, 0, 200 );  
  71.     imshow( rook_window, rook_image );  
  72.     moveWindow( rook_window, w, 200 );  
  73.   
  74.     waitKey( 0 );  
  75.     return(0);  
  76. }  
  77.   
  78. /// Function Declaration  
  79.   
  80. /** 
  81. * @function MyEllipse 
  82. * @brief Draw a fixed-size ellipse with different angles 
  83. */  
  84. void MyEllipse( Mat img, double angle )  
  85. {  
  86.     int thickness = 2;  
  87.     int lineType = 8;  
  88.   
  89.     ellipse( img,  
  90.         Point( w/2, w/2 ),  
  91.         Size( w/4, w/16 ),  
  92.         angle,  
  93.         0,  
  94.         360,  
  95.         Scalar( 255, 0, 0 ),  
  96.         thickness,  
  97.         lineType );  
  98. }  
  99.   
  100. /** 
  101. * @function MyFilledCircle 
  102. * @brief Draw a fixed-size filled circle 
  103. */  
  104. void MyFilledCircle( Mat img, Point center )  
  105. {  
  106.     int thickness = -1;  
  107.     int lineType = 8;  
  108.   
  109.     circle( img,  
  110.         center,  
  111.         w/32,  
  112.         Scalar( 0, 0, 255 ),  
  113.         thickness,  
  114.         lineType );  
  115. }  
  116.   
  117. /** 
  118. * @function MyPolygon 
  119. * @function Draw a simple concave polygon (rook) 
  120. */  
  121. void MyPolygon( Mat img )  
  122. {  
  123.     int lineType = 8;  
  124.   
  125.     /** Create some points */  
  126.     Point rook_points[1][20];  
  127.     rook_points[0][0]  = Point(    w/4,   7*w/8 );  
  128.     rook_points[0][1]  = Point(  3*w/4,   7*w/8 );  
  129.     rook_points[0][2]  = Point(  3*w/4,  13*w/16 );  
  130.     rook_points[0][3]  = Point( 11*w/16, 13*w/16 );  
  131.     rook_points[0][4]  = Point( 19*w/32,  3*w/8 );  
  132.     rook_points[0][5]  = Point(  3*w/4,   3*w/8 );  
  133.     rook_points[0][6]  = Point(  3*w/4,     w/8 );  
  134.     rook_points[0][7]  = Point( 26*w/40,    w/8 );  
  135.     rook_points[0][8]  = Point( 26*w/40,    w/4 );  
  136.     rook_points[0][9]  = Point( 22*w/40,    w/4 );  
  137.     rook_points[0][10] = Point( 22*w/40,    w/8 );  
  138.     rook_points[0][11] = Point( 18*w/40,    w/8 );  
  139.     rook_points[0][12] = Point( 18*w/40,    w/4 );  
  140.     rook_points[0][13] = Point( 14*w/40,    w/4 );  
  141.     rook_points[0][14] = Point( 14*w/40,    w/8 );  
  142.     rook_points[0][15] = Point(    w/4,     w/8 );  
  143.     rook_points[0][16] = Point(    w/4,   3*w/8 );  
  144.     rook_points[0][17] = Point( 13*w/32,  3*w/8 );  
  145.     rook_points[0][18] = Point(  5*w/16, 13*w/16 );  
  146.     rook_points[0][19] = Point(    w/4,  13*w/16 );  
  147.   
  148.     const Point* ppt[1] = { rook_points[0] };  
  149.     int npt[] = { 20 };  
  150.   
  151.     fillPoly( img,  
  152.         ppt,  
  153.         npt,  
  154.         1,  
  155.         Scalar( 255, 255, 255 ),  
  156.         lineType );  
  157. }  
  158.   
  159. /** 
  160. * @function MyLine 
  161. * @brief Draw a simple line 
  162. */  
  163. void MyLine( Mat img, Point start, Point end )  
  164. {  
  165.     int thickness = 2;  
  166.     int lineType = 8;  
  167.     line( img,  
  168.         start,  
  169.         end,  
  170.         Scalar( 0, 0, 0 ),  
  171.         thickness,  
  172.         lineType );  
  173. }  
结果


0 0
原创粉丝点击