How to seed randn function opencv

smttsp picture smttsp · Jan 29, 2014 · Viewed 7k times · Source

I want to seed randn function but I'm not able to do it.

srand(time(NULL));
Mat mymat = Mat::zeroes(1024,1024,CV_32F);
randn(mymat,0,1); 

Should it not give me random mat, named mymat, whose mean = 0, and variance = 1? However, it gives the same mymat in every run.

Here is the link for randn which claims srand to work.

I tried to give different numbers instead of time(NULL), but all have the same output randoms. I have checked the same thing from another machine, it gives the same output with the first machine. So seeding is not working.

Thanks,

Answer

Andrey Kamaev picture Andrey Kamaev · Jan 29, 2014

You can set seed for OpenCV functions using using the following snippet:

cv::theRNG().state = seed;

There is a subtlety for multithreaded programs - OpenCV uses thread-local random number generators so you need (re)set seed from the same thread.