opencv/samples/cpp/fback.cpp

69 lines
1.8 KiB
C++
Raw Normal View History

#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-12-04 16:29:51 +08:00
#include <iostream>
using namespace cv;
2010-12-04 16:29:51 +08:00
using namespace std;
static void help(char** argv)
2010-12-04 16:29:51 +08:00
{
2012-06-08 01:21:29 +08:00
cout <<
"\nThis program demonstrates dense optical flow algorithm by Gunnar Farneback\n"
"Mainly the function: calcOpticalFlowFarneback()\n"
"Call:\n"
<< argv[0]
<< "This reads from video camera 0\n" << endl;
2010-12-04 16:29:51 +08:00
}
2012-06-08 01:21:29 +08:00
static void drawOptFlowMap(const Mat& flow, Mat& cflowmap, int step,
2010-07-17 18:35:17 +08:00
double, const Scalar& color)
{
for(int y = 0; y < cflowmap.rows; y += step)
for(int x = 0; x < cflowmap.cols; x += step)
{
const Point2f& fxy = flow.at<Point2f>(y, x);
line(cflowmap, Point(x,y), Point(cvRound(x+fxy.x), cvRound(y+fxy.y)),
color);
circle(cflowmap, Point(x,y), 2, color, -1);
}
}
2015-08-01 23:24:23 +08:00
int main(int argc, char** argv)
{
2015-08-01 23:24:23 +08:00
cv::CommandLineParser parser(argc, argv, "{help h||}");
if (parser.has("help"))
{
help(argv);
2015-08-01 23:24:23 +08:00
return 0;
}
VideoCapture cap(0);
help(argv);
if( !cap.isOpened() )
return -1;
2012-06-08 01:21:29 +08:00
2014-08-09 02:45:07 +08:00
Mat flow, cflow, frame;
UMat gray, prevgray, uflow;
namedWindow("flow", 1);
2012-06-08 01:21:29 +08:00
for(;;)
{
cap >> frame;
cvtColor(frame, gray, COLOR_BGR2GRAY);
2012-06-08 01:21:29 +08:00
2014-08-09 02:45:07 +08:00
if( !prevgray.empty() )
{
2014-08-09 02:45:07 +08:00
calcOpticalFlowFarneback(prevgray, gray, uflow, 0.5, 3, 15, 3, 5, 1.2, 0);
cvtColor(prevgray, cflow, COLOR_GRAY2BGR);
2014-08-09 02:45:07 +08:00
uflow.copyTo(flow);
drawOptFlowMap(flow, cflow, 16, 1.5, Scalar(0, 255, 0));
imshow("flow", cflow);
}
if(waitKey(30)>=0)
break;
std::swap(prevgray, gray);
}
return 0;
}