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

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

9 10
using namespace cv;
using namespace std;
11

12
static void help()
Gary Bradski's avatar
Gary Bradski committed
13 14 15
{
    // print a welcome message, and the OpenCV version
    cout << "\nThis is a demo of Lukas-Kanade optical flow lkdemo(),\n"
16 17
            "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
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
    if( event == EVENT_LBUTTONDOWN )
32
    {
33
        point = Point2f((float)x, (float)y);
34
        addRemovePt = true;
35 36 37 38 39
    }
}

int main( int argc, char** argv )
{
40
    VideoCapture cap;
41
    TermCriteria termcrit(TermCriteria::COUNT|TermCriteria::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 49
    help();
    cv::CommandLineParser parser(argc, argv, "{@input|0|}");
ValeryTyumen's avatar
ValeryTyumen committed
50
    string input = parser.get<string>("@input");
51 52

    if( input.size() == 1 && isdigit(input[0]) )
ValeryTyumen's avatar
ValeryTyumen committed
53 54 55
        cap.open(input[0] - '0');
    else
        cap.open(input);
56

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

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

66
    Mat gray, prevGray, image, frame;
67
    vector<Point2f> points[2];
68

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

75
        frame.copyTo(image);
76
        cvtColor(image, gray, COLOR_BGR2GRAY);
77

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

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

                if( !status[i] )
                    continue;

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

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

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

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

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

    return 0;
}