opencv_contrib icon indicating copy to clipboard operation
opencv_contrib copied to clipboard

CUDA SparsePyrLKOpticalFlow is not ThreadSafe ?

Open keivanmoazami opened this issue 3 years ago • 2 comments

System information (version)
  • OpenCV => 4.5.4
  • OpenCV Contrib => Last Version
  • Operating System / Platform => Windows 10 64 Bit
  • Compiler => Visual Studio 2019
Detailed description

When I using cuda::SparsePyrLKOpticalFlow in multi thread application failed to calc optical flow properly. I run it on different cuda::GpuMats and in separate cuda::Streams with separate Algorithm instances. I find similar problem in other cuda functions that are not thread safe. https://github.com/opencv/opencv/issues/18051 Is cuda::SparsePyrLKOpticalFlow thread safe ?

Steps to reproduce

I call bellow code in different thread simultaneously

mSparseLK->calc(GpuPervFrame, GpuFrame, gOldFeaturePoints, gNewFeaturePoints, gStatus, gErrors, mStream);

keivanmoazami avatar Jul 25 '22 12:07 keivanmoazami

Algorithm instance is not thread safe by default (due to efficiency). Different threads must use different instances. (exceptions are containers like cv::Mat/UMat/etc)

Usage questions should go to Users OpenCV Q/A forum: https://forum.opencv.org/

alalek avatar Jul 25 '22 14:07 alalek

Convert pixel format from CV_8U to CV_16U fix the problem even without use cv::Stream https://forum.opencv.org/t/how-to-use-cuda-sparsepyrlkopticalflow-in-multi-thread-environment/9632/9

keivanmoazami avatar Jul 30 '22 11:07 keivanmoazami