# Confusion about number of distortion parameters

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 close merge delete

_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