imgproc: Port 4.x enable boxFilter OpenCL fast paths on all aligned GPUs (5.x)
This PR only removes the Intel vendor gate as a port of upstream OpenCV PR #29805.
No changes are made to the underlying OpenCL kernels or dispatch logic.
Regression is observed on Apple M5 when filterSmall is used, including cases up to about 3.5× slower at odd, large sizes.
Earlier benchmark data in imgproc: Enable boxFilter OpenCL fast paths on Non-Intel GPUs. #29805 showed gains on AMD and NVIDIA GPUs for the small-kernel cases.
The filterSmall gate is therefore extended from Intel to AMD and NVIDIA. Apple and other vendors stay on the general kernel.
Any potential kernel correctness, ROI, aliasing, boundary handling, or in-place processing issues are pre-existing implementation concerns and should be evaluated separately in follow-up PRs. Such issues are outside the scope of this change.
core, imgproc: support 0D Mat in LUT and threshold
Give a 0D input a 1x1 header before the row-based code runs, the same way #30704 normalizes 1D inputs in gemm:
threshold / thresholdWithMask: src (and mask) are viewed as 1x1, so the output is 1x1, the same as the other imgproc functions return for 0D input.
LUT: src and dst get 1x1 headers for the HAL and parallel paths. dst is created with createSameSize, so the result itself stays 0D.
* imgproc: support 5.x depths in remap/warp with INTER_NEAREST
nn_tab in cv::remap only had entries for CV_8U..CV_64F, so remap,
warpAffine and warpPerspective with INTER_NEAREST failed with
"nnfunc != 0" for CV_16F, CV_16BF, CV_Bool, CV_64U, CV_64S, CV_32U and
both fp8 depths. Nearest neighbor only copies pixels, so add the
remapNearest instances for these types, in both the absolute and the
WARP_RELATIVE_MAP rows. Using the real element types keeps the border
value conversion right (e.g. CV_Bool border 2 becomes 1).
Add an Imgproc_Warp_NearestNewDepths test.
* imgproc: use MatDepth in Imgproc_Warp_NearestNewDepths
A plain int parameter prints a failing case as GetParam() = 13. MatDepth prints the depth name instead.
* imgproc: convert remap nearest border once, dispatch copy by element size
Fix sanitizer bugs in OpenCV image warping
Strip BORDER_ISOLATED and validate borderType (0 <= borderType <= BORDER_TRANSPARENT)
Align cv::borderInterpolate (copy.cpp) and borderInterpolate_fast
Only compute tl_ofs in scalar bicubicCoeffs when all_inliers is true
Remove the Intel-only gate for medianFilter3_u/medianFilter5_u when cn==1 and dimensions meet the alignment requirements.
Port of https://github.com/opencv/opencv/pull/29803.
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.
* 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
- Strip BORDER_ISOLATED and validate borderType
(0 <= borderType <= BORDER_TRANSPARENT)
- Align cv::borderInterpolate (copy.cpp) and borderInterpolate_fast
- Only compute tl_ofs in scalar bicubicCoeffs when all_inliers is true
imgproc: Port 4.x Optimized remap interpolation to 5.x
Add remap SIMD kernels for single-channel bilinear, bicubic, and Lanczos4 paths.
Keep remap dispatch in a separate translation unit to avoid the existing warp_kernels dispatch declarations in imgwarp.cpp.
Preserve the 5.x fallback routing needed to match the 4.x remap performance cases.
Remove the Intel-only gate for medianFilter3_u/medianFilter5_u when cn==1 and dimensions meet the alignment requirements.
Port of https://github.com/opencv/opencv/pull/29803.
* Document transparent border for warpPerspective
* Keep warpPerspective clarification documentation-only
Remove the focused transparent-border test as requested in review. Preserve the documented border behavior and leave the existing image-warp test suite unchanged.
docs(imgproc): correct the documented Sobel kernel size range - #30661
## Problem
`cv::Sobel` and `cv::getDerivKernels` document the kernel size as
> `@param ksize size of the extended Sobel kernel; it must be 1, 3, 5, or 7.`
> `@param ksize Aperture size. It can be FILTER_SCHARR, 1, 3, 5, or 7.`
but `getDerivKernels` (modules/imgproc/src/deriv.cpp) actually enforces a different constraint:
```cpp
if( _ksize % 2 == 0 || _ksize > 31 )
CV_Error( cv::Error::StsOutOfRange, "The kernel size must be odd and not larger than 31" );
```
so every odd size up to 31 is accepted, and `FILTER_SCHARR` (-1) selects the 3x3 Scharr kernels.
Reproduced with opencv-python-headless 5.0.0:
```
getDerivKernels(1, 0, 9) -> OK, 9-tap kernel
getDerivKernels(1, 0, 15) -> OK, 15-tap kernel
getDerivKernels(1, 0, 31) -> OK, 31-tap kernel
getDerivKernels(1, 0, 33) -> error: "The kernel size must be odd and not larger than 31"
getDerivKernels(1, 0, -1) -> OK (Scharr 3x3)
cv2.Sobel(img, cv2.CV_32F, 1, 0, ksize=9/15) -> OK
cv2.Sobel(img, cv2.CV_32F, 1, 0, ksize=101) -> error (out of range)
```
## Change
Documentation-only: the two `@param ksize` entries now state the constraint the
implementation enforces (odd, not larger than 31) while keeping 1, 3, 5, 7 as the typical
choices. No code change.
Signed-off-by: NUAAwqy <162230229@nuaa.edu.cn>
Porting of perf tests extension from 4.x to 5.x - #30663
Following PRs are being ported:
* #29908
* #29922
* #29972
No more changes are introduced.
PR for opencv-extra: https://github.com/opencv/opencv_extra/pull/1458
### Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
docs(imgproc): medianBlur also accepts ksize = 1 - #30662
## Problem
`cv::medianBlur` documents the aperture as
> `@param ksize aperture linear size; it must be odd and greater than 1, for example: 3, 5, 7 ...`
but the implementation only requires an odd size: the aperture check is
`(ksize % 2 == 1) && (_src0.dims() <= 2)`, and `ksize = 1` is accepted and returns the image
unchanged (a 1x1 median is the pixel itself).
Reproduced with opencv-python-headless 5.0.0:
```
cv2.medianBlur(img, 1) -> OK, output is identical to the input
cv2.medianBlur(img, 2) -> error: "(ksize % 2 == 1) && (_src0.dims() <= 2)" in 'medianBlur'
```
## Change
Documentation-only: mention `ksize = 1` so the docs match the implemented behavior.
No code change.
Signed-off-by: NUAAwqy <162230229@nuaa.edu.cn>
imgproc: handle empty input image in findContours and findTRUContours - #30690
Since cv::copyMakeBorder now asserts that the source matrix is non-empty, calling cv::findContours or cv::findTRUContours on an empty image threw an exception instead of returning an empty contour list.
Guard against empty input images by clearing the output contours (and hierarchy) and returning early, matching cv::findContoursLinkRuns.
### Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
compilation fix: force 256-bit SIMD width under AVX2 in imgwarp - #30678
warp_kernels.simd.hpp and warp_common.vector.hpp only implement up to 256-bit SIMD and mix explicit v256_load* calls with width-generic v_* types. Force CV__SIMD_FORCE_WIDTH=256 when CV_AVX2 is enabled so builds with AVX-512 in CPU_BASELINE (-DCPU_BASELINE=AVX512_SKX) compile cleanly.
The following now compiles:
cmake -B build -DCPU_BASELINE=AVX512_SKX
cmake --build build --target opencv_imgproc --parallel
### Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
Vectorize the contiguous channel dimension of the float32 bilinear inlier loop for C>4. Coordinates, interpolation tables, border handling, and scalar tails keep their existing routes; C1–C4 kernels are unchanged. https://github.com/opencv/opencv/pull/30650
HoughLines, HoughLinesP, and HoughLinesPointSet document that lines are
returned when votes > threshold. Because both accumulator votes and
threshold are integers, threshold = 0 is the only value that selects
1-vote bins (votes >= 1), as already supported by HoughLinesPointSet,
CPU HoughLines, and the OpenCL paths.
PR #30286 added CV_CheckGT(threshold, 0) at the HoughLines/HoughLinesP
entry points to avoid a divide-by-zero (`nz * numangle / threshold`) in
the IPP path. Relax this check to CV_CheckGE(threshold, 0) and guard the
IPP fast paths with `threshold > 0` instead (IPP also requires
threshold > 0).
Disable undefined behavior sanitizer in imgwarp - #30307
### Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [ ] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
Add a calcHistRunParallel wrapper that splits the input image across
threads, each accumulating into a private histogram, then sums the
local copies at the end. This avoids atomic contention on shared bins.
The thread count follows the suggestion from PR #28635 review:
- if N < 256K elements, use a single thread
- otherwise nstripes = clamp(cvRound(N*0.001/M), 1, 8),
where M is the total number of histogram bins
- nstripes is further capped by getNumThreads(), so setNumThreads(0)
and setNumThreads(1) keep the single-histogram path
This keeps large histograms (e.g. 2D 256x256) single-threaded to avoid
excessive memory, while small histograms (e.g. 1D 256 bins) on large
images get up to 8x parallelism.
Performance (1920x1080, 8 threads, median of 20 runs):
- 8U 1D: 0.62ms -> 0.17ms (3.7x)
- 16U 1D: 3.81ms -> 0.95ms (4.0x)
- 32F 1D: 3.62ms -> 0.37ms (9.7x)
Adds a parameterized consistency test verifying that results match a
row-by-row single-threaded baseline bit-exactly across 8U/16U/32F,
1D-3D and various image sizes. Both stripe-splitting branches are
covered: a non-contiguous ROI takes the row-split path, a contiguous
clone takes the column-split path. A 1D mask bypasses IPP/HAL.
imgproc: accept CV_Bool input in morphological operations - #29799
### Pull Request Readiness Checklist
Fixes#29798
In 5.0 `cv::Mat_<bool>::depth()` returns `CV_Bool` instead of `CV_8U`, so the morphology filter factories in `morph.simd.hpp` no longer matched any branch and threw `Unsupported data type`. This worked in 4.x.
`CV_Bool` is a 1-byte 0/1 type, so it is dispatched through the same `uchar` min/max filters as `CV_8U`, and the default border value follows the `CV_8U` case (1 for erode, 0 for dilate). The OpenCL path bails out for `CV_Bool` and falls back to the CPU implementation, since `ocl::typeToStr` has no mapping for it.
Accepting `CV_Bool` also exposed a latent bug in `FilterEngine::init`: `borderElemSize` was computed from `CV_MAT_DEPTH(srcType) >= CV_32S`, which assumes the depth enum is ordered by element size. `CV_Bool` is 9, so a 1-byte type took the `sizeof(int)` branch and `borderElemSize` became 0, leaving `borderTab` empty and making every non-`BORDER_CONSTANT` erode/dilate read through a null pointer. It now keys off `CV_ELEM_SIZE1` instead. `CV_16F`/`CV_16BF` had the same latent miscomputation.
Added a test comparing `Mat_<bool>` against the equivalent `CV_8UC1` mask for erode, dilate, open and close over the three structuring element shapes and four border types, plus in-place and multi-iteration calls.
`MORPH_GRADIENT`/`TOPHAT`/`BLACKHAT` remain unsupported for `CV_Bool`: they subtract two masks, which has no meaning for a boolean type, and they throw rather than returning a wrong result.
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake