opencv 2.x学习笔记(十)随机数发生器、绘制文字
来源:互联网 发布:java服务器有哪些 编辑:程序博客网 时间:2024/05/29 19:05
在上一篇中,我们了解了如何使用opencv为我们提供的基本绘图函数来为我们来绘制一个简单的图像。在这一篇中,我们将继续使用上一篇中所讲到的函数来演示opencv的随机数产生功能以及如何绘制文字。
随机数产生器
在我们的程序中,经常需要使用随机数生成功能。为此,opencv为我们提供了一个随机数生成器类,RNG。它封装了一个64位的int类型整数,为此我们课可以得到一个随机的Scalar和随机的数组。目前,它支持产生均匀分布随机数和高斯分布随机数。
它的构造函数的初始化可以传入一个64位的整型参数作为随机数产生器的初值。RNG类的定义大致如下:
/*! Random Number Generator The class implements RNG using Multiply-with-Carry algorithm*/class CV_EXPORTS RNG{public: enum { UNIFORM=0, NORMAL=1 }; RNG(); RNG(uint64 state); //! updates the state and returns the next 32-bit unsigned integer random number unsigned next(); operator uchar(); operator schar(); operator ushort(); operator short(); operator unsigned(); //! returns a random integer sampled uniformly from [0, N). unsigned operator ()(unsigned N); unsigned operator ()(); operator int(); operator float(); operator double(); //! returns uniformly distributed integer random number from [a,b) range int uniform(int a, int b); //! returns uniformly distributed floating-point random number from [a,b) range float uniform(float a, float b); //! returns uniformly distributed double-precision floating-point random number from [a,b) range double uniform(double a, double b); void fill( InputOutputArray mat, int distType, InputArray a, InputArray b, bool saturateRange=false ); //! returns Gaussian random variate with mean zero. double gaussian(double sigma); uint64 state;};其中主要函数的含义:
- next():取出下一个随机数。
- uniform():返回指定范围的均匀分布的随机数。
- gaussian():返回一个高斯随机数。
- fill():利用随机数填充矩阵。
因此我们可以这样来构造一个rng对象:
RNG rng( 0xFFFFFFFFFFFFFFFF );可以这样来产生一个随机数。
pt1.x = rng.uniform( 0.0, (double)window_width );同时可以这样来得到它的下一个随机数。
int icolor = (unsigned) rng;(调用unsigned(), 在该函数内部,调用next())。
绘制文字
了解了如何产生一个随机数之后,我们的任务已经进行了一半,那么我们该如何在一个图像中绘制文字呢?
opencv为我们提供了putText函数来完成这一功能,具体声明如下:
//! renders text string in the imageCV_EXPORTS_W void putText( Mat& img, const string& text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=8, bool bottomLeftOrigin=false );
- img: 要绘制文字的图像。
- text: 要绘制的文字。
- org: 图像中文字字符串的左下角坐标。
- fontFace:字体类型,可以在下面类型中取值。
FONT_HERSHEY_SIMPLEX, FONT_HERSHEY_PLAIN, FONT_HERSHEY_DUPLEX, FONT_HERSHEY_COMPLEX, FONT_HERSHEY_TRIPLEX,
FONT_HERSHEY_COMPLEX_SMALL, FONT_HERSHEY_SCRIPT_SIMPLEX, FONT_HERSHEY_SCRIPT_COMPLEX。
- fontScale: 字体的比例因子,用以乘以字体特定的基本尺寸。
- color: 文本颜色。
- bottomLeftOrigin: 如果为true,图像数据的起点在左下角,否则在左上角。
我们可以这样来使用:
putText( image2, "Hello OpenCV!", org, FONT_HERSHEY_COMPLEX, 3, Scalar( i, i, 255 ), 5 );了解这些之后,我们就可以来做一个随机产生的线条和三角形的一个演示实例。为了获得一个图像渐变的效果,我们在代码中每隔一段时间,就对原来的图像减去一个指定值。如下所示:
Mat image2;for( int i = 0; i < 255; i += 2 ){image2 = image - Scalar::all( i );putText( image2, "Hello OpenCV!", org, FONT_HERSHEY_COMPLEX, 3, Scalar( i, i, 255 ), 5 );waitKey(5);imshow( window_name, image2 );}
完整实例:
#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <iostream>using namespace std;using namespace cv;const int window_height = 600;const int window_width = 900;const int NUMBER = 100;Scalar randomColor( RNG& rng );void Drawing_Random_Lines( Mat image, char * window_name, RNG rng );void Drawing_Random_Filled_Polygons( Mat image, char * window_name, RNG rng );void Drawing_Text( Mat image, char * window_name, RNG rng );int main(){RNG rng( 0xFFFFFFFFFFFFFFFF );Mat image = Mat::zeros( window_height, window_width, CV_8UC3 );char window_name[] = "Random image";Drawing_Random_Lines( image, window_name, rng );Drawing_Random_Filled_Polygons( image, window_name, rng );Drawing_Text( image, window_name, rng );waitKey(0);return 0;}Scalar randomColor( RNG& rng ){int icolor = (unsigned) rng;return Scalar( icolor&255, (icolor>>8)&255, (icolor>>16)&255 );}void Drawing_Random_Lines( Mat image, char * window_name, RNG rng ){Point pt1, pt2;for( int i = 0; i < NUMBER; i ++ ){pt1.x = rng.uniform( 0.0, (double)window_width );pt1.y = rng.uniform( 0.0, (double)window_height );pt2.x = rng.uniform( 0.0, (double)window_width );pt2.y = rng.uniform( 0.0, (double)window_height );line( image, pt1, pt2, randomColor( rng ), 2 );waitKey(5);imshow( window_name, image );}}void Drawing_Random_Filled_Polygons( Mat image, char * window_name, RNG rng ){for( int i = 0; i < NUMBER; i ++ ){Point pt[2][3];pt[0][0].x = rng.uniform(0, window_width);pt[0][0].y = rng.uniform(0, window_height);pt[0][1].x = rng.uniform(0, window_width);pt[0][1].y = rng.uniform(0, window_height);pt[0][2].x = rng.uniform(0, window_width);pt[0][2].y = rng.uniform(0, window_height);pt[1][0].x = rng.uniform(0, window_width);pt[1][0].y = rng.uniform(0, window_height);pt[1][1].x = rng.uniform(0, window_width);pt[1][1].y = rng.uniform(0, window_height);pt[1][2].x = rng.uniform(0, window_width);pt[1][2].y = rng.uniform(0, window_height);const Point* ppt[2] = { pt[0], pt[1] };int npt[] = { 3, 3 };fillPoly( image, ppt, npt, 2, randomColor( rng ) );imshow( window_name, image );waitKey(5);}}void Drawing_Text( Mat image, char * window_name, RNG rng ){Size textsize = getTextSize( "Hello OpenCV!", FONT_HERSHEY_COMPLEX, 3, 5, 0 );Point org((window_width - textsize.width)/2, (window_height - textsize.height)/2 );Mat image2;for( int i = 0; i < 255; i += 2 ){image2 = image - Scalar::all( i );putText( image2, "Hello OpenCV!", org, FONT_HERSHEY_COMPLEX, 3, Scalar( i, i, 255 ), 5 );waitKey(5);imshow( window_name, image2 );}}
运行结果
0 0
- opencv 2.x学习笔记(十)随机数发生器、绘制文字
- OpenCV学习:随机数发生器&绘制文字
- 学习OpenCV(七)——随机数发生器和绘制文字
- 随机数发生器&绘制文字-----学习记录(6)
- 【OpenCV 学习】自带示例:随机数发生器&绘制文字 代码注释解析
- opencv c++函数 基础7 随机数发生器&绘制文字
- 随机数发生器&绘制文字---opencv2.4.11
- core核心模块--随机数发生器&文字绘制
- OpenCV之core 模块. 核心功能(2)基本绘图 随机数发生器&绘制文字 离散傅立叶变换 输入输出XML和YAML文件 与 OpenCV 1 同时使用
- opencv 2.x学习笔记(十三)绘制直方图
- opencv学习日常之随机数发生器RNG
- opencv学习笔记(三)-绘制基本图形及文字
- OpenCV的随机数发生器
- opencv随机数发生器RNG
- 学习笔记---绘制文字
- STM32F4学习笔记12——RNG随机数发生器
- OpenCV学习笔记(六):绘制几何图形
- openCv学习笔记(十)-滤波
- leetCode :Validate Binary Search Tree
- linux系统中各个路由表的研究
- Ansible@一个高效的配置管理工具--Ansible configure management--翻译(四)
- 下载破解ZendStudio 10.5
- G - Specialized Four-Digit Numbers(1.5.2)
- opencv 2.x学习笔记(十)随机数发生器、绘制文字
- 说一说struts2 验证框架使用过程中遇到的问题
- php多表连接mysql数据库,输出相同的字段名
- 初见
- 远程桌面连接增加端口号
- 2014.7.13
- 数据结构学习之树---AVL树的实现
- vi/vim使用进阶: 剑不离手 – quickfix
- sql plan baselines(计划基线)Oracle 原文