calcOpticalFlowPyrLK segmentation fault

asked 2018-03-21 06:28:33 -0600

Ldaniel gravatar image

updated 2018-03-21 07:31:14 -0600

berak gravatar image

Hi!

I am using the calcOpticalFlowPyrLK in combination with object detection.

But sometimes this function causes a segmentation fault. I still don't figure it out why.

It happens often in the third or forth step of the loop So:

  1. Detect and get points to track

  2. Calculate the flow and get new points P_new

  3. Use p_new to calculate the next points in the next frame.

I am using it with a webcam such that each 5. Frame the object will be detected and then tracked for the next 4 frames

I already checked that the points which I pass to the function are not empty

The frames are not empty as well.

The error and status a configurares like in the example

But still it causes an segmentation fault.

Does anybody what could cause the seg fault?

There is no error message. It just crashes

        auto & prev = track->prev_points;
        auto & cur = track->cur_points;
        auto & orig = track->orig_points;
​        std::vector<cv::Point2f> curP;
        //cur.clear();
        std::vector<uchar> status;
        std::vector<float> err;
        try{

            cv::calcOpticalFlowPyrLK(lastFrame,currentFrame, track->prev_points,track->cur_points,status,err);
        }   
        catch(cv::Exception& e ){
            const char* err_msg = e.what();
            std::cerr << "exception caught: " << err_msg << std::endl;
        }    

        size_t i,  k ;
        auto it_1 = cur.begin();
        auto it_2 = prev.begin();
        auto it_3 = orig.begin();

        for (i = k = 0; i < cur.size(); i++)
        {

            if (!status[i])
                continue;
​
            cur[k] = cur[i] ;
            std::cerr << cur[k] <<std::endl;
            prev[k] = prev[i];
            orig[k] = orig[i];
            k++;
        }
        cur.resize(k);
        prev.resize(k);
        orig.resize(k);
        prev = cur;
edit retag flag offensive close merge delete

Comments

not enough information here.

code? opencv/os version ? exact errormsg ?

berak gravatar imageberak ( 2018-03-21 06:29:31 -0600 )edit
  • a lot of garbage there. clean up. also use of auto is far from helpful, without seing any context.
  • try/catch is only useful if there's an exception.
  • please explain the for (i = k = 0; i < cur.size(); i++) loop.

  • prev=cur should be a cv::swap()see here (aliasing problem)

  • it will loose points over time, be prepared to reinitialize them from gftt or similar

berak gravatar imageberak ( 2018-03-21 07:39:09 -0600 )edit