Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV: randn() generator like in matlab

Tags:

opencv

matlab

I am looking for the best solution to generate (in OpenCV) a matrix(2xN) of random numbers with mean 0 and variance 1, like the function randn() in Matlab.

There is a randn() function in the OpenCV libraries, but I don't know how to pass arguments to this function to generate numbers with mean 0 and variance 1.

like image 718
Jan Valiska Avatar asked Sep 26 '13 19:09

Jan Valiska


1 Answers

OpenCV has a randn() function and also a RNG class. Below is the Matlab code you might want to replace, and the equivalent OpenCV code.

Matlab:

matrix2xN = randn(2,N)

OpenCV:

cv::Mat mean = cv::Mat::zeros(1,1,CV_64FC1);
cv::Mat sigma= cv::Mat::ones(1,1,CV_64FC1);
cv::RNG rng(optional_seed);
cv::Mat matrix2xN(2,N,CV_64FC1);
rng.fill(matrix2xN, cv::RNG::NORMAL, mean, sigma);

or

cv::Mat mean = cv::Mat::zeros(1,1,CV_64FC1);
cv::Mat sigma= cv::Mat::ones(1,1,CV_64FC1);
cv::randn(matrix2xN,  mean, sigma);

Internally, OpenCV implements randn() using RNG. The disadvantage if using randn() is that you lose control over the seed.

If matrix2xN above had more than one channel then a different mean/sigma is used for each channel. In which case you would need to inrease the number of rows (or cols) in mean and sigma to match the number of channels in matrix2xN.

like image 172
Bull Avatar answered Nov 08 '22 03:11

Bull