我使用一组20个棋盘图像校准鱼眼镜头。这些点都被正确标记,我得到了内在函数和镜头失真系数。
定义方形尺寸'对于15mm,变量为15,得到0个失真系数,焦距为~600。将其设置为0.015可以得到失真系数和~1200的焦距。在这两种情况下,我的投影中心大致是图像的中点,这是合理的。
当我尝试将无失真应用于我的图像时,我得到了一个非常奇怪的结果。
我不明白为什么。在失真系数为0时,我仍然得到这个图像。如果有人能提供帮助,我们将不胜感激!
void undistortFisheye()
{
std::vector <cv::Mat> inputImages;
std::vector<std::vector<cv::Point3f> > objectPoints(1);
std::vector<std::vector<cv::Point2f>> image_points;
//Load images
for (int i = 0; i < 13; i++)
{
std::cout << "Loading: " << "C:/Data/CheckerboardCalib/" << std::to_string(i + 1) << ".png" << std::endl;
inputImages.push_back(cv::imread("C:/Data/CheckerboardCalib/" + std::to_string(i + 1) + ".png"));
}
int chessBoardFlags = cv::CALIB_CB_ADAPTIVE_THRESH | cv::CALIB_CB_NORMALIZE_IMAGE;
for (int i = 0; i < inputImages.size(); i++)
{
std::vector<cv::Point2f> pointBuf;
cv::Mat view;
std::cout << i << std::endl;
bool found = cv::findChessboardCorners(inputImages[i], cv::Size(21, 14), pointBuf, chessBoardFlags);
if (found) // If done with success,
{
std::cout << "Corners found!" << std::endl;
// improve the found corners' coordinate accuracy for chessboard
cv::Mat viewGray;
cvtColor(inputImages[i], viewGray, cv::COLOR_BGR2GRAY);
cornerSubPix(viewGray, pointBuf, cv::Size(11, 11), cv::Size(-1, -1), cv::TermCriteria(cv::TermCriteria::EPS + cv::TermCriteria::COUNT, 30, 0.1));
image_points.push_back(pointBuf);
// Draw the corners.
// inputImages[i].copyTo(view);
//cv::drawChessboardCorners(view, cv::Size(21, 14), cv::Mat(pointBuf), found);
//cv::imshow("Window", view);
//cv::waitKey(0);
}
}
float squareSize = 15.0f;
calcBoardCornerPositions(cv::Size(21,14), squareSize, objectPoints[0]);
objectPoints.resize(image_points.size(), objectPoints[0]);
cv::Matx33f K;
cv::Vec4f D;
std::vector<cv::Vec3f> rvec;
std::vector<cv::Vec3f> tvec;
//int flag = cv::fisheye::CALIB_CHECK_COND | cv::fisheye::CALIB_FIX_SKEW;
int flag = cv::fisheye::CALIB_FIX_SKEW | cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC;
//| cv::fisheye::CALIB_FIX_K1 | cv::fisheye::CALIB_FIX_K2 | cv::fisheye::CALIB_FIX_K3;// | cv::fisheye::CALIB_FIX_K4;// | cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC;// | cv::fisheye::CALIB_FIX_K1;// | cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC;
cv::fisheye::calibrate(objectPoints, image_points, cv::Size(1920, 1080), K, D, rvec, tvec, flag, cv::TermCriteria(3, 20, 1e-6));
std::cout << "Points detected" << std::endl;
//double rms = calibrateCamera(objectPoints, image_points, cv::Size(1920, 1080), K, D, rvec, tvec, flag, cv::TermCriteria(3, 20, 1e-6));
std::cout << "Calibrated." << std::endl;
std::cout << K << std::endl;
std::cout << D << std::endl;
for (int i = 0; i < inputImages.size(); i++)
{
cv::Mat src = cv::imread("C:/Data/CheckerboardCalib/" + std::to_string(i + 1) + ".png");
cv::Mat temp;
cv::fisheye::undistortImage(src, temp, K, D, cv::Mat::eye(3,3, CV_64F));
cv::imwrite("C:/Data/CheckerboardCalib/undistorted/" + std::to_string(i+1) + ".png", temp);
}
std::string inputLine;
std::cin >> inputLine;
}