samples/cpp/lkdemo.cpp#

An example using the Lucas-Kanade optical flow algorithm

  1#include "opencv2/video/tracking.hpp"
  2#include "opencv2/imgproc.hpp"
  3#include "opencv2/features.hpp"
  4#include "opencv2/videoio.hpp"
  5#include "opencv2/highgui.hpp"
  6
  7#include <iostream>
  8#include <ctype.h>
  9
 10using namespace cv;
 11using namespace std;
 12
 13static void help()
 14{
 15    // print a welcome message, and the OpenCV version
 16    cout << "\nThis is a demo of Lukas-Kanade optical flow lkdemo(),\n"
 17            "Using OpenCV version " << CV_VERSION << endl;
 18    cout << "\nIt uses camera by default, but you can provide a path to video as an argument.\n";
 19    cout << "\nHot keys: \n"
 20            "\tESC - quit the program\n"
 21            "\tr - auto-initialize tracking\n"
 22            "\tc - delete all the points\n"
 23            "\tn - switch the \"night\" mode on/off\n"
 24            "To add/remove a feature point click it\n" << endl;
 25}
 26
 27Point2f point;
 28bool addRemovePt = false;
 29
 30static void onMouse( int event, int x, int y, int /*flags*/, void* /*param*/ )
 31{
 32    if( event == EVENT_LBUTTONDOWN )
 33    {
 34        point = Point2f((float)x, (float)y);
 35        addRemovePt = true;
 36    }
 37}
 38
 39int main( int argc, char** argv )
 40{
 41    VideoCapture cap;
 42    TermCriteria termcrit(TermCriteria::COUNT|TermCriteria::EPS,20,0.03);
 43    Size subPixWinSize(10,10), winSize(31,31);
 44
 45    const int MAX_COUNT = 500;
 46    bool needToInit = false;
 47    bool nightMode = false;
 48
 49    help();
 50    cv::CommandLineParser parser(argc, argv, "{@input|0|}");
 51    string input = parser.get<string>("@input");
 52
 53    if( input.size() == 1 && isdigit(input[0]) )
 54        cap.open(input[0] - '0');
 55    else
 56        cap.open(input);
 57
 58    if( !cap.isOpened() )
 59    {
 60        cout << "Could not initialize capturing...\n";
 61        return 0;
 62    }
 63
 64    namedWindow( "LK Demo", 1 );
 65    setMouseCallback( "LK Demo", onMouse, 0 );
 66
 67    Mat gray, prevGray, image, frame;
 68    vector<Point2f> points[2];
 69
 70    for(;;)
 71    {
 72        cap >> frame;
 73        if( frame.empty() )
 74            break;
 75
 76        frame.copyTo(image);
 77        cvtColor(image, gray, COLOR_BGR2GRAY);
 78
 79        if( nightMode )
 80            image = Scalar::all(0);
 81
 82        if( needToInit )
 83        {
 84            // automatic initialization
 85            goodFeaturesToTrack(gray, points[1], MAX_COUNT, 0.01, 10, Mat(), 3, 3, 0, 0.04);
 86            cornerSubPix(gray, points[1], subPixWinSize, Size(-1,-1), termcrit);
 87            addRemovePt = false;
 88        }
 89        else if( !points[0].empty() )
 90        {
 91            vector<uchar> status;
 92            vector<float> err;
 93            if(prevGray.empty())
 94                gray.copyTo(prevGray);
 95            calcOpticalFlowPyrLK(prevGray, gray, points[0], points[1], status, err, winSize,
 96                                 3, termcrit, 0, 0.001);
 97            size_t i, k;
 98            for( i = k = 0; i < points[1].size(); i++ )
 99            {
100                if( addRemovePt )
101                {
102                    if( norm(point - points[1][i]) <= 5 )
103                    {
104                        addRemovePt = false;
105                        continue;
106                    }
107                }
108
109                if( !status[i] )
110                    continue;
111
112                points[1][k++] = points[1][i];
113                circle( image, points[1][i], 3, Scalar(0,255,0), -1, 8);
114            }
115            points[1].resize(k);
116        }
117
118        if( addRemovePt && points[1].size() < (size_t)MAX_COUNT )
119        {
120            vector<Point2f> tmp;
121            tmp.push_back(point);
122            cornerSubPix( gray, tmp, winSize, Size(-1,-1), termcrit);
123            points[1].push_back(tmp[0]);
124            addRemovePt = false;
125        }
126
127        needToInit = false;
128        imshow("LK Demo", image);
129
130        char c = (char)waitKey(10);
131        if( c == 27 )
132            break;
133        switch( c )
134        {
135        case 'r':
136            needToInit = true;
137            break;
138        case 'c':
139            points[0].clear();
140            points[1].clear();
141            break;
142        case 'n':
143            nightMode = !nightMode;
144            break;
145        }
146
147        std::swap(points[1], points[0]);
148        cv::swap(prevGray, gray);
149    }
150
151    return 0;
152}