Files
opencv-MIRROR/modules/imgproc
NIKHIL ef0ee908a7 imgproc: fix stackBlur results on full-range 16-bit images (5.x) 🤖🤖🤖 (#30675)
cv::stackBlur returns wrong values on 16-bit images whose values use the upper part of the range. A constant image doesn't stay constant, and the output changes with the thread count.

There are two causes, both only in the universal-intrinsics paths for 16-bit data:

opRow<ushort>, opRow<short>, opColumn<ushort, int> and opColumn<short, int> compute (sum * mul) >> shr. stackblurMul is sized for 8-bit sums, so with a 16-bit sum the product overflows 32 bits.
opRow<ushort> and opRow<short> use v_add on two 16-bit samples before the kernel multiply. That add saturates. It's the same loss on any two samples whose sum doesn't fit in 16 bits.
The existing StackBlur.regression test doesn't reach this, because it converts an 8-bit image and the values stay small.

Fix
Add stackblurDivide, which computes trunc(float(sum) * mulVal). That is the same arithmetic the scalar loops already use, so the two now agree, and stripe boundaries no longer change the result. The 8-bit paths are unchanged.
Multiply each 16-bit sample after widening instead of adding two samples first.
Use int64_t sums where the largest sum doesn't fit int: from radius 181 for CV_16U, radius 255 for CV_16S and radius 2901 for CV_8U. Those radii take the scalar loops. Radii that fit keep the 32-bit SIMD paths.
opColumn<uchar, int> multiplies and shifts in v_uint32. From radius 181, sum * stackblurMul[radius] no longer fits a signed 32-bit int for values from 128 up, so vertical CV_8U blurs with kernels 363 to 509 saturated to 255. The largest product fits 32 bits unsigned. This was already on the base; nothing changes below radius 181.
Reject a kernel whose worst-case sum doesn't fit int64_t either, with a CV_Check: radius 11,863,373 and up for CV_16U, 16,777,215 for CV_16S, 190,184,348 for CV_8U. Only images tens of millions of pixels long reach these.
2026-11-20 08:39:58 +05:30
..
2024-10-13 03:25:24 +03:00