Ask Your Question

OpenCV Error: Assertion failed (prevPyr[level * lvlStep1].size() == nextPyr[level * lvlStep2].size()) in void cv::calcOpticalFlowPyrLK

asked 2016-07-18 08:23:07 -0500

elish gravatar image

updated 2016-07-31 03:02:02 -0500

I am working on 2 images, on the first one i found goodFeaturesToTrack and draw it then on it, i am trying to to tracking with that to the second image but the line of calling calcOpticalFlowPyrLK gets an error:

error(): OpenCV Error: Assertion failed (prevPyr[level * lvlStep1].size() == nextPyr[level * lvlStep2].size()) in void  cv::calcOpticalFlowPyrLK(InputArray, InputArray, InputArray, InputOutputArray, OutputArray, OutputArray, Size, int, cv::TermCriteria, int, double), file /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/video/src/lkpyramid.cpp, line 979

And i do not understand what the proble, is.

    MatOfPoint2f prevPts = new MatOfPoint2f();
    MatOfPoint2f nextPts = new MatOfPoint2f();
    MatOfByte status = new MatOfByte();
    MatOfFloat err = new MatOfFloat();
    //when to stop
    TermCriteria optical_flow_termination_criteria=new TermCriteria();
    optical_flow_termination_criteria.maxCount =20;
    int maxlevel = 2;
    Size s = new Size( 21, 21);

    MatOfPoint corners = new MatOfPoint();

    org.opencv.core.Point[] points = corners.toArray();
    for (org.opencv.core.Point p : points) {, p, 8, circleColor);

    corners.convertTo(prevPts, CvType.CV_32FC2);

    Video.calcOpticalFlowPyrLK(tmp, secondtmp, prevPts, nextPts, status, err,s,maxlevel,optical_flow_termination_criteria,2,1);

Thank in advance!!

edit retag flag offensive close merge delete


can you take a look at your tmp, secondtmp images ? it seems, they're not both valid.

berak gravatar imageberak ( 2016-07-31 03:12:28 -0500 )edit

@berak They seem to be ok, with tmp i was able to find goodFeaturesToTrack and draw it on the image, and second tmp seems to be the same.

elish gravatar imageelish ( 2016-07-31 03:17:32 -0500 )edit

I encountered the same error. It was because the the images didn't had the same size, width and height. The pyramid images are just a series of scaled down images of the original images, and if the original images didn't had the same size, any two corresponding pyramid images will also be of different size.

mihaicl gravatar imagemihaicl ( 2016-11-15 06:05:27 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-01-05 13:53:53 -0500

Thanks a lot. It works. Thanks for the explanation.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2016-07-18 08:23:07 -0500

Seen: 2,150 times

Last updated: Jul 31 '16