Repository navigation
Description
System information (version)
- OpenCV => 4.2
- Operating System / Platform => Windows 10 64bit, Linux 20.04 64bit
- Compiler => prebuilt opencv-python & opencv plugin for matlab with mex, compiler VS 2017
Detailed description
I am using the DISOpticalFlow optical flow class to calculate flow between pairs of images. I call the optical flow via the python bindings and/or through matlab by compiling C++ code to MEX using the Matlab OpenCV plugin. I expect the code to not crash and in the case that it does crash to raise a meaningful exception.
When run with some fixed/moving image pairs the process crashes. Until opencv-python 4.5.1.48 these crahes were silent, without even raising an exception. Since the latest 4.5.2.52 opencv-python release, an "Unknown C++ exception from OpenCV code" is raised. This is a huge improvement already, since it can be worked around in some cases, but it is still unhelpful in tracking down the issue and resolving it.
After extensive testing it seems that whether the process will crash depends on at least the image pair, which image is considered fixed and the patchSize parameter. Changing the preset seems to also effect whether the code will crash or not with FAST/ULTRAFAST less likely to crash, but also sometimes crashes.
Steps to reproduce
Image to reproduce the issue are attached in (large) zip file.
optical_flow_issue_images.zip
The following code demonstrate the issue by calculating optical flow between two pairs of images, fixed + moving_w (working) and fixed + moving_nw (not working), and patchSize 15 or 25.
When run in the reverse direction (from moving to fixed), there is no error regardless of which image pair is used. When using patchSize=15 also everything is working for both pairs.
When calculating optical flow from fixed to moving (straight direction) and patchSize=25 the running crashes.
I have run this code mostly using the python bindings (opencv-python), but I have experienced the same issue when compiling C++ code to MEX using the matlab OpenCV plugin, so I do not think this issue is due to the python binding directly.
I was able to reproduce this issue on both Win and Linux machines.
import cv2
def get_DIS_flow(moving, fixed, patch_size):
preset = cv2.DISOPTICAL_FLOW_PRESET_MEDIUM
patch_stride = 10
DIS_flow = cv2.DISOpticalFlow.create(preset)
DIS_flow.setPatchSize(patch_size)
DIS_flow.setPatchStride(patch_stride)
flow = DIS_flow.calc(fixed, moving, None)
return flow
if __name__ == '__main__':
fixed = cv2.imread('./fixed_gray.tiff', cv2.IMREAD_GRAYSCALE)
moving_nw = cv2.imread('./moving_nw_gray.tiff', cv2.IMREAD_GRAYSCALE)
moving_w = cv2.imread('./moving_w_gray.tiff', cv2.IMREAD_GRAYSCALE)
patch_size = 15
# with patch_size=15 everything is working
print('patch_size {} moving_w call straight direction'.format(patch_size))
D = get_DIS_flow(moving_w, fixed, patch_size)
print('ok')
print('patch_size {} moving_w call reverse direction'.format(patch_size))
D = get_DIS_flow(fixed, moving_w, patch_size)
print('ok')
print('patch_size {} moving_nw call straight direction'.format(patch_size))
D = get_DIS_flow(moving_nw, fixed, patch_size)
print('ok')
print('patch_size {} moving_nw call reverse direction'.format(patch_size))
D = get_DIS_flow(fixed, moving_nw, patch_size)
print('ok')
patch_size = 25
# with patch_size=25 moving_w calls still work
print('patch_size {} moving_w call straight direction'.format(patch_size))
D = get_DIS_flow(moving_w, fixed, patch_size)
print('ok')
print('patch_size {} moving_w call reverse direction'.format(patch_size))
D = get_DIS_flow(fixed, moving_w, patch_size)
print('ok')
# with patch_size=25 moving_nw in reverse direction works
print('patch_size {} moving_nw call reverse direction'.format(patch_size))
D = get_DIS_flow(fixed, moving_nw, patch_size)
print('ok')
# with patch_size=25 moving_nw in straight direction crashes
print('patch_size {} moving_nw call straight direction'.format(patch_size))
try:
D = get_DIS_flow(moving_nw, fixed, patch_size)
print('ok')
except Exception as e:
print('only gets here in version 4.5.2.52')
print(e)
Thank you for taking the time to look into this.
Issue submission checklist
- [* ] I report the issue, it's not a question
- [ *] I checked the problem with documentation, FAQ, open issues,
forum.opencv.org, Stack Overflow, etc and have not found solution - [* ] I updated to latest OpenCV version and the issue is still there
- [* ] There is reproducer code and related data files: videos, images, onnx, etc
Activity
danielenricocahall commentedon Jun 2, 2021
Hello @odedbd ! I rewrote the code in C++, ran on master, and couldn't replicate (no crash):
Did I miss something?
odedbd commentedon Jun 2, 2021
@danielenricocahall I think you may be testing the wrong moving/fixed order. I'm afraid my repro code is (for historical reasons) a bit confusing since the order of moving/fixed in get_DIS_flow is reversed in respect to calc.
So,
get_DIS_flow(moving_nw, fixed, patch_size)means that moving_nw is the moving image and fixed is the reference image, which should be translated in the C++ code to:algo->calc(fixed, moving_nw, flow);Or did I get confused myself?
danielenricocahall commentedon Jun 2, 2021
@odedbd I have tried both cases, and both work successfully.
odedbd commentedon Jun 3, 2021
@danielenricocahall are you using Windows or Ubuntu?
Following your comments I retested the repro code on both Windows and Ubuntu and I see I can only reproduce it on Windows. We did see a similar error using Ubuntu before, but perhaps that was with a different set of images. I apologize for the confusion in my OP.
odedbd commentedon Jun 20, 2021
@danielenricocahall I managed to reproduce the problem in c++ on windows, using the mingw64 compiler and prebuilt opencv 4.5.2 (as well as 4.1.1) binaries I got from here - https://github.com/huihut/OpenCV-MinGW-Build.
The code I've been running is this-
I added the multiple calls with the reverse direction just to make sure this is stable in that direction.
Expected output is:
aa
bb
bb
bb
cc
Instead, when running the last statement, with fixed as the first argument of calc the program exits. When run directly from the terminal (just running main.exe) I don't get any error messages. When running via gdb and advancing through the program I see it indeed crashes on the
algo->calc(fixed, moving_nw, flow);statement and I get the following error message:Thread 1 received signal SIGSEGV, Segmentation fault. 0x000000006d4c51c0 in cv::DISOpticalFlowImpl::PatchInverseSearch_ParBody::operator()(cv::Range const&) const () from C:\opencv\OpenCV-MinGW-Build-OpenCV-4.5.2-x64\x64\mingw\bin\libopencv_video452.dllI hope this helps advancing the issue. I'd appreciate any pointers on what test I can run to provide more useful information on this.
odedbd commentedon Jun 23, 2021
Hi All,
I rebuilt OpenCV from source (opencv-master), using Virtual Studio 2019 community edition. I am now able to debug and step into OpenCV functions in VS.
I found that the exception is thrown when the code reaches the finest level (1). I see that the exception gets thrown from COMPUTE_SSD. the exception itself is:
Exception thrown: read access violation.
I1_ptr was 0x111032168B87104.
Setting the finest scale to 2 instead of 1 fixed the problem as far as the exception not being thrown. I haven't tested yet whether that's a good enough workaround for my use case or whether it is even consistently working on other image pairs.
If anyone has any idea on further debug information I can get from stepping through the code please let me know and I'd be happy to update here.
odedbd commentedon Jun 24, 2021
@danielenricocahall I followed the OpenCV tutorial for building and using opencv C++ on linux and was able to verify that the problem does not happen there for the image pairs I attached to the issue.
So, it appear that at least for these the problem is with in the windows build only.
odedbd commentedon Jun 24, 2021
@alalek if you have any pointers for anything I could check on my windows build that could help resolve this please let me know. I would like to give as much helpful info as possible.
odedbd commentedon Oct 10, 2021
@alalek I have run some more debugging on this set of images. I found that if I change the patch size from 25 to 24, I get the same exception, but now on the opposite direction, that is it is now crashing on the very first call to algo->calc.
I tried stepping through the code, but I find it hard to follow, in main part due to the parallel nature and to my lack of detailed understanding of its workings. I would greatly appreciate any possible help on this.
Thanks!
alalek commentedon Oct 21, 2021
Thank you for update!
cv::setNumThreads(1)before call may help here.odedbd commentedon Oct 25, 2021
@alalek thanks for the suggestion! I'll try that and see if it makes it easier to debug.
wenyan96 commentedon Feb 3, 2022
Hi, @odedbd was this problem solved by using DIS optical flow on Linux for you?
I use ubuntu 18.04, OpenCV 3.4.5. I also meet the same error
cv::optflow::DISOpticalFlowImpl::PatchInverseSearch_ParBody::operator()(cv::Range const&) const 0x00007f5c02842cec (anonymous namespace)::ParallelLoopBodyWrapper::operator()(cv::Range const&) const 0x00007f5bfe0fbd35 cv::ParallelJob::execute(bool) 0x00007f5bfe0fc70a cv::parallel_for_pthreads(cv::Range const&, cv::ParallelLoopBody const&, double) 0x00007f5bfe100789 cv::parallel_for_(cv::Range const&, cv::ParallelLoopBody const&, double) 0x00007f5bfe0fc0fd cv::optflow::DISOpticalFlowImpl::calc(cv::_InputArray const&, cv::_InputArray const&, cv::_InputOutputArray const&) 0x00007f5c0284d021 main 0x0000561ce9ca6d60 __libc_start_main 0x00007f5bfd9d60b3 _start 0x0000561ce9ca853eCould you please tell me the opencv version you used on linux? Really thanks for your help!
And looking forward to your further official update! @alalek
odedbd commentedon Feb 8, 2022
Hi @wenyan96 , it wasn't solved for me on Linux, but the specific example I provided above only causes the issue on Windows. I haven't focused on looking for examples that fail on Linux though, since use under Windows is an important use case for me. Perhaps if you could provide a repro with a pair of images and a set of parameters that cause this error it would help others to assist in debugging the core issue.
asmorkalov commentedon Oct 31, 2023
Reproduced the issue with address sanitizer on Ubuntu 18.04:
video: fix out-of-bounds read in DISOpticalFlow with large patch size
Address review: don't mutate border_size, use theRNG(), trim comments
Merge pull request #29853 from Thebinary110:4x-fix-dis-opticalflow-ov…
Merge pull request #29715 from Thebinary110:fix-dis-opticalflow-overflow