本文主要是介绍OpenCV之RNG生成随机数类详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
OpenCV中主要是通过RNG类来生成随机数,默认定义RNG类对象的时候需要初始化一个种子(默认种子为0xFFFFFFFF,64位无符号值),对种子进行运算从而生成随机数,RNG类定义如下:
如果将种子设定为默认种子的话,每次运行种子及其种子运算所得随机数不变,往往不利于程序需求,通常可将种子设置为当前时间,这样每次获得的种子及其运算所得随机数都不同。
默认种子RNG类定义:RNG rng(0xFFFFFFFF)
时间种子RNG类定义:RNG rng((unsigned)time(NULL))
uniform(a,b)函数可以随机产生一个[a,b)的随机数,其类型可以是int,double,float,例如 int x=rng.uniform((int)0,(int)255),x等于一个0-255的随机整数。
RNG类在opencv中常用于产生随机的RGB颜色值Scalar和随机点中,距离如下:
产生随机颜色值:int color = (int)rng ; Scalar(color&255,(color>>8)&255,(color>>16)&255);
产生随机点:Point pt ; pt.x=rng.uniform((double)0,(double)255) ; pt.y=rng.uniform((double)0,(double)255);
- Random Number Generator
- The class implements RNG using Multiply-with-Carry algorithm
- */
- class CV_EXPORTS RNG
- {
- public:
- enum { UNIFORM=0, NORMAL=1 };
- RNG();//默认构造函数
- // inline RNG::RNG() { state = 0xffffffff; }
- RNG(uint64 state);//带参数的构造函数,接受一个64位无符号的值。
- //inline RNG::RNG(uint64 _state) { state = _state ? _state : 0xffffffff; }
- //! updates the state and returns the next 32-bit unsigned integer random number
- unsigned next();
- /*
- inline unsigned RNG::next()
- {
- state = (uint64)(unsigned)state*CV_RNG_COEFF + (unsigned)(state >> 32);
- return (unsigned)state;
- }
- #define CV_RNG_COEFF 4164903690U
- 用两个很大的无符号数相乘,乘积结果要转换为64位无符号数,转换的时候两个乘数应该向高精度看起,所以应该也先转换为64位再相乘。把state右移32位得到一个数,把这两个数相加。函数返回一个32位的无符号数,其值为截断前面求得的和。
- */
- //以下几个函数是从类到uchar.schar,ushort,short,usinged的显示转换函数
- operator uchar();//返回一个8位无符号类型的随机数,把next返回的数截断
- //inline RNG::operator uchar() { return (uchar)next(); }
- operator schar();//返回一个8为有符号类型的随机数。???会产生负数吗,返回的也是截断的next返回值。莫非是截断后得到的最高位作为符号位,这样也可能是随机的。???
- //inline RNG::operator schar() { return (schar)next(); }
- operator ushort();//返回一个无符号16为整数
- //inline RNG::operator ushort() { return (ushort)next(); }
- operator short();//返回一个有符号16为整数
- // inline RNG::operator short() { return (short)next(); }
- operator unsigned();//返回一个无符号32为整数
- // inline RNG::operator unsigned() { return next(); }
- //! returns a random integer sampled uniformly from [0, N).
- unsigned operator ()(unsigned N);//重载括号操作符,带参数。在(0,N)之间返回一个整数,调用uniform成员函数
- //inline unsigned RNG::operator ()(unsigned N) {return (unsigned)uniform(0,N);}
- unsigned operator ()();//重载括号操作符,无参数。直接返回next结果。
- // inline unsigned RNG::operator ()() {return next();}
- //放在这个位置有点奇怪,为什么不和前边同类放一起呢?放回一个带符//号32为整数
- operator int();
- // inline RNG::operator int() { return (int)next(); }
- //返回一个float型(具体多少位看平台)数。
- operator float();
- // inline RNG::operator float() { return next()*2.3283064365386962890625e-10f; }
- //两个数按位或一下,解释起来好麻烦
- operator double();
- /*
- inline RNG::operator double()
- {
- unsigned t = next();
- return (((uint64)t << 32) | next())*5.4210108624275221700372640043497e-20;
- }*/
- //! returns uniformly distributed integer random number from [a,b) range
- int uniform(int a, int b);//[a,b)内随机产生一个int型值,均匀的哦!
- // inline int RNG::uniform(int a, int b) { return a == b ? a : (int)(next()%(b - a) + a); }
- //! returns uniformly distributed floating-point random number from [a,b) range
- float uniform(float a, float b); //[a,b)内随机产生一个float型值,均匀的哦!
- // inline float RNG::uniform(float a, float b) { return ((float)*this)*(b - a) + a; }
- //! returns uniformly distributed double-precision floating-point random number from [a,b) range
- double uniform(double a, double b); //[a,b)内随机产生一个double型值,均匀的
- // inline double RNG::uniform(double a, double b) { return ((double)*this)*(b - a) + a; }
- void fill( InputOutputArray mat, int distType, InputArray a, InputArray b, bool saturateRange=false );//这个函数实现很长,暂时略过。
- //! returns Gaussian random variate with mean zero.
- double gaussian(double sigma);//返回均值为0的高斯随机变量,
- /*double RNG::gaussian(double sigma)
- {
- float temp;
- randn_0_1_32f( &temp, 1, &state );
- return temp*sigma;
- }*/
- uint64 state;//种子,next中需要这样一个初始值
- };
-
这篇关于OpenCV之RNG生成随机数类详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!