码迷,mamicode.com
首页 > 其他好文 > 详细

OpenCV官方文档学习记录(5)

时间:2014-12-04 11:41:41      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

随机数生成类:RNG以及文字渲染

 1 #include <opencv2\opencv.hpp>
 2 #include <iostream>
 3 #include <string>
 4 
 5 #pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
 6 
 7 #define NUMBER 20
 8 
 9 using namespace std;
10 using namespace cv;
11 
12 void Show(std::string name,Mat img)
13 {
14     namedWindow(name, CV_WINDOW_AUTOSIZE);
15     imshow(name, img);
16 }
17 
18 static Scalar randomColor(RNG &rng)
19 {
20     return Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
21 }
22 
23 void Drawing_Random_Lines(Mat img, char *win_name, RNG& rng)
24 {
25     int linetype = 8;
26     int w = 400;
27     Point pt1, pt2;
28     int x_1 = 0, x_2 = w;
29 
30     for (int i = 0; i < NUMBER; ++i)
31     {
32         pt1.x = rng.uniform(x_1, x_2);
33         pt1.y = rng.uniform(x_1, x_2);
34         pt2.x = rng.uniform(x_1, x_2);
35         pt2.y = rng.uniform(x_1, x_2);
36 
37         line(img, pt1, pt2, randomColor(rng), rng.uniform(1, 10), 8);
38         Show(win_name, img);
39     }
40 }
41 
42 void Display_Random_Text(Mat img, char *win_name, RNG &rng)
43 {
44     int linetype = 8;
45     int w = 400;
46     int x1 = 0, x2 = w;
47 
48     for (int i = 0; i < NUMBER; ++i)
49     {
50         Point org;
51         org.x = rng.uniform(x1, x2);
52         org.y = rng.uniform(x1, x2);
53 
54         putText(img, "Testing Text", org, rng.uniform(0, 8), rng.uniform(0, 100)*0.05 + 0.1, randomColor(rng), rng.uniform(1, 10), linetype);
55 
56         Show(win_name, img);
57     }
58 }
59 
60 void Display_Big_End(Mat img, char* win_name, RNG rng)
61 {
62     int w = 400;
63     Size textSize = getTextSize("OpenCV Forever", CV_FONT_HERSHEY_COMPLEX, 3, 5, 0);
64     Point org((w - textSize.width) / 2, (w + textSize.height) / 2);
65     int linetype = 8;
66 
67     Mat img2;
68 
69     for (int i = 0; i < NUMBER; ++i)
70     {
71         img2 = img - Scalar::all(i);
72         putText(img2, "OpenCV Forever", org, CV_FONT_HERSHEY_COMPLEX, 3, Scalar(i, i, 255), 5, linetype);
73 
74         Show(win_name, img2);
75     }
76 }
77 
78 int main()
79 {
80     int w = 400;
81     RNG rng(0xFFFFFFFF);
82     char *win_name = "Drawing";
83     Mat img = Mat::zeros(w, w, CV_8UC3);
84 
85     Drawing_Random_Lines(img, win_name, rng);
86     //Drawing_Random_Rectangle(img, win_name, rng);
87     //Drawing_Random_Ellipse(img, win_name, rng);
88     //Drawing_Random_Polylines(img, win_name, rng);
89     //Drawing_Random_FilledPolygon(img, win_name, rng);
90     //Drawing_Random_Circle(img, win_name, rng);
91     Display_Random_Text(img, win_name, rng);
92     Display_Big_End(img, win_name, rng);
93 
94     waitKey();
95     return 0;
96 }

 

文档有解释,不再赘述。

 

结果是:

bubuko.com,布布扣

 

这里说明putText函数:

摘自core.hpp

 1 enum
 2 {
 3     FONT_HERSHEY_SIMPLEX = 0,
 4     FONT_HERSHEY_PLAIN = 1,
 5     FONT_HERSHEY_DUPLEX = 2,
 6     FONT_HERSHEY_COMPLEX = 3,
 7     FONT_HERSHEY_TRIPLEX = 4,
 8     FONT_HERSHEY_COMPLEX_SMALL = 5,
 9     FONT_HERSHEY_SCRIPT_SIMPLEX = 6,
10     FONT_HERSHEY_SCRIPT_COMPLEX = 7,
11     FONT_ITALIC = 16
12 };
13 
14 //! renders text string in the image
15 CV_EXPORTS_W void putText( Mat& img, const string& text, Point org,
16                          int fontFace, double fontScale, Scalar color,
17                          int thickness=1, int lineType=8,
18                          bool bottomLeftOrigin=false );
19 
20 //! returns bounding box of the text string
21 CV_EXPORTS_W Size getTextSize(const string& text, int fontFace,
22                             double fontScale, int thickness,
23                             CV_OUT int* baseLine);

 

int fontFace接受的是字体的参数,因此有以上的enum体内的定义。

int fontScale接受的是一个字体的缩放系数;

最后一个布尔型参数可能是判断是否上下翻转;

 

以上。

OpenCV官方文档学习记录(5)

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/lhyz/p/4142194.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!