【opencv】产生随机数

在opencv中我们有时要随机生成一些数值,用于图像的边缘。

RNG::uniform

int  RNG::(int a,int b)

float  RNG::uniform(float a,float b)

double RNG::uniform(double a,double b)

随机类RNG:计算机的伪随机数是由随机种子根据一定的计算方法计算出来的数值,所以只要计算方法一定,随机种子一定,那么产生的随机数就是固定的。

    RNG rng(12345)

opencv 里RNG类构造函数初始化为固定值后,随机种子也是固定的,所以在相同的平台环境下,编译后每次运行它,显示的随机数是一样的。

 例如:

#include<iostream>
#include "opencv2/highgui/highgui.hpp"

using namespace cv;
using namespace std;

RNG rng(12345);

int main(void)
{
for (int i = 0; i < 10; i++) {
int a = rng.uniform(1, 100);
cout << a << endl;
}
return 0;
}

只要平台不变,每次生成的都是:
6
33
12
54
71
65
94
11
64
76
请按任意键继续. . .

如果想改变成随机生成的数,使用下面的方法:

#include<iostream>
#include <opencv2/highgui/highgui.hpp>
#include <ctime>

using namespace cv;
using namespace std;

RNG rng((unsigned)time(NULL));

int main(void)
{
for (int i = 0; i < 10; i++) {
int a = rng.uniform(1, 100);
cout << a << endl;
}
return 0;
}


第一次结果:
76
79
3
3
83
2
34
65
97
9
请按任意键继续. . .

第二次结果:


35
46
76
52
32
68
69
42
49
91
请按任意键继续. . .

猜你喜欢

转载自blog.csdn.net/qq_34106574/article/details/82220022