{畸变矫正}图像去畸变opencv各种方法与matlab各种方法-联合分析(一统江湖)

 参考  https://docs.opencv.org/3.3.0/da/d54/group__imgproc__transform.html#ga69f2545a8b62a6b0fc2ee060dc30559d

方法一undistort()   与matlab标定去畸变显示相同

§ undistort()

void cv::undistort ( InputArray  src,
    OutputArray  dst,
    InputArray  cameraMatrix,
    InputArray  distCoeffs,
    InputArray  newCameraMatrix = noArray() 
  )    

Transforms an image to compensate for lens distortion.

The function transforms an image to compensate radial and tangential lens distortion.

The function is simply a combination of cv::initUndistortRectifyMap (with unity R ) and cv::remap (with bilinear interpolation). See the former function for details of the transformation being performed.

Those pixels in the destination image, for which there is no correspondent pixels in the source image, are filled with zeros (black color).

A particular subset of the source image that will be visible in the corrected image can be regulated by newCameraMatrix. You can use cv::getOptimalNewCameraMatrix to compute the appropriate newCameraMatrix depending on your requirements.

The camera matrix and the distortion parameters can be determined using cv::calibrateCamera. If the resolution of images is different from the resolution used at the calibration stage, fx,fy,cx and cy need to be scaled accordingly, while the distortion coefficients remain the same.

Parameters
src Input (distorted) image.
dst Output (corrected) image that has the same size and type as src .
cameraMatrix Input camera matrix A=fx000fy0cxcy1 .
distCoeffs Input vector of distortion coefficients (k1,k2,p1,p2[,k3[,k4,k5,k6[,s1,s2,s3,s4[,τx,τy]]]]) of 4, 5, 8, 12 or 14 elements. If the vector is NULL/empty, the zero distortion coefficients are assumed.
newCameraMatrix Camera matrix of the distorted image. By default, it is the same as cameraMatrix but you may additionally scale and shift the result by using a different matrix.



方法二   cv::initUndistortRectifyMap  and cv::remap

initUndistortRectifyMap()

void cv::initUndistortRectifyMap ( InputArray  cameraMatrix,
    InputArray  distCoeffs,
    InputArray  R,
    InputArray  newCameraMatrix,
    Size  size,
    int  m1type,
    OutputArray  map1,
    OutputArray  map2 
  )    

Computes the undistortion and rectification transformation map.

The function computes the joint undistortion and rectification transformation and represents the result in the form of maps for remap. The undistorted image looks like original, as if it is captured with a camera using the camera matrix =newCameraMatrix and zero distortion. In case of a monocular camera, newCameraMatrix is usually equal to cameraMatrix, or it can be computed by cv::getOptimalNewCameraMatrix for a better control over scaling. In case of a stereo camera, newCameraMatrix is normally set to P1 or P2 computed by cv::stereoRectify .

Also, this new camera is oriented differently in the coordinate space, according to R. That, for example, helps to align two heads of a stereo camera so that the epipolar lines on both images become horizontal and have the same y- coordinate (in case of a horizontally aligned stereo camera).

The function actually builds the maps for the inverse mapping algorithm that is used by remap. That is, for each pixel (u,v) in the destination (corrected and rectified) image, the function computes the corresponding coordinates in the source image (that is, in the original image from camera). The following process is applied:

x(ucx)/fxy(vcy)/fy[XYW]TR1[xy1]TxX/WyY/Wr2x2+y2xx1+k1r2+k2r4+k3r61+k4r2+k5r4+k6r6+2p1xy+p2(r2+2x2)+s1r2+s2r4yy1+k1r2+k2r4+k3r61+k4r2+k5r4+k6r6+p1(r2+2y2)+2p2xy+s3r2+s4r4sxy1=R33(τx,τy)000R33(τx,τy)0R13((τx,τy)R23(τx,τy)1R(τx,τy)xy1mapx(u,v)xfx+cxmapy(u,v)yfy+cy

where (k1,k2,p1,p2[,k3[,k4,k5,k6[,s1,s2,s3,s4[,τx,τy]]]]) are the distortion coefficients.

In case of a stereo camera, this function is called twice: once for each camera head, after stereoRectify, which in its turn is called after cv::stereoCalibrate. But if the stereo camera was not calibrated, it is still possible to compute the rectification transformations directly from the fundamental matrix using cv::stereoRectifyUncalibrated. For each camera, the function computes homography H as the rectification transformation in a pixel domain, not a rotation matrix R in 3D space. R can be computed from H as

猜你喜欢

转载自blog.csdn.net/kyjl888/article/details/81043133