From a63ebb3dd1f586f8d8bec85a31ec48a96d29ebc2 Mon Sep 17 00:00:00 2001 From: Nishida Daishiro Date: Wed, 28 May 2025 18:23:47 +0900 Subject: [PATCH] fix getUndistortRectangles to ignore corners --- modules/calib3d/src/calibration_base.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/modules/calib3d/src/calibration_base.cpp b/modules/calib3d/src/calibration_base.cpp index f90a85a064..71836367e3 100644 --- a/modules/calib3d/src/calibration_base.cpp +++ b/modules/calib3d/src/calibration_base.cpp @@ -1527,7 +1527,7 @@ void cv::getUndistortRectangles(InputArray _cameraMatrix, InputArray _distCoeffs { const int N = 9; int x, y, k; - Mat _pts(1, 4*(N-1), CV_64FC2); + Mat _pts(1, 4*(N-2), CV_64FC2); Point2d* pts = _pts.ptr(); // generate a grid of points across the image to estimate the distortion deformation @@ -1543,6 +1543,12 @@ void cv::getUndistortRectangles(InputArray _cameraMatrix, InputArray _distCoeffs // have no influence on the two deformation rectangles that are calculated below continue; } + if ((x == 0 || x == N - 1) && (y == 0 || y == N - 1)) + { + // skip corners, because undistortPoints is likely to fail and return the same + // value + continue; + } pts[k++] = Point2d(x * stepX, y * stepY); } } @@ -1561,6 +1567,10 @@ void cv::getUndistortRectangles(InputArray _cameraMatrix, InputArray _distCoeffs { continue; } + if ((x == 0 || x == N - 1) && (y == 0 || y == N - 1)) + { + continue; + } Point2d p = pts[k++]; oX0 = MIN(oX0, p.x);