Opencv中的两种去畸变函数
Opencv中的两种去畸变函数
前⾔
两者的不同
在说两者不同之前,我们先介绍下getOptimalNewCameraMatrix()函数。
getOptimalNewCameraMatrix(cv::InputArray _cameraMatrix,cv::InputArray _distCoeffs,cv::Size imgSize, double alpha, cv::Size newImgSize,cv::Rect* valid PixROI, bool centerPrincipalPoint)
alpha之前的参数⾃不必说,alpha之后的参数都可以视为默认。关键就在alpha的含义。
icvGetRectangles(cameraMatrix, distCoeffs, 0, 0, imgSize, inner, outer);
// Projection mapping inner rectangle to viewport
double fx0 = (newImgSize.width) / inner.width;
double fy0 = (newImgSize.height) / inner.height;
double cx0 = -fx0 * inner.x;
double cy0 = -fy0 * inner.y;
// Projection mapping outer rectangle to viewport
double fx1 = (newImgSize.width) / outer.width;
double fy1 = (newImgSize.height) / outer.height;
double cx1 = -fx1 * outer.x;
double cy1 = -fy1 * outer.y;
// Interpolate between the two optimal projections
M[0][0] = fx0*(1 - alpha) + fx1*alpha;
M[1][1] = fy0*(1 - alpha) + fy1*alpha;rectangle函数opencv
M[0][2] = cx0*(1 - alpha) + cx1*alpha;
M[1][2] = cy0*(1 - alpha) + cy1*alpha;
在getOptimalNewCameraMatrix()函数中,有上述⼀段代码,意思是从内参/畸变系数中得到两个inner和outer矩阵,当alpha为0时,取inner即内矩阵,⽤内矩阵⼤⼩作为新的图像⼤⼩,重新得到fx,fy,cx,cy,因此新的内参矩阵诞⽣了. 当alpha为1时,取outer即外矩阵。当alpha介于0~1时,则按照⽐例重新计算fx,fy,cx,cy。
事实上,内矩阵等同于不含任何⿊⾊边框的图幅⼤⼩,⽽外矩阵等同于原图⼤⼩。
继续深究,那么它的inner和outer⼜是怎么来的呢,
inner和outer的来源
inner和outer⼜是怎么来的呢,和原来的内参与畸变⼜有什么关系呢?
在icvGetRectangles()函数中有这样⼀段代码
for (y = k = 0; y < N; y++) //opencv 默认为 9
for (x = 0; x < N; x++)
pts[k++] = cvPoint2D32f((float)x*imgSize.width / (N - 1),
(float)y*imgSize.height / (N - 1));
cvUndistortPoints2(_pts, _pts, cameraMatrix, distCoeffs, R, newCameraMatrix);
float iX0 = -FLT_MAX, iX1 = FLT_MAX, iY0 = -FLT_MAX, iY1 = FLT_MAX;
float oX0 = FLT_MAX, oX1 = -FLT_MAX, oY0 = FLT_MAX, oY1 = -FLT_MAX;
/
/ find the inscribed rectangle.
// the code will likely not work with extreme rotation matrices (R) (>45%)
for (y = k = 0; y < N; y++)
for (x = 0; x < N; x++)
{
CvPoint2D32f p = pts[k++];
oX0 = MIN(oX0, p.x);
oX1 = MAX(oX1, p.x);
oY0 = MIN(oY0, p.y);
oY1 = MAX(oY1, p.y);
if (x == 0)
iX0 = MAX(iX0, p.x);
if (x == N - 1)
iX1 = MIN(iX1, p.x);
if (y == 0)
iY0 = MAX(iY0, p.y);
if (y == N - 1)
iY1 = MIN(iY1, p.y);
}
inner = cv::Rect_<float>(iX0, iY0, iX1 - iX0, iY1 - iY0);
outer = cv::Rect_<float>(oX0, oY0, oX1 - oX0, oY1 - oY0);
这段代码的含义是指,按照输⼊图像的⼤⼩,⽣成长宽分别按照 imgSize.width / 8的间隔和 imgSize.height / 8 间隔的9*9个点。再对这81个点,进⾏去畸变转换得到新的点。
再从去畸变后的点中相互⽐较,得到外围矩阵oX0,oY0,oX1,oY1,及由原四个⾓点得到的内围矩阵,也就是outer与inner。
结束语

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。