calcOpticalFlowPyrLK assertion failure

asked 2015-04-12 17:42:57 -0600

215 gravatar image

updated 2015-04-12 18:19:37 -0600

I am bit unsure on why i am getting this assertion error. here is the code

Rect trace( Mat frame )
{
    Mat grayframe, prevGray, image;

        if (frame.type()!= 8) {
            cvtColor(frame, frame, CV_8U);
        }
    cvtColor(frame, grayframe, CV_RGB2GRAY);
    vector<Point2f> points[2];
    String face_cascade_XML = "/Users/x/Documents/x/facedetect/facedetect/haarcascade_frontalface_alt.xml";
    //String face_cascade_XML = "/Users/x/Documents/x/opencv/opencv-2.4.10/data/haarcascades/haarcascade_upperbody.xml";
    CascadeClassifier face_cascade;
    vector<Rect> faces(1);
    Point center_of_frame(grayframe.size().width/2,grayframe.size().height/2);
    pair<Point, Point> corners;

    //-- 1. Load the cascades
    if( !face_cascade.load( face_cascade_XML ) ){
        cout << "Cascade Error" << endl;
    };

        circle(grayframe, center_of_frame, 1, CV_RGB(0,255,255),8,8,0);
        //-- Detect faces
        face_cascade.detectMultiScale( grayframe, faces, 1.1, 2, 0, Size(200, 200) );

    if (!faces.empty()) {
        for( auto faces : faces)
        {
            Point center_position_of_face((faces.br().x+faces.tl().x)/2,(faces.tl().y+faces.br().y)/2);
            Point corner_1(faces.br().x,faces.tl().y);
            Point corner_2 = faces.tl();
            Point corner_3 = faces.br();
            Point corner_4(faces.tl().x,faces.br().y);
            rectangle(frame, faces, CV_RGB(0,255,0),4,8,0);
            circle(frame, center_position_of_face, 8, CV_RGB(128,128,128),8,8,0);
            circle(frame, corner_1, 1, CV_RGB(128,128,128),8,8,0);
            circle(frame, corner_2, 1, CV_RGB(128,128,128),8,8,0);
            circle(frame, corner_3, 1, CV_RGB(128,128,128),8,8,0);
            circle(frame, corner_4, 1, CV_RGB(128,128,128),8,8,0);
            line(frame, center_position_of_face, center_of_frame, CV_RGB(0,200,0),1,8);

        }

        Mat mask(frame.size(),CV_8UC1 ,Scalar(0));
        mask(faces.back()).setTo(Scalar(255));
        goodFeaturesToTrack(grayframe, points[1], 300, 0.01, 10, mask, 3, 0, 0.04);
        size_t i, k;
        vector<uchar> status;
        vector<float> err;
        if(prevGray.empty())
        {
            grayframe.copyTo(prevGray);
        }
            for (i = k = 0; i < points[1].size(); i++)
            {
                points[1][k++] = points[1][i];
                circle( frame, points[1][i], 4, Scalar(0,255,0), -1, 8, 0 );
            }
        calcOpticalFlowPyrLK(prevGray, grayframe, points[0], points[1], status, err, winSize,3, termcrit, 0, 0.001);

    }

    flip(frame, frame, 1);
    imshow("Facedetection", frame);

    return (faces.back());
}

the error I'm getting is

OpenCV Error: Assertion failed ((npoints = prevPtsMat.checkVector(2, CV_32F, true)) >= 0) in calcOpticalFlowPyrLK, file /opt/local/var/macports/build/_opt_mports_dports_graphics_opencv/opencv/work/opencv-2.4.10/modules/video/src/lkpyramid.cpp, line 845
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /opt/local/var/macports/build/_opt_mports_dports_graphics_opencv/opencv/work/opencv-2.4.10/modules/video/src/lkpyramid.cpp:845: error: (-215) (npoints = prevPtsMat.checkVector(2, CV_32F, true)) >= 0 in function calcOpticalFlowPyrLK
edit retag flag offensive close merge delete

Comments

were you able to resolve this ?

kjoshi gravatar imagekjoshi ( 2018-03-20 07:08:32 -0600 )edit