opencv/samples/cpp/lkdemo.cpp

152 lines
3.9 KiB
C++
Raw Normal View History

#include "opencv2/video/tracking.hpp"
2010-11-28 20:41:55 +01:00
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
2010-11-28 20:41:55 +01:00
#include <iostream>
#include <ctype.h>
2010-11-28 20:41:55 +01:00
using namespace cv;
using namespace std;
2012-06-07 19:21:29 +02:00
static void help()
2010-12-04 09:30:00 +01:00
{
// print a welcome message, and the OpenCV version
cout << "\nThis is a demo of Lukas-Kanade optical flow lkdemo(),\n"
2012-06-07 19:21:29 +02:00
"Using OpenCV version %s\n" << CV_VERSION << "\n"
<< endl;
2010-12-04 09:30:00 +01: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;
}
Point2f point;
2010-11-28 20:41:55 +01:00
bool addRemovePt = false;
2012-06-07 19:21:29 +02:00
static void onMouse( int event, int x, int y, int /*flags*/, void* /*param*/ )
{
if( event == CV_EVENT_LBUTTONDOWN )
{
point = Point2f((float)x,(float)y);
2010-11-28 20:41:55 +01:00
addRemovePt = true;
}
}
int main( int argc, char** argv )
{
2010-11-28 20:41:55 +01:00
VideoCapture cap;
TermCriteria termcrit(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.03);
Size subPixWinSize(10,10), winSize(31,31);
2012-06-07 19:21:29 +02:00
2010-11-28 20:41:55 +01:00
const int MAX_COUNT = 500;
bool needToInit = false;
bool nightMode = false;
2012-06-07 19:21:29 +02:00
if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
2010-11-28 20:41:55 +01:00
cap.open(argc == 2 ? argv[1][0] - '0' : 0);
else if( argc == 2 )
2010-11-28 20:41:55 +01:00
cap.open(argv[1]);
2010-11-28 20:41:55 +01:00
if( !cap.isOpened() )
{
2010-11-28 20:41:55 +01:00
cout << "Could not initialize capturing...\n";
return 0;
}
2010-12-04 09:30:00 +01:00
help();
2010-11-28 20:41:55 +01:00
namedWindow( "LK Demo", 1 );
setMouseCallback( "LK Demo", onMouse, 0 );
2010-11-28 20:41:55 +01:00
Mat gray, prevGray, image;
vector<Point2f> points[2];
2012-06-07 19:21:29 +02:00
for(;;)
{
2010-11-28 20:41:55 +01:00
Mat frame;
cap >> frame;
if( frame.empty() )
break;
2010-11-28 20:41:55 +01:00
frame.copyTo(image);
2012-06-07 19:21:29 +02:00
cvtColor(image, gray, CV_BGR2GRAY);
2010-11-28 20:41:55 +01:00
if( nightMode )
image = Scalar::all(0);
2010-11-28 20:41:55 +01:00
if( needToInit )
{
2010-11-28 20:41:55 +01:00
// automatic initialization
goodFeaturesToTrack(gray, points[1], MAX_COUNT, 0.01, 10, Mat(), 3, 0, 0.04);
cornerSubPix(gray, points[1], subPixWinSize, Size(-1,-1), termcrit);
2010-11-28 20:41:55 +01:00
addRemovePt = false;
}
2010-11-28 20:41:55 +01:00
else if( !points[0].empty() )
{
2010-11-28 20:41:55 +01:00
vector<uchar> status;
vector<float> err;
if(prevGray.empty())
gray.copyTo(prevGray);
calcOpticalFlowPyrLK(prevGray, gray, points[0], points[1], status, err, winSize,
3, termcrit, 0, 0.001);
2010-11-28 20:41:55 +01:00
size_t i, k;
for( i = k = 0; i < points[1].size(); i++ )
{
2010-11-28 20:41:55 +01:00
if( addRemovePt )
{
if( norm(point - points[1][i]) <= 5 )
{
2010-11-28 20:41:55 +01:00
addRemovePt = false;
continue;
}
}
if( !status[i] )
continue;
points[1][k++] = points[1][i];
2010-11-28 20:41:55 +01:00
circle( image, points[1][i], 3, Scalar(0,255,0), -1, 8);
}
2010-11-28 20:41:55 +01:00
points[1].resize(k);
}
if( addRemovePt && points[1].size() < (size_t)MAX_COUNT )
{
2010-11-28 20:41:55 +01:00
vector<Point2f> tmp;
tmp.push_back(point);
2010-11-28 20:41:55 +01:00
cornerSubPix( gray, tmp, winSize, cvSize(-1,-1), termcrit);
points[1].push_back(tmp[0]);
addRemovePt = false;
}
2010-11-28 20:41:55 +01:00
needToInit = false;
imshow("LK Demo", image);
2010-11-28 20:41:55 +01:00
char c = (char)waitKey(10);
if( c == 27 )
break;
2010-11-28 20:41:55 +01:00
switch( c )
{
case 'r':
2010-11-28 20:41:55 +01:00
needToInit = true;
break;
case 'c':
2010-11-28 20:41:55 +01:00
points[1].clear();
break;
case 'n':
2010-11-28 20:41:55 +01:00
nightMode = !nightMode;
break;
default:
;
}
2012-06-07 19:21:29 +02:00
2010-11-28 20:41:55 +01:00
std::swap(points[1], points[0]);
swap(prevGray, gray);
}
return 0;
}