Skip to content

Add image dimension check to avoid StereoSGBM non-determinism#27305

Merged
asmorkalov merged 3 commits intoopencv:4.xfrom
chengolivia:add-check-sgbm-nondeterminism
May 17, 2025
Merged

Add image dimension check to avoid StereoSGBM non-determinism#27305
asmorkalov merged 3 commits intoopencv:4.xfrom
chengolivia:add-check-sgbm-nondeterminism

Conversation

@chengolivia
Copy link
Copy Markdown
Contributor

@chengolivia chengolivia commented May 13, 2025

Addresses #25828

Users noticed that StereoSGBM would occasionally give non-deterministic results for .compute(imgL, imgR).

I and others traced the cause to out-of-bounds access that was not being caught when the input images were not wide enough for the input block size and number of disparities to StereoSGBM. The specific math and logic can be found in the above issue's discussion.

This PR adds a CV_Check to make sure images are wider than 1/2 of the block size + the max disparity the algorithm will search.

The check was only added to the regular compute method for StereoSGBM and not to the other modes, as I did not observe the non-deterministic behavior with the other compute modes like HH.

In addition, this PR adds a test case to Calib3d to make sure the check is being thrown in the problem case and that the results are deterministic in the good case.

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

@chengolivia
Copy link
Copy Markdown
Contributor Author

I see in the build failure that test_videoio failed for precommit_linux64_no_opt. However, when I click "test summary" for test_videoio, I see:

Total tests: 216
Tests failed: 0
Tests passed: 216
Exception has been thrown. Please see stdio log.

In the stdio log, the exceptions I see are related to not being able to open files or devices, e.g.:

[ WARN:0@0.034] global cap.cpp:177 open VIDEOIO(OPENNI2): raised OpenCV exception:

OpenCV(4.12.0-dev) /build/precommit_linux64_no_opt/4.x/opencv/modules/videoio/src/cap_openni2.cpp:275: error: (-2:Unspecified error) in function 'CvCapture_OpenNI2'
> OpenCVKinect2: Failed to open device: 	DeviceOpen: Couldn't open device '/build/precommit_linux64_no_opt/4.x/opencv_extra/testdata/highgui/audio/test_audio.mp4'

This appears to be unrelated to my changes, which are only within calib3d.

@chengolivia
Copy link
Copy Markdown
Contributor Author

The non-default failing checks from the previous run are:

  1. Ubuntu2004-x64-CUDA / BuildAndTest: Merge opencv with add-check-sgbm-nondeterminism branch
Run cd /home/ci/opencv
fatal: unable to access 'https://github.com/chengolivia/opencv/': gnutls_handshake() failed: The TLS connection was non-properly terminated.
Error: Process completed with exit code 1.
  1. Windows10-x64 / BuildAndTest: Accuracy:core

I do not see any tests failing in the job, but it is stuck on:

[ RUN      ] Core_SubMixed/ArithmMixedTest.accuracy/0, where GetParam() = (16-byte object <20-93 2A-6A 02-02 00-00 70-34 2D-6A 02-02 00-00>, (CV_8U, CV_16U), 1)
  1. Windows10-x64-Vulkan / BuildAndTest: Accuracy:dnn

I also do not see any tests failing in the job, but it is stuck on:

[ RUN      ] Test_Model.Detection_normalized/0, where GetParam() = VKCOM/VULKAN
  1. macOS-AARM64-Vulkan / BuildAndTest
[==========] 7770 tests from 90 test cases ran. (137910 ms total)
[  PASSED  ] 7769 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] Test_Darknet_nets.YOLOv4x_mish/0, where GetParam() = VKCOM/VULKAN

None of these failures seem to be related to my change in StereoSGBM in calib3d.

@asmorkalov Would you be able to restart the failing jobs or approve the PR despite them? This is my first contribution, so apologies if I missed something.

@chengolivia
Copy link
Copy Markdown
Contributor Author

All the checks besides default are passing now. @asmorkalov Would you be able to restart the default check? Thanks!

@asmorkalov asmorkalov self-requested a review May 15, 2025 14:14
@asmorkalov asmorkalov self-assigned this May 15, 2025
@asmorkalov asmorkalov added this to the 4.12.0 milestone May 17, 2025
@asmorkalov asmorkalov merged commit 250b500 into opencv:4.x May 17, 2025
50 of 55 checks passed
@asmorkalov asmorkalov mentioned this pull request May 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants