本文通過攝像頭參數(shù)(fx,fy,cx,cy,k1,k2,p1,p2,p3(標(biāo)定得到))去矯正攝像頭拍出來的圖像畸變
詳細(xì)代碼在底部
首先
這里我們先介紹兩個(gè)函數(shù):他們都可以用來矯正畸變,但是一個(gè)是輸入是Mat型,一個(gè)輸入是IplImage* 型
本文用Mat格式讀取圖像,故采用第一個(gè)函數(shù)。


其次
標(biāo)定一下攝像頭,得到參數(shù)矩陣。(我用matlab標(biāo)定工具箱搞出來的~)

再次
這里通過paramtersInit()將參數(shù)轉(zhuǎn)換為系數(shù)陣
然后傳入到undistort里面去,這就結(jié)束了~
效果

代碼
```
#include <iostream>
#include<opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
Mat matrix;
Mat coeff;
Mat new_matrix;
double fx = 682.421509, fy = 682.421509, cx = 633.947449, cy = 404.559906;
double k1k2Distortion[2] = {-0.15656,-0.00404};
double p1p2p3Distortion[3] = {-0.00078,-0.00048,0.00000};
using namespace cv;
using namespace std;
void paramtersInit();//將參數(shù)轉(zhuǎn)換為系數(shù)陣
int main()
{
cv::Mat srcMat, dstMat;
paramtersInit();
srcMat = imread("img.jpg");
undistort(srcMat, dstMat, matrix, coeff, new_matrix);
imwrite("undistortImg.png", dstMat);
imshow("front", srcMat);
imshow("after",dstMat);
waitKey(0);
system("pause");
return 0;
}
void paramtersInit()
{
matrix = Mat(3, 3, CV_64F);
matrix.at<double>(0, 0) = fx;
matrix.at<double>(0, 1) = 0;
matrix.at<double>(0, 2) = cx;
matrix.at<double>(1, 0) = 0;
matrix.at<double>(1, 1) = fy;
matrix.at<double>(1, 2) = cy;
matrix.at<double>(2, 0) = 0;
matrix.at<double>(2, 1) = 0;
matrix.at<double>(2, 2) = 1;
coeff = Mat(1, 4, CV_64F);
coeff.at<double>(0, 0) = k1k2Distortion[0];
coeff.at<double>(0, 1) = k1k2Distortion[1];
coeff.at<double>(0, 2) = p1p2p3Distortion[0];
coeff.at<double>(0, 3) = p1p2p3Distortion[1];
}
```