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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With