Commit Graph

7 Commits

Author SHA1 Message Date
Rahul Kumar
5f335351c9 Merge pull request #30859 from pctablet505:fix-video-dis-collectgarbage-crash
video: fix DISOpticalFlow::calc() crash after collectGarbage() 🤖🤖🤖 - #30859

Fixes #30858

`DISOpticalFlow::calc()` crashed after `collectGarbage()`, which cleared the vector of `VariationalRefinement` objects that only the constructor creates.

### Change

- Keep the objects and call `collectGarbage()` on each of them instead.
- Also release the buffers that `collectGarbage()` missed: `I0x_buf`, `I0y_buf` and their `_aux` copies, the initial-flow pyramids, and their OpenCL counterparts.

### Tests

`DenseOpticalFlow_DIS.calc_after_collectGarbage` calls `collectGarbage()` between `calc()` calls, after a call with an initial flow, before the first call and after parameter changes, on `Mat` and `UMat`. Each result must equal that of a fresh object.

| Check (Linux x86-64, GCC 15.2, with opencv_extra) | 4.x | This PR |
|---|---|---|
| new test | segfault | passes, 3 seeds |
| `calc()` after `collectGarbage()` under AddressSanitizer | heap-use-after-free in `prepareBuffers()` | no report |
| `opencv_test_video`, 178 existing tests | 8 failures (GOTURN model, data) | same 8 |

The `UMat` part also passed with OpenCL on an NVIDIA RTX PRO 6000. 5.x has the same code; the test sits before `regression_20185_patch_larger_than_border` so that the regular 4.x to 5.x merge is clean. Not run on Windows.

Prepared with an AI assistant; every result was reproduced by running the code.

### 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
2026-11-17 16:46:45 +03:00
Rahul Kumar
53d70cb630 Merge pull request #30860 from pctablet505:fix-video-dis-stale-initial-flow
video: clear stale initial flow in DISOpticalFlow::calc() 🤖🤖🤖 - #30860

Fixes #30857

`DISOpticalFlow::calc()` kept the initial-flow pyramid of an earlier call, so a later call without an initial flow still used it, or read past it when the frame grew.

### Change

- Clear `initial_Ux` and `initial_Uy` in `prepareBuffers()` when no initial flow is given.
- The OpenCL path is unchanged: it never reads an initial flow.

### Tests

`DenseOpticalFlow_DIS.StaleInitialFlow` gives one object an initial flow, then calls it without one, on the same frame and on a larger one, and compares with a fresh object (`NORM_INF == 0`).

| Check (Linux x86-64, GCC 15.2, with opencv_extra) | 4.x | This PR |
|---|---|---|
| new test | fails, then segfaults on the larger frame | passes, 4 seeds |
| clear removed |  | fails, then segfaults |
| larger frame under AddressSanitizer | heap-buffer-overflow | no report |
| `opencv_test_video`, 178 existing tests | 8 failures (GOTURN model, data) | same 8 |

5.x has the same code; the change merges cleanly. Not run on Windows or macOS.

Prepared with an AI assistant; every result was reproduced by running the code.

### 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
2026-11-17 12:53:30 +03:00
47022578z-collab
d21a05f1af Merge pull request #30086 from 47022578z-collab:fix-farneback-initial-flow
video: add validation for OPTFLOW_USE_INITIAL_FLOW in calcOpticalFlowFarneback - #30086

Add explicit parameter check for initial flow in `calcOpticalFlowFarneback`. 
When `OPTFLOW_USE_INITIAL_FLOW` flag is passed but the `flow` matrix is empty/missing, this provides a descriptive runtime error instead of non-deterministic behavior or garbage values.

Fixes #7276.

