From d89fbd99cf0ad890642b5fd745ec2d9877d64182 Mon Sep 17 00:00:00 2001 From: melody419 <118264117+melody419@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:03:16 +0800 Subject: [PATCH] imgproc: fix warpPolar inverse mapping at the angular seam and for sub-matrix input (5.x) (#30851) * imgproc: fix warpPolar inverse mapping at the angular seam and for sub-matrix input * imgproc: use a mask for the ring check in the warpPolar seam test --- modules/imgproc/src/imgwarp.cpp | 7 +++-- modules/imgproc/test/test_imgwarp.cpp | 43 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/modules/imgproc/src/imgwarp.cpp b/modules/imgproc/src/imgwarp.cpp index 90e9193b0f..b4b8325d95 100644 --- a/modules/imgproc/src/imgwarp.cpp +++ b/modules/imgproc/src/imgwarp.cpp @@ -3278,8 +3278,11 @@ void cv::warpPolar(InputArray _src, OutputArray _dst, Size dsize, } else { - const int ANGLE_BORDER = 1; - cv::copyMakeBorder(_src, _dst, ANGLE_BORDER, ANGLE_BORDER, 0, 0, BORDER_WRAP); + // The angle is periodic, so the source is extended with wrapped rows. + // Their number has to cover the interpolation kernel: 2 taps for linear, 4 for cubic, 8 for Lanczos. + const int interpolation = flags & cv::INTER_MAX; + const int ANGLE_BORDER = interpolation == INTER_LANCZOS4 ? 4 : interpolation == INTER_CUBIC ? 2 : 1; + cv::copyMakeBorder(_src, _dst, ANGLE_BORDER, ANGLE_BORDER, 0, 0, BORDER_WRAP | BORDER_ISOLATED); Mat src = _dst.getMat(); Size ssize = _dst.size(); ssize.height -= 2 * ANGLE_BORDER; diff --git a/modules/imgproc/test/test_imgwarp.cpp b/modules/imgproc/test/test_imgwarp.cpp index 58009007c4..1e2a7d2e25 100644 --- a/modules/imgproc/test/test_imgwarp.cpp +++ b/modules/imgproc/test/test_imgwarp.cpp @@ -1089,6 +1089,49 @@ TEST(Imgproc_warpPolar, identity) #endif } +typedef testing::TestWithParam Imgproc_warpPolar_Inverse; + +// the inverse transform of a constant polar image has to be constant up to the angular seam +TEST_P(Imgproc_warpPolar_Inverse, angular_seam) +{ + const int interpolation = GetParam(); + const Mat polar(360, 100, CV_8UC1, Scalar::all(200)); + const Point2f center(100.f, 100.f); + const double radius = 100; + + for (int fill = 0; fill < 2; fill++) + { + Mat dst; + warpPolar(polar, dst, Size(201, 201), center, radius, + interpolation | WARP_POLAR_LINEAR | WARP_INVERSE_MAP | (fill ? (int)WARP_FILL_OUTLIERS : 0)); + + // skip the centre and the outer rim, where the kernel leaves the source along the radius + Mat mask = Mat::zeros(dst.size(), CV_8UC1); + circle(mask, Point(100, 100), 90, Scalar(255), FILLED); + circle(mask, Point(100, 100), 4, Scalar(0), FILLED); + EXPECT_EQ(0, countNonZero((dst != 200) & mask)) << "fill outliers: " << fill; + } +} + +// a sub-matrix has to give the same result as its copy +TEST_P(Imgproc_warpPolar_Inverse, submatrix) +{ + const int interpolation = GetParam(); + Mat big(140, 140, CV_8UC1); + theRNG().fill(big, RNG::UNIFORM, 0, 256); + const Rect roi(20, 25, 100, 90); + const int flags = interpolation | WARP_POLAR_LINEAR | WARP_INVERSE_MAP | WARP_FILL_OUTLIERS; + + Mat dst_roi, dst_copy; + warpPolar(big(roi), dst_roi, Size(120, 120), Point2f(60.f, 60.f), 60, flags); + warpPolar(big(roi).clone(), dst_copy, Size(120, 120), Point2f(60.f, 60.f), 60, flags); + + EXPECT_EQ(0, cvtest::norm(dst_roi, dst_copy, NORM_INF)); +} + +INSTANTIATE_TEST_CASE_P(/**/, Imgproc_warpPolar_Inverse, + testing::Values((int)INTER_NEAREST, (int)INTER_LINEAR, (int)INTER_CUBIC, (int)INTER_LANCZOS4)); + TEST(Imgproc_Remap, issue_23562) { cv::RNG rng(17);