二维高斯分布随机数

来源:互联网 发布:mac口红lady bug试色 编辑:程序博客网 时间:2024/06/04 22:38

第一种方式:

#include<iostream>#include<opencv2\opencv.hpp>using namespace cv;using namespace std;#define PI 3.1415926double AverageRandom(double min, double max)//生成一个平均分布的随机数{int minInteger = (int)(min * 10000);int maxInteger = (int)(max * 10000);int randInteger = rand()*rand();int diffInteger = maxInteger - minInteger;int resultInteger = randInteger % diffInteger + minInteger;return resultInteger / 10000.0;}double Normal2(double x, double y, double miu1, double miu2, Mat matrix, float detCov) //二维正态分布密度函数{float a = matrix.at<float>(0, 0);float b = matrix.at<float>(0, 1);float c = matrix.at<float>(1, 0);float d = matrix.at<float>(1, 1);double A = 2 * PI*detCov;double B = a*(x - miu1)*(x - miu1) + (b + c)*(x - miu1)*(y - miu2) + d*(y - miu2)*(y - miu2);//cout << "==" << 1.0 / A*exp(-0.5*B)<<endl;return 1.0/A*exp(-0.5*B);}Point2i NormalRandom2(double miu1, double miu2, Mat matrix, float detCov, double min, double max)//产生二维正态分布随机数{Point2i p;double z;double dScope;do{p.x = (int)AverageRandom(min, max);p.y = (int)AverageRandom(min, max);z = Normal2(p.x, p.y, miu1, miu2, matrix, detCov);dScope = AverageRandom(0, Normal2(miu1, miu2, miu1, miu2, matrix, detCov));//cout << "*******" << dScope << "******" << endl;} while (dScope >= z);return p;}//产生二维正态分布随机数void covMatrix(float a, float b, float c, float d, Mat& inverse_matrix, float& detCov)  //协方差矩阵{Mat cov_matrix(2, 2, CV_32FC1);    //协方差矩阵cov_matrix.at<float>(0, 0) = a;cov_matrix.at<float>(0, 1) = b;cov_matrix.at<float>(1, 0) = c;cov_matrix.at<float>(1, 1) = d;detCov = abs(a*d - b*c);//Mat inverse_matrix(2, 2, CV_32FC1);   //协方差逆矩阵inverse_matrix.at<float>(0, 0) = cov_matrix.at<float>(1, 1) / detCov;inverse_matrix.at<float>(0, 1) = -cov_matrix.at<float>(0, 1) / detCov;inverse_matrix.at<float>(1, 0) = -cov_matrix.at<float>(1, 0) / detCov;inverse_matrix.at<float>(1, 1) = cov_matrix.at<float>(0, 0) / detCov;cout << "【默认风格】" << endl << cov_matrix << endl << endl;cout << "【默认风格】" << endl << inverse_matrix / detCov << endl << endl;cout << "detCov= " << detCov << endl;}int main(){Mat src(200, 200, CV_8UC1);for (int i = 0; i < src.rows; i++){for (int j = 0; j < src.cols; j++){src.at<uchar>(i, j) = 0;}}float detCov = 0;   //协方差矩阵detMat inverse_matrix(2, 2, CV_32FC1);   //协方差逆矩阵covMatrix(1, 0, 0, 6, inverse_matrix, detCov);int min = 0;int max = 200;Point2i p;for (int i = 0; i < 12000; i++){p = NormalRandom2(100, 100, inverse_matrix,detCov,min,max);cout << p.x << "  " << p.y << endl;src.at<uchar>((int)p.x, (int)p.y) = 255;}imshow("src", src);imwrite("1006.jpg", src);waitKey(0);return 0;}

第二种方式:效果好像不是很好,思路很好,第一种就是参考这个的

#include<iostream>#include<opencv2\opencv.hpp>using namespace cv;using namespace std;#define PI 3.1415926/*http://blog.csdn.net/fdybit/article/details/8018687*/struct vector1{ double x;double y; };//二维向量double AverageRandom(double min, double max)//生成一个平均分布的随机数{int minInteger = (int)(min * 10000);int maxInteger = (int)(max * 10000);int randInteger = rand()*rand();int diffInteger = maxInteger - minInteger;int resultInteger = randInteger % diffInteger + minInteger;return resultInteger / 10000.0;}double Normal2(double x, double y, double miu1, double miu2, double sigma1, double sigma2, double r) //二维正态分布密度函数{double m = 1.0 / (sqrt(1 - r*r)*(2 * PI*sigma1*sigma2)) * exp((-1 / (2 * (1 - r*r)))*((x - miu1)*(x - miu1) / (sigma1*sigma1) - 2 * r*(x - miu1)*(y - miu2) / (sigma1*sigma2) + (y - miu2)*(y - miu2) / (sigma2*sigma2)));//cout << "====" << m << "=====" << endl;return m;}vector1 NormalRandom2(double miu1, double miu2, double sigma1, double sigma2, double r, double min, double max)//产生二维正态分布随机数{vector1 vec;double z;double dScope;do{vec.x = AverageRandom(min, max);vec.y = AverageRandom(min, max);z = Normal2(vec.x, vec.y, miu1, miu2, sigma1, sigma2, r);dScope = AverageRandom(0, Normal2(miu1, miu2, miu1, miu2, sigma1, sigma2, r));} while (dScope > z);return vec;}int main(){struct vector1 P;Mat src(200, 200, CV_8UC1);for (int i = 0; i < src.rows; i++){for (int j = 0; j < src.cols; j++){src.at<uchar>(i, j) = 0;}}for (int i = 0; i < 11300; i++){P = NormalRandom2(100, 100, 20, 4, 0, 0, 200);//cout << P.x << "  " << P.y << endl;src.at<uchar>((int)P.x, (int)P.y) = 255;}imshow("src", src);waitKey(0);return 0;}



原创粉丝点击