#include <iostream>
#include <ctype.h>
static void help()
{
cout << "\nThis is a demo of Lukas-Kanade optical flow lkdemo(),\n"
cout << "\nIt uses camera by default, but you can provide a path to video as an argument.\n";
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;
}
bool addRemovePt = false;
static void onMouse( int event, int x, int y, int , void* )
{
{
point =
Point2f((
float)x, (
float)y);
addRemovePt = true;
}
}
int main( int argc, char** argv )
{
Size subPixWinSize(10,10), winSize(31,31);
const int MAX_COUNT = 500;
bool needToInit = false;
bool nightMode = false;
help();
string input = parser.
get<
string>(
"@input");
if( input.size() == 1 && isdigit(input[0]) )
cap.
open(input[0] -
'0');
else
{
cout << "Could not initialize capturing...\n";
return 0;
}
Mat gray, prevGray, image, frame;
vector<Point2f> points[2];
for(;;)
{
cap >> frame;
if( frame.empty() )
break;
if( nightMode )
if( needToInit )
{
addRemovePt = false;
}
else if( !points[0].empty() )
{
vector<uchar> status;
vector<float> err;
3, termcrit, 0, 0.001);
size_t i, k;
for( i = k = 0; i < points[1].size(); i++ )
{
if( addRemovePt )
{
if(
norm(point - points[1][i]) <= 5 )
{
addRemovePt = false;
continue;
}
}
if( !status[i] )
continue;
points[1][k++] = points[1][i];
}
points[1].resize(k);
}
if( addRemovePt && points[1].size() < (size_t)MAX_COUNT )
{
vector<Point2f> tmp;
tmp.push_back(point);
points[1].push_back(tmp[0]);
addRemovePt = false;
}
needToInit = false;
if( c == 27 )
break;
switch( c )
{
case 'r':
needToInit = true;
break;
case 'c':
points[0].clear();
points[1].clear();
break;
case 'n':
nightMode = !nightMode;
break;
}
}
return 0;
}