Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

lkdemo.cpp crashing upon cornerSubPix() and calcOpticalFlowPyrLK() call

Hello there,

I'm new to this forum, new to opencv and not that experienced as a C++ programmer, so please bear with me for a few seconds..

I recently tried to run the lkdemo.cpp (code below with comments for your convenience)


#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

#include <iostream>
#include <ctype.h>

using namespace cv;
using namespace std;

static void help()
{
    // print a welcome message, and the OpenCV version
    cout << "\nThis is a demo of Lukas-Kanade optical flow lkdemo(),\n"
            "Using OpenCV version " << CV_VERSION << endl;
    cout << "\nIt uses camera by default, but you can provide a path to video as an argument.\n";
    cout << "\nHot keys: \n"
            "\tESC - quit the program\n"
            "\tr - auto-initialize tracking\n"
            "\tc - delete all the points\n"
            "\tn - switch the \"night\" mode on/off\n"
            "To add/remove a feature point click it\n" << endl;
}

Point2f point;
bool addRemovePt = false;

static void onMouse( int event, int x, int y, int /*flags*/, void* /*param*/ )
{
    if( event == CV_EVENT_LBUTTONDOWN )
    {
        point = Point2f((float)x, (float)y);
        addRemovePt = true;
    }
}

int main( int argc, char** argv )
{
    help();

    VideoCapture cap;
    TermCriteria termcrit(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 20, 0.03);
    Size subPixWinSize(10,10), winSize(31,31);

    const int MAX_COUNT = 500;
    bool needToInit = false;
    bool nightMode = false;

    if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
        cap.open(argc == 2 ? argv[1][0] - '0' : 0);
    else if( argc == 2 )
        cap.open(argv[1]);

    if( !cap.isOpened() )
    {
        cout << "Could not initialize capturing...\n";
        return 0;
    }

    namedWindow( "LK Demo", 1 );
    setMouseCallback( "LK Demo", onMouse, 0 );

    Mat gray, prevGray, image;
    vector<point2f> points[2];

    for(;;) // <------------------------- THE FOR LOOP
    {
        Mat frame;
        cap >> frame;
        if( frame.empty() )
            break;

        frame.copyTo(image);
        cvtColor(image, gray, COLOR_BGR2GRAY);

        if( nightMode )
            image = Scalar::all(0);

        if( needToInit )
        {
            // automatic initialization
            goodFeaturesToTrack(gray, points[1], MAX_COUNT, 0.01, 10, Mat(), 3, 0, 0.04);
            cornerSubPix(gray, points[1], subPixWinSize, Size(-1,-1), termcrit);
            addRemovePt = false;
        }
        else if( !points[0].empty() )
        {
            vector<uchar> status;
            vector<float> err;
            if(prevGray.empty())
                gray.copyTo(prevGray);
// ==================================== CRASH B ================================================
            calcOpticalFlowPyrLK(prevGray, gray, points[0], points[1], status, err, winSize,
                                 3, termcrit, 0, 0.001);
            size_t i, k;
            for( i = k = 0; i < points[1].size(); i++ )
            {
                if( addRemovePt )
                {
                    if( norm(point - points[1][i]) <= 5 )
                    {
                        addRemovePt = false;
                        continue;
                    }
                }

                if( !status[i] )
                    continue;

                points[1][k++] = points[1][i];
                circle( image, points[1][i], 3, Scalar(0,255,0), -1, 8);
            }
            points[1].resize(k);
        }

        if( addRemovePt && points[1].size() < (size_t)MAX_COUNT )
        {
            vector<point2f> tmp;
            tmp.push_back(point);
// ===================================== CRASH A =================================================
            cornerSubPix( gray, tmp, winSize, cvSize(-1,-1), termcrit);
            points[1].push_back(tmp[0]);
            addRemovePt = false;
        }

        needToInit = false;
        imshow("LK Demo", image); // <--------------------------- imshow()

        char c = (char)waitKey(10);
        if( c == 27 )
            break;
        switch( c )
        {
        case 'r':
            needToInit = true;
            break;
        case 'c':
            points[0].clear();
            points[1].clear();
            break;
        case 'n':
            nightMode = !nightMode;
            break;
        }

        std::swap(points[1], points[0]);
        cv::swap(prevGray, gray);
    }

    return 0;
}

