cv::calcOpticalFlowPyrLK Assertion failed(line 1138) [closed]

asked 2016-11-30 05:38:19 -0600

Vintez gravatar image

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 ...
(more)
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Vintez
close date 2016-12-01 02:35:30.255999

Comments

Try making the cv::Size() for your optical flow an odd number.

Tetragramm gravatar imageTetragramm ( 2016-11-30 12:29:04 -0600 )edit

Well my fault was, that I left the windowSize at the second opticalFlow as default, so it was bigger than the windowSize of the buildOpticalFlowPyr. Nevertheless I tried to insert your suggestion into my Android Application, which did not help to solve the Error :/

Vintez gravatar imageVintez ( 2016-12-01 02:35:13 -0600 )edit