Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/cpp/lkdemo.cpp
16337 views
1
#include "opencv2/video/tracking.hpp"
2
#include "opencv2/imgproc.hpp"
3
#include "opencv2/videoio.hpp"
4
#include "opencv2/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
VideoCapture cap;
41
TermCriteria termcrit(TermCriteria::COUNT|TermCriteria::EPS,20,0.03);
42
Size subPixWinSize(10,10), winSize(31,31);
43
44
const int MAX_COUNT = 500;
45
bool needToInit = false;
46
bool nightMode = false;
47
48
help();
49
cv::CommandLineParser parser(argc, argv, "{@input|0|}");
50
string input = parser.get<string>("@input");
51
52
if( input.size() == 1 && isdigit(input[0]) )
53
cap.open(input[0] - '0');
54
else
55
cap.open(input);
56
57
if( !cap.isOpened() )
58
{
59
cout << "Could not initialize capturing...\n";
60
return 0;
61
}
62
63
namedWindow( "LK Demo", 1 );
64
setMouseCallback( "LK Demo", onMouse, 0 );
65
66
Mat gray, prevGray, image, frame;
67
vector<Point2f> points[2];
68
69
for(;;)
70
{
71
cap >> frame;
72
if( frame.empty() )
73
break;
74
75
frame.copyTo(image);
76
cvtColor(image, gray, COLOR_BGR2GRAY);
77
78
if( nightMode )
79
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
vector<uchar> status;
91
vector<float> err;
92
if(prevGray.empty())
93
gray.copyTo(prevGray);
94
calcOpticalFlowPyrLK(prevGray, gray, points[0], points[1], status, err, winSize,
95
3, termcrit, 0, 0.001);
96
size_t i, k;
97
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
continue;
105
}
106
}
107
108
if( !status[i] )
109
continue;
110
111
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
points[1].push_back(tmp[0]);
123
addRemovePt = false;
124
}
125
126
needToInit = false;
127
imshow("LK Demo", image);
128
129
char c = (char)waitKey(10);
130
if( c == 27 )
131
break;
132
switch( c )
133
{
134
case 'r':
135
needToInit = true;
136
break;
137
case 'c':
138
points[0].clear();
139
points[1].clear();
140
break;
141
case 'n':
142
nightMode = !nightMode;
143
break;
144
}
145
146
std::swap(points[1], points[0]);
147
cv::swap(prevGray, gray);
148
}
149
150
return 0;
151
}
152
153