I am using Visual Studio 2013, and the program compiles fine. When I tried to run the code, the first thing that went wrong was that "imshow()" would create a new window for each iteration within the loop. So, instead of rewriting over the previous image, imshow would create a new window entirely. I was able to solve this problem by creating a "string" containing the window name "LK DEMO" and replacing the name with the string variable in all functions.

Now onto the big problem. Once I had the video up and running I tried clicking within the window to add a point for the program to track... CRASH! After commenting things in and out and using several "cout" statements, I finally found the place/s were the program crash, which as the name suggests, are the calls to the function cornerSubPix() and calcOpticalFlowPyrLK(). I labeled the places where these are called in the code above as CRASH A and CRASH B.

Once I reach CRASH A, I get the following warning in the screen: image description

And following this I press "Break" and the screen shows the following code (were I am assuming the code stopped)

image description

Finally, after commenting "cornerSubPix()" I ran the program and a similar thing happened. This time, however, the program crashed when calling "calcOpticalFlowPyrLK()", CRASH B in the code above: image description

The following screen appeared after pressing "Break"

image description

Now I am stuck and I don't even know how to proceed to solve this issue.. any suggestions would be greatly appreciated!

Thank you in advance!

Best, MrRed

lkdemo.cpp crashing upon cornerSubPix() and calcOpticalFlowPyrLK() call

Hello there,

I'm new to this forum, new to opencv and not that experienced as a C++ programmer, so please bear with me for a few seconds..

I recently tried to run the lkdemo.cpp (code below with comments for your convenience)


#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

#include <iostream>
#include <ctype.h>

using namespace cv;
using namespace std;

static void help()
{
    // print a welcome message, and the OpenCV version
    cout << "\nThis is a demo of Lukas-Kanade optical flow lkdemo(),\n"
            "Using OpenCV version " << CV_VERSION << endl;
    cout << "\nIt uses camera by default, but you can provide a path to video as an argument.\n";
    cout << "\nHot keys: \n"
            "\tESC - quit the program\n"
            "\tr - auto-initialize tracking\n"
            "\tc - delete all the points\n"
            "\tn - switch the \"night\" mode on/off\n"
            "To add/remove a feature point click it\n" << endl;
}

Point2f point;
bool addRemovePt = false;

static void onMouse( int event, int x, int y, int /*flags*/, void* /*param*/ )
{
    if( event == CV_EVENT_LBUTTONDOWN )
    {
        point = Point2f((float)x, (float)y);
        addRemovePt = true;
    }
}

int main( int argc, char** argv )
{
    help();

    VideoCapture cap;
    TermCriteria termcrit(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 20, 0.03);
    Size subPixWinSize(10,10), winSize(31,31);

    const int MAX_COUNT = 500;
    bool needToInit = false;
    bool nightMode = false;

    if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
        cap.open(argc == 2 ? argv[1][0] - '0' : 0);
    else if( argc == 2 )
        cap.open(argv[1]);

    if( !cap.isOpened() )
    {
        cout << "Could not initialize capturing...\n";
        return 0;
    }

    namedWindow( "LK Demo", 1 );
    setMouseCallback( "LK Demo", onMouse, 0 );

    Mat gray, prevGray, image;
    vector<point2f> points[2];

    for(;;) // <------------------------- THE FOR LOOP
    {
        Mat frame;
        cap >> frame;
        if( frame.empty() )
            break;

        frame.copyTo(image);
        cvtColor(image, gray, COLOR_BGR2GRAY);

        if( nightMode )
            image = Scalar::all(0);

        if( needToInit )
        {
            // automatic initialization
            goodFeaturesToTrack(gray, points[1], MAX_COUNT, 0.01, 10, Mat(), 3, 0, 0.04);
            cornerSubPix(gray, points[1], subPixWinSize, Size(-1,-1), termcrit);
            addRemovePt = false;
        }
        else if( !points[0].empty() )
        {
            vector<uchar> status;
            vector<float> err;
            if(prevGray.empty())
                gray.copyTo(prevGray);
// ==================================== CRASH B ================================================
            calcOpticalFlowPyrLK(prevGray, gray, points[0], points[1], status, err, winSize,
                                 3, termcrit, 0, 0.001);
            size_t i, k;
            for( i = k = 0; i < points[1].size(); i++ )
            {
                if( addRemovePt )
                {
                    if( norm(point - points[1][i]) <= 5 )
                    {
                        addRemovePt = false;
                        continue;
                    }
                }

                if( !status[i] )
                    continue;

                points[1][k++] = points[1][i];
                circle( image, points[1][i], 3, Scalar(0,255,0), -1, 8);
            }
            points[1].resize(k);
        }

        if( addRemovePt && points[1].size() < (size_t)MAX_COUNT )
        {
            vector<point2f> tmp;
            tmp.push_back(point);
// ===================================== CRASH A =================================================
            cornerSubPix( gray, tmp, winSize, cvSize(-1,-1), termcrit);
            points[1].push_back(tmp[0]);
            addRemovePt = false;
        }

        needToInit = false;
        imshow("LK Demo", image); // <--------------------------- imshow()

        char c = (char)waitKey(10);
        if( c == 27 )
            break;
        switch( c )
        {
        case 'r':
            needToInit = true;
            break;
        case 'c':
            points[0].clear();
            points[1].clear();
            break;
        case 'n':
            nightMode = !nightMode;
            break;
        }

        std::swap(points[1], points[0]);
        cv::swap(prevGray, gray);
    }

    return 0;
}

