lkdemo.cpp 4.12 KB
Newer Older
1
#include "opencv2/video/tracking.hpp"
2
#include "opencv2/imgproc/imgproc.hpp"
3
#include "opencv2/videoio/videoio.hpp"
4
#include "opencv2/highgui/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

ValeryTyumen's avatar
ValeryTyumen committed
48 49 50 51 52 53 54 55 56 57 58 59 60
    cv::CommandLineParser parser(argc, argv, "{@input||}{help h||}");
    string input = parser.get<string>("@input");
    if (parser.has("help"))
    {
        help();
        return 0;
    }
    if( input.empty() )
        cap.open(0);
    else if( input.size() == 1 && isdigit(input[0]) )
        cap.open(input[0] - '0');
    else
        cap.open(input);
61

62
    if( !cap.isOpened() )
63
    {
64 65
        cout << "Could not initialize capturing...\n";
        return 0;
66 67
    }

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

71
    Mat gray, prevGray, image, frame;
72
    vector<Point2f> points[2];
73

74 75
    for(;;)
    {
76 77
        cap >> frame;
        if( frame.empty() )
78 79
            break;

80
        frame.copyTo(image);
81
        cvtColor(image, gray, COLOR_BGR2GRAY);
82

83 84
        if( nightMode )
            image = Scalar::all(0);
85

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

                if( !status[i] )
                    continue;

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

122
        if( addRemovePt && points[1].size() < (size_t)MAX_COUNT )
123
        {
124
            vector<Point2f> tmp;
125
            tmp.push_back(point);
126
            cornerSubPix( gray, tmp, winSize, Size(-1,-1), termcrit);
127 128
            points[1].push_back(tmp[0]);
            addRemovePt = false;
129 130
        }

131 132
        needToInit = false;
        imshow("LK Demo", image);
133

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

151
        std::swap(points[1], points[0]);
152
        cv::swap(prevGray, gray);
153 154 155 156
    }

    return 0;
}