Skip to content

DISOpticalFlow crashing with unknown exception for a particular choice of image pair, flow direction and params #20185

Closed

Description

@odedbd
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

  1. danielenricocahall commented on Jun 2, 2021

    @danielenricocahall
    Contributor

    Hello @odedbd ! I rewrote the code in C++, ran on master, and couldn't replicate (no crash):

        Mat fixed = cv::imread(getDataDir() + "optflow/bug_20185/fixed_gray.tiff", IMREAD_GRAYSCALE);
        Mat moving_nw = cv::imread(getDataDir() + "optflow/bug_20185/moving_nw_gray.tiff", IMREAD_GRAYSCALE);
        Mat moving_w = cv::imread(getDataDir() + "optflow/bug_20185/moving_w_gray.tiff", IMREAD_GRAYSCALE);
        Ptr<DISOpticalFlow> algo = DISOpticalFlow::create( DISOpticalFlow::PRESET_MEDIUM);
        algo->setPatchSize(25);
        algo->setPatchStride(10);
        Mat flow(moving_nw.size(), moving_nw.type());
        algo->calc(moving_nw, fixed, flow);
    

    Did I miss something?

  2. odedbd commented on Jun 2, 2021

    @odedbd
    Author

    @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?

  3. danielenricocahall commented on Jun 2, 2021

    @danielenricocahall
    Contributor

    @odedbd I have tried both cases, and both work successfully.

        Mat fixed = cv::imread(getDataDir() + "optflow/bug_20185/fixed_gray.tiff", IMREAD_GRAYSCALE);
        Mat moving_nw = cv::imread(getDataDir() + "optflow/bug_20185/moving_nw_gray.tiff", IMREAD_GRAYSCALE);
        Mat moving_w = cv::imread(getDataDir() + "optflow/bug_20185/moving_w_gray.tiff", IMREAD_GRAYSCALE);
        Ptr<DISOpticalFlow> algo = DISOpticalFlow::create( DISOpticalFlow::PRESET_MEDIUM);
        algo->setPatchSize(25);
        algo->setPatchStride(10);
        Mat flow(moving_nw.size(), moving_nw.type());
        algo->calc(moving_nw, fixed, flow);
        algo->calc(fixed, moving_nw, flow);
    
  4. odedbd commented on Jun 3, 2021

    @odedbd
    Author

    @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.

  5. odedbd commented on Jun 20, 2021

    @odedbd
    Author

    @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-

        std::cout << "aa" << std::endl;
     
        Mat fixed = cv::imread("fixed_gray.tiff", IMREAD_GRAYSCALE);
        Mat moving_nw = cv::imread("moving_nw_gray.tiff", IMREAD_GRAYSCALE);
        Mat moving_w = cv::imread("moving_w_gray.tiff", IMREAD_GRAYSCALE);
        Ptr<DISOpticalFlow> algo = DISOpticalFlow::create( DISOpticalFlow::PRESET_MEDIUM);
        algo->setPatchSize(25);
        algo->setPatchStride(10);
        Mat flow(moving_nw.size(), moving_nw.type());
    
        
        algo->calc(moving_nw, fixed, flow);
        std::cout << "bb" << std::endl;
    
        algo->calc(moving_nw, fixed, flow);
        std::cout << "bb" << std::endl;
    
        algo->calc(moving_nw, fixed, flow);
        std::cout << "bb" << std::endl;
    
    
        algo->calc(fixed, moving_nw, flow);
        std::cout << "cc" << std::endl;
    

    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.dll

    I hope this helps advancing the issue. I'd appreciate any pointers on what test I can run to provide more useful information on this.

  6. odedbd commented on Jun 23, 2021

    @odedbd
    Author

    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.

  7. odedbd commented on Jun 24, 2021

    @odedbd
    Author

    @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.

  8. odedbd commented on Jun 24, 2021

    @odedbd
    Author

    @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.

  9. odedbd commented on Oct 10, 2021

    @odedbd
    Author

    @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!

  10. alalek commented on Oct 21, 2021

    @alalek
    Member

    Thank you for update!

    I find it hard to follow, in main part due to the parallel nature

    cv::setNumThreads(1) before call may help here.

  11. odedbd commented on Oct 25, 2021

    @odedbd
    Author

    @alalek thanks for the suggestion! I'll try that and see if it makes it easier to debug.

  12. wenyan96 commented on Feb 3, 2022

    @wenyan96

    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 0x0000561ce9ca853e

    Could 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

  13. odedbd commented on Feb 8, 2022

    @odedbd
    Author

    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.

  14. modified the milestones: 4.8.0, 4.9.0 on May 15, 2023
  15. asmorkalov commented on Oct 31, 2023

    @asmorkalov
    Contributor

    Reproduced the issue with address sanitizer on Ubuntu 18.04:

    ==21872==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7efd9e2d77b2 at pc 0x7efdb83fee66 bp 0x7ffe429c6560 sp 0x7ffe429c6550
    READ of size 1 at 0x7efd9e2d77b2 thread T0
        #0 0x7efdb83fee65 in cv::computeSSDMeanNorm(unsigned char*, unsigned char*, int, int, float, float, float, float, int) (/home/alexander/Projects/OpenCV/opencv-build/lib/libopencv_video.so.408+0xa6e65)
        #1 0x7efdb8401204 in cv::DISOpticalFlowImpl::PatchInverseSearch_ParBody::operator()(cv::Range const&) const (/home/alexander/Projects/OpenCV/opencv-build/lib/libopencv_video.so.408+0xa9204)
        #2 0x7efdb6bf5d89 in cv::(anonymous namespace)::ParallelLoopBodyWrapper::operator()(cv::Range const&) const (/home/alexander/Projects/OpenCV/opencv-build/lib/libopencv_core.so.408+0x8c0d89)
        #3 0x7efdb6c193c4 in cv::ParallelJob::execute(bool) (/home/alexander/Projects/OpenCV/opencv-build/lib/libopencv_core.so.408+0x8e43c4)
        #4 0x7efdb6c1b2c6 in cv::ThreadPool::run(cv::Range const&, cv::ParallelLoopBody const&, double) (/home/alexander/Projects/OpenCV/opencv-build/lib/libopencv_core.so.408+0x8e62c6)
        #5 0x7efdb6c1ba28 in cv::parallel_for_pthreads(cv::Range const&, cv::ParallelLoopBody const&, double) (/home/alexander/Projects/OpenCV/opencv-build/lib/libopencv_core.so.408+0x8e6a28)
        #6 0x7efdb6bf6c1a in cv::parallel_for_impl(cv::Range const&, cv::ParallelLoopBody const&, double) (/home/alexander/Projects/OpenCV/opencv-build/lib/libopencv_core.so.408+0x8c1c1a)
        #7 0x7efdb6bf63bf in cv::parallel_for_(cv::Range const&, cv::ParallelLoopBody const&, double) (/home/alexander/Projects/OpenCV/opencv-build/lib/libopencv_core.so.408+0x8c13bf)
        #8 0x7efdb8412c66 in cv::DISOpticalFlowImpl::calc(cv::_InputArray const&, cv::_InputArray const&, cv::_InputOutputArray const&) (/home/alexander/Projects/OpenCV/opencv-build/lib/libopencv_video.so.408+0xbac66)
        #9 0x558644213ee7 in main (/home/alexander/tmp/opencv-repro/src/build/test+0x2ee7)
        #10 0x7efdb59c4c86 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21c86)
        #11 0x558644212ef9 in _start (/home/alexander/tmp/opencv-repro/src/build/test+0x1ef9)
    
    0x7efd9e2d77b2 is located 3434 bytes to the right of 3002952-byte region [0x7efd9dff9800,0x7efd9e2d6a48)
    allocated by thread T0 here:
        #0 0x7efdb8899b40 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xdeb40)
        #1 0x7efdb65923a0 in cv::fastMalloc(unsigned long) (/home/alexander/Projects/OpenCV/opencv-build/lib/libopencv_core.so.408+0x25d3a0)
        #2 0x7efdb699c6f9 in cv::StdMatAllocator::allocate(int, int const*, int, void*, unsigned long*, cv::AccessFlag, cv::UMatUsageFlags) const (/home/alexander/Projects/OpenCV/opencv-build/lib/libopencv_core.so.408+0x6676f9)
        #3 0x7efdb69a53ce in cv::Mat::create(int, int const*, int) (/home/alexander/Projects/OpenCV/opencv-build/lib/libopencv_core.so.408+0x6703ce)
        #4 0x7efdb69a26b6 in cv::Mat::create(int, int, int) (/home/alexander/Projects/OpenCV/opencv-build/lib/libopencv_core.so.408+0x66d6b6)
        #5 0x7efdb8415c60 in cv::Mat_<short>::create(int, int) (/home/alexander/Projects/OpenCV/opencv-build/lib/libopencv_video.so.408+0xbdc60)
        #6 0x7efdb83f0d80 in cv::DISOpticalFlowImpl::prepareBuffers(cv::Mat&, cv::Mat&, cv::Mat&, bool) (/home/alexander/Projects/OpenCV/opencv-build/lib/libopencv_video.so.408+0x98d80)
        #7 0x7efdb8412379 in cv::DISOpticalFlowImpl::calc(cv::_InputArray const&, cv::_InputArray const&, cv::_InputOutputArray const&) (/home/alexander/Projects/OpenCV/opencv-build/lib/libopencv_video.so.408+0xba379)
        #8 0x55864421399e in main (/home/alexander/tmp/opencv-repro/src/build/test+0x299e)
        #9 0x7efdb59c4c86 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21c86)
    
    SUMMARY: AddressSanitizer: heap-buffer-overflow (/home/alexander/Projects/OpenCV/opencv-build/lib/libopencv_video.so.408+0xa6e65) in cv::computeSSDMeanNorm(unsigned char*, unsigned char*, int, int, float, float, float, float, int)
    Shadow bytes around the buggy address:
      0x0fe033c52ea0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
      0x0fe033c52eb0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
      0x0fe033c52ec0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
      0x0fe033c52ed0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
      0x0fe033c52ee0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
    =>0x0fe033c52ef0: fa fa fa fa fa fa[fa]fa fa fa fa fa fa fa fa fa
      0x0fe033c52f00: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
      0x0fe033c52f10: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
      0x0fe033c52f20: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
      0x0fe033c52f30: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
      0x0fe033c52f40: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
    Shadow byte legend (one shadow byte represents 8 application bytes):
      Addressable:           00
      Partially addressable: 01 02 03 04 05 06 07 
      Heap left redzone:       fa
      Freed heap region:       fd
      Stack left redzone:      f1
      Stack mid redzone:       f2
      Stack right redzone:     f3
      Stack after return:      f5
      Stack use after scope:   f8
      Global redzone:          f9
      Global init order:       f6
      Poisoned by user:        f7
      Container overflow:      fc
      Array cookie:            ac
      Intra object redzone:    bb
      ASan internal:           fe
      Left alloca redzone:     ca
      Right alloca redzone:    cb
    ==21872==ABORTING
    
  16. added
    confirmed
    on Oct 31, 2023
  17. modified the milestones: 4.9.0, 4.10.0 on Dec 1, 2023
  18. modified the milestones: 4.10.0, 4.11.0 on Apr 27, 2024
  19. removed this from the 4.11.0 milestone on Dec 17, 2024
  20. added a commit that references this issue on Jul 25, 2026
    f43237e
  21. added this to the 4.15.0 milestone on Aug 14, 2026
  22. added a commit that references this issue on Sep 7, 2026
    0c6b4a9
  23. added a commit that references this issue on Sep 21, 2026
    889ad96
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcategory: videoconfirmed

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions