Opencv 图像畸变矫正(after 相机标定, 获得内参和畸变参数)
参数解析的部分,根据自己的存放形式修改
#include <iostream> #include <fstream> #include <io.h> #include <opencv2/opencv.hpp> using namespace std; inline void getParams(char *pathName, CvMat* intrinsics, CvMat *distortion_coeff) { FILE* ifs = fopen(pathName, "r"); fscanf(ifs, "%f\t%f\n",&(intrinsics->data.fl[0]), &(intrinsics->data.fl[4])); fscanf(ifs, "%f\t%f\n",&intrinsics->data.fl[2], &intrinsics->data.fl[5]); intrinsics->data.fl[1] = 0; intrinsics->data.fl[3] = 0; intrinsics->data.fl[6] = 0; intrinsics->data.fl[7] = 0; intrinsics->data.fl[8] = 1; float tmp; fscanf(ifs, "%f\n",&tmp); for (int i=0; i<4; i++) fscanf(ifs, "%f",&distortion_coeff->data.fl[i]); //打印输出: for (int i=0; i<3; i++) { cout<<endl; for(int j=0; j<3; j++) cout<<intrinsics->data.fl[i*3+j]<<"\t"; } for (int i=0; i<4; i++) cout<<distortion_coeff->data.fl[i]<<endl; fclose(ifs); } int main() { CvMat* intrinsics = cvCreateMat(3,3,CV_32FC1); CvMat* distortion_coeff = cvCreateMat(1,4, CV_32FC1); getParams("E:\\cameraInsparams.txt", intrinsics, distortion_coeff); IplImage *img = cvLoadImage("123.bmp",8,1); IplImage *undistImg = cvCloneImage(img); cvUndistort2(img,undistImg ,intrinsics,distortion_coeff); cvShowImage("ori", img); cvShowImage("undist", undistImg);j cvWaitKey(); cvReleaseImage(&img); cvReleaseImage(&undistImg); return 0; }
or:
IplImage* myGetUdisImg(IplImage* inputImg, CvMat* intrinsics, CvMat* distortion_coeff) { IplImage* udistImg = cvCloneImage(inputImg); cvUndistort2(inputImg,udistImg,intrinsics,distortion_coeff); return udistImg; }
矫正之后:
Trouble is a Friend