1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-21 19:33:03 +04:00
Commit Graph

36284 Commits

Author SHA1 Message Date
Alexander Smorkalov bc18ec2ed3 Merge pull request #29032 from asmorkalov:as/musl_c_tuning_4.x
Musl-C CI and related fixes for 4.x.
2026-06-18 11:06:19 +03:00
Jonas Perolini 853ee9b961 Merge pull request #29329 from JonasPerolini:get-border-errors-once
Optimization ArUco: get border errors in one pass for both normal and inverted markers #29329

This PR is a follow up to optimization in https://github.com/opencv/opencv/pull/29267.

## Context:

When detecting inverted markers, we call `_getBorderErrors` twice:
- `int borderErrors = _getBorderErrors(cellPixelRatio, ...);`
- `Mat invCellPixelRatio = 1.f - cellPixelRatio;`
- `int invBError = _getBorderErrors(invCellPixelRatio, ...);`

meaning:

1. Scan border cells once.
2. Allocate a new Mat.
3. Invert the entire `cellPixelRatio` matrix, not just the border.
4. Scan border cells again on the inverted matrix.
5. If inverted marker is better, copy the full inverted matrix back

## Solution

only call `_getBorderErrors` once and compute both 
- borderErrors: `cellPixelRatio > validBitIdThreshold`
- invBorderErrors: `1 - cellPixelRatio > validBitIdThreshold`

This saves: 
1. full invert into temporary Mat: (`Mat invCellPixelRatio = 1.f - cellPixelRatio;`)
2. scan temporary border: the second `_getBorderErrors`
3. copy temporary Mat back into `cellPixelRatio`: `invCellPixelRatio.copyTo(cellPixelRatio);`

Note that the solution does not implement: 

```
 if(params.detectInvertedMarker) {
    _getBorderErrorsBoth(...);
} else {
    borderErrors = _getBorderErrors(...);
}
```

because the extra cost to computing invBorderErrors: `if(1.f - ratio > validBitIdThreshold) invBorderErrors++;` is negligible. this also avoids having to maintain two functions: `_getBorderErrorsBoth` and `_getBorderErrors`

## Tests:

No visible results when testing the full marker detection pipeline because this step is not expensive. There is a roughly 8% noise when re-running the marker detections making it hard to spot small speed diffs. 

When testing only this specific step with a float matrix of size: `(markerSize + 2*border)^2` on a AMD Ryzen 7 (8 cores / 16 threads):
 
-  The new code is 30-45× faster at this stage depending on the marker size and whether the candidate was actually an inverted one, saving ~530-580 ns per candidate, mainly because we removed `Mat invCellPixelRatio = 1.f - cellPixelRatio`
- The new code is equivalent when detecting non-inverted markers +/- 3 ns

---

### 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
2026-06-18 09:40:20 +03:00
Alexander Smorkalov c796023029 Musl-C CI and related fixes for 4.x. 2026-06-18 09:14:36 +03:00
Pratham Kumar 837f715eec Merge pull request #29283 from pratham-mcw:exp-neon-opt
Restricting the condition with _M_IX86/_M_X64 so it only applies to x86/x64 MSVC builds. MSVC ARM64 now falls through to the existing `#else` branch, which already has a portable CV_SIMD-based exp32f/exp64f implementation

**Performance Benchmarks:**
<img width="976" height="486" alt="image" src="https://github.com/user-attachments/assets/62daf2c3-34ac-4fc7-92d0-268073f746f3" />

