unity 畸变_去畸变过程中内参矩阵的变化

OpenCV中去畸变的方法一种是:通过cv::getOptimalNewCameraMatrix()得到新的内参矩阵,再通过cv::initUndistortRectifyMap()得到原图和去畸变后图的映射关系,最后通过cv::remap()将原图映射到新图;另一种是使用cv::undistort()直接得到去畸变的图像,The function is simply a combination of initUndistortRectifyMap (with unity R ) and remap (with bilinear interpolation)。也就是说这两种方法本质上是一样的,只需要理解第一种方法就行。

void cv::undistort(InputArray src,

OutputArray dst,

InputArray cameraMatrix,

InputArray distCoeffs,

InputArray newCameraMatrix = noArray()

)

首先要关注cv::getOptimalNewCameraMatrix()函数,在cv::initUndistortRectifyMap()和cv::undistort()都需要用到(参数newCameraMatrix )。

Mat cv::getOptimalNewCameraMatrix(InputArray cameraMatrix,

InputArray distCoeffs,

Size imageSize,

double alpha,

Size newImgSize = Size(),

Rect * validPixROI = 0,

bool centerPrincipalPoint = false

)

主要关注参数alpha:Free scaling parameter between 0 (when all the pixels in the undistorted image are valid) and 1 (when all the source image pixels are retained in the undistorted image).

OpenCV对该函数的介绍:The function computes and returns the optimal new camera matrix based on the free scaling parameter. By varying this parameter, you may retrieve only sensible pixels alpha=0 , keep all the original image pixels if there is valuable information in the corners alpha=1 , or get something in between. When alpha>0 , the undistorted result is likely to have some black pixels corresponding to "virtual" pixels outside of the captured distorted image. The original camera matrix, distortion coefficients, the computed new camera matrix, and newImageSize should be passed to initUndistortRectifyMap to produce the maps for remap .意思就是alpha控制着下图中红色框的大小,alpha=0时,去畸变后的图像为小的红色框;alpha=1时,图像为大的红色框,包含全部原始图像的像素。alpha为0到1之间时去畸变后的图像介于两个红色框之间。

实现过程是

// Interpolate between the two optimal projections

M[0][0] = fx0*(1 - alpha) + fx1*alpha;

M[1][1] = fy0*(1 - alpha) + fy1*alpha;

M[0][2] = cx0*(1 - alpha) + cx1*alpha;

M[1][2] = cy0*(1 - alpha) + cy1*alpha;

其中fx0,cx0和cy0是alpha=0时的内参,fx1,cx1和cy1是alpha=1时的内参。

我们可以通过cv::getOptimalNewCameraMatrix()得到对应的内参矩阵用于后面我们的计算。


版权声明:本文为weixin_36431145原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。