I am using Visual Studio 2013, and the program compiles fine. When I tried to run the code, the first thing that went wrong was that "imshow()" would create a new window for each iteration within the loop. So, instead of rewriting over the previous image, imshow would create a new window entirely. I was able to solve this problem by creating a "string" containing the window name "LK DEMO" and replacing the name with the string variable in all functions.

Now onto the big problem. Once I had the video up and running I tried clicking within the window to add a point for the program to track... CRASH! After commenting things in and out out, and using several "cout" statements, statements to pinpoint the troublemaker within the code, I finally found the place/s were the program crash, which would crash (which as the name suggests, suggests) are the calls to the function cornerSubPix() and calcOpticalFlowPyrLK(). I labeled the places where these are called in the code above as CRASH A and CRASH B.

Once I reach CRASH A, I get the following warning in the screen: image description

And following this I press "Break" and the screen shows the following code (were I am assuming the code stopped)

image description

Finally, after commenting "cornerSubPix()" I ran the program and a similar thing happened. This time, however, the program crashed when calling "calcOpticalFlowPyrLK()", CRASH B in the code above: image description

The following screen appeared after pressing "Break"

image description

Now I am stuck and I don't even know how to proceed to solve this issue.. any suggestions would be greatly appreciated!

Thank you in advance!

Best, MrRed

lkdemo.cpp crashing upon cornerSubPix() and calcOpticalFlowPyrLK() call

Hello there,

I'm new to this forum, new to opencv and not that experienced as a C++ programmer, so please bear with me for a few seconds..

I recently tried to run the lkdemo.cpp (code below with comments for your convenience)


#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

#include <iostream>
#include <ctype.h>

using namespace cv;
using namespace std;

static void help()
{
    // print a welcome message, and the OpenCV version
    cout << "\nThis is a demo of Lukas-Kanade optical flow lkdemo(),\n"
            "Using OpenCV version " << CV_VERSION << endl;
    cout << "\nIt uses camera by default, but you can provide a path to video as an argument.\n";
    cout << "\nHot keys: \n"
            "\tESC - quit the program\n"
            "\tr - auto-initialize tracking\n"
            "\tc - delete all the points\n"
            "\tn - switch the \"night\" mode on/off\n"
            "To add/remove a feature point click it\n" << endl;
}

Point2f point;
bool addRemovePt = false;

static void onMouse( int event, int x, int y, int /*flags*/, void* /*param*/ )
{
    if( event == CV_EVENT_LBUTTONDOWN )
    {
        point = Point2f((float)x, (float)y);
        addRemovePt = true;
    }
}

