cv::Mat img;
cv::Mat img_undistort;
double fx,fy, cx, cy, k1, k2, p1, p2, k3;for(int v = 0; v < img.rows; v++){for(int u = 0; u < img.cols; u++){//根据内参数,计算归一化坐标系下的坐标点double x = (u-cx)/fx;double y = (v-cy)/fy;double r2 = x*x + y*y;//添加畸变double x_distort = x*(1+k1*r2 + k2*r2*r2 + k3*r2*r2*r2) + 2*p1*x*y + p2*(r2+2*x*x);double y_distort = y*(1+k1*r2 + k2*r2*r2 + k3*r2*r2*r2) + p1*(r2+2*y*y)+2*p2*x*y;//转换到像素平面下double u_distort = x_distort*fx + cx;double v_distort = y_distort*fy + cy;// 到目前为止,已经得到了畸变前后点的对应位置了,那就把畸变后图像上的点//投影到去畸变的位置。if(u_distort > 0 && u_distort < img.cols && v_distort >0 && v_distort < img.rows){img_undistort.at<uchar>(v, u) = img.at<uchar>((int)v_distort, (int)u_distort);}else{img_undistort.at<uchar>(v,u) = 0;
}
}
}