RNG類是OpenCV中的一個(gè)基本隨機(jī)數(shù)生成工具
在OpenCV中,
RNG
類是用于生成隨機(jī)數(shù)的偽隨機(jī)數(shù)生成器(Pseudo-Random Number Generator),它可以用于生成各種類型的隨機(jī)數(shù),如整數(shù)、浮點(diǎn)數(shù)和隨機(jī)顏色,這個(gè)類位于opencv2/core/types.hpp
頭文件中;
RNG可以產(chǎn)生3種隨機(jī)數(shù)文章來源:http://www.zghlxwxcb.cn/news/detail-681850.html
- RNG(int seed) 使用種子seed產(chǎn)生一個(gè)64位隨機(jī)整數(shù),seed默認(rèn)-1
- RNG::uniform( ) 產(chǎn)生一個(gè)均勻分布的隨機(jī)數(shù)
- RNG::gaussian( ) 產(chǎn)生一個(gè)高斯分布(又叫正態(tài)分布)的隨機(jī)數(shù)
1、初始化一個(gè)RNG對(duì)象
cv::RNG rng; // 默認(rèn)情況下隨機(jī)數(shù)種子seed=-1,也可以指定隨機(jī)數(shù)種子RNG rng(12345)
// rng既是一個(gè)RNG對(duì)象,也是一個(gè)隨機(jī)整數(shù)
// 得到一個(gè)64位隨機(jī)整數(shù)
int N1 = rng;
2、產(chǎn)生一個(gè)均勻分布的隨機(jī)數(shù)
(1)產(chǎn)生一個(gè)[min_value, max_value)范圍的均勻分布的整數(shù)
int randomInt = rng.uniform(min_value, max_value);
(2)產(chǎn)生一個(gè)[min_value, max_value)范圍的均勻分布的浮點(diǎn)數(shù)
double randomDouble = rng.uniform(min_value, max_value);
示例:
//總是得到double類型數(shù)據(jù)0.000000,因?yàn)闀?huì)調(diào)用uniform(int,int),只會(huì)取整數(shù),所以只產(chǎn)生0
double N1a = rng.uniform(0,1);
//產(chǎn)生[0,1)范圍內(nèi)均勻分布的double類型數(shù)據(jù)
double N1b = rng.uniform((double)0,(double)1);
//產(chǎn)生[0,1)范圍內(nèi)均勻分布的float類型數(shù)據(jù),注意被自動(dòng)轉(zhuǎn)換為double了。
double N1c = rng.uniform(0.f,1.f);
//產(chǎn)生[0,1)范圍內(nèi)均勻分布的double類型數(shù)據(jù)。
double N1d = rng.uniform(0.,1.);
3、產(chǎn)生一個(gè)高斯分布(又叫正態(tài)分布)的隨機(jī)數(shù)
// 產(chǎn)生一個(gè)均值為0,標(biāo)準(zhǔn)差為1的正態(tài)分布的隨機(jī)數(shù)
double g = rng.gaussian(1.0);
// 產(chǎn)生一個(gè)均值為0,標(biāo)準(zhǔn)差為2的正態(tài)分布的隨機(jī)數(shù)
double N1g = rng.gaussian(2);
4、獲取下一個(gè)隨機(jī)數(shù)
上面一次只能返回一個(gè)隨機(jī)數(shù),實(shí)際上系統(tǒng)已經(jīng)生成一個(gè)隨機(jī)數(shù)組,如果我們要連續(xù)獲得隨機(jī)數(shù),沒有必要重新定義一個(gè)RNG類,只需要取出隨機(jī)數(shù)組的下一個(gè)隨機(jī)數(shù)即可;文章來源地址http://www.zghlxwxcb.cn/news/detail-681850.html
- RNG:: next 返回下一個(gè)64位隨機(jī)整數(shù)
- RNG:: operator 返回下一個(gè)指定類型的隨機(jī)數(shù)
示例:
RNG rng;
// 得到一個(gè)64位隨機(jī)整數(shù)
int N1 = rng;
int N2 = rng.next(); // 返回下一個(gè)隨機(jī)整數(shù),即N1.next();
//返回下一個(gè)指定類型的隨機(jī)數(shù)
int N2a = rng.operator uchar(); //返回下一個(gè)無符號(hào)字符數(shù)
int N2b = rng.operator schar(); //返回下一個(gè)有符號(hào)字符數(shù)
int N2c = rng.operator ushort(); //返回下一個(gè)無符號(hào)短型
int N2d = rng.operator short int(); //返回下一個(gè)短整型數(shù)
int N2e = rng.operator int(); //返回下一個(gè)整型數(shù)
int N2f = rng.operator unsigned int(); //返回下一個(gè)無符號(hào)整型數(shù)
int N2g = rng.operator float(); //返回下一個(gè)浮點(diǎn)數(shù)
int N2h = rng.operator double(); //返回下一個(gè)double型數(shù)
int N2i = rng.operator ()(); //和rng.next( )等價(jià)
int N2j = rng.operator ()(100); //返回[0,100)范圍內(nèi)的隨機(jī)數(shù)
5、實(shí)例演示
#include <opencv2\opencv.hpp>
#include <iostream>
#include <demo.h>
using namespace cv;
using namespace std;
int main() {
system("chcp 65001");
Mat canvas = Mat::zeros(Size(512, 512), CV_8UC3);
int w = canvas.cols;
int h = canvas.rows;
RNG rng(12345);
while (true) {
int c = waitKey(10);
if (c == 27) { // 退出
break;
}
int x1 = rng.uniform(0, w);
int y1 = rng.uniform(0, h);
int x2 = rng.uniform(0, w);
int y2 = rng.uniform(0, h);
int b = rng.uniform(0, 255);
int g = rng.uniform(0, 255);
int r = rng.uniform(0, 255);
canvas = Scalar(0, 0, 0);
line(canvas, Point(x1, y1), Point(x2, y2), Scalar(b, g, r), 1, LINE_AA, 0);
imshow("隨機(jī)繪制演示", canvas);
}
waitKey();
destroyAllWindows();
return 0;
}
到了這里,關(guān)于RNG類是OpenCV中的一個(gè)基本隨機(jī)數(shù)生成工具的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!