您的位置:首页 > 运维架构

core核心模块--随机数发生器&文字绘制

2017-06-07 09:58 387 查看
1.目的

(1)使用随机数发生器(RNG)获得均匀分布的随机数

(2)使用putText函数显示文字

2.部分代码实现

(1)实例化一个随机数发生器

//使用0xffffffff初始化随机数生成器
RNG rng(0xffffffff)


(2)使用随机数发生器产生均匀分布

//产生0-100的随机整数
//uniform:均匀分布
int min = 0;
int max = 100;
rng.uniform(min, max);


(3)使用随机数发生器随机产生Scalar

static Scalar RandomColor(RNG rng){
return(Scalar(rng.uniform(0,255),rng.uniform(0,255),rng.uniform(0,255)));
}


(4)使用随机数发生器绘制随机线条

int DrawingRandomLine(Mat& image, char windowName[], RNG rng){
if(!image.data){
cout << "more parameters are required!!!" << endl;
return(-1);
}
int lineType = 8;
Point X,Y;
for(int i=0; i<100; i++){
//使用rng.uniform获取1-500的随机整数
X.x = rng.uniform(1,500);
X.y = rng.uniform(1,500);
Y.x = rng.uniform(1,500);
Y.y = rng.uniform(1,500);
//RandomColor(rng):随机产生颜色
line(image, X, Y, RandomColor(rng), rng.uniform(1,10), lineType);
imshow(windowName, image);
//waitKey(DELAY)等待键盘输入,并延迟
if(waitKey(DELAY)>=0){
return(-1);
}
}
return(0);
}


(4)使用随机数发生器绘制文字

int DrawingText(Mat& image, char windowName[], RNG rng){
int lineType = 8;
Point org;
for(int i=0; i<100; i++){
org.x = rng.uniform(1,500);
org.y = rng.uniform(1,500);
/*
putText参数解释
image:绘制画板
“OpenCV Forever”:绘制的文字信息
org:绘制的文字的左上角坐标
rng.uniform(0,8):字体类型
rng.uniform(1,5):文字缩放比例
RandomColor(rng):字体颜色
rng.uniform(1,10):字体粗细
lineType:画线类型
*/
putText(image, "OpenCV Forever", org, rng.uniform(0,8), rng.uniform(1,5), RandomColor(rng), rng.uniform(1,10), lineType);
imshow(windowName, image);
if(waitKey(DELAY)>=0){
return(-1);
}
}
waitKey(0);
return(0);
}


3.完整代码

Random.cpp

#include "CommonInclude.h"
#define DELAY 10
static Scalar RandomColor(RNG rng){ return(Scalar(rng.uniform(0,255),rng.uniform(0,255),rng.uniform(0,255))); }
int DrawingRandomLine(Mat& image, char windowName[], RNG rng){
if(!image.data){
cout << "more parameters are required!!!" << endl;
return(-1);
}
int lineType = 8;
Point X,Y;
for(int i=0; i<100; i++){
//使用rng.uniform获取1-500的随机整数
X.x = rng.uniform(1,500);
X.y = rng.uniform(1,500);
Y.x = rng.uniform(1,500);
Y.y = rng.uniform(1,500);
line(image, X, Y, RandomColor(rng), rng.uniform(1,10), lineType);
imshow(windowName, image);
//waitKey(DELAY)等待键盘输入,并延迟
if(waitKey(DELAY)>=0){
return(-1);
}
}
return(0);
}

int DrawingText(Mat& image, char windowName[], RNG rng){ int lineType = 8; Point org; for(int i=0; i<100; i++){ org.x = rng.uniform(1,500); org.y = rng.uniform(1,500); /* putText参数解释 image:绘制画板 “OpenCV Forever”:绘制的文字信息 org:绘制的文字的左上角坐标 rng.uniform(0,8):字体类型 rng.uniform(1,5):文字缩放比例 RandomColor(rng):字体颜色 rng.uniform(1,10):字体粗细 lineType:画线类型 */ putText(image, "OpenCV Forever", org, rng.uniform(0,8), rng.uniform(1,5), RandomColor(rng), rng.uniform(1,10), lineType); imshow(windowName, image); if(waitKey(DELAY)>=0){ return(-1); } } waitKey(0); return(0); }

int main(int argc, char** argv){
int imageX;
int cReturn;
char windowName[] = "RandomDrawing";
cout << "Input size of image:";
cin >> imageX;
Mat image = Mat::zeros(imageX,imageX,CV_8UC3);
//实例化一个随机数生成器
RNG rng(0xFFFFFFFF);
cReturn = DrawingRandomLine(image, windowName, rng);
if(cReturn == -1){
cout << "end of the function!!!" << endl;
return(-1);
}
cReturn = DrawingText(image, windowName, rng);
if(cReturn == -1){
cout << "end of the function!!!" << endl;
return(-1);
}
return(0);
}


CommonInclude.h

#ifndef COMMON_INCLUDE
#define COMMON_INCLUDE
#include<iostream>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
#endif


参考文献

1.http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/core/random_generator_and_text/random_generator_and_text.html#drawing-2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  openCV 图像处理
相关文章推荐