- [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
2026-06-17 11:50:58 +03:00
Pierre Chatelier 6eb0dc97f5 Merge pull request #28907 from chacha21:more_autobuffer
More use of AutoBuffer #28907

When possible, AutoBuffer should be faster than std::vector<>, and should not be worse if it requires a heap allocation rather than a stack allocation.

### 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
- [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
2026-06-16 18:51:37 +03:00
Alexander Smorkalov 3def56d25a Merge pull request #29096 from Rishiii57:fix/matchtemplate-sqdiff-precision-21786
imgproc: fix matchTemplate TM_SQDIFF precision loss for CV_8U images
2026-06-16 13:52:16 +03:00
Alexander Smorkalov 62bcd12f9c Merge pull request #29305 from uwezkhan:darknet-cfg-index-bound
bound darknet cfg layer and anchor indices before vector reads
2026-06-16 12:24:35 +03:00
熊阔豪 6a2d8d24da Merge pull request #29145 from MrBear999-ui:fix-remap-nearest-rounding
Fix remap nearest rounding #29145

Thanks, I checked the Windows x64 test logs. The imgproc failures are from the OpenCL `Remap_INTER_NEAREST` tests with `(16SC2, 16UC1)` maps. I missed the OpenCL fixed-point path in the first patch.

I updated `modules/imgproc/src/opencl/remap.cl` to use the same nearest rounding condition as the CPU path. This should address the OCL remap failures.

The `opencv_test_video` failure seems unrelated to this PR (`ECCfixtures/Video_ECC.accuracy/6` invalid memory access), since this PR only touches remap rounding in imgproc.

### 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
2026-06-16 12:22:44 +03:00
Alexander Smorkalov d878b04c6b Merge pull request #29307 from StephanTLavavej:angle-brackets
Consistently include `<math.h>` with angle brackets
2026-06-16 10:07:48 +03:00
Stephan T. Lavavej 4208a89b3e Consistently include <math.h> with angle brackets 2026-06-15 14:53:29 -07:00
Alexander Smorkalov 41d48d6eeb Merge pull request #29306 from asmorkalov:as/more_ipp_core
Moved IPP math functions from core module to HAL.
2026-06-16 00:15:30 +03:00
Alexander Smorkalov 0a4d6a849a Moved IPP math functions from core module to HAL. 2026-06-15 21:53:10 +03:00
Alexander Smorkalov 017a5138d1 Merge pull request #29303 from asmorkalov:as/win32_objdetect_warn_fix
Fixed new objdetect warnings on Windows.
2026-06-15 21:29:42 +03:00
uwezkhan afbfa275d8 bound darknet cfg layer and anchor indices before vector reads 2026-06-15 21:36:38 +05:30
arshsmith 6df9732c81 Merge pull request #29296 from arshsmith:fix-pam-grayalpha-overflow
imgcodecs(pam): fix out-of-bounds write when reading 2-channel PAM #29296

While poking at the PAM decoder I noticed basic_conversion() writes past the
end of the destination buffer when a GRAYSCALE_ALPHA (2 channel) image is read
with IMREAD_GRAYSCALE.

The 1-channel branch was written as if the destination had 3 channels:

    for( ; s < end; d += 3, s += src_sampe_size )
        d[0] = d[1] = d[2] = s[layout->graychan];

So for every source pixel it writes 3 bytes and advances d by 3, even though
the output row only has m_width bytes (1 channel). With m_channels == 2 that
ends up writing ~1.5 * m_width bytes per row, which runs off the row and, on
the last row, off the end of the Mat. m_width is taken straight from the file
header (up to 2^20), so the overflow size and contents are attacker controlled.
It's reachable with a plain imread(file, IMREAD_GRAYSCALE) on a crafted file.

While looking at it I also realised the loop bound was off for any multi
channel source: end was set to src + src_width, but the source has
src_width * channels samples, so it only ever processed m_width/channels pixels
instead of all of them. That's why GRAYSCALE_ALPHA / RGB_ALPHA come out only
partially filled.

Fix both at once:
- end now covers the whole row (src_width * src_sampe_size)
- the 1-channel branch writes one byte and advances d by 1

For the common grayscale->color case (channels == 1) the new end is identical
to the old one (m_width * 1 == m_width), so that path is byte for byte the same
and the existing PAM read_write test is unaffected. The only outputs that
change are the GRAYSCALE_ALPHA/RGB_ALPHA conversions, which were already broken.

Added a regression test (Imgcodecs_Pam.decode_graya_as_gray) that builds a small
2-channel PAM with an odd width, decodes it as grayscale and checks the result
matches the gray channel. It overflows/crashes on the old code and passes now.
2026-06-15 17:49:12 +03:00
Alexander Smorkalov 40d6727ea2 Merge pull request #29295 from uwezkhan:exif-rawprofile-len
reject under-length raw exif profile in processRawProfile
2026-06-15 13:51:48 +03:00
Alexander Smorkalov cfb3a8bbb7 Fixed new objdetect warnings on Windows. 2026-06-15 12:32:11 +03:00
Ayush Das 194db6f5cb Merge pull request #29299 from AyushDas4890:fix/doc-typos-tutorials
Fix typos in tutorial documentation #29299

This PR fixes spelling typos across several tutorial documentation files (dnn, calib3d, objdetect, app, js_tutorials), including `export=dowload` -> `export=download` in the DNN text-spotting tutorial's Google Drive URLs.

Documentation-only change; no functional code is affected.

Pull Request Readiness Checklist:

[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 code under GPL or another license that is incompatible with OpenCV.

[x] The PR is proposed to the proper branch (4.x).

[x] Documentation-only change; no accuracy/performance tests or opencv_extra patch are applicable.
2026-06-15 12:28:35 +03:00
Alexander Smorkalov 5aad95cb1d Merge pull request #29297 from Kumataro:fix_doxygen_warning_at_mat
doc(core): fix doxygen warning for missing endcode
2026-06-15 10:21:33 +03:00
Alexander Smorkalov 86433eac87 Merge pull request #29301 from uwezkhan:persistence-nd-dim-bound
reject nd-matrix dim count above CV_MAX_DIM in FileStorage read
2026-06-15 10:16:16 +03:00
uwezkhan 2906d4d73a reject nd-matrix dim count above CV_MAX_DIM in FileStorage read 2026-06-14 02:51:20 +05:30
Kumataro 96a71a8d83 doc(core): fix doxygen warning for missing endcode 2026-06-13 20:45:30 +09:00
uwezkhan 19ec7ea28b reject under-length raw exif profile in processRawProfile 2026-06-13 15:04:24 +05:30
Rishiii57 12eaf9bd4c imgproc: fix matchTemplate TM_SQDIFF precision loss for CV_8U images
When computing TM_SQDIFF for 8-bit images, crossCorr stores intermediate
results in CV_32F which loses precision for large patch sums (e.g. 25x25
patch of value 255 gives sum=40603125, exceeding float32's ~7 significant
digits). This causes the final SQDIFF result to have incorrect non-zero
values even when image and template are identical.

Fix: use a CV_64F intermediate buffer for crossCorr and common_matchTemplate
when depth==CV_8U and method is TM_SQDIFF or TM_SQDIFF_NORMED, then convert
back to CV_32F for the final output.

Fixes #21786
2026-06-12 00:06:26 +05:30
ZNNAXLRQ cadcb7e4e0 Merge pull request #29059 from ZNNAXLRQ:Project4_ZHUWanqi
Improving Parallelism and Memory Access Efficiency for Hough Transform Accumulator #29059

### 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
- [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

# [Optimization] Improving Parallelism and Memory Access Efficiency for Hough Transform Accumulator

## Overview / Motivation

The original implementation mainly rely on single‑threaded nested loops, which do not fully utilize the computational power of modern multi‑core CPUs. There is also room for improvement in memory access patterns and branch prediction. **Without altering any core mathematical logic or ensuring result consistency**, this work improves the execution efficiency of these two processes by introducing `cv::parallel_for_` and refactoring data access patterns.

---

## Detailed Changes

* **Original Code Analysis**: The original code uses a double loop – the outer loop scans the image for edge points (`if image[...] != 0`), and the inner loop iterates over all angles, computes the radius, and writes to the global `accum` array. This pattern is hard to parallelize directly because different threads writing to the same accumulator would cause **data races**. Moreover, the sparse conditional branch inside the loop is extremely unfriendly to CPU branch prediction.
* **Optimization Strategy (Sparse Extraction → Parallel over Angles)**:
  1. **Sparse Feature Extraction**: First, pre‑scan the image in a single thread, extract the coordinates (`x`, `y`) and values of all non‑zero pixels (edge points) into a contiguous `std::vector` (pre‑allocate 10% of the image size to reduce dynamic reallocation overhead).
  2. **Switch the Parallel Dimension**: Instead of splitting the image region, change the parallelization dimension to **angle (`numangle`)**. Use `cv::parallel_for_` to assign different angle ranges to different threads.
  3. **Conflict‑free Memory Writes**: Because the `accum` array is strictly partitioned by angle `n` in memory, threads assigned to different angles write to completely non‑overlapping memory blocks in the global `accum`.

---

## Correctness Testing
Correctness tests are based on the provided `opencv_test`. For the Hough tests, no failing test cases were observed.

---

## Performance Benchmarks
> The following tests were run on Intel(R) Core(TM) i9‑14900HX @ 2.2GHz / Windows 11 (WSL: Ubuntu) with GCC 13.3.0.
Using `opencv_perf` tests that include Hough, the results are as follows:

| Test Case | 1st mean | 5th mean | Diff |
|-----------|----------|----------|------|
| `PerfHoughCircles.Basic` | 4.75 | 4.91 | +3.4% |
| `PerfHoughCircles2.ManySmallCircles` | 56.76 | 59.37 | +4.6% |
| `PerfHoughCircles4f.Basic` | 4.14 | 4.03 | -2.7% |

#### II. `OCL_HoughLines` (Standard Hough Line, OCL wrapper, actually executed on CPU)

| Parameters (size, rho, theta) | 1st mean | 5th mean | Diff |
|-------------------------------|----------|----------|------|
| (640×480, 0.1, 0.01745) | 2.57 | 3.21 | +24.9% |
| (640×480, 1, 0.1)       | 0.18 | 0.21 | +16.7% |
| (1920×1080, 0.1, 0.01745)|12.27 |15.55 | +26.7% |
| (1920×1080, 1, 0.1)     | 0.69 | 0.99 | +43.5% |
| (3840×2160, 0.1, 0.01745)|28.43 |34.78 | +22.3% |
| (3840×2160, 1, 0.1)     | 2.15 | 3.51 | +63.3% |

#### III. `OCL_HoughLinesP` (Probabilistic Hough Line)

| Image and parameters (rho, theta) | 1st mean | 5th mean | Diff |
|------------------------------------|----------|----------|------|
| pic5.png, 0.1, 0.01745 | 1.20 | 1.22 | +1.7% |
| pic5.png, 1, 0.01745   | 1.03 | 0.92 | -10.7% |
| a1.png, 0.1, 0.01745   | 8.71 | 9.32 | +7.0% |
| a1.png, 1, 0.01745     | 6.29 | 6.68 | +6.2% |

#### IV. Standard Hough Line (CPU implementation, non‑OCL) `Image_RhoStep_ThetaStep_Threshold_HoughLines`

| Parameters (image, rho, theta, thresh) | 1st mean | 5th mean | Diff |
|-----------------------------------------|----------|----------|------|
| pic5.png, 1, 0.01, 0.5        | 2.19 | 0.82 | **-62.6%** |
| pic5.png, 10, 0.01, 0.5       | 2.08 | 0.53 | **-74.5%** |
| pic5.png, 1, 0.1, 0.5         | 0.24 | 0.15 | -37.5% |
| a1.png, 1, 0.01, 0.5          | 16.06 | 2.65 | **-83.5%** |
| a1.png, 10, 0.01, 0.5         | 17.05 | 1.92 | **-88.7%** |
| a1.png, 1, 0.1, 0.5           | 1.82 | 1.00 | -45.1% |

#### V. Floating‑Point Hough Line (`...HoughLines3f`)

| Parameters (image, rho, theta, thresh) | 1st mean | 5th mean | Diff |
|-----------------------------------------|----------|----------|------|
| pic5.png, 1, 0.01, 0.5        | 1.97 | 0.86 | **-56.3%** |
| pic5.png, 10, 0.01, 0.5       | 1.66 | 0.54 | **-67.5%** |
| a1.png, 1, 0.01, 0.5          | 18.52 | 2.59 | **-86.0%** |
| a1.png, 10, 0.01, 0.5         | 16.30 | 2.05 | **-87.4%** |
| a1.png, 1, 0.1, 0.5           | 1.72 | 0.97 | -43.6% |

### Conclusion

- **Circle detection, probabilistic Hough line**: Differences between the two runs are very small (within ±11%), indicating stable performance.
- **`OCL_HoughLines`**: The parallel test is generally 10%~63% slower than the serial one, but still within a reasonable fluctuation range. Special handling was attempted but did not bring significant improvement and slightly reduced the gains in the subsequent two groups, so it was left unchanged after comprehensive consideration.
- **CPU standard Hough line (non‑OCL)**: The parallel test is significantly faster than the serial one (up to 88% faster), demonstrating that parallel optimization can bring obvious improvements.

By the way at last, I am extremely grateful to the maintainers for helping me test the last PR. However, since I was unable to obtain the development board, I was unable to continue modifying the previous RVV optimization.
2026-06-11 20:59:32 +03:00
Vincent Rabaud fa55ed2837 Merge pull request #29267 from vrabaud:aruco
Fix speed regression in Aruco identify #29267

### 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
2026-06-11 20:54:39 +03:00
Pranav Kaushik 4dee742731 Merge pull request #29281 from pranavkaushik1:improve-qrcode-colors
samples: add color-coded bounding boxes for multi QR code detection #29281

Improves the existing qrcode.py sample by adding color-coded bounding boxes when multiple QR codes are detected simultaneously.

Previously all QR codes were drawn with the same green color, making it hard to distinguish between them visually.

This change adds a QR_COLORS palette so each detected QR code gets a unique color, improving visual clarity for multi-QR detection.

Co-authored-by: Ayush Gupta <gupta.ayushg@gmail.com>
2026-06-11 18:43:27 +03:00
Alexander Smorkalov a77474f3b1 Merge pull request #29255 from ArneshBanerjee:samples-blob-contours-29254
samples: demonstrate SimpleBlobDetector contour collection
2026-06-11 18:30:34 +03:00
Yixuan Tang 590d8f6439 Merge pull request #29240 from plain-noodle-expert:fix/brisk-ubsan-negative-shift
fix(features2d): fix UB left-shift of negative value in BRISK subpixel2D #29240

## Summary

Fixes #29239

`BriskScaleSpace::subpixel2D` computes quadratic surface coefficients for sub-pixel keypoint refinement using AGAST scores from a 3×3 neighbourhood. Two of those coefficients were computed via left-shift:

```cpp
// Before (UB when operand is negative)
int coeff5 = (s_0_0 - s_0_2 - s_2_0 + s_2_2) << 2;
int coeff6 = -(s_0_0 + s_0_2 - ((s_1_0 + s_0_1 + s_1_2 + s_2_1) << 1) - 5 * s_1_1 + s_2_0 + s_2_2) << 1;
```

AGAST scores are non-negative (0–255), but their **differences can be negative**. Left-shifting a negative signed integer is **undefined behaviour** under C++11 [expr.shift]/2. UBSan reports:

```
brisk.cpp:2037:48: runtime error: left shift of negative value -3
```

## Fix

Replace the outer left-shifts with multiplications (`* 4` / `* 2`), which are always well-defined. Modern compilers (GCC, Clang, MSVC) emit identical code for the positive case.

```cpp
// After (safe for all inputs, identical semantics)
int coeff5 = (s_0_0 - s_0_2 - s_2_0 + s_2_2) * 4;
int coeff6 = -(s_0_0 + s_0_2 - ((s_1_0 + s_0_1 + s_1_2 + s_2_1) << 1) - 5 * s_1_1 + s_2_0 + s_2_2) * 2;
```

The inner `<< 1` inside the parenthesis of `coeff6` is applied to a sum of non-negative scores and remains safe.

## Tests

Four regression tests added to `test_brisk.cpp` (all verified to pass under `-fsanitize=undefined`):

| Test | Image | Purpose |
|---|---|---|
| `regression_ubsan_negative_shift_isolated_pixel` | 40×40, single pixel = 3 | Matches exact crash parameters (s_0_2=3, others=0) |
| `regression_ubsan_negative_shift_rect_corner` | 60×60 white rect on black | Strong AGAST responses at corners |
| `regression_ubsan_negative_shift_random_image` | 30×30 LCG noise (0–15) | Broad coverage of score combinations |
| `regression_ubsan_negative_shift_multi_octave` | 80×80 checkerboard, 3 octaves | Exercises cross-layer subpixel refinement |

## Crash Details

- **File**: `modules/features2d/src/brisk.cpp:2037`
- **Parameters at crash**: `s_0_0=0, s_0_1=0, s_0_2=3, s_1_0=0, s_1_1=0, s_1_2=0, s_2_0=0, s_2_1=0, s_2_2=0`
- **Expression**: `(0 - 3 - 0 + 0) << 2` = `-3 << 2` → **UB**
- **Detected by**: libFuzzer + UBSan (`-fsanitize=address,undefined`)
- **Affected path**: `detectAndCompute` → `computeKeypointsNoOrientation` → `getKeypoints` → `refine3D` → `getScoreMaxAbove` → `subpixel2D`
2026-06-11 13:08:05 +03:00
Srujan rai aed41fdabe Merge pull request #28981 from Srujan-rai:fix/opengl-extensions-memory-leak
core(opengl): fix memory leak in OpenCL extensions gathering #28981

## Problem

Fixes #28980

In `modules/core/src/opengl.cpp`, inside `initializeContextFromGL()`, a `char[]` buffer is allocated to query OpenCL device extensions:

```cpp
extensions = new char[extensionSize];
status = clGetDeviceInfo(..., extensions, &extensionSize);

if (status != CL_SUCCESS)
    continue;  // leaks `extensions`
```

When `clGetDeviceInfo()` fails, `continue` skips the corresponding `delete[]` on the success path, causing the allocated buffer to leak.

Additionally, the `catch (...)` block also bypasses cleanup, so any thrown exception leaks the buffer as well.

## Fix

Replace the raw `char*` allocation with `std::unique_ptr<char[]>`.

This ensures the buffer is automatically released on all exit paths, including:

- normal execution
- early `continue`
- exception handling paths

## Checklist

- [x] I agree to contribute to the project under the Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on code under GPL or another license incompatible with OpenCV.
- [x] The PR is proposed to the proper branch (`4.x`).
- [x] There is a reference to the original bug report and related work, if applicable.
- [x] Accuracy/performance tests and test data in `opencv_extra` are included, if applicable.
- [x] The feature/change is documented and sample code can be built with the project CMake configuration, if applicable.
2026-06-10 20:26:55 +03:00
Alexander Smorkalov f80d02e1c7 Merge pull request #29277 from intel-staging:perf_blobFromImages
Added more perf tests for blobFromImage
2026-06-10 19:24:55 +03:00
Ding zhehao ffa38e1b74 Merge pull request #29194 from Enilrats:opt-emd-performance
imgproc: optimize EMD (Earth Mover's Distance) solver performance #29194

### imgproc: optimize EMD solver performance using O(V) spanning tree traversal

---
This PR significantly optimizes the performance of the Earth Mover's Distance (`cv::EMD`) solver in `modules/imgproc/src/emd_new.cpp`. 

Specifically, it refactors the dual-variable calculation in `EMDSolver::findBasicVars()` from a naive $O((N+M)^2)$ linked-list scanning approach to an optimized $O(N+M)$ BFS tree traversal utilizing existing adjacency lists, leading to a massive speedup.

---

#### Technical Details & Core Bottleneck Fixed

1. **Algorithmic Complexity Reduction in `findBasicVars()`:**
   - **Before**: The original implementation solved the dual variables $u_i$ and $v_j$ by traversing the entire unmarked rows (`u0_head`) or columns (`v0_head`) linked-lists and invoking `getIsX(i, j)` inside nested loops to find connected basic variables. This resulted in an $O((N+M)^2)$ complexity per simplex iteration. For a scale of $2000 \times 2000$, this performed up to $16,000,000$ operations per iteration.
   - **After**: Since `EMDSolver` already maintains the adjacency lists of the basic variables tree (`rows_x` and `cols_x`), we can traverse the spanning tree in linear time. This PR implements a dual-queue BFS tree traversal. The complexity per iteration is drastically reduced to $O(N+M)$, performing at most $4000$ operations per iteration.

2. **Cache Locality & Pointer-Chasing Elimination:**
   - Replaced pointer-chasing on dynamically-allocated linked lists with contiguous, stack-allocated array queues (`cv::AutoBuffer`), significantly improving CPU L1/L2 cache hit rates and enabling hardware prefetching.


---

#### Performance Benchmarks

Below is the benchmark comparison evaluated on a standard CPU.
#### Test 1: dims = 64
| Scale ($N, M$) | Original EMD (ms) | Optimized EMD (This PR) | Speedup |
| :--- | :--- | :--- | :--- |
| **100** | 5.473 | 3.419 | **1.60x** |
| **500** | 381.871 | 244.355 | **1.56x** |
| **1000** | 1893.053 | 1369.016 | **1.38x** |
| **2000** | 11387.792 | 8331.221 | **1.37x** |

#### Test 2: dims = 3
| Scale ($N, M$) | Original EMD (ms) | Optimized EMD (This PR) | Speedup |
| :--- | :--- | :--- | :--- |
| **100** | 4.433 | 3.042 | **1.46x** |
| **500** | 365.762 | 259.735 | **1.41x** |
| **1000** | 1989.400 | 1421.952 | **1.40x** |
| **2000** | 12731.836 | 7952.210 | **1.60x** |


*(Note: The exact performance figures may vary slightly depending on the compiler and test machine.)*

---

#### Verification
- All existing tests in `opencv_test_imgproc` (including EMD tests) pass successfully. No regressions were introduced.

---

### 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
2026-06-10 18:23:35 +03:00
Alexander Smorkalov 1780a86075 Merge pull request #29117 from SofianElmotiem:4.x
calib3d: use Delaunay triangulation in computeRNG for findCirclesGrid
2026-06-10 17:39:52 +03:00
Andrei Fedorov 48b8099214 added more perf tests for blobFromImages 2026-06-10 13:55:20 +02:00
Sofian Elmotiem 3d02d863f9 calib3d: use Delaunay triangulation in computeRNG for findCirclesGrid
The relative neighborhood graph (RNG) is a subgraph of the Delaunay
triangulation, so only Delaunay edges are candidates for RNG membership.
The previous implementation tested all N^2 pairs against all N points,
giving O(N^3). The new implementation builds the Delaunay triangulation
with Subdiv2D (O(N log N)), then checks only those ~3N edges for the RNG
lune-emptiness condition, reducing computeRNG to O(N^2) in the worst case
and much better in practice for regular grids.

Added synthetic-grid accuracy tests for both symmetric and asymmetric
patterns across several sizes, and a perf test parameterized by grid size.
2026-06-10 13:17:26 +02:00
Alexander Smorkalov 3d55d2fcef Merge pull request #29201 from Primary-H:project4_David
features2d: improve detector parameter validation
2026-06-10 14:10:56 +03:00
胡晨宇 d10138fa1c Merge pull request #29132 from hcy11123323:4.x
core: fix inverted continuity check in cvReshapeMatND() #29132

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [√] I agree to contribute to the project under Apache 2 License.
- [√] 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
2026-06-10 13:11:27 +03:00
Alexander Smorkalov 9780b3b329 Merge pull request #29274 from vrabaud:ub_lut
Fix undefined behavior with pointer alignment
2026-06-10 11:27:54 +03:00
Vincent Rabaud 63b59865d3 Fix undefined behavior with pointer alignment
While at it, replace MMX _mm_setr_epi64 to not do the MMX to XMM
move.
2026-06-09 15:43:34 +02:00
Alexander Smorkalov b9a38df0e6 Merge pull request #29271 from uwezkhan:torch-storage-size-cap
cap torch storage size to int to prevent heap overflow
2026-06-09 12:49:17 +03:00
uwezkhan fe61ae0e41 cap torch storage size to int to prevent heap overflow 2026-06-09 10:09:52 +05:30
Alexander Smorkalov 6f29af625b Merge pull request #29268 from asmorkalov:as/disable_ocl_imterop_sample_4.x
Disable OpenCV-OpenCL interop sample for now 4.x
2026-06-08 17:03:23 +03:00
Alexander Smorkalov 0397596e17 Merge pull request #29231 from ozhanghe:4.x
Spelling: Fix typos in HAL
2026-06-08 14:25:52 +03:00
WITHOUTNAMESES 2a70226181 Merge pull request #29191 from WITHOUTNAMESES:project4_czn
imgproc: fix missing tipLength validation in arrowedLine #29191

### Pull Request Description

**Issue/Bug:**
Currently, the `cv::arrowedLine` function in `modules/imgproc/src/drawing.cpp` lacks boundary validation for the `tipLength` parameter. Passing an invalid ratio (e.g., negative values or values > 1.0) leads to mathematically distorted arrowhead coordinates being silently passed to the underlying `line()` drawing function, resulting in severely deformed output without any warning.

**Solution:**
Added a `CV_Assert` to enforce the geometric contract of `tipLength`, ensuring it strictly falls within the logical bounds `(0.0, 1.0]`. This adheres to the fail-fast principle and prevents silent geometric failures. 

**Testing:**
Successfully built the project locally and added a dedicated regression test (`arrowedLine_tipLength_validation`) using Google Test in `modules/imgproc/test/test_drawing.cpp`. Tested with boundary values (e.g., `tipLength = 1.5` and `-0.5`), and the assertion correctly intercepts the invalid parameters before rasterization.

*(Note: This contribution is part of a university engineering project evaluating open-source workflow and C++ robustness.)*
2026-06-08 14:19:00 +03:00
Alexander Smorkalov 21356eed2b Merge pull request #29253 from ssam18:fix-29222-bgr2lab-first-call
Speed up first BGR2Lab and BGR2Luv call by building the color LUT faster
2026-06-08 13:40:02 +03:00
Arnesh Banerjee 11b0bd15cf samples: demonstrate SimpleBlobDetector contour collection
Enable collectContours in detect_blob.cpp and draw the per-blob
contours returned by getBlobContours(), so the sample exercises the
public contour-collection API added in #21942.

Refs #25904
2026-06-07 12:18:39 +05:30
Samaresh Kumar Singh c936dcaef7 Speed up first BGR2Lab and BGR2Luv call by building the color LUT faster
The first conversion to Lab or Luv lazily builds a softfloat lookup table, and that build was doing about 108000 software emulated pow calls plus a fully serial 33x33x33 cube loop. This memoizes applyGamma over the 33 grid values and runs the cube loop with parallel_for_, cutting the first call from around 110 ms to around 25 ms on my machine. The per point computation is unchanged so the tables stay bit exact.
2026-06-06 16:36:07 -05:00
Alexander Smorkalov ab1aaa75aa Merge pull request #29219 from Kumataro:show_jpeg-turbo-version
build: show libjpeg-turbo version for bundled and system-wide libraries
2026-06-06 13:44:26 +03:00
Alexander Smorkalov d9f89b028b Merge pull request #29187 from sparklejin:project4_sparklejin
imgproc: fix floodFill simple path never being used (small fix)
2026-06-06 13:40:25 +03:00
ozhanghe 517710fe56 Spelling: Fix typos in HAL 2026-06-04 20:26:54 -07:00