官網:https://docs.opencv.org/3.4/db/d58/group__calib3d__fisheye.html
魚眼相機去畸變,即圖像矯正,分為整幅圖像去畸變和點去畸變.
1.去畸變函數
先來看一下魚眼相機的去畸變函數.
已知魚眼相機的內參矩陣和畸變系數
image.png
step1.先估計新的相機內參矩陣
這個新的相機內參矩陣是去畸變后的,圖像矯正后的.
Estimates new camera matrix for undistortion or rectification.
image.png
image.png
P New camera matrix (3x3) or new projection matrix (3x4)
balance Sets the new focal length in range between the min focal length and the max focal
length. Balance is in range of [0, 1].
step2. initUndistortRectifyMap()
這個函數的作用是初始化畸變矯正矩陣,
將KDRP轉換為map1和map2.
之后就可以通過remap函數計算無畸變圖像了.
image.png
image.png
這里有個問題,為什么要把KDRP轉換為map1,map2之后在使用remap矯正?
https://docs.opencv.org/master/d4/d94/tutorial_camera_calibration.html
這里有個解釋.
之所以擴展undistort函數為initUndistortRectifyMap,是為了提高算法運行速度.
image.png
step3.remap()圖像矯正函數
// 圖像去畸變
cv::Mat cam_im = imread("1.png");
cv::Mat correct_image;
cv::remap(cam_im, correct_image, map1, map2, cv::INTER_LINEAR);
(4)undistortImage()函數
這個函數是fisheye::initUndistortRectifyMap和remap函數的合并
Knew是畸變圖像的相機內參矩陣,默認是單位陣.
(5)undistortPoints是點去畸變函數
程序
//已知相機內參和畸變系數
//step1.估計新矩陣
cv::Mat newCamMat;
// 估計新的相機內參矩陣,無畸變后的
cv::fisheye::estimateNewCameraMatrixForUndistortRectify(
camera_intrinsic_matrix, distort_coeff_matrix, img_size,
cv::Matx33d::eye(), newCamMat, 1);
//step2.計算map1,map2
cv::Mat map1, map2;
cv::fisheye::initUndistortRectifyMap(
camera_intrinsic_matrix,
distort_coeff_matrix,
cv::Matx33d::eye(), newCamMat, img_size,
CV_16SC2, map1, map2);
// step3.remap圖像去畸變
cv::Mat cam_im = imread("1.png");
cv::Mat correct_image;
cv::remap(cam_im, correct_image, map1, map2, cv::INTER_LINEAR);
//step4. undistortImage圖像去畸變
cv::Mat undistort_im;
cv::fisheye::undistortImage(
cam_im,undistort_im,
camera_intrinsic_matrix,
distort_coeff_matrix,
newCamMat,
cam_im.size());
//step5.比較一下這兩個圖像是否一直
cv::Mat substrct_im;
cv::subtract(undistort_im,correct_image,substrct_im);
cv::imwrite("substrct_im.jpg",substrct_im);
//step6.undistortPoints圖像點去畸變
std::vector<cv::Point2f> src_pts{ cv::Point2f(500,500)};
std::vector<cv::Point2f> dst_pts;
cv::fisheye::undistortPoints(
src_pts,dst_pts,
camera_intrinsic_matrix,
distort_coeff_matrix,
cv::noArray(),
newCamMat);
cout<<"dst_pts= "<<dst_pts[0]<<endl;
說明這兩個函數結果一致.
去畸變點的坐標為(725,514)和去畸變圖像中一致.