Optical Flow OpenCV "lkpyramid.cpp" throws exception

736 views Asked by At

I am trying to use calcOpticalFlowPyrLK function of openCV for motion detection but I am getting error message :

OpenCV Error: Assertion failed ((npoints = prevPtsMat.checkVector(2, CV_32F, tru
e)) >= 0) in cv::calcOpticalFlowPyrLK, file ..\..\..\..\opencv\modules\video\src
\lkpyramid.cpp, line 845

I have checked that the previous corners stored in corners1 of my code is not empty and are valid points."goodFeaturesToTrack" function works fine. I have been stuck here for 2 days any help would be appreciated.

Code Block:

int opticalflow()
{

    Mat frame1, frame2, frame3, difference1, difference2, frame, mask;
    VideoCapture capture;
    vector <Point2d> corners1;
    vector <Point2d> corners2;
    int maxCorners = 200;
    double qualityLevel = 0.01;
    double minDistance = 5;
    int blockSize = 3;
    bool useHarrisDetector = false;
    double k = 0.04;
    vector <unsigned char> optical_flow_found_feature;
    vector <float > optical_flow_feature_error;


    //Read the video stream
    capture.open(0);
    if (!capture.isOpened())
    { 
        printf("Error Opening Video Capture!!!\n"); 

    }
    waitKey(10);
    capture.read(frame1);
    cvtColor( frame1, frame1, COLOR_BGR2GRAY );

    goodFeaturesToTrack( frame1, corners1, maxCorners, qualityLevel, minDistance, mask, blockSize, useHarrisDetector, k );

    for(int i=0; i<corners1.size(); i++)
        cout<<corners1[i].x<<"  "<<corners1[i].y<<endl;

    while (1)
    {
        capture.read(frame2);
        cvtColor( frame2, frame2, COLOR_BGR2GRAY );

        if(corners1.size()>10)
        calcOpticalFlowPyrLK(frame1, frame2, corners1, corners2, optical_flow_found_feature, optical_flow_feature_error);// Size(21,21), 3, TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 0.01), 0, 1e-4);


        cout<<"Reached Here\n";
        for(int i=0; i<maxCorners; i++)
        {

            if(optical_flow_found_feature[i] == 0) 
                continue;
            int line_thickness = 1;
            CvScalar line_color = CV_RGB(255,0,0);

            Point2d p,q;
            p.x = (int) corners1[i].x;
            p.y = (int) corners1[i].y;
            q.x = (int) corners2[i].x;
            q.y = (int) corners2[i].y;
            double angle = atan2( (double) p.y - q.y, (double) p.x - q.x );
            double hypotenuse = sqrt((long double)(p.y - q.y)*(p.y - q.y) + (p.x - q.x)*(p.x - q.x) );


            q.x = (int) (p.x - 3 * hypotenuse * cos(angle));
            q.y = (int) (p.y - 3 * hypotenuse * sin(angle));


            line( frame1, p, q, line_color, line_thickness, CV_AA, 0 );

            p.x = (int) (q.x + 9 * cos(angle + pi / 4));
            p.y = (int) (q.y + 9 * sin(angle + pi / 4));
            line( frame1, p, q, line_color, line_thickness, CV_AA, 0 );
            p.x = (int) (q.x + 9 * cos(angle - pi / 4));
            p.y = (int) (q.y + 9 * sin(angle - pi / 4));
            line( frame1, p, q, line_color, line_thickness, CV_AA, 0 );
        }

        display(frame1);
        int c = waitKey(25);

        //Exit if escape is pressed
        if((char)c == 27) 
        { 
            break; 
        }

    }

return 0;
}

Platform: I am using Visual Studio 2010 OpenCV 2.4.9 on Windows 8

0

There are 0 answers