mirror of
https://github.com/opencv/opencv.git
synced 2026-12-02 21:22:43 -06:00
ef0ee908a76a89e2835101f63d9e2e18f8fdc567
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.
OpenCV: Open Source Computer Vision Library
Resources
- Homepage: https://opencv.org
- Courses: https://opencv.org/courses
- Docs: https://docs.opencv.org/5.x/
- Q&A forum: https://forum.opencv.org
- previous forum (read only): http://answers.opencv.org
- Issue tracking: https://github.com/opencv/opencv/issues
- Additional OpenCV functionality: https://github.com/opencv/opencv_contrib
- Donate to OpenCV: https://opencv.org/support/
Contributing
Please read the contribution guidelines before starting work on a pull request.
Summary of the guidelines:
- One pull request per issue;
- Choose the right base branch;
- Include tests and documentation;
- Clean up "oops" commits before submitting;
- Follow the coding style guide.
Additional Resources
- Submit your OpenCV-based project for inclusion in Community Friday on opencv.org
- Subscribe to the OpenCV YouTube Channel featuring OpenCV Live, an hour-long streaming show
- Follow OpenCV on LinkedIn for daily posts showing the state-of-the-art in computer vision & AI
- Apply to be an OpenCV Volunteer to help organize events and online campaigns as well as amplify them
- Follow OpenCV on Mastodon in the Fediverse
- Follow OpenCV on X
- OpenCV.ai: Computer Vision and AI development services from the OpenCV team.
Description
Languages
C++
87.7%
C
3.1%
Python
2.9%
CMake
2%
Java
1.5%
Other
2.6%