lkdemo.cpp 3.94 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 " << CV_VERSION << endl;
    cout << "\nIt uses camera by default, but you can provide a path to video as an argument.\n";
Gary Bradski's avatar
Gary Bradski committed
17 18 19 20 21 22 23 24
    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;
}

25
Point2f point;
26
bool addRemovePt = false;
27

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

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

41
    VideoCapture cap;
42
    TermCriteria termcrit(TermCriteria::COUNT|TermCriteria::EPS,20,0.03);
43
    Size subPixWinSize(10,10), winSize(31,31);
44

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

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

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

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

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

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

73
        frame.copyTo(image);
74
        cvtColor(image, gray, COLOR_BGR2GRAY);
75

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

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

                if( !status[i] )
                    continue;

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

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

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

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

144
        std::swap(points[1], points[0]);
145
        cv::swap(prevGray, gray);
146 147 148 149
    }

    return 0;
}