Potentially wrong calibrateCamera standard deviation calculation

I’m trying to get standard deviations of parameters from a calibrateCameraRO call for for an interactive calibration.
The calibration works but the standard deviations always return NaN - those of intrinsic, extrinsic and object points (in case Release Object is used).

I’m using 4.5.1. (custom build with only calibration, that is core+features2d+imgproc+flann+calib3d).
I’ve traced the problem to these lines. Most notably, sigma2 is negative, since the number of nonzero parameters exceeds the number of total image points.
In one example run, I put in 22 6-point markers. The variables are as follows: total=132 (22x6), nparams=150(22x6+18), nparams_nz=139.

Though these lines have been under scrutiny before they obviously result in wrong results in my case.

First, I assume the correct line should have been
double sigma2 = norm(allErrors, NORM_L2SQR) / (nparams - nparams_nz);
though that is purely based on logic and not knowledge of propagation of standard deviations.

Second, I assume the reason this error has not been detected so far is that I use unusually few points per markers, and few points to start. In a normal calibration, the ratio of total points / total points + 18 is much closer to 1 so the chance that all of them are nonzero might be lower.

Changing the line as described yields me correct looking standard deviations.

Are my assumptions correct?
If so I would go ahead and create a pull request.

1 Like

Just to add to this, I’ve created an issue, though my proposed fix is perhaps NOT the correct fix! While it still makes sense logically, if it was correct, that’d mean it was even more wrong before than I’d thought, and I assume somebody would have caught on to that.
So a fix is still required.