fixed warnings
[profile/ivi/opencv.git] / samples / cpp / lkdemo.cpp
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 #include <ctype.h>
8
9 using namespace cv;
10 using namespace std;
11
12 static void help()
13 {
14     // print a welcome message, and the OpenCV version
15     cout << "\nThis is a demo of Lukas-Kanade optical flow lkdemo(),\n"
16             "Using OpenCV version " << CV_VERSION << endl;
17     cout << "\nIt uses camera by default, but you can provide a path to video as an argument.\n";
18     cout << "\nHot keys: \n"
19             "\tESC - quit the program\n"
20             "\tr - auto-initialize tracking\n"
21             "\tc - delete all the points\n"
22             "\tn - switch the \"night\" mode on/off\n"
23             "To add/remove a feature point click it\n" << endl;
24 }
25
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 int main( int argc, char** argv )
39 {
40     help();
41
42     VideoCapture cap;
43     TermCriteria termcrit(TermCriteria::COUNT|TermCriteria::EPS,20,0.03);
44     Size subPixWinSize(10,10), winSize(31,31);
45
46     const int MAX_COUNT = 500;
47     bool needToInit = false;
48     bool nightMode = false;
49
50     if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
51         cap.open(argc == 2 ? argv[1][0] - '0' : 0);
52     else if( argc == 2 )
53         cap.open(argv[1]);
54
55     if( !cap.isOpened() )
56     {
57         cout << "Could not initialize capturing...\n";
58         return 0;
59     }
60
61     namedWindow( "LK Demo", 1 );
62     setMouseCallback( "LK Demo", onMouse, 0 );
63
64     Mat gray, prevGray, image;
65     vector<Point2f> points[2];
66
67     for(;;)
68     {
69         Mat frame;
70         cap >> frame;
71         if( frame.empty() )
72             break;
73
74         frame.copyTo(image);
75         cvtColor(image, gray, COLOR_BGR2GRAY);
76
77         if( nightMode )
78             image = Scalar::all(0);
79
80         if( needToInit )
81         {
82             // automatic initialization
83             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             vector<uchar> status;
90             vector<float> err;
91             if(prevGray.empty())
92                 gray.copyTo(prevGray);
93             calcOpticalFlowPyrLK(prevGray, gray, points[0], points[1], status, err, winSize,
94                                  3, termcrit, 0, 0.001);
95             size_t i, k;
96             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                         continue;
104                     }
105                 }
106
107                 if( !status[i] )
108                     continue;
109
110                 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             cornerSubPix( gray, tmp, winSize, Size(-1,-1), termcrit);
121             points[1].push_back(tmp[0]);
122             addRemovePt = false;
123         }
124
125         needToInit = false;
126         imshow("LK Demo", image);
127
128         char c = (char)waitKey(10);
129         if( c == 27 )
130             break;
131         switch( c )
132         {
133         case 'r':
134             needToInit = true;
135             break;
136         case 'c':
137             points[0].clear();
138             points[1].clear();
139             break;
140         case 'n':
141             nightMode = !nightMode;
142             break;
143         }
144
145         std::swap(points[1], points[0]);
146         cv::swap(prevGray, gray);
147     }
148
149     return 0;
150 }