lkdemo.cpp 3.86 KB
Newer Older
1
#include "opencv2/video/tracking.hpp"
2
#include "opencv2/imgproc/imgproc.hpp"
3
#include "opencv2/highgui/highgui.hpp"
4

5
#include <iostream>
6 7
#include <ctype.h>

8 9
using namespace cv;
using namespace std;
10

11
static void help()
Gary Bradski's avatar
Gary Bradski committed
12 13 14
{
    // print a welcome message, and the OpenCV version
    cout << "\nThis is a demo of Lukas-Kanade optical flow lkdemo(),\n"
15 16
            "Using OpenCV version %s\n" << CV_VERSION << "\n"
            << endl;
Gary Bradski's avatar
Gary Bradski committed
17 18 19 20 21 22 23 24 25

    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;
}

26
Point2f point;
27
bool addRemovePt = false;
28

29
static void onMouse( int event, int x, int y, int /*flags*/, void* /*param*/ )
30 31 32
{
    if( event == CV_EVENT_LBUTTONDOWN )
    {
33
        point = Point2f((float)x,(float)y);
34
        addRemovePt = true;
35 36 37 38 39
    }
}

int main( int argc, char** argv )
{
40 41
    VideoCapture cap;
    TermCriteria termcrit(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.03);
42
    Size subPixWinSize(10,10), winSize(31,31);
43

44 45 46
    const int MAX_COUNT = 500;
    bool needToInit = false;
    bool nightMode = false;
47

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

53
    if( !cap.isOpened() )
54
    {
55 56
        cout << "Could not initialize capturing...\n";
        return 0;
57 58
    }

Gary Bradski's avatar
Gary Bradski committed
59
    help();
60

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

64 65
    Mat gray, prevGray, image;
    vector<Point2f> points[2];
66

67 68
    for(;;)
    {
69 70 71
        Mat frame;
        cap >> frame;
        if( frame.empty() )
72 73
            break;

74
        frame.copyTo(image);
75
        cvtColor(image, gray, CV_BGR2GRAY);
76

77 78
        if( nightMode )
            image = Scalar::all(0);
79

80
        if( needToInit )
81
        {
82 83
            // automatic initialization
            goodFeaturesToTrack(gray, points[1], MAX_COUNT, 0.01, 10, Mat(), 3, 0, 0.04);
84
            cornerSubPix(gray, points[1], subPixWinSize, Size(-1,-1), termcrit);
85
            addRemovePt = false;
86
        }
87
        else if( !points[0].empty() )
88
        {
89 90 91 92 93
            vector<uchar> status;
            vector<float> err;
            if(prevGray.empty())
                gray.copyTo(prevGray);
            calcOpticalFlowPyrLK(prevGray, gray, points[0], points[1], status, err, winSize,
94
                                 3, termcrit, 0, 0.001);
95 96
            size_t i, k;
            for( i = k = 0; i < points[1].size(); i++ )
97
            {
98
                if( addRemovePt )
99
                {
100
                    if( norm(point - points[1][i]) <= 5 )
101
                    {
102
                        addRemovePt = false;
103 104 105 106 107 108 109 110
                        continue;
                    }
                }

                if( !status[i] )
                    continue;

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

116
        if( addRemovePt && points[1].size() < (size_t)MAX_COUNT )
117
        {
118
            vector<Point2f> tmp;
119
            tmp.push_back(point);
120 121 122
            cornerSubPix( gray, tmp, winSize, cvSize(-1,-1), termcrit);
            points[1].push_back(tmp[0]);
            addRemovePt = false;
123 124
        }

125 126
        needToInit = false;
        imshow("LK Demo", image);
127

128 129
        char c = (char)waitKey(10);
        if( c == 27 )
130
            break;
131
        switch( c )
132 133
        {
        case 'r':
134
            needToInit = true;
135 136
            break;
        case 'c':
137
            points[1].clear();
138 139
            break;
        case 'n':
140
            nightMode = !nightMode;
141 142 143 144
            break;
        default:
            ;
        }
145

146 147
        std::swap(points[1], points[0]);
        swap(prevGray, gray);
148 149 150 151
    }

    return 0;
}