主要用到的是 initUndistortRectifyMap这个函数
在opencv中这个函数是用于 去除镜头畸变的图像拉伸
为了快速算法:使用了坐标查找变和双线性差值的方法
先上结果图
原图
去畸变至全图
去畸变并保留最大图
#include "opencv.hpp"
using namespace std;
using namespace cv;
void ReadIntrinsics(Mat &cameraMatrix, Mat &distCoeffs, Size &imageSize, char *IntrinsicsPath)
{
bool FSflag = false;
FileStorage readfs;
FSflag = readfs.open(IntrinsicsPath, FileStorage::READ);
if (FSflag == false) cout << "Cannot open the file" << endl;
readfs["Camera_Matrix"] >> cameraMatrix;
readfs["Distortion_Coefficients"] >> distCoeffs;
readfs["image_Width"] >> imageSize.width;
readfs["image_Height"] >> imageSize.height;
cout << cameraMatrix << endl << distCoeffs << endl << imageSize << endl;
readfs.release();
}
void Undistort_img(Mat map1, Mat map2, char *path)
{
Mat img1, img2;
img1 = imread(path);
if (img1.empty()) cout << "Cannot open the image" << endl;
remap(img1, img2, map1, map2, INTER_LINEAR);
// imwrite(path, img2);
imshow("src img", img1);
imshow("dst img", img2);
waitKey();
}
void main()
{
Mat cameraMatrix, distCoeffs, map1, map2;
Size imageSize;
char * IntrinsicsPath = "Intrinsics.yml";
ReadIntrinsics(cameraMatrix, distCoeffs, imageSize, IntrinsicsPath);
// 去畸变并保留最大图
initUndistortRectifyMap(cameraMatrix, distCoeffs, Mat(),
getOptimalNewCameraMatrix(cameraMatrix, distCoeffs, imageSize, 1, imageSize, 0),
imageSize, CV_16SC2, map1, map2);
Undistort_img(map1, map2, "E:/VS13/undistort/undistort/1.bmp");
// 去畸变至全图
initUndistortRectifyMap(cameraMatrix, distCoeffs, Mat(), Mat(),
imageSize, CV_16SC2, map1, map2);
Undistort_img(map1, map2, "E:/VS13/undistort/undistort/1.bmp");
}