Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

cv::calcOpticalFlowPyrLK Assertion failed(line 1138)

The Question is related to my previous Question.

I ported the code of my Android Application which calculates the Optical Flow to a xCode console Project. There i get the same behaviour like in the Android app. The Recognition runs and gets good points, the first LK calculation also works fine and gives good results, but the second LK throws a Assertion Error (in my Application it just gives totally confusing Results (points which are not in the image anymore).

Here my Code (its a bit messy!):

std::cout << "_-_-_-_-_BruteForce KLT_-_-_-_-_" << std::endl;

cv::Mat nextImg = cv::imread(imgPathtest7, -1);
cv::Mat nextImgGry;

cvtColor(nextImg, nextImgGry, CV_BGR2GRAY);
cv::Mat downscaledGry;

cv::resize(nextImgGry, downscaledGry, cv::Size(nextImgGry.cols/4, nextImgGry.rows/4));
cv::resize(gry, gry, cv::Size(gry.cols/4, gry.rows/4));

std::vector<cv::Mat> prevPyr;
std::vector<cv::Mat> nextPyr;

cv::buildOpticalFlowPyramid(gry, prevPyr, cv::Size(8,8), 3);
cv::buildOpticalFlowPyramid(downscaledGry, nextPyr, cv::Size(8,8), 3);

//found points by recognition
for(int i = 0; i < ransacs.size(); i++)
    ransacs[i] *= 0.25;

std::vector<cv::Point2f> estPoints2;
std::vector<uchar> stat2;
std::vector<float> errs2;

cv::calcOpticalFlowPyrLK(prevPyr, nextPyr, ransacs, estPoints, stat, errs, cv::Size(8,8));

std::vector<cv::Point2f> estCorners;
std::vector<cv::Point2f> goodPoints;
std::vector<cv::Point2f> leftsrc;

for(int i = 0; i < estPoints2.size(); i++){
    if(errs[i] < 20.0f && stat[i]){
        cv::circle(nextImg, estPoints[i] *= 4, 5, cv::Scalar(0, 0, 255), 2);
        //std::cout << "Estimated Point:" << estPoint2[i] << std::endl;
        //std::cout << "Status: " << stat2[i] << std::endl;
        goodPoints.push_back(estPoints[i]);
        leftsrc.push_back(ransacs[i] *= 4);
    }
    //std::cout << "Error: " << errs[i] << std::endl;
}

//std::cout << "Size of ransacs/estPoints: " << leftsrc.size() << " " << goodPoints.size() << std::endl;

cv::Mat g = cv::findHomography(leftsrc, goodPoints);

//std::cout << "Homo: " << g << std::endl;

cv::perspectiveTransform(scene_corners, estCorners, g);

cv::line(nextImg, estCorners[0], estCorners[1], cv::Scalar(0, 255, 0), 4);
cv::line(nextImg, estCorners[1], estCorners[2], cv::Scalar(0, 255, 0), 4);
cv::line(nextImg, estCorners[2], estCorners[3], cv::Scalar(0, 255, 0), 4);
cv::line(nextImg, estCorners[3], estCorners[0], cv::Scalar(0, 255, 0), 4);

cv::imshow("First Iteration BruteForce", nextImg);

ransacs = goodPoints;
goodPoints.clear();
estPoints.clear();
leftsrc.clear();
scene_corners = estCorners;
estCorners.clear();
stat.clear();
errs.clear();

std::cout << "**** NEXT KLT TRACK ****" << std::endl;

//Second Image
cv::Mat nextMatchImg = cv::imread(imgPathtest6, -1);//STRING)
cv::Mat nextMatchGry;
cv::Mat nextsmallerMatchGry;

prevPyr.swap(nextPyr);
nextPyr.clear();

cvtColor(nextMatchImg, nextMatchGry, CV_BGR2GRAY);

for(int i = 0; i < ransacs.size(); i++){
    ransacs[i] *= 0.25;
}

cv::resize(nextMatchGry, nextsmallerMatchGry, cv::Size(nextMatchGry.cols/4, nextMatchGry.rows/4));

cv::buildOpticalFlowPyramid(nextsmallerMatchGry, nextPyr, cv::Size(8,8), 3);

//Assertion thrown here!
cv::calcOpticalFlowPyrLK(prevPyr, nextPyr, ransacs, estPoints, stat, errs);

for(int i = 0; i < estPoints.size(); i++){
    if(errs[i] < 20.0f && stat[i]){
        cv::circle(nextMatchImg, estPoints[i] *=4, 5, cv::Scalar(0, 0, 255), 2);
        //std::cout << "Estimated Point:" << estPoints[i] << std::endl;
        //std::cout << "Status: " << stat[i] << std::endl;
        goodPoints.push_back(estPoints[i]);
        leftsrc.push_back(ransacs[i]*=4);
    }
    //std::cout << "Error: " << errs[i] << std::endl;
}

//std::cout << "Size of ransacs/estimatedPoints: " << leftsrc.size() << " " << goodPoints.size() << std::endl;


cv::Mat g2 = cv::findHomography(leftsrc, goodPoints);

//std::cout << "Homo: " << g2 << std::endl;


cv::perspectiveTransform(scene_corners, estCorners, g2);

cv::line(nextMatchImg, estCorners[0], estCorners[1], cv::Scalar(0, 255, 0), 4);
cv::line(nextMatchImg, estCorners[1], estCorners[2], cv::Scalar(0, 255, 0), 4);
cv::line(nextMatchImg, estCorners[2], estCorners[3], cv::Scalar(0, 255, 0), 4);
cv::line(nextMatchImg, estCorners[3], estCorners[0], cv::Scalar(0, 255, 0), 4);

cv::imshow("Example KLT Bruteforce", nextMatchImg2);

It was intended to be seperated and not in a loop for debugging and testing reasons. So the Assertion I get is the following:

OpenCV Error: Assertion failed (ofs.x >= winSize.width && ofs.y >= winSize.height && ofs.x + prevPyr[lvlStep1].cols + winSize.width <= fullSize.width && ofs.y + prevPyr[lvlStep1].rows + winSize.height <= fullSize.height) in calc

And the Program stops with an uncaught Exception. Whats the problem here? or why does this Assertion fails?