core核心模块--随机数发生器&文字绘制
2017-06-07 09:58
387 查看
1.目的
(1)使用随机数发生器(RNG)获得均匀分布的随机数
(2)使用putText函数显示文字
2.部分代码实现
(1)实例化一个随机数发生器
(2)使用随机数发生器产生均匀分布
(3)使用随机数发生器随机产生Scalar
(4)使用随机数发生器绘制随机线条
(4)使用随机数发生器绘制文字
3.完整代码
Random.cpp
CommonInclude.h
参考文献
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
(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之core 模块. 核心功能(2)基本绘图 随机数发生器&绘制文字 离散傅立叶变换 输入输出XML和YAML文件 与 OpenCV 1 同时使用
- OpenCV学习笔记6 OpenCV核心模块与核心功能Core Module & Core Functionality(五)
- OpenCV学习笔记2 OpenCV核心模块与核心功能Core Module & Core Functionality(一)
- OpenCV学习笔记7 OpenCV核心模块与核心功能Core Module & Core Functionality(六)
- OpenCV学习笔记4 OpenCV核心模块与核心功能Core Module & Core Functionality(三)
- OpenCV学习笔记5 OpenCV核心模块与核心功能Core Module & Core Functionality(四)
- OpenCV学习笔记3 OpenCV核心模块与核心功能Core Module & Core Functionality(二)
- 《Windows程序设计》读书笔记------------->>输出文字----绘制和更新<<
- CALayer&Core Animation核心动画
- Core Architecture Blocks (核心架构模块)(二)
- Core Graphics框架学习之在图片上绘制文字
- Core Graphics使用 >> 绘制心电图
- opencv 2.x学习笔记(十)随机数发生器、绘制文字
- Nginx HTTP核心模块(HTTP Core)指令和内置变量中文说明
- iOS开发的2D绘制--CoreGraphics的简单使用三(画文字和图片)
- coreservlet&JSP核心编程总结(1)
- 12&nbsp;Nginx的核心模块
- Core Architecture Blocks (核心架构模块)(二)
- 生产成功,但点击EXE提示无法加载DLL"coredll.dll"找不到指定的模块(异常来自HRESULT:0x8007007E)
- 学习OpenCV(七)——随机数发生器和绘制文字