getAbsdiffFunc_() documents that a signed depth has two outputs: R == T, the saturating |a-b| kept
in the signed range, which is the depth cv::absdiff returns; and R == the unsigned type of the same
width. CV_8S, CV_16S and CV_32S each have both. CV_64S only had the unsigned one, so the signed
form fell through to a path that wraps:
absdiff(INT64_MIN, 0) -> -9223372036854775808
absdiff(INT64_MIN, INT64_MAX) -> -1
at every length, scalar included, in a CV_64S destination. The narrower depths all saturate at
their maximum: absdiff(INT_MIN, INT_MAX) on CV_32S is INT_MAX.
The missing row now uses EwAbsdiffS, whose scalar step gains a 64-bit overload. The generic one
widens into the work type, which the narrower depths have (short, int, int64_t) and 64-bit does not,
and |INT64_MIN - b| does not fit in int64_t anyway. The difference is therefore taken in uint64,
which is exact for every pair, and clamped to INT64_MAX. The narrower depths reach that overload
only through their int64 work type, where |a - b| is at most 2^32 and the clamp is a no-op.
Core_AbsdiffSigned checks both properties - never negative, and saturating at the depth's maximum -
across CV_8S, CV_16S, CV_32S and CV_64S.
vectorized and parallel DFT/DCT + matchTemplate - #30061
(Made with a help from Claude Code Fable 5.1)
## Summary
This PR replaces the scalar 1D DFT/DCT kernels in `modules/core/src/dxt.cpp` with a new engine
built on universal intrinsics and compiled per ISA through `ocv_add_dispatched_file`, and makes
the 2D transforms, the `DFT_ROWS`/`DCT_ROWS` batches and `matchTemplate` (`crossCorr`) multi-threaded.
The public API, the output formats (CCS packing of real spectra, `DFT_COMPLEX_OUTPUT` layout,
DCT ordering and normalization) and the IPP/OpenCL paths are unchanged. Only floating-point
rounding may differ (the new twiddle tables are more accurate than the old recurrence-generated
ones, so the error goes down, not up).
Measured on Apple M4 Max (16 cores, NEON), macOS 26.5.x, Xcode 26.6, Release build
(`-O3`, no IPP). All numbers are the minimum over repeated runs, `5.x` vs this PR.
## Changes
### New 1D engine (`dxt.hpp`, `dxt.simd.hpp`, `dxt.cpp`)
- **Split re/im layout.** The input pass gathers the data into separate `re[]`/`im[]` workspace
arrays; all middle stages operate on the split arrays, so a complex multiplication is four
independent vector FMAs with no lane shuffling. The output pass interleaves back (or packs the
CCS / DCT layout) with the scaling fused in.
- **Mixed-radix decimation-in-time with a fused first stage.** The plan chooses the first stage
(radix 1/2/4/8, twiddle-free, fused with the gather and the digit-reversal permutation) from
the power of two in the length, then radix-4 stages, then radix-3, radix-5 and a generic odd
radix. There are no radix-2 middle stages.
- **Vectorized butterflies with universal intrinsics** (`v_float32`/`v_float64`, VLA-safe): one
flat loop per stage with a single clamped counter for both the data and the twiddle offsets
(the back-off clamp handles spans that are not multiples of the vector width; such stages run
out of place, ping-ponging between two workspace pairs, the others run in place).
- **Vectorized first stage.** The radix-2/4/8 first stage reads the input columns contiguously
(`v_load_deinterleave`), runs the small DFT in registers and scatters the outputs of every
lane with one 4-channel interleaved store. Only odd lengths use a scalar table-driven gather.
- **Vectorized edge passes**: interleave/deinterleave, the real-FFT untangling in both directions
(with `v_reverse` for the mirrored half), DCT-II post-twiddle and DCT-III pre-twiddle, the
Makhoul permutations.
- **Inverse = conjugation trick**: the middle stages are direction-free and use forward twiddles
only; the inverse conjugates on the gather and negates the imaginary scale on the output.
- **Immutable plan + per-executor workspace, zero allocations per transform.** The plan
(radix sequence, permutation table, per-stage split twiddle tables) is built once in `init()`
and shared by all rows of a batch; every executor owns a workspace. Twiddles are generated in
double precision from a two-level exact table (a few ulps, much cheaper to build than the old
N-point recurrence, which matters because `cv::dft()` builds a plan on every call).
- **Per-ISA dispatch**: `ocv_add_dispatched_file(dxt SSE2 AVX2 AVX512_ICL RVV)`; the kernels are
reached through a function-pointer table resolved once per depth, following the pattern of
`arithm.simd.hpp` / `warp_kernels.simd.hpp`. The plan is built with the vector width of the
dispatched kernels (never with the baseline `VTraits`).
- The legacy scalar engine, `DFTInit`, the SSE3 `DFT_VecR*` butterflies and the `noPermute`/
`factors[0] >>= 1` machinery are removed (−1400 lines). The legacy `cv_hal_dft` hook (which
took the old tables) is no longer called from the 1D path; the in-tree RISC-V HAL
implementation of it will be updated in a separate PR.
### Parallelism
- `crossCorr` (`matchTemplate`): the correlation tiles are independent and are now distributed
with `parallel_for_`; every worker creates its DFT buffers and `DFT2D` contexts once per range.
(The tile loop had been sequential since the C++ rewrite in 2010, which dropped the OpenMP
pragma of `icvCrossCorr`.)
- 2D DFT/DCT and `DFT_ROWS`/`DCT_ROWS` batches: the row pass, the column pass and the DCT line
loop distribute their lines over threads; the 1D plan is shared, each worker allocates its
workspace once per range. The number of stripes is derived from the N log N cost, so a 256x256
transform stays on the calling thread and 1024x1024 gets ~20 stripes. Nested calls (the 2D DFTs
inside the parallel `crossCorr` tiles) are serialized by `parallel_for_` itself.
- The column pass copies columns in batches (`CopyFromColumns`/`CopyToColumns`, 4..16 adjacent
columns copied row by row into contiguous buffers sized to stay within 128 KB) instead of two
at a time: with power-of-two row steps the old strided copies aliased in the cache and took
60-70% of the 2D time.
### Tests
- `Core_DFT.sweep_1d`, `Core_DCT.sweep_1d`: every 5-smooth length up to 2048 plus odd, prime and
"awkward" lengths, both depths, C2C forward/inverse, R2C (CCS and complex output), C2R (CCS and
complex input), DCT/IDCT, in-place, column vectors, round trips; compared against the naive
O(N^2) references of `test_dxt.cpp` with a tolerance of `32*eps*(log2(N) + 2 + maxfactor/16)`.
- `Core_DFT.sweep_rows`: `DFT_ROWS` batches sharing one plan.
- `Core_DFT.nonzero_rows`: 2D transforms with `nonzero_rows` (previously uncovered).
- `Core_DFT.parallel_large`: large 2D / rows / DCT transforms, multi-threaded result vs the
single-threaded one.
- The naive DCT reference reduces the angle exactly before `cos()` (its accuracy was limiting
the comparison for n > 100 in double precision).
- Perf: `perf_dft.cpp` gains 1D row/column cases for `dft` and `dct`.
- Existing `opencv_test_core` (incl. `Core_DFT*`, `Core_DCT*`, `Core_MulSpectrums`,
`Core_DivSpectrums`) and `opencv_test_imgproc` (phaseCorrelate, matchTemplate, filter2D) pass;
the x86-64 build (SSE3 baseline + SSE4.1/AVX2/AVX512_ICL dispatch) passes the DFT/DCT tests
under Rosetta 2 (AVX2/AVX-512 paths compile-checked only on this machine).
## Performance
### 1D transforms (single row, one thread), microseconds
`C2C` = complex-to-complex, `R2C` = real-to-CCS, `C2R` = CCS-to-real (inverse, scaled),
`32`/`64` = float/double.
| n | C2C32 | C2C64 | R2C32 | C2R32 | R2C64 | DCT32 | IDCT32 | DCT64 |
|---|---|---|---|---|---|---|---|---|
| 256 | 0.82 → 0.55 | 0.86 → 0.74 | 0.67 → 0.53 | 0.63 → 0.52 | 0.64 → 0.56 | 0.88 → 0.56 | 0.88 → 0.56 | 0.90 → 0.66 |
| 1000 | 3.84 → 1.70 | 4.08 → 2.58 | 2.86 → 1.36 | 2.88 → 1.38 | 2.77 → 1.74 | 3.95 → 1.69 | 4.02 → 1.72 | 3.97 → 2.15 |
| 1024 | 3.25 → 1.65 | 3.44 → 2.40 | 2.46 → 1.23 | 2.35 → 1.27 | 2.42 → 1.59 | 3.57 → 1.57 | 3.51 → 1.59 | 3.45 → 2.02 |
| 4096 | 14.9 → 6.4 | 16.4 → 11.5 | 10.7 → 4.3 | 10.2 → 4.5 | 11.0 → 6.9 | 15.4 → 6.0 | 15.3 → 6.0 | 17.0 → 9.5 |
| 4725 (3³·5²·7) | 27.5 → 17.2 | 29.4 → 24.1 | 28.2 → 16.4 | 29.8 → 18.3 | 30.3 → 24.7 | – | – | – |
| 65536 | 350 → 273 | 411 → 494 | 223 → 136 | 274 → 139 | 251 → 249 | 318 → 167 | 299 → 159 | 372 → 320 |
Each `cv::dft()` call builds its plan anew; at n = 4096 the plan takes ~1.9 µs of the 6.4 µs
(the old code spent ~4 µs of 14.9 on its tables). For n = 65536 both versions are bound by the
memory traffic of the out-of-cache data, and C2C64 gets slightly slower (the split layout needs
one more pass over 1 MB of data; a plan cache or larger radices could recover this later).
### 2D transforms, single thread, microseconds
| size | C2C32 | R2C32 | C2R32 | R2C64 | DCT32 | IDCT32 | R2C32 rows |
|---|---|---|---|---|---|---|---|
| 256×256 | 297 → 140 | 162 → 75 | 165 → 77 | 178 → 126 | 259 → 166 | 290 → 174 | 72 → 29 |
| 512×512 | 1660 → 569 | 765 → 306 | 723 → 317 | 896 → 551 | 1381 → 946 | 1377 → 957 | 303 → 125 |
| 480×640 | 1555 → 715 | 895 → 377 | 918 → 392 | 978 → 677 | 1248 → 707 | 1262 → 695 | 389 → 147 |
| 1024×1024 | 7508 → 2625 | 3781 → 1387 | 3883 → 1398 | 4964 → 3201 | 6438 → 4556 | 6805 → 4583 | 1341 → 516 |
| 1080×1920 | 13141 → 5543 | 7143 → 2880 | 7269 → 2970 | 7770 → 5348 | 9735 → 5190 | 9477 → 5381 | 3116 → 1192 |
| 2048×2048 | 38151 → 16822 | 18381 → 6973 | 18784 → 7294 | 25146 → 17476 | 30291 → 21631 | 30540 → 22003 | 5809 → 2242 |
### 2D transforms, default thread count (16), microseconds
| size | C2C32 | R2C32 | C2R32 | R2C64 | DCT32 | IDCT32 | R2C32 rows |
|---|---|---|---|---|---|---|---|
| 256×256 | 309 → 138 | 157 → 75 | 161 → 77 | 174 → 126 | 265 → 167 | 293 → 170 | 71 → 29 |
| 512×512 | 1660 → 162 | 785 → 155 | 788 → 160 | 866 → 256 | 1410 → 235 | 1366 → 228 | 303 → 41 |
| 480×640 | 1531 → 178 | 895 → 142 | 916 → 141 | 957 → 222 | 1241 → 172 | 1248 → 173 | 393 → 46 |
| 1024×1024 | 7495 → 381 | 3755 → 274 | 3767 → 269 | 4875 → 478 | 6323 → 650 | 6583 → 630 | 1343 → 94 |
| 1080×1920 | 12905 → 681 | 7131 → 403 | 7240 → 382 | 7760 → 657 | 9673 → 605 | 9513 → 607 | 3124 → 143 |
| 2048×2048 | 38155 → 1481 | 18324 → 826 | 18559 → 857 | 24861 → 1776 | 30120 → 2592 | 30741 → 2734 | 5890 → 274 |
(5.x is single-threaded for all of these; 256×256 stays single-threaded in the new code by design.)
### matchTemplate (`opencv_perf_imgproc`, `*matchTemplate*`), milliseconds
Whole suite (156 cases): 921 → 284 ms. Selected cases (8-bit image, 16×16 template unless noted):
| image, template | SQDIFF | SQDIFF_NORMED | CCORR | CCORR_NORMED | CCOEFF | CCOEFF_NORMED |
|---|---|---|---|---|---|---|
| 128×128 | 0.15 → 0.12 | 0.14 → 0.12 | 0.09 → 0.05 | 0.13 → 0.09 | 0.11 → 0.08 | 0.13 → 0.10 |
| 320×240 | 0.99 → 0.52 | 1.09 → 0.58 | 0.79 → 0.23 | 0.96 → 0.42 | 0.93 → 0.38 | 1.02 → 0.53 |
| 640×480 | 3.19 → 1.17 | 3.35 → 1.22 | 2.27 → 0.29 | 3.12 → 1.04 | 2.94 → 0.90 | 3.43 → 1.38 |
| 800×600 | 5.86 → 1.64 | 5.61 → 1.77 | 4.11 → 0.34 | 5.48 → 1.53 | 5.19 → 1.28 | 5.82 → 2.04 |
| 1024×768 | 9.73 → 2.72 | 9.59 → 2.90 | 6.88 → 0.48 | 9.11 → 2.45 | 8.44 → 2.05 | 9.84 → 3.44 |
| 1280×1024 | 14.92 → 4.33 | 15.01 → 4.58 | 10.70 → 0.64 | 14.31 → 3.97 | 13.40 → 3.33 | 15.37 → 5.58 |
| 1280×1024, 1260×1000 | 13.06 → 7.90 | 13.08 → 7.74 | 10.75 → 3.58 | 12.23 → 5.03 | 11.11 → 3.94 | 12.03 → 5.05 |
`TM_CCORR` is now dominated by the (still single-threaded) `integral()` and normalization passes
in the other methods; those are candidates for a follow-up.
## Notes for reviewers
- `modules/core/src/dxt.hpp` documents the engine layout; the plan builder (`DftPlan::build`)
and the driver (`runDft`) are in `dxt.cpp`, the kernels in `dxt.simd.hpp`.
- Stages whose span is smaller than the vector width run scalar (on NEON this only affects
lengths of the form 2·odd; on AVX2/AVX-512 also the first radix-4 stage of 4^k lengths);
handling them with narrower fixed-width vectors is a possible follow-up.
- A radix-8 middle stage was implemented and measured slower on NEON than two radix-4 stages
(register pressure), so it is not included.
### 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
video(TrackerNano): fix target scale and penalty calculation - #30353
This PR fixes an algorithmic bug and two precision issues in `cv::TrackerNanoImpl::update`:
Resolves#27190
### 1. Fix scale penalty calculation (`targetPos` -> `targetSz`)
In the Siamese tracking scale penalty calculation:
```cpp
// BEFORE (Bug):
Mat sc = sizeCal(predX2 - predX1, predY2 - predY1)/sizeCal(targetPos[0], targetPos[1]);
// AFTER (Fix):
Mat sc = sizeCal(predX2 - predX1, predY2 - predY1)/sizeCal(targetSz[0], targetSz[1]);
```
`sizeCal` takes `(width, height)`, but `targetPos` holds the target's center pixel coordinates `(x, y)` in the full frame. Passing `targetPos` normalizes candidate scale by screen position rather than object size, breaking translation invariance and heavily dampening candidate scores as the target translates across the frame.
Furthermore, `targetSz` is explicitly multiplied by `scale_z` before this calculation (line 252) and divided back afterwards (line 323). The only reason `targetSz` was brought into the search patch coordinate frame was for this exact line. Passing `targetPos` was apparently a typo.
### 2. Fix integer division in search crop scaling
```cpp
// BEFORE: instanceSize (255) and exemplarSize (127) are const int
float sx = sz * (instanceSize / exemplarSize); // Evaluates to sz * 2 instead of 2.007874f
// AFTER:
float sx = sz * (float(instanceSize) / exemplarSize);
```
### 3. Preserve subpixel floating-point precision in `targetSzSum`
Removed the `(int)` truncation from `targetSzSum` in `update()`:
```cpp
// BEFORE:
int targetSzSum = (int)(targetSz[0] + targetSz[1]);
// AFTER:
float targetSzSum = targetSz[0] + targetSz[1];
```
---
### Verification and Test Results
Tested on a sequence where an object expands from 60x60 to 138x138 across 40 frames while translating across the frame:
* **Before Fix (`targetPos`):**
* Final Tracked Size: `133x131`
* Final Area Ratio (Tracked / Ground Truth): **0.915** (persistent size lag)
core: saturate OpenCL conversions to CV_32U, CV_64U and CV_64S - #30386Fixes#30385
### Problem
`ocl::convertTypeStr()` uses `ddepth >= CV_32F` / `sdepth >= CV_32F` to mean "is a float type". The 5.x integer depths are numbered above `CV_32F`, so conversions to `CV_32U`, `CV_64U` and `CV_64S` are emitted without `_sat` (and without `_rte` from float sources). `UMat::convertTo` and other OpenCL kernels using `convertTypeStr()` then wrap around instead of saturating, e.g. `-5` → `CV_32U` gives `4294967291` instead of `0`.
### Fix
In `convertTypeStr()`:
- use `CV_IS_FLOAT_TYPE(ddepth)` / `CV_IS_FLOAT_TYPE(sdepth)` instead of comparing with `CV_32F`;
- for float → integer, always use `_sat_rte`, matching the CPU's saturating `cvRound` since #30007 (was: `_sat` only below `CV_32S`).
This also fixes `CV_64S`/`CV_64U`/`CV_32U` → `CV_32S` in `UMat::convertTo`, which converts through that float → `CV_32S` path. Conversions between the 4.x depths are otherwise unchanged.
### Tests
New `OCL_MatrixOperation/ConvertToSaturate.Accuracy` compares `UMat::convertTo` with `Mat::convertTo` in the destination type for `CV_8S`/`CV_16S`/`CV_32S`/`CV_64S`/`CV_32F` → `CV_32S`/`CV_32U`/`CV_64U`/`CV_64S`, with values that need clamping or rounding. On current 5.x it fails for 10 of the pairs; with this patch it passes. All `OCL_*` and `convertTo` tests in `opencv_test_core` pass locally (7522 tests).
`CV_64U` sources are left out for now: the CPU path for `CV_64U` → `CV_64S` is a bit copy until #30206 lands.
### 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.
- [ ] The feature is well documented and sample code can be built with the project CMake
Port of #30403 to 5.x. PoseSolver::rot2vec reads the rotation axis
from R - R^T and divides by sin(theta). Both vanish when the rotation
is close to 180 degrees, which is the pose of a square tag seen
exactly head-on, so SOLVEPNP_IPPE_SQUARE returned a pose flipped by
180 degrees. Fall back to Rodrigues in that case and clamp the acos
argument. The fast path for all other rotations is unchanged.
objdetect: default CCheckerDetector chart type to MCC24 - #30412Fixes#30410.
`CCheckerDetectorImpl::m_chartType` had no initializer and the constructor is empty, so a detector from `CCheckerDetector::create()` read an indeterminate chart type unless `setColorChartType()` was called first. It now defaults to `MCC24`, the first `ColorChart` value. With `operator new` filling memory with 0xFF, 5.x reports chart type -1 and `process()` fails on `mcc/MCC24.png`; with the fix it reports `MCC24` and finds the chart. Added `CV_mccRunCCheckerDetectorBasic.default_chart_type`, which checks the default and runs detection without setting the type.
### 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.
- [ ] The feature is well documented and sample code can be built with the project CMake
video: clip TrackerVit box to the image - #30394
Port of #30358 to 5.x, as requested there. Fixes#30350.
`TrackerVitImpl::update()` never bounds the predicted box to the frame, and that box sets the size of the next search crop, so it can keep growing past the image. The box is now clipped to the image after each update, the same way OSTrack does it (`clip_box(..., H, W, margin=10)`). The 5.x code is the same as 4.x, so the change is identical. Added `vittrack.bbox_inside_image`, which fails on 5.x without the fix (box `[498 x 358 from (-9, -6)]` on a 480x352 frame); `accuracy_vittrack` still passes.
### 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.
- [ ] The feature is well documented and sample code can be built with the project CMake
core: fall back to CPU for CV_16BF in OpenCL arithmetic - #30383Fixes#30382
### Problem
`arithm_op()` calls the OpenCL path directly and relies on it returning `false` when it can't run. `ocl_arithm_op()` and `ocl_binary_op()` don't check the depth and pass it to `ocl::typeToStr()`, which asserts for `CV_16BF`. So `add`, `subtract`, `multiply`, `divide`, `absdiff`, `min`, `max`, `addWeighted` and add-with-scalar on a `CV_16BF` `UMat` throw, while the same calls on a `Mat` work.
### Fix
In `modules/core/src/arithm.cpp`, add `noOclType()` for the depths that have no OpenCL type (`CV_16BF`, `CV_Bool`, `CV_8F_E4M3FN`, `CV_8F_E4M3FNUZ`) and return `false` for them:
- `ocl_arithm_op()`: if any of the source, destination or working depths has no OpenCL type.
- `ocl_binary_op()`: if the source depth has no OpenCL type and the op is not bitwise (bitwise ops use `memopTypeToStr()`, not `typeToStr()`).
The CPU path then handles them, as it already does for `Mat`. Same approach as #30252 for `threshold`.
### Tests
New `OCL_Arithm.bf16_falls_back_to_cpu` runs the 9 ops on `CV_16BF` `Mat` and `UMat` inputs and checks the results are identical. It fails on current 5.x with the `typeToStr` assertion and passes with this patch. All `OCL_Arithm*`, `Core_Arithm*`, the CPU add/sub/mul/div/min/max/absdiff/addWeighted tests and the `UMat` tests pass locally (7933 tests).
### 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.
- [ ] The feature is well documented and sample code can be built with the project CMake
dnn(cuda): move the CUDA backend to UMat device buffers - #29807
This PR is in continuation with #29658 and #30259
### 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
dnn: speed up LSTM2 (gate math, input projection, recurrence) - #30309
Speeds up the ONNX LSTM layer (`LSTM2`) in `recurrent2_layers.cpp`:
- Default sigmoid/tanh gates are computed on raw rows with the dispatched activation kernels. Peephole, clip and custom activations keep the old path.
- The input projection runs before the per-direction loop, so the GEMM uses all threads. It goes through MLAS when available.
- Batch 1 (hidden size >= 128): hidden units are split across threads, with one `parallel_for_` per timestep.
- Batch > 1: one packed GEMM per step replaces one GEMV per row.
Ryzen 9 9950X, default threads, 5.x vs this PR:
| Model | 5.x, ms | PR, ms | x-factor |
|---|:-:|:-:|:-:|
| `DNNTestNetwork.CRNN` | 2.693 | 1.562 | 1.72 |
| CRNN EN 2021sep | 2.498 | 1.472 | 1.70 |
| crnn.onnx | 2.650 | 1.434 | 1.85 |
| LSTM2 bidirectional, batch 1, in 512, hidden 256, 24 steps (CRNN layer 1) | 0.710 | 0.212 | 3.36
### 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
Fix code according to fuzzer findings - #30286
### 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
Mat(const std::vector<_Tp>&, bool) in 5.x creates a 1D matrix
(dims == 1, rows == 1, cols == vec.size()), not a single column.
Update the doc to match the implementation and the Mat_ overload wording.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CV_64U is the only unsigned depth whose range does not fit in the signed type of the same width, so
reading its bit pattern as signed goes unnoticed until the top bit is set. Three places did.
1. CV_64U -> CV_64S dispatched to cvt64s, a raw byte copy, so nothing saturated: 2^63 became
INT64_MIN and UINT64_MAX became -1, at every length. CV_32U -> CV_32S saturates, so this is now
a saturating conversion like the rest.
2. vx_load_pair_as(const uint64_t*, v_float64&, v_float64&) and
vx_load_as(const uint64_t*, v_float32&) both converted with v_cvt_f64(), which takes signed
lanes, so CV_64U to CV_64F, CV_32F, CV_16F and CV_16BF returned negative values above INT64_MAX.
Vector body only, so the same value was right at length 1 and wrong at length 16.
The unsigned conversion splits into two halves below 2^32: each converts exactly, scaling the high
half by 2^32 is exact, and the single add is the only rounding, so the result matches (double)x.
Converting the bit pattern as signed and adding 2^64 back would round twice - for 2^63 + 1025 that
gives 2^63 where (double)x is 2^63 + 2048.
Core_ConvertFrom64U covers the integer and floating-point destinations, with 2^63 + 1025 among the
values since it is the case that distinguishes one rounding from two.
x2 is computed from the disparity value and was used as an index into
disp2cost and disp2buf without a range check, in both the CV_16S and the
CV_32S cost branches. A disparity outside the range given by minDisparity and
numberOfDisparities made x2 negative or not less than cols.
Skip such pixels in the reverse mapping pass. The final consistency pass
already bounds-checks the mapped columns.
Ports #30316 to 5.x. Relates to #25367.