我使用一组 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;
}