I have been struggling to get right calibration done using openCV with c++. Below I have included my code snippet for its' calculation, would greatly appreciate if you can point out what I am doing wrong, or how I can improve it.
// Initialize and reset calibration params
void InitCaliberation()
{
//numBoards = 0;
numCornersHor = horizontalCorners;
numCornersVer = verticalCorners;
numSquares = horizontalCorners * verticalCorners;
board_sz = Size(horizontalCorners, verticalCorners);
frame_sz = Size(frameWidth, frameHeight);
sqSizeInmm = sqSizemm; //25 mm
object_points.clear();
image_points.clear();
corners.clear();
finishedCalberation = false;
}
//Process frames
bool CheckCheckerboardFrame(Mat image, bool debug=false)
{
vector<Point3f> obj;
Mat grayImage;
cvtColor(image, grayImage, COLOR_BGR2GRAY);
for (int j = 0; j < numSquares; j++)
obj.push_back(Point3f(sqSizeInmm * j / numCornersHor, sqSizeInmm * j%numCornersHor, 0.0f));
bool found = findChessboardCorners(grayImage, board_sz, corners, CALIB_CB_ADAPTIVE_THRESH | CALIB_CB_FILTER_QUADS);
if (found)
{
//sub-pixel accurate location
cornerSubPix(grayImage, corners, Size(11, 11), Size(-1, -1), TermCriteria(TermCriteria::MAX_ITER | TermCriteria::EPS, 30, 0.1));
if(debug)
drawChessboardCorners(image, board_sz, corners, found);
else {
image_points.push_back(corners);
object_points.push_back(obj);
}
}
return found;
}
// Calculate params
void FinishCaliberation()
{
calibrateCamera(object_points, image_points, frame_sz, intrinsic, distCoeffs, R, T);
finishedCalberation = true;
cout <<"\nIntrinsic: "<< intrinsic.size<<endl;
for (int i = 0; i < intrinsic.rows; i++)
{
for (int j = 0; j < intrinsic.cols; j++)
cout << intrinsic.at<float>(i, j) << " ";
cout << endl;
}
cout << "\nDist coeff: " << distCoeffs.size << endl;
for (int i = 0; i < distCoeffs.rows; i++)
{
for (int j = 0; j < distCoeffs.cols; j++)
cout << distCoeffs.at<float>(i, j) << " ";
cout << endl;
}
//cout<<
}
Below is the output that I am getting with openCV.
And this is the expected output. I have calculated this calibration from matlab.
I am not trying to match the results! but want to understand why Fy and Cx values are 0, and why intrinsic.at(0,1) is non-zero? and what am I doing wrong here?
here is the minimal code of the calibration process: http://collabedit.com/3vay2 if anyone's interested.