图像径向畸变矫正

来源:互联网 发布:上海 日料 知乎 编辑:程序博客网 时间:2024/04/27 13:24

1. 最近遇到图像畸变矫正的问题,


因此学习了张正友的"A Flexible New Technique for CameraCalibration"一文,只考虑二阶径向畸变,描述如下:

2. 校正径向畸变

径向畸变的校正过程就是给理想像素点位置赋予畸变后的坐标处的像素值的过程。

其中(u,v)为理想的像素坐标,由于径向畸变的存在,实际得到的有畸变的位置变成了u',v'. 为了达到矫正的目的,将理想图像坐标(u,v)处的坐标值赋为实际得到的图像中的(u'.v')处的像素值即可。

畸变校正对比如下图:


校正后:


实现代码如下:

void undistortion(Mat &sourceImg, Mat &compensatedImg){float normalizedX;//nomalizeX = Xw / Zwfloat normalizedY;//nomalizeY = Yw / Zwfloat distance;//radiusfloat radialDistor;//radial distortion factorint CAM_U0=269;int CAM_V0=244;float CAM_FX=1674.549990;float CAM_FY=1675.318550;float CAM_K1=-0.273573;float CAM_K2=-0.790143;uchar sourceV;//gray value of current positionint distorPixelPositionX;//畸变后的位置int distorPixelPositionY;              //畸变后的位置//compensatedImg = cvCreateImage(cvSize(sourceImg->height, sourceImg->width), IPL_DEPTH_8U, 1);//show_image(sourceImg);for(int h = 0; h < sourceImg.rows; h++){for(int w = 0; w < sourceImg.cols; w++){normalizedX = ((float)w - CAM_U0) / CAM_FX;normalizedY = ((float)h - CAM_V0) / CAM_FY;distance = normalizedX * normalizedX + normalizedY * normalizedY;radialDistor = CAM_K1 * distance + CAM_K2 * pow(distance , 2) ;//compensatedX = (((float)w - CAM_U0) / CAM_FX ) / radialDistor;//compensatedY = (((float)h - CAM_V0) / CAM_FY ) / radialDistor;distorPixelPositionX = w+((float)w-CAM_U0)*radialDistor;distorPixelPositionY = h+((float)h-CAM_V0)*radialDistor;if(distorPixelPositionX < 0 || distorPixelPositionX > sourceImg.cols - 1){distorPixelPositionX = 0;}if(distorPixelPositionY < 0 || distorPixelPositionY > sourceImg.rows- 1){distorPixelPositionY = 0;}//sourceV = cvGet2D(sourceImg, h, w);sourceV = sourceImg.at<uchar>(distorPixelPositionY,distorPixelPositionX);;compensatedImg.at<uchar>(h,w)=sourceV;//cvSet2D(compensatedImg, newPixelPositionY, newPixelPositionX, sourceV);}}}
0 0