#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 )
{
VideoCapture cap;
TermCriteria termcrit(TermCriteria::COUNT|TermCriteria::EPS,20,0.03);
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
cap.open(input);
if( !cap.isOpened() )
{
cout << "Could not initialize capturing...\n";
return 0;
}
Mat gray, prevGray, image, frame;
vector<Point2f> points[2];
for(;;)
{
cap >> frame;
if( frame.empty() )
break;
frame.copyTo(image);
if( nightMode )
image = Scalar::all(0);
if( needToInit )
{
addRemovePt = false;
}
else if( !points[0].empty() )
{
vector<uchar> status;
vector<float> err;
if(prevGray.empty())
gray.copyTo(prevGray);
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;
}
Designed for command line parsing.
Definition: utility.hpp:890
#define CV_VERSION
Definition: core/include/opencv2/core/version.hpp:19
double norm(InputArray src1, int normType=NORM_L2, InputArray mask=noArray())
Calculates the absolute norm of an array.
void swap(Mat &a, Mat &b)
Swaps two matrices.
Size2i Size
Definition: modules/core/include/opencv2/core/types.hpp:370
Scalar_< double > Scalar
Definition: modules/core/include/opencv2/core/types.hpp:709
Point_< float > Point2f
Definition: modules/core/include/opencv2/core/types.hpp:207
@ circle
Definition: gr_skig.hpp:62
@ EVENT_LBUTTONDOWN
indicates that the left mouse button is pressed.
Definition: highgui.hpp:168
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
int waitKey(int delay=0)
Waits for a pressed key.
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
void setMouseCallback(const String &winname, MouseCallback onMouse, void *userdata=0)
Sets mouse handler for the specified window.
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0, AlgorithmHint hint=cv::ALGO_HINT_DEFAULT)
Converts an image from one color space to another.
@ COLOR_BGR2GRAY
convert between RGB/BGR and grayscale, color conversions
Definition: imgproc/include/opencv2/imgproc.hpp:555
void goodFeaturesToTrack(InputArray image, OutputArray corners, int maxCorners, double qualityLevel, double minDistance, InputArray mask=noArray(), int blockSize=3, bool useHarrisDetector=false, double k=0.04)
Determines strong corners on an image.
void cornerSubPix(InputArray image, InputOutputArray corners, Size winSize, Size zeroZone, TermCriteria criteria)
Refines the corner locations.
void calcOpticalFlowPyrLK(InputArray prevImg, InputArray nextImg, InputArray prevPts, InputOutputArray nextPts, OutputArray status, OutputArray err, Size winSize=Size(21, 21), int maxLevel=3, TermCriteria criteria=TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 0.01), int flags=0, double minEigThreshold=1e-4)
Calculates an optical flow for a sparse feature set using the iterative Lucas-Kanade method with pyra...
int main(int argc, char *argv[])
Definition: highgui_qt.cpp:3
GOpaque< Size > size(const GMat &src)
Gets dimensions from Mat.
Definition: core/include/opencv2/core.hpp:107