### 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 a code under GPL or another license that is incompatible with OpenCV.
- [x] The PR is proposed to the proper branch (`4.x`).
- [x] There is a reference to the original bug report (#7276).
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable.
- [x] The feature is well documented and sample code can be built with the project CMake.
2026-09-29 19:44:42 +03:00
CodeCraftsman
0c6b4a9b6d Merge pull request #29853 from Thebinary110:4x-fix-dis-opticalflow-overflow
video: fix DISOpticalFlow heap-buffer-overflow with patch_size > border_size - #29853

Fixes #20185. 4.x companion to #29715.

@asmorkalov asked to retarget #29715 to 4.x since the issue reproduces there too, but that PR's branch descends from 5.x, so literally changing its base produces an unreviewable ~2M-line diff (the two branches have diverged far beyond this module). Opening a separate PR instead: same two commits, cherry-picked cleanly onto 4.x's current tip with zero conflicts (`modules/video/src/dis_flow.cpp` is byte-for-byte identical between the branches apart from this fix).

### What / why
See #29715 for the full writeup. Summary: `DISOpticalFlowImpl` pads `I1` with a fixed 16px border, while `PatchInverseSearch`'s search-position clamp lets a patch be placed up to `patch_size - 1` px outside the image -- safe only while `patch_size <= border_size`. Since `patch_size` is user-settable with no upper bound relative to the hardcoded border, `setPatchSize()` past 16 (or a large-enough temporal-candidate flow) reads past the end of the padded buffer (confirmed via AddressSanitizer).

Fix (per review on #29715): rather than growing the persistent `border_size` object field to match `patch_size` (which a reviewer correctly flagged as the wrong place for a per-call derived quantity), the search clamp itself (`i/j_lower_limit`, `i/j_upper_limit` in `PatchInverseSearch_ParBody`) now accounts for the read window needing to stay inside the *existing* padded buffer. For `patch_size <= border_size` (every built-in preset) the bounds are algebraically identical to the originals -- `border_size` itself is untouched.

### Testing
Verified this reproduces identically on 4.x: built an ASan-instrumented Debug configuration (`core+imgproc+imgcodecs+features2d+flann+video+ts`), confirmed the crash reproduces on pristine 4.x (same crash site as the original report), confirmed it's gone with this fix, and ran the new `regression_20185_patch_larger_than_border` / `regression_20185_stress` tests (5 repeated runs) plus the full `opencv_test_video` suite -- no failures attributable to this change (everything else failing needs `opencv_extra` test data not configured in this scoped build).

### PR 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 incompatible license.
- [x] The PR is proposed to the proper branch (4.x, per maintainer request on #29715).
- [x] Accuracy tests included (see above).
- [x] No public API/behavior change, so no documentation or sample updates needed.
2026-09-07 21:43:55 +03:00
satyam yadav
2b42788803 Merge pull request #28217 from satyam102006:feature/dis-coarsest-scale
[video] Add setCoarsestScale to DISOpticalFlow (Fixes #25068) #28217

### Description
This PR addresses issue #25068 regarding the number of scales in `DISOpticalFlow`.

Currently, `DISOpticalFlow` automatically computes the `coarsest_scale` based on the image size. While generally effective, this behavior can cause errors in specific use cases (e.g., mechanics/speckle pattern analysis) where high pyramid levels degrade quality.

This change introduces a manual override:
- Added `setCoarsestScale(int val)` and `getCoarsestScale()` to the public API.
- Updated `DISOpticalFlowImpl::calc` and `ocl_calc` to use the user-defined scale if set.
- Preserved the existing "automatic" behavior as the default (when set to -1).

### Changes
- **`modules/video/include/opencv2/video/tracking.hpp`**: Added virtual method declarations.
- **`modules/video/src/dis_flow.cpp`**: Implemented `set/getCoarsestScale` in `DISOpticalFlowImpl` and updated calculation logic.
- **`modules/video/test/test_OF_accuracy.cpp`**: Added `DenseOpticalFlow_DIS.ManualCoarsestScale` regression test.


Fixes #25068
2025-12-20 13:28:32 +03:00
Thang Tran
d1615ba11a video:fixed DISOpticalFlow segfault from small img 2019-05-27 08:18:26 +02:00
Vadim Pisarevsky
96bf26611e Merge pull request #13084 from vpisarev:shuffle_optflow_algos
* moved DIS optical flow from opencv_contrib to opencv, moved TVL1 from opencv to opencv_contrib

* fixed compile warning

* TVL1 optical flow example moved to opencv_contrib
2018-11-09 17:52:06 +03:00