2010-11-27 01:59:40 +08:00
|
|
|
#include "opencv2/video/tracking.hpp"
|
2016-02-15 21:37:29 +08:00
|
|
|
#include "opencv2/imgproc.hpp"
|
|
|
|
#include "opencv2/videoio.hpp"
|
|
|
|
#include "opencv2/highgui.hpp"
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-11-29 03:41:55 +08:00
|
|
|
#include <iostream>
|
2010-05-12 01:44:00 +08:00
|
|
|
#include <ctype.h>
|
|
|
|
|
2010-11-29 03:41:55 +08:00
|
|
|
using namespace cv;
|
|
|
|
using namespace std;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2012-06-08 01:21:29 +08:00
|
|
|
static void help()
|
2010-12-04 16:30:00 +08:00
|
|
|
{
|
|
|
|
// print a welcome message, and the OpenCV version
|
|
|
|
cout << "\nThis is a demo of Lukas-Kanade optical flow lkdemo(),\n"
|
2013-04-11 21:57:33 +08:00
|
|
|
"Using OpenCV version " << CV_VERSION << endl;
|
|
|
|
cout << "\nIt uses camera by default, but you can provide a path to video as an argument.\n";
|
2010-12-04 16:30:00 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-06-12 22:46:12 +08:00
|
|
|
Point2f point;
|
2010-11-29 03:41:55 +08:00
|
|
|
bool addRemovePt = false;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2012-06-08 01:21:29 +08:00
|
|
|
static void onMouse( int event, int x, int y, int /*flags*/, void* /*param*/ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2013-04-08 02:45:38 +08:00
|
|
|
if( event == EVENT_LBUTTONDOWN )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2013-04-11 21:57:33 +08:00
|
|
|
point = Point2f((float)x, (float)y);
|
2010-11-29 03:41:55 +08:00
|
|
|
addRemovePt = true;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main( int argc, char** argv )
|
|
|
|
{
|
2010-11-29 03:41:55 +08:00
|
|
|
VideoCapture cap;
|
2013-04-11 21:38:33 +08:00
|
|
|
TermCriteria termcrit(TermCriteria::COUNT|TermCriteria::EPS,20,0.03);
|
2011-07-28 03:07:45 +08:00
|
|
|
Size subPixWinSize(10,10), winSize(31,31);
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2010-11-29 03:41:55 +08:00
|
|
|
const int MAX_COUNT = 500;
|
|
|
|
bool needToInit = false;
|
|
|
|
bool nightMode = false;
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2015-08-01 23:24:23 +08:00
|
|
|
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);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-11-29 03:41:55 +08:00
|
|
|
if( !cap.isOpened() )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-11-29 03:41:55 +08:00
|
|
|
cout << "Could not initialize capturing...\n";
|
|
|
|
return 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2010-11-29 03:41:55 +08:00
|
|
|
namedWindow( "LK Demo", 1 );
|
|
|
|
setMouseCallback( "LK Demo", onMouse, 0 );
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2015-03-24 17:06:55 +08:00
|
|
|
Mat gray, prevGray, image, frame;
|
2010-11-29 03:41:55 +08:00
|
|
|
vector<Point2f> points[2];
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
for(;;)
|
|
|
|
{
|
2010-11-29 03:41:55 +08:00
|
|
|
cap >> frame;
|
|
|
|
if( frame.empty() )
|
2010-05-12 01:44:00 +08:00
|
|
|
break;
|
|
|
|
|
2010-11-29 03:41:55 +08:00
|
|
|
frame.copyTo(image);
|
2013-04-11 21:38:33 +08:00
|
|
|
cvtColor(image, gray, COLOR_BGR2GRAY);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-11-29 03:41:55 +08:00
|
|
|
if( nightMode )
|
|
|
|
image = Scalar::all(0);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-11-29 03:41:55 +08:00
|
|
|
if( needToInit )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-11-29 03:41:55 +08:00
|
|
|
// automatic initialization
|
|
|
|
goodFeaturesToTrack(gray, points[1], MAX_COUNT, 0.01, 10, Mat(), 3, 0, 0.04);
|
2011-07-28 03:07:45 +08:00
|
|
|
cornerSubPix(gray, points[1], subPixWinSize, Size(-1,-1), termcrit);
|
2010-11-29 03:41:55 +08:00
|
|
|
addRemovePt = false;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2010-11-29 03:41:55 +08:00
|
|
|
else if( !points[0].empty() )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-11-29 03:41:55 +08:00
|
|
|
vector<uchar> status;
|
|
|
|
vector<float> err;
|
|
|
|
if(prevGray.empty())
|
|
|
|
gray.copyTo(prevGray);
|
|
|
|
calcOpticalFlowPyrLK(prevGray, gray, points[0], points[1], status, err, winSize,
|
2012-03-19 20:55:48 +08:00
|
|
|
3, termcrit, 0, 0.001);
|
2010-11-29 03:41:55 +08:00
|
|
|
size_t i, k;
|
|
|
|
for( i = k = 0; i < points[1].size(); i++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-11-29 03:41:55 +08:00
|
|
|
if( addRemovePt )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2012-06-12 22:46:12 +08:00
|
|
|
if( norm(point - points[1][i]) <= 5 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-11-29 03:41:55 +08:00
|
|
|
addRemovePt = false;
|
2010-05-12 01:44:00 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !status[i] )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
points[1][k++] = points[1][i];
|
2010-11-29 03:41:55 +08:00
|
|
|
circle( image, points[1][i], 3, Scalar(0,255,0), -1, 8);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2010-11-29 03:41:55 +08:00
|
|
|
points[1].resize(k);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2010-11-29 23:31:25 +08:00
|
|
|
if( addRemovePt && points[1].size() < (size_t)MAX_COUNT )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-11-29 03:41:55 +08:00
|
|
|
vector<Point2f> tmp;
|
2012-06-12 22:46:12 +08:00
|
|
|
tmp.push_back(point);
|
2013-04-11 21:38:33 +08:00
|
|
|
cornerSubPix( gray, tmp, winSize, Size(-1,-1), termcrit);
|
2010-11-29 03:41:55 +08:00
|
|
|
points[1].push_back(tmp[0]);
|
|
|
|
addRemovePt = false;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2010-11-29 03:41:55 +08:00
|
|
|
needToInit = false;
|
|
|
|
imshow("LK Demo", image);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-11-29 03:41:55 +08:00
|
|
|
char c = (char)waitKey(10);
|
|
|
|
if( c == 27 )
|
2010-05-12 01:44:00 +08:00
|
|
|
break;
|
2010-11-29 03:41:55 +08:00
|
|
|
switch( c )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
case 'r':
|
2010-11-29 03:41:55 +08:00
|
|
|
needToInit = true;
|
2010-05-12 01:44:00 +08:00
|
|
|
break;
|
|
|
|
case 'c':
|
2013-04-11 21:50:10 +08:00
|
|
|
points[0].clear();
|
2010-11-29 03:41:55 +08:00
|
|
|
points[1].clear();
|
2010-05-12 01:44:00 +08:00
|
|
|
break;
|
|
|
|
case 'n':
|
2010-11-29 03:41:55 +08:00
|
|
|
nightMode = !nightMode;
|
2010-05-12 01:44:00 +08:00
|
|
|
break;
|
|
|
|
}
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2010-11-29 03:41:55 +08:00
|
|
|
std::swap(points[1], points[0]);
|
2013-04-11 21:57:33 +08:00
|
|
|
cv::swap(prevGray, gray);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|