(10)绘制形状和文字

基本代码,这个我自己没有实验

 1 #include "../common/common.hpp"
 2 
 3     static Mat bgImage;
 4     static void drawLines();
 5     static void drawRectangle();
 6     static void drawEllipse();
 7     static void drawCircle();
 8     static void drawPolygon();
 9     static void drawRandomLine();
10 
11     void main1_8(int argc, char** argv)
12     {
13         bgImage = imread(getCVImagesPath("images/test1.png"), IMREAD_COLOR);
14         drawLines();
15         drawRectangle();
16         drawEllipse();
17         drawCircle();
18         drawPolygon();
19         //参数:Mat,文字(不识别中文?),文字的位置(锚点?),字体,缩放,颜色,线宽,线类型
20         putText(bgImage, "Hello OpenCV", Point(0, 200), CV_FONT_HERSHEY_COMPLEX, 1.0, Scalar(0), 3, LINE_8);//绘制文字
21         imshow("src", bgImage);
22 
23         drawRandomLine();
24 
25         waitKey(0);
26     }
27 
28     void drawLines()//绘制线条
29     {
30         Point p1 = Point(20, 30);//Point表示平面上的点,OpenCV中的这些坐标值都是相对于图像本身像素值的
31         Point p2;
32         p2.x = 400;
33         p2.y = 400;
34         Scalar color = Scalar(0, 0, 255);
35         line(bgImage, p1, p2, color, 10, LINE_AA);//绘制线条到Mat,10是线宽、LINE_4或LINE_8是带锯齿的,LINE_AA是抗锯齿的
36     }
37 
38     void drawRectangle()//绘制矩形
39     {
40         //OpenCV的坐标系,原点在屏幕左上角,x朝右正,y朝下正
41         Rect rect = Rect(200, 100, 300, 300);//起始位置x、y,宽,高
42         Scalar color = Scalar(255, 0, 0);
43         rectangle(bgImage, rect, color, LINE_8);//绘制矩形到Mat,自带圆角。。
44     }
45 
46     void drawEllipse()//绘制椭圆
47     {
48         int width = bgImage.cols;
49         int height = bgImage.rows;
50         Scalar color = Scalar(0, 255, 0);
51         //绘制椭圆,参数:Mat,中心点,椭圆的长短轴半径,椭圆的朝向(0表示水平),起始角度,终点角度
52         ellipse(bgImage, Point(width / 2, height / 2), Size(width / 4, height / 8), 0, 0, 270, color, 2, LINE_8);
53     }
54 
55     void drawCircle()//绘制圆
56     {
57         Scalar color = Scalar(0, 255, 255);
58         circle(bgImage, Point(bgImage.cols / 2, bgImage.rows / 2), 150, color, 2, LINE_8);
59     }
60 
61     void drawPolygon()//绘制多边形,绘制的多边形是填充的
62     {
63         Point pts[1][5];
64         pts[0][0] = Point(100, 100);
65         pts[0][1] = Point(100, 200);
66         pts[0][2] = Point(200, 200);
67         pts[0][3] = Point(200, 100);
68         pts[0][4] = Point(100, 100);
69 
70         const Point * ppts[] = { pts[0] };
71         int npt[] = { 5 };
72         Scalar color = Scalar(255, 0, 255);
73         //参数:Mat,顶点数据指针,顶点个数,1表示只绘制一个contour(轮廓)
74         fillPoly(bgImage, ppts, npt, 1, color, LINE_8);
75     }
76 
77     void drawRandomLine()//循环绘制随机位置随机颜色的线段
78     {
79         Mat randomLineMat = Mat::zeros(bgImage.size(), bgImage.type());
80         RNG rng(12345);//生成高斯随机数,参数种子
81         Point p1, p2;
82         for (int i = 0; i < 100000; i++)
83         {
84             p1.x = rng.uniform(0, randomLineMat.cols);//生成正态分布 0-bgImage.cols 范围内的随机数
85             p1.y = rng.uniform(0, randomLineMat.rows);
86             p2.x = rng.uniform(0, randomLineMat.cols);
87             p2.y = rng.uniform(0, randomLineMat.rows);
88             Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
89             if (waitKey(50) > 0)//参数delay如果传0表示阻塞等待键盘事件,传大于0表示只阻塞等待delay毫秒,键盘无响应返回-1
90             {
91                 break;
92             }
93             line(randomLineMat, p1, p2, color, 1, LINE_8);
94             imshow("randomLine", randomLineMat);
95         }
96     }

 

posted @ 2018-12-26 19:11  小油菜1  阅读(235)  评论(0编辑  收藏  举报