#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
static void help(char** argv)
{
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;
}
//没step个像素 绘制一次。 cflowmap:上一彩色图像 flow:当前图像相对上一图像的光流
static void drawOptFlowMap(const Mat& flow, Mat& cflowmap, int step,
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);//上一图像上的离散点
}
}
int main(int argc, char** argv)
{
cv::CommandLineParser parser(argc, argv, "{help h||}{video|balltest.mp4|}");
if (parser.has("help"))
{
help(argv);
return 0;
}
//VideoCapture cap(0);
VideoCapture cap(samples::findFile(parser.get<String>("video")));
help(argv);
if (!cap.isOpened())
return -1;
Mat flow, cflow, frame;
UMat gray, prevgray, uflow;
namedWindow("flow", 1);
for (;;)
{
cap >> frame;
if (frame.empty())
{
break;
}
cvtColor(frame, gray, COLOR_BGR2GRAY);
if (!prevgray.empty())//上一灰度图非空
{ //利用用Gunnar Farneback的算法计算全局性的稠密光流算法(即图像上所有像素点的光流都计算出来)
/// <summary>
/// /
/// </summary>
/// prevgray : 输入前一帧图像
/// gray : 输入后一帧图像
/// uflow: flow 输出的光流
/// 0.5:pyr_scale 金字塔上下两层之间的尺度关系
/// 3:levels :金字塔层数
/// 15: winsize 均值窗口大小,越大越能denoise并且能够检测快速移动目标,但会引起模糊运动区域
/// 3:iterations 迭代次数
/// 5: poly_n 像素领域大小,一般为5,7等
/// 1.2 : poly_sigma 高斯标注差,一般为1-1.5
/// 0: flags 计算方法。主要包括OPTFLOW_USE_INITIAL_FLOW和OPTFLOW_FARNEBACK_GAUSSIAN
/// <returns></returns>
/*CV_EXPORTS_W void calcOpticalFlowFarneback(InputArray prev, InputArray next, InputOutputArray flow,
double pyr_scale, int levels, int winsize,
int iterations, int poly_n, double poly_sigma,
int flags);*/
calcOpticalFlowFarneback(prevgray, gray, uflow, 0.5, 3, 15, 3, 5, 1.2, 0);//计算光流
cvtColor(prevgray, cflow, COLOR_GRAY2BGR);//上一灰度图的彩色图
uflow.copyTo(flow);//光流
drawOptFlowMap(flow, cflow, 12, 1.5, Scalar(0, 255, 0));//在上一彩色图上绘制绿色光流图
imshow("flow", cflow);
}
if (waitKey(30) >= 0)
break;
std::swap(prevgray, gray);//更新上一灰度图
}
return 0;
}
运行效果
参考:
OpenCV 光流 | D栈 - Delft Stackhttps://www.delftstack.com/zh/howto/python/opencv-optical-flow/