int main( int argc, char** argv )
{
    help();

    VideoCapture cap;
    TermCriteria termcrit(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 20, 0.03);
    Size subPixWinSize(10,10), winSize(31,31);

    const int MAX_COUNT = 500;
    bool needToInit = false;
    bool nightMode = false;

    if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
        cap.open(argc == 2 ? argv[1][0] - '0' : 0);
    else if( argc == 2 )
        cap.open(argv[1]);

    if( !cap.isOpened() )
    {
        cout << "Could not initialize capturing...\n";
        return 0;
    }

    namedWindow( "LK Demo", 1 );
    setMouseCallback( "LK Demo", onMouse, 0 );

    Mat gray, prevGray, image;
    vector<point2f> points[2];

    for(;;) // <------------------------- THE FOR LOOP
    {
        Mat frame;
        cap >> frame;
        if( frame.empty() )
            break;

        frame.copyTo(image);
        cvtColor(image, gray, COLOR_BGR2GRAY);

        if( nightMode )
            image = Scalar::all(0);

        if( needToInit )
        {
            // automatic initialization
            goodFeaturesToTrack(gray, points[1], MAX_COUNT, 0.01, 10, Mat(), 3, 0, 0.04);
            cornerSubPix(gray, points[1], subPixWinSize, Size(-1,-1), termcrit);
            addRemovePt = false;
        }
        else if( !points[0].empty() )
        {
            vector<uchar> status;
            vector<float> err;
            if(prevGray.empty())
                gray.copyTo(prevGray);
// ==================================== CRASH B ================================================
            calcOpticalFlowPyrLK(prevGray, gray, points[0], points[1], status, err, winSize,
                                 3, termcrit, 0, 0.001);
            size_t i, k;
            for( i = k = 0; i < points[1].size(); i++ )
            {
                if( addRemovePt )
                {
                    if( norm(point - points[1][i]) <= 5 )
                    {
                        addRemovePt = false;
                        continue;
                    }
                }

                if( !status[i] )
                    continue;

                points[1][k++] = points[1][i];
                circle( image, points[1][i], 3, Scalar(0,255,0), -1, 8);
            }
            points[1].resize(k);
        }

        if( addRemovePt && points[1].size() < (size_t)MAX_COUNT )
        {
            vector<point2f> tmp;
            tmp.push_back(point);
// ===================================== CRASH A =================================================
            cornerSubPix( gray, tmp, winSize, cvSize(-1,-1), termcrit);
            points[1].push_back(tmp[0]);
            addRemovePt = false;
        }

        needToInit = false;
        imshow("LK Demo", image); // <--------------------------- imshow()

        char c = (char)waitKey(10);
        if( c == 27 )
            break;
        switch( c )
        {
        case 'r':
            needToInit = true;
            break;
        case 'c':
            points[0].clear();
            points[1].clear();
            break;
        case 'n':
            nightMode = !nightMode;
            break;
        }

        std::swap(points[1], points[0]);
        cv::swap(prevGray, gray);
    }

    return 0;
}

I am using Visual Studio 2013, and the program compiles fine. When I tried to run the code, the first thing that went wrong was that "imshow()" would create a new window for each iteration within of the loop. So, instead of rewriting over the previous image, imshow would create a new window entirely. I was able to solve this problem by creating a "string" containing the window name "LK DEMO" and replacing the name with the string variable in all functions.

Now onto the big problem. Once I had the video up and running I tried clicking within the window to add a point for the program to track... CRASH! After commenting things in and out, and using several "cout" statements to pinpoint the troublemaker within the code, I finally found the place/s were the program would crash (which as the name suggests) are the calls to the function cornerSubPix() and calcOpticalFlowPyrLK(). I labeled the places where these are called in the code above as CRASH A and CRASH B.

Once I reach CRASH A, I get the following warning in the screen: image description

And following this I press "Break" and the screen shows the following code (were I am assuming the code stopped)

image description

Finally, after commenting "cornerSubPix()" I ran the program and a similar thing happened. This time, however, the program crashed when calling "calcOpticalFlowPyrLK()", CRASH B in the code above: image description

The following screen appeared after pressing "Break"

image description

Now I am stuck and I don't even know how to proceed to solve this issue.. any suggestions would be greatly appreciated!

Thank you in advance!

Best, MrRed

