11/03/2014

opencv randn(...) example

opencv randn is like in matlab.

The randn function make values of normal distribution random

in matlab
randn is usage like this..

randn()
>> 0.4663

randn(10,1)'
>>   -0.1465    1.0143    0.4669    1.5750   -1.1900    0.2689   -0.2967   -0.4877    0.5671    0.5632

to use mean 5, variance 3
5+3*rand(10,1)
>> 6.2932   12.5907    6.6214    1.6941    4.8522    3.1484    6.1745    4.5230    5.2183    5.6888


OK, now consider case of OpenCV
We will make mean 10 and variance 2 normal distribution random values and fill in 2x10 matrix.

example 1)
randn
..
cv::Mat matrix2xN(2, 10, CV_32FC1);
randn(matrix2xN, 10, 2);
for (int i = 0; i < 10; ++i)
{
  cout << matrix2xN.at<float>(0, i) << " ";
  cout << matrix2xN.at<float>(1, i) << endl;
}
..

example 2)
randn and randu
..
cv::Mat matrix2xN(2, 10, CV_32FC1);
randn(matrix2xN, 10, 2);
for (int i = 0; i < 10; ++i)
{
    cout << matrix2xN.at< float>(0, i) << " ";
    cout << matrix2xN.at< float>(1, i) << endl;
}

//gaussian generation example
Mat Gnoise = Mat(5, 5, CV_8SC1);
randn(Gnoise, 5, 10); //mean, variance
cout << Gnoise << endl;
//
Mat Unoise = Mat(5, 5, CV_8SC1);
randu(Unoise, 5, 10); //low, high
cout << Unoise << endl;


//noise adapt
Mat Gaussian_noise = Mat(img.size(), img.type());
double mean = 0;
double std = 10;
randn(Gaussian_noise, mean, std); //mean, std
Mat colorNoise = img + Gaussian_noise;
..

No comments:

Post a Comment