RANSAC-like estimators not random across multiple runs?

Hi,

I have been testing the fundamental matrix estimation with different robust estimators, such as RANSAC and USAC (MAGSAC++), using opencv-python. For testing, I similarly used the code from this tutorial example and included the estimator as follows:

ransacReprojThreshold = 3.0
confidence = 0.99
maxIters = 500

F, status	=	cv2.findFundamentalMat(pts1, pts2, cv2.USAC_MAGSAC, ransacReprojThreshold, confidence, maxIters)
# F, status	=	cv2.findFundamentalMat(pts1, pts2, cv2.FM_RANSAC, ransacReprojThreshold, confidence, maxIters)

if F is None or F.shape == (1, 1):
	print('No fundamental matrix found')
	return np.zeros((3, 3, 1), dtype = "uint8"), 0

if F.shape[0] > 3:
	# more than one matrix found, just pick the first
	print('More than one matrix found')
	print(F)
	F = F[0:3, 0:3]

ninliers =  np.sum(status)

if ninliers >= min_num_inliers:
	return F, ninliers
else:
	return np.zeros((3, 3, 1), dtype = "uint8"), ninliers

I run the code 100 times on two example images from the public EuRoC MAV Dataset (Machine Hall 05, frames 470 and 480). I was expecting to obtain different results for each run due to the sampling approach of the estimators (RANSAC, MAGSAC), but the results were always the same. Note that the values of the parameters confidence, reprojection error, max iterations, and minimum number of inliers are kept fixed across runs.

Have anybody experienced the same behaviour? Is there any issue or random seed set inside OpenCV code that causes this behaviour?

My setup
OpenCV: 4.5.5
Python: 3.10
Ubuntu 18.04 LTS

Click here for the code running the test and observing the above-mentioned behaviour.

This interface allows setting some USACParams and more especially there is a randomGeneratorState():

Thank you for the reply and suggestions.

Indeed, I previously look at those links and OpenCV source code, but before trying the USACParams option I wanted to have a better understanding of the behaviour of the default function. I can confirm that the functions have a seed that makes the results reproducible across multiple runs on the same data and using the same parameters.

As per your suggestion, setting a random state in the USACParams for each run makes the function behave again in a non-deterministic way, as one should expect.

Therefore, I would recommend to always use USACParams as argument of findFundamentalMat() and avoid using the default functions.

I also updated the code on my GitHub repo.