lkdemo.cpp crashing upon cornerSubPix() and calcOpticalFlowPyrLK() call

Hello there,

I'm new to this forum, new to opencv and not that experienced as a C++ programmer, so please bear with me for a few seconds..

I recently tried to run the lkdemo.cpp (code below with comments for your convenience)


#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

#include <iostream>
#include <ctype.h>

using namespace cv;
using namespace std;

static void help()
{
    // print a welcome message, and the OpenCV version
    cout << "\nThis is a demo of Lukas-Kanade optical flow lkdemo(),\n"
            "Using OpenCV version " << CV_VERSION << endl;
    cout << "\nIt uses camera by default, but you can provide a path to video as an argument.\n";
    cout << "\nHot keys: \n"
            "\tESC - quit the program\n"
            "\tr - auto-initialize tracking\n"
            "\tc - delete all the points\n"
            "\tn - switch the \"night\" mode on/off\n"
            "To add/remove a feature point click it\n" << endl;
}

Point2f point;
bool addRemovePt = false;

static void onMouse( int event, int x, int y, int /*flags*/, void* /*param*/ )
{
    if( event == CV_EVENT_LBUTTONDOWN )
    {
        point = Point2f((float)x, (float)y);
        addRemovePt = true;
    }
}

int main( int argc, char** argv )
{
    help();

    VideoCapture cap;
    TermCriteria termcrit(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 20, 0.03);
    Size subPixWinSize(10,10), winSize(31,31);

    const int MAX_COUNT = 500;
    bool needToInit = false;
    bool nightMode = false;

    if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
        cap.open(argc == 2 ? argv[1][0] - '0' : 0);
    else if( argc == 2 )
        cap.open(argv[1]);

    if( !cap.isOpened() )
    {
        cout << "Could not initialize capturing...\n";
        return 0;
    }

    namedWindow( "LK Demo", 1 );
    setMouseCallback( "LK Demo", onMouse, 0 );

    Mat gray, prevGray, image;
    vector<point2f> points[2];

    for(;;) // <------------------------- THE FOR LOOP
    {
        Mat frame;
        cap >> frame;
        if( frame.empty() )
            break;

        frame.copyTo(image);
        cvtColor(image, gray, COLOR_BGR2GRAY);

        if( nightMode )
            image = Scalar::all(0);

        if( needToInit )
        {
            // automatic initialization
            goodFeaturesToTrack(gray, points[1], MAX_COUNT, 0.01, 10, Mat(), 3, 0, 0.04);
            cornerSubPix(gray, points[1], subPixWinSize, Size(-1,-1), termcrit);
            addRemovePt = false;
        }
        else if( !points[0].empty() )
        {
            vector<uchar> status;
            vector<float> err;
            if(prevGray.empty())
                gray.copyTo(prevGray);
// ==================================== CRASH B ================================================
            calcOpticalFlowPyrLK(prevGray, gray, points[0], points[1], status, err, winSize,
                                 3, termcrit, 0, 0.001);
            size_t i, k;
            for( i = k = 0; i < points[1].size(); i++ )
            {
                if( addRemovePt )
                {
                    if( norm(point - points[1][i]) <= 5 )
                    {
                        addRemovePt = false;
                        continue;
                    }
                }

                if( !status[i] )
                    continue;

                points[1][k++] = points[1][i];
                circle( image, points[1][i], 3, Scalar(0,255,0), -1, 8);
            }
            points[1].resize(k);
        }

        if( addRemovePt && points[1].size() < (size_t)MAX_COUNT )
        {
            vector<point2f> tmp;
            tmp.push_back(point);
// ===================================== CRASH A =================================================
            cornerSubPix( gray, tmp, winSize, cvSize(-1,-1), termcrit);
            points[1].push_back(tmp[0]);
            addRemovePt = false;
        }

        needToInit = false;
        imshow("LK Demo", image); // <--------------------------- imshow()

        char c = (char)waitKey(10);
        if( c == 27 )
            break;
        switch( c )
        {
        case 'r':
            needToInit = true;
            break;
        case 'c':
            points[0].clear();
            points[1].clear();
            break;
        case 'n':
            nightMode = !nightMode;
            break;
        }

        std::swap(points[1], points[0]);
        cv::swap(prevGray, gray);
    }

    return 0;
}

