本文通过摄像头参数(fx,fy,cx,cy,k1,k2,p1,p2,p3(标定得到))去矫正摄像头拍出来的图像畸变
详细代码在底部
首先
这里我们先介绍两个函数:他们都可以用来矫正畸变,但是一个是输入是Mat型,一个输入是IplImage* 型
本文用Mat格式读取图像,故采用第一个函数。
其次
标定一下摄像头,得到参数矩阵。(我用matlab标定工具箱搞出来的~)
再次
这里通过paramtersInit()将参数转换为系数阵
然后传入到undistort里面去,这就结束了~
效果
代码
```
#include
#include
#include
#include
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();//将参数转换为系数阵
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
matrix.at
matrix.at
matrix.at
matrix.at
matrix.at
matrix.at
matrix.at
matrix.at
coeff = Mat(1, 4, CV_64F);
coeff.at
coeff.at
coeff.at
coeff.at
}
```