關(guān)于opencv圖像畸變矯正

本文通過攝像頭參數(shù)(fx,fy,cx,cy,k1,k2,p1,p2,p3(標(biāo)定得到))去矯正攝像頭拍出來的圖像畸變

詳細(xì)代碼在底部

首先

這里我們先介紹兩個(gè)函數(shù):他們都可以用來矯正畸變,但是一個(gè)是輸入是Mat型,一個(gè)輸入是IplImage* 型

本文用Mat格式讀取圖像,故采用第一個(gè)函數(shù)。

undistort()? ? ? ? //用于Mat圖像
cvUndistort()? ? ?//用于 IplImage* 圖像

其次

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

Matlab2019 標(biāo)定工具箱結(jié)果

再次

這里通過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];

}

```

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容