From e39eb949ea617c8598cc43ddcc023899a13defa2 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 3 Mar 2025 11:35:05 +0100 Subject: [PATCH] Use only image contour for camera matrix undistortion. --- modules/calib3d/src/calibration_base.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/modules/calib3d/src/calibration_base.cpp b/modules/calib3d/src/calibration_base.cpp index 166f297917..cb12037a38 100644 --- a/modules/calib3d/src/calibration_base.cpp +++ b/modules/calib3d/src/calibration_base.cpp @@ -1527,12 +1527,20 @@ void cv::getUndistortRectangles(InputArray _cameraMatrix, InputArray _distCoeffs { const int N = 9; int x, y, k; - Mat _pts(1, N*N, CV_64FC2); + Mat _pts(1, 4*(N-1), CV_64FC2); Point2d* pts = _pts.ptr(); for( y = k = 0; y < N; y++ ) + { for( x = 0; x < N; x++ ) + { + if (x != 0 && x != N - 1 && y != 0 && y != N - 1) + { + continue; + } pts[k++] = Point2d((double)x*(imgSize.width-1)/(N-1), (double)y*(imgSize.height-1)/(N-1)); + } + } undistortPoints(_pts, _pts, _cameraMatrix, _distCoeffs, R, newCameraMatrix); @@ -1541,8 +1549,14 @@ void cv::getUndistortRectangles(InputArray _cameraMatrix, InputArray _distCoeffs // 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++ ) { + if (x != 0 && x != N - 1 && y != 0 && y != N - 1) + { + continue; + } + Point2d p = pts[k++]; oX0 = MIN(oX0, p.x); oX1 = MAX(oX1, p.x); @@ -1558,6 +1572,7 @@ void cv::getUndistortRectangles(InputArray _cameraMatrix, InputArray _distCoeffs if( y == N-1 ) iY1 = MIN(iY1, p.y); } + } inner = Rect_(iX0, iY0, iX1-iX0, iY1-iY0); outer = Rect_(oX0, oY0, oX1-oX0, oY1-oY0); }