Confusion about number of distortion parameters

asked 2015-10-25 16:25:34 -0500

Baiz gravatar image

Hello, I have a problem with the camera calibration function. I haven't found an existing thread for this particular problem, though I could have overlooked one.

Main problem:

The function calibrateCamera returns only 5 or 8 distortion parameters, but from the documentation, it should also support 4.

Explanantion:

The documentation (http://docs.opencv.org/modules/calib3...) for the function "calibrateCamera" states:

distCoeffs – Output vector of distortion coefficients (k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6]]) of 4, 5, or 8 elements

From this I gathered, that I can decide if OpenCV calibrates the camera distortion with 4, 5 or 8 parameters, depending on the size of the cv::Mat object I pass to the function (one with a size of 4, 5, or 8 elements). However, when passing a cv::Mat object with only 4 elements, the functions reallocates the object to a size of 5. Also, the 5th parameter is not zero. So OpenCV actually fits the distortion with 5 parameters, even though only 4 are expected from the caller. This can lead to some nasty bugs (as it did for me), especially when the first 4 distortion coefficients (of the cv::Mat object with 5 elements) differ strongly from the distortion coefficients if the camera had been calibrated using only 4 coefficients.

I used OpenCV 2.4.7

Example:

_distortionCoefficients = cv::Mat::zeros(4, 1, CV_64F);
_reprojectionError = cv::calibrateCamera(objectPoints, imagePoints, imageSize, _cameraMatrix, _distortionCoefficients, _rvecs, _tvecs, flags, terminationCriteria);
// From here on, _distortionCoefficients is of size (5,1).

Am I using the function incorrectly or is this a bug?

Regards,

Baiz

edit retag flag offensive close merge delete

Comments

1

"From this I gathered, that I can decide" - imho, that's wrong, it is not your decision.

_distortionCoefficients = cv::Mat::zeros(4, 1, CV_64F); - don't do that. opencv will resize/reallocate/overwrite this to its own needs, this way you're only fooling yourself into believing, it had a certain shape/type

berak gravatar imageberak ( 2015-10-26 01:07:10 -0500 )edit

@berak This makes sense, especially since the flag CV_CALIB_RATIONAL_MODEL decides if 5 or 8 parameters are used, so the overall mechanism simply does not cover a 4 parameter scenario. However, talking about the distortion coefficients with "4, 5, or 8 elements" is still a bit misleading. The only time I know of 4 distortion parameters being returned is with the fisheye calibration, but it has a separate documentation where the 4 distortion parameters are described.

Baiz gravatar imageBaiz ( 2015-10-26 04:59:49 -0500 )edit