Ask Your Question

calcOpticalFlowSF() doesn't respond

asked 2013-10-02 07:51:49 -0600


I try to use the calcOpticalFlowSF() function, but when I launch it, the programm doesn't repond, here the part of the code that use it:

 frame1 = cv::imread("frame10.png");
frame2 = cv::imread("frame11.png");

if (frame1.empty()) {
   cout<<"could not read image oldori"<<endl;


    if (frame2.empty()) {
       cout<<"could not read image ori"<<endl;

    if (frame1.rows != frame2.rows && frame1.cols != frame2.cols) {
         cout<<"images should be of equal sizes "endl;

    if (frame1.type() != 16 || frame2.type() != 16) {
         cout<<"images should be of equal type CV_8UC3")endl;

cv::Mat flow;

cv::calcOpticalFlowSF(frame1, frame2, flow, 2, 2, 4);
// calcOpticalFlowSF(frame1, frame1,  // doesn't work too.
//                    flow,
//                    3, 2, 4, 4.1, 25.5, 18, 55.0, 25.5, 0.35, 18, 55.0, 25.5, 10);

I know that the error come from the function calcOpticalFlowSF, because if I comment it, the programm works. I use the same pictures as they use in the SimpleFlow demo.

Do you have an idea why it doesn't work?


best regards.

edit retag flag offensive close merge delete


Did you find the reason for this?

Tharindu gravatar imageTharindu ( 2014-07-01 22:35:32 -0600 )edit

1 answer

Sort by » oldest newest most voted

answered 2014-08-04 10:52:58 -0600

erenik gravatar image

updated 2014-08-04 10:54:58 -0600

Not sure if this is a valid answer, but I had some problems with this function as well and took my time to do some testing on it, using just initialized cv::Mats with specified sizes and CV_8UC3 type.

What I found was that the time complexity of this function seems off the scales (at least in the CV version I am currently using). Size is the width and height of the matrix.

Size  Time in Milliseconds
4      20
8      100
16    400~500
32    2100~2200

The code could be simplified to something like the following:

int size = 16;
cv::Mat lastFrame, frame, flow;
frame = cv::Mat(size, size, CV_8UC3);
cv::calcOpticalFlowSF(lastFrame, frame,
    3, 2, 4, 4.1, 25.5, 18, 55.0, 25.5, 0.35, 18, 55.0, 25.5, 10);

I'm guessing there is some issue with the code base, or that it was meant to be used on the GPU with parallel computing. I have tried both the calcOpticalFlowPyrLK and calcOpticalFlowFarneback methods, and they work without problems. From what I've seen this "SimpleFlow" might not be suitable for real-time analysis, but It could also be this specific implementation.

I noticed that the authors of the algorithm were talking in seconds, while using the algorithm on a GPU, which could be of relevance:

edit flag offensive delete link more

Question Tools


Asked: 2013-10-02 07:51:49 -0600

Seen: 1,351 times

Last updated: Aug 04 '14