-
-
Notifications
You must be signed in to change notification settings - Fork 55.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Standard Deviation calculation of calibrateCamera incorrect #19803
Comments
is this bug reproducible on windows or is this system dependent? |
System independent, the same code runs on linux and windows (though rn I'm away from my windows machine so I can't verify the fix works) |
I want to note that I overlooked one critical thing in my proposed fix: So we'd need someone with knowledge of the algorithm to verify that it is indeed |
Perhaps standard deviation can't be estimated for that case with Not sure how Least Squares Estimation problem works for that case, so probably we should update this check. /cc @catree |
I don't see any obvious errors, but I think the code is slightly relying on undefined behavior and numerical perfectness. Let us consider three different situations:
The above observations apply directly to linear, unconstrained least-squares problems. The camera calibration problem is nonlinear with some (non-negativity) constraints. However, the last stage of the calibration is optimization with Levenberg-Marquardt method which is based on linearization, so in practice the observations still hold. Let me know if you agree or disagree. If you agree that the correct result in cases 2) and 3) is NaN, then I think it should be made more explicit in the code. In particular, case 2) is unlikely to result in NaN even in IEEE 754 conformant implementations, because reprojection errors in the numerator (here) are unlikely to be exactly zero, whereas the integer denominator will be zero. Consequently, the standard error value will be positive or negative infinity (I didn't yet try an example where this would actually happen). |
I think that the problem is that in stddev calculation the |
Yes. My post above was written under the (wrong) assumption that |
By the way, looks like we have the same bug here: https://github.com/opencv/opencv/blob/4.x/modules/calib3d/src/fisheye.cpp#L1597 Other calibration code should be checked for that too. |
System information (version)
Detailed description
The standard deviation calculation of calibrateCamera is returning NaN for all standard deviations in all cases when provided with comparably low numbers of points.
This is due to this line, where the divisor and thus
sigma2
can become negative. The variabletotal
usually isnimages*maxPoints
or less,nparams = nimages*6+18
(for standard calibration method), andnparams_nz < nparams
.For the case
total < nparams_nz < nparams
the resultingsigma2
is negative.This can (and nearly always does) happen when
maxPoints = 6
sototal/nparams < 1.
This is likely also why it has been unnoticed so far.
Changing the
total
tonparams
yields seemingly correct results. EDIT: Only for my case wheremaxPoints = 6
! This might not be a general or even correct fix!I've also opened a forum question previously, although I'm positive it is a bug.
Steps to reproduce
calibrateCameraRO
, requesting the standard deviations to be calculatedtotal=132 (22*6), nparams=150(22*6+18), nparams_nz=139
An example implementation can be found in this larger project (calibrateCameraRO call here).
Issue submission checklist
forum.opencv.org, Stack Overflow, etc and have not found solution
The text was updated successfully, but these errors were encountered: