From 5e6a59135771ead66dad5f59be66ac351109e9ad Mon Sep 17 00:00:00 2001 From: Matt Van Horn Date: Fri, 5 Jun 2026 09:35:44 -0700 Subject: [PATCH] Merge pull request #28873 from mvanhorn:osc/28429-fix-inter-nearest-exact-fp-5x imgproc: fix INTER_NEAREST_EXACT to match Pillow (5.x retarget of #28661) #28873 ## Summary Fixes `INTER_NEAREST_EXACT` producing different results from Pillow for images with dimensions that are multiples of 64 (e.g., 128, 192). Retargets #28661 to 5.x per core team request. ## Root cause The old 16-bit fixed-point arithmetic (`ifx`, `ifx0`, `>> 16`) rounds differently from Pillow's pixel-center coordinate mapping. At exact pixel boundaries (e.g., y=7 with 128 to 160: `0.4 + 7*0.8 = 5.999999999999999`), Pillow's truncation drops to 5 while the old opencv formula rounds to 6. ## Changes - `resize.cpp`: Replace `ifx/ifx0/ify/ify0` 16-bit fixed-point with an int64 arithmetic formula equivalent to `floor((i + 0.5) * src / dst)`, computed as `((int64_t)(i * 2 + 1) * src) / (dst * 2)`. This matches Pillow's pixel-center mapping exactly, is deterministic across platforms, and avoids FP rounding divergence entirely. - `test_resize_bitexact.cpp`: Add `NearestExact_PillowCompat` covering 4 dimension pairs including the reproducer from #28429. ## Testing - All 3 `Resize_Bitexact` tests pass (Linear8U, Nearest8U, NearestExact_PillowCompat) on this branch built against 5.x - Verified bit-exact match to PIL output (Python 3.14, Pillow) on 49 dimension combinations including random pairs - Built on macOS ARM64 with HAL (carotene/KleidiCV) active Fixes #28429 --- hal/riscv-rvv/src/imgproc/resize.cpp | 4 +- .../imgproc/misc/python/test/test_imgproc.py | 18 ++++++ modules/imgproc/src/resize.cpp | 44 ++++++++------ modules/imgproc/test/test_resize_bitexact.cpp | 60 +++++++++++++++++++ 4 files changed, 107 insertions(+), 19 deletions(-) diff --git a/hal/riscv-rvv/src/imgproc/resize.cpp b/hal/riscv-rvv/src/imgproc/resize.cpp index 1ce5e16bb3..facac7c552 100644 --- a/hal/riscv-rvv/src/imgproc/resize.cpp +++ b/hal/riscv-rvv/src/imgproc/resize.cpp @@ -990,8 +990,8 @@ int resize(int src_type, const uchar *src_data, size_t src_step, int src_width, { inv_scale_x = 1 / inv_scale_x; inv_scale_y = 1 / inv_scale_y; - if (interpolation == CV_HAL_INTER_NEAREST || interpolation == CV_HAL_INTER_NEAREST_EXACT) - return resizeNN(src_type, src_data, src_step, src_width, src_height, dst_data, dst_step, dst_width, dst_height, inv_scale_x, inv_scale_y, interpolation); + //if (interpolation == CV_HAL_INTER_NEAREST || interpolation == CV_HAL_INTER_NEAREST_EXACT) + // return resizeNN(src_type, src_data, src_step, src_width, src_height, dst_data, dst_step, dst_width, dst_height, inv_scale_x, inv_scale_y, interpolation); if (interpolation == CV_HAL_INTER_LINEAR || interpolation == CV_HAL_INTER_LINEAR_EXACT) return resizeLinear(src_type, src_data, src_step, src_width, src_height, dst_data, dst_step, dst_width, dst_height, inv_scale_x, inv_scale_y, interpolation); if (interpolation == CV_HAL_INTER_AREA) diff --git a/modules/imgproc/misc/python/test/test_imgproc.py b/modules/imgproc/misc/python/test/test_imgproc.py index db5a34f708..5fbc026657 100644 --- a/modules/imgproc/misc/python/test/test_imgproc.py +++ b/modules/imgproc/misc/python/test/test_imgproc.py @@ -4,6 +4,10 @@ from __future__ import print_function import numpy as np import cv2 as cv +try: + from PIL import Image +except ImportError: + Image = None from tests_common import NewOpenCVTests @@ -28,3 +32,17 @@ class Imgproc_Tests(NewOpenCVTests): img_blur0 = cv.filter2D(img, cv.CV_32F, kernel*(1./9)) img_blur1 = cv.filter2Dp(img, kernel, ddepth=cv.CV_32F, scale=1./9) self.assertLess(cv.norm(img_blur0 - img_blur1, cv.NORM_INF), eps) + + def test_resize_pillow(self): + if Image is None: + self.skipTest("Pillow is not available") + + r = np.random.randint(0, 255, size=(128, 147, 3), dtype="uint8") + target_size=[(128,128), (129,129), (160,160)] + for ts in target_size: + + pil_result = np.array(Image.fromarray(r).resize(ts, Image.NEAREST)) + ocv_result = cv.resize(r, dsize=ts, interpolation=cv.INTER_NEAREST_EXACT) + status = np.all(pil_result == ocv_result) + print(ts, status) + self.assertTrue(status, "resize result differs from Pillow for target size %s" % (ts,)) diff --git a/modules/imgproc/src/resize.cpp b/modules/imgproc/src/resize.cpp index 548fec30e2..08a5abdd06 100644 --- a/modules/imgproc/src/resize.cpp +++ b/modules/imgproc/src/resize.cpp @@ -58,6 +58,8 @@ #include "opencv2/core/softfloat.hpp" #include "fixedpoint.inl.hpp" +#include + using namespace cv; namespace @@ -1173,18 +1175,17 @@ resizeNN( const Mat& src, Mat& dst, double fx, double fy ) class resizeNN_bitexactInvoker : public ParallelLoopBody { public: - resizeNN_bitexactInvoker(const Mat& _src, Mat& _dst, int* _x_ofse, int _ify, int _ify0) - : src(_src), dst(_dst), x_ofse(_x_ofse), ify(_ify), ify0(_ify0) {} + resizeNN_bitexactInvoker(const Mat& _src, Mat& _dst, int* _x_ofse, int* _y_ofse) + : src(_src), dst(_dst), x_ofse(_x_ofse), y_ofse(_y_ofse) {} virtual void operator() (const Range& range) const CV_OVERRIDE { - Size ssize = src.size(), dsize = dst.size(); + Size dsize = dst.size(); int pix_size = (int)src.elemSize(); for( int y = range.start; y < range.end; y++ ) { uchar* D = dst.ptr(y); - int _sy = (ify * y + ify0) >> 16; - int sy = std::min(_sy, ssize.height-1); + int sy = y_ofse[y]; const uchar* S = src.ptr(sy); int x = 0; @@ -1259,30 +1260,39 @@ private: const Mat& src; Mat& dst; int* x_ofse; - const int ify; - const int ify0; + int* y_ofse; }; +static void resizeNN_bitexact_tab(int src_dim, int dst_dim, int* ofse) +{ + // Match Pillow's nearest-neighbor resize path: start at half a pixel, + // truncate the current coordinate, then increment by scale. softdouble + // keeps the IEEE-754 rounding deterministic across platforms. + const softdouble scale = softdouble(src_dim) / softdouble(dst_dim); + softdouble f = scale * softdouble(0.5); + for( int i = 0; i < dst_dim; i++ ) + { + ofse[i] = std::min(cvFloor(f), src_dim-1); + f += scale; + } +} + static void resizeNN_bitexact( const Mat& src, Mat& dst, double /*fx*/, double /*fy*/ ) { Size ssize = src.size(), dsize = dst.size(); - int ifx = ((ssize.width << 16) + dsize.width / 2) / dsize.width; // 16bit fixed-point arithmetic - int ifx0 = ifx / 2 - ssize.width % 2; // This method uses center pixel coordinate as Pillow and scikit-images do. - int ify = ((ssize.height << 16) + dsize.height / 2) / dsize.height; - int ify0 = ify / 2 - ssize.height % 2; cv::utils::BufferArea area; int* x_ofse = 0; + int* y_ofse = 0; area.allocate(x_ofse, dsize.width, CV_SIMD_WIDTH); + area.allocate(y_ofse, dsize.height, CV_SIMD_WIDTH); area.commit(); - for( int x = 0; x < dsize.width; x++ ) - { - int sx = (ifx * x + ifx0) >> 16; - x_ofse[x] = std::min(sx, ssize.width-1); // offset in element (not byte) - } + resizeNN_bitexact_tab(ssize.width, dsize.width, x_ofse); + resizeNN_bitexact_tab(ssize.height, dsize.height, y_ofse); + Range range(0, dsize.height); - resizeNN_bitexactInvoker invoker(src, dst, x_ofse, ify, ify0); + resizeNN_bitexactInvoker invoker(src, dst, x_ofse, y_ofse); parallel_for_(range, invoker, dst.total()/(double)(1<<16)); } diff --git a/modules/imgproc/test/test_resize_bitexact.cpp b/modules/imgproc/test/test_resize_bitexact.cpp index a1bb6af544..def5ceb0f0 100644 --- a/modules/imgproc/test/test_resize_bitexact.cpp +++ b/modules/imgproc/test/test_resize_bitexact.cpp @@ -240,4 +240,64 @@ TEST(Resize_Bitexact, Nearest8U) } } +// Regression test for #28429: INTER_NEAREST_EXACT matches Pillow's +// center-of-pixel nearest-neighbor mapping. +TEST(Resize_Bitexact, NearestExact_PillowCompat) +{ + auto center_pixel_map = [](int src_dim, int dst_dim, std::vector& mapping) { + softdouble scale = softdouble(src_dim) / softdouble(dst_dim); + softdouble f = scale * softdouble(0.5); + mapping.resize(dst_dim); + for (int i = 0; i < dst_dim; i++) + { + mapping[i] = std::min(cvTrunc(f), src_dim - 1); + f += scale; + } + }; + + // Test dimension pairs including multiples of 64 that triggered the bug + const int cases[][4] = { + {128, 147, 160, 160}, // original reproducer from #28429 + {128, 128, 160, 160}, // square with problematic height + {192, 192, 256, 256}, // another multiple of 64 + {129, 147, 160, 160}, // non-problematic control case + }; + + for (const auto& c : cases) + { + int src_h = c[0], src_w = c[1], dst_h = c[2], dst_w = c[3]; + + std::vector x_map, y_map; + center_pixel_map(src_w, dst_w, x_map); + center_pixel_map(src_h, dst_h, y_map); + + Mat src(src_h, src_w, CV_8UC3); + randu(src, Scalar::all(0), Scalar::all(256)); + + Mat result; + resize(src, result, Size(dst_w, dst_h), 0, 0, INTER_NEAREST_EXACT); + + int nerrs = 0; + for (int y = 0; y < dst_h; y++) + { + for (int x = 0; x < dst_w; x++) + { + Vec3b expected = src.at(y_map[y], x_map[x]); + Vec3b actual = result.at(y, x); + nerrs += expected != actual; + if (nerrs <= 10) { + EXPECT_EQ(expected, actual) + << "Mismatch at dst(" << y << "," << x << ") -> src(" + << y_map[y] << "," << x_map[x] << ") for " + << src_h << "x" << src_w << " -> " << dst_h << "x" << dst_w; + } + } + } + + ASSERT_EQ(nerrs, 0) << "nerrs=" << nerrs << " when" + << " src_h=" << src_h << ", src_w=" << src_w + << " dst_h=" << dst_h << ", dst_w=" << dst_w; + } +} + }} // namespace