# OpenCV:鱼眼相机去畸变=图像去畸变+点去畸变

## 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()

image.png

image.png

https://docs.opencv.org/master/d4/d94/tutorial_camera_calibration.html

image.png

step3.remap()图像矫正函数

``````// 图像去畸变
cv::Mat correct_image;
cv::remap(cam_im, correct_image, map1, map2, cv::INTER_LINEAR);
``````

(4)undistortImage()函数

Knew是畸变图像的相机内参矩阵,默认是单位阵.

image.png

image.png

(5)undistortPoints是点去畸变函数

image.png

## 程序

``````//已知相机内参和畸变系数
//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 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;

``````

image.png