I am using Visual Studio 2013, and the program compiles fine. When I tried to run the code, the first thing that went wrong was that "imshow()" would create a new window for each iteration of the loop. So, instead of rewriting over the previous image, imshow would create a new window entirely. I was able to solve this problem by creating a "string" containing the window name "LK DEMO" and replacing the name with the string variable in all functions.

Now onto the big problem. Once I had the video up and running I tried clicking within the window to add a point for the program to track... CRASH! After commenting things in and out, and using several "cout" statements to pinpoint the troublemaker within the code, I finally found the place/s were the program would crash (which as the name suggests) are the calls to the function cornerSubPix() and calcOpticalFlowPyrLK(). I labeled the places where these are called in the code above as CRASH A and CRASH B.

Once I reach CRASH A, I get the following warning in the screen: image description

And following this I press "Break" and the screen shows the following code (were (where I am assuming the code stopped)

image description

Finally, after commenting "cornerSubPix()" I ran the program and a similar thing happened. This time, however, the program crashed when calling "calcOpticalFlowPyrLK()", CRASH B in the code above: image description

The following screen appeared after pressing "Break"

image description

Now I am stuck and I don't even know how to proceed to solve this issue.. any suggestions would be greatly appreciated!

Thank you in advance!

Best, MrRed

lkdemo.cpp crashing upon cornerSubPix() and calcOpticalFlowPyrLK() call

Hello there,

I'm new to this forum, new to opencv and not that experienced as a C++ programmer, so please bear with me for a few seconds..

I recently tried to run the lkdemo.cpp (code below with comments for your convenience)


#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

#include <iostream>
#include <ctype.h>

using namespace cv;
using namespace std;

static void help()
{
    // print a welcome message, and the OpenCV version
    cout << "\nThis is a demo of Lukas-Kanade optical flow lkdemo(),\n"
            "Using OpenCV version " << CV_VERSION << endl;
    cout << "\nIt uses camera by default, but you can provide a path to video as an argument.\n";
    cout << "\nHot keys: \n"
            "\tESC - quit the program\n"
            "\tr - auto-initialize tracking\n"
            "\tc - delete all the points\n"
            "\tn - switch the \"night\" mode on/off\n"
            "To add/remove a feature point click it\n" << endl;
}

Point2f point;
bool addRemovePt = false;

static void onMouse( int event, int x, int y, int /*flags*/, void* /*param*/ )
{
    if( event == CV_EVENT_LBUTTONDOWN )
    {
        point = Point2f((float)x, (float)y);
        addRemovePt = true;
    }
}

int main( int argc, char** argv )
{
    help();

    VideoCapture cap;
    TermCriteria termcrit(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 20, 0.03);
    Size subPixWinSize(10,10), winSize(31,31);

    const int MAX_COUNT = 500;
    bool needToInit = false;
    bool nightMode = false;

    if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
        cap.open(argc == 2 ? argv[1][0] - '0' : 0);
    else if( argc == 2 )
        cap.open(argv[1]);

    if( !cap.isOpened() )
    {
        cout << "Could not initialize capturing...\n";
        return 0;
    }

    namedWindow( "LK Demo", 1 );
    setMouseCallback( "LK Demo", onMouse, 0 );

    Mat gray, prevGray, image;
    vector<point2f> points[2];

    for(;;) // <------------------------- THE FOR LOOP
    {
        Mat frame;
        cap >> frame;
        if( frame.empty() )
            break;

        frame.copyTo(image);
        cvtColor(image, gray, COLOR_BGR2GRAY);

        if( nightMode )
            image = Scalar::all(0);

        if( needToInit )
        {
            // automatic initialization
            goodFeaturesToTrack(gray, points[1], MAX_COUNT, 0.01, 10, Mat(), 3, 0, 0.04);
            cornerSubPix(gray, points[1], subPixWinSize, Size(-1,-1), termcrit);
            addRemovePt = false;
        }
        else if( !points[0].empty() )
        {
            vector<uchar> status;
            vector<float> err;
            if(prevGray.empty())
                gray.copyTo(prevGray);
// ==================================== CRASH B ================================================
            calcOpticalFlowPyrLK(prevGray, gray, points[0], points[1], status, err, winSize,
                                 3, termcrit, 0, 0.001);
            size_t i, k;
            for( i = k = 0; i < points[1].size(); i++ )
            {
                if( addRemovePt )
                {
                    if( norm(point - points[1][i]) <= 5 )
                    {
                        addRemovePt = false;
                        continue;
                    }
                }

                if( !status[i] )
                    continue;

                points[1][k++] = points[1][i];
                circle( image, points[1][i], 3, Scalar(0,255,0), -1, 8);
            }
            points[1].resize(k);
        }

        if( addRemovePt && points[1].size() < (size_t)MAX_COUNT )
        {
            vector<point2f> tmp;
            tmp.push_back(point);
// ===================================== CRASH A =================================================
            cornerSubPix( gray, tmp, winSize, cvSize(-1,-1), termcrit);
            points[1].push_back(tmp[0]);
            addRemovePt = false;
        }

        needToInit = false;
        imshow("LK Demo", image); // <--------------------------- imshow()

        char c = (char)waitKey(10);
        if( c == 27 )
            break;
        switch( c )
        {
        case 'r':
            needToInit = true;
            break;
        case 'c':
            points[0].clear();
            points[1].clear();
            break;
        case 'n':
            nightMode = !nightMode;
            break;
        }

        std::swap(points[1], points[0]);
        cv::swap(prevGray, gray);
    }

    return 0;
}

I am using Visual Studio 2013, and the program compiles fine. When I tried to run the code, the first thing that went wrong was that "imshow()" would create a new window for each iteration of the loop. So, instead of rewriting over the previous image, imshow would create a new window entirely. I was able to solve this problem by creating a "string" containing the window name "LK DEMO" and replacing the name with the string variable in all functions.

Now onto the big problem. Once I had the video up and running I tried clicking within the window to add a point for the program to track... CRASH! After commenting things in and out, and using several "cout" statements to pinpoint the troublemaker within the code, I finally found the place/s were the program would crash (which as the name suggests) are the calls to the function cornerSubPix() and calcOpticalFlowPyrLK(). I labeled the places where these are called in the code above as CRASH A and CRASH B.

Once I reach CRASH A, I get the following warning in the screen: image description

And following this I press "Break" and the screen shows the following code (where I am assuming the code stopped)

image description

Finally, after commenting "cornerSubPix()" I ran the program and a similar thing happened. This time, however, the program crashed when calling "calcOpticalFlowPyrLK()", CRASH B in the code above: image description

The following screen appeared after pressing "Break"

image description

Now I am stuck and I don't even know how to proceed to solve this issue.. any suggestions would be greatly appreciated!

Thank you in advance!

Best, MrRed

lkdemo.cpp crashing upon cornerSubPix() and calcOpticalFlowPyrLK() call

Hello there,

I'm new to this forum, new to opencv and not that experienced as a C++ programmer, so please bear with me for a few seconds..

I recently tried to run the lkdemo.cpp (code below with comments for your convenience)


#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

#include <iostream>
#include <ctype.h>

using namespace cv;
using namespace std;

static void help()
{
    // print a welcome message, and the OpenCV version
    cout << "\nThis is a demo of Lukas-Kanade optical flow lkdemo(),\n"
            "Using OpenCV version " << CV_VERSION << endl;
    cout << "\nIt uses camera by default, but you can provide a path to video as an argument.\n";
    cout << "\nHot keys: \n"
            "\tESC - quit the program\n"
            "\tr - auto-initialize tracking\n"
            "\tc - delete all the points\n"
            "\tn - switch the \"night\" mode on/off\n"
            "To add/remove a feature point click it\n" << endl;
}

Point2f point;
bool addRemovePt = false;

static void onMouse( int event, int x, int y, int /*flags*/, void* /*param*/ )
{
    if( event == CV_EVENT_LBUTTONDOWN )
    {
        point = Point2f((float)x, (float)y);
        addRemovePt = true;
    }
}

int main( int argc, char** argv )
{
    help();

    VideoCapture cap;
    TermCriteria termcrit(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 20, 0.03);
    Size subPixWinSize(10,10), winSize(31,31);
winSize(31,31); // <------------------------------ subPixWinSize

    const int MAX_COUNT = 500;
    bool needToInit = false;
    bool nightMode = false;

    if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
        cap.open(argc == 2 ? argv[1][0] - '0' : 0);
    else if( argc == 2 )
        cap.open(argv[1]);

    if( !cap.isOpened() )
    {
        cout << "Could not initialize capturing...\n";
        return 0;
    }

    namedWindow( "LK Demo", 1 );
    setMouseCallback( "LK Demo", onMouse, 0 );

    Mat gray, prevGray, image;
    vector<point2f> points[2];

    for(;;) // <------------------------- THE FOR LOOP
    {
        Mat frame;
        cap >> frame;
        if( frame.empty() )
            break;

        frame.copyTo(image);
        cvtColor(image, gray, COLOR_BGR2GRAY);

        if( nightMode )
            image = Scalar::all(0);

        if( needToInit )
        {
            // automatic initialization
            goodFeaturesToTrack(gray, points[1], MAX_COUNT, 0.01, 10, Mat(), 3, 0, 0.04);
            cornerSubPix(gray, points[1], subPixWinSize, Size(-1,-1), termcrit);
            addRemovePt = false;
        }
        else if( !points[0].empty() )
        {
            vector<uchar> status;
            vector<float> err;
            if(prevGray.empty())
                gray.copyTo(prevGray);
// ==================================== CRASH B ================================================
            calcOpticalFlowPyrLK(prevGray, gray, points[0], points[1], status, err, winSize,
                                 3, termcrit, 0, 0.001);
            size_t i, k;
            for( i = k = 0; i < points[1].size(); i++ )
            {
                if( addRemovePt )
                {
                    if( norm(point - points[1][i]) <= 5 )
                    {
                        addRemovePt = false;
                        continue;
                    }
                }

                if( !status[i] )
                    continue;

                points[1][k++] = points[1][i];
                circle( image, points[1][i], 3, Scalar(0,255,0), -1, 8);
            }
            points[1].resize(k);
        }

        if( addRemovePt && points[1].size() < (size_t)MAX_COUNT )
        {
            vector<point2f> tmp;
            tmp.push_back(point);
// ===================================== CRASH A =================================================
            cornerSubPix( gray, tmp, winSize, cvSize(-1,-1), termcrit);
            points[1].push_back(tmp[0]);
            addRemovePt = false;
        }

        needToInit = false;
        imshow("LK Demo", image); // <--------------------------- imshow()

        char c = (char)waitKey(10);
        if( c == 27 )
            break;
        switch( c )
        {
        case 'r':
            needToInit = true;
            break;
        case 'c':
            points[0].clear();
            points[1].clear();
            break;
        case 'n':
            nightMode = !nightMode;
            break;
        }

        std::swap(points[1], points[0]);
        cv::swap(prevGray, gray);
    }

    return 0;
}

I am using Visual Studio 2013, and the program compiles fine. When I tried to run the code, the first thing that went wrong was that "imshow()" would create a new window for each iteration of the loop. So, instead of rewriting over the previous image, imshow would create a new window entirely. I was able to solve this problem by creating a "string" containing the window name "LK DEMO" and replacing the name with the string variable in all functions.

Now onto the big problem. Once I had the video up and running I tried clicking within the window to add a point for the program to track... CRASH! After commenting things in and out, and using several "cout" statements to pinpoint the troublemaker within the code, I finally found the place/s were the program would crash (which as the name suggests) are the calls to the function cornerSubPix() and calcOpticalFlowPyrLK(). I labeled the places where these are called in the code above as CRASH A and CRASH B.

Once I reach CRASH A, I get the following warning in the screen: image description

And following this I press "Break" and the screen shows the following code (where I am assuming the code stopped)

image description

Finally, after commenting "cornerSubPix()" I ran the program and a similar thing happened. This time, however, the program crashed when calling "calcOpticalFlowPyrLK()", CRASH B in the code above: image description

The following screen appeared after pressing "Break"

image description

Now I am stuck and I don't even know how to proceed to solve this issue.. any suggestions would be greatly appreciated!

Thank you in advance!

Best, MrRed