Advertisement

【OpenCV C++】光流法进行运动目标检测

阅读量:

OpenCV C++光流法进行运动目标检测

  • 什么是光流
  • 程序说明
  • 代码
  • 运行效果

什么是光流

光流(optical flow)是空间运动物体在观察成像平面上的像素运动的瞬时速度。

光流法是利用图像序列中像素在时间域上的变化以及相邻帧之间的相关性来找到上一帧跟当前帧之间存在的对应关系,从而计算出相邻帧之间物体的运动信息的一种方法。

通常将二维图像平面特定坐标点上的灰度瞬时变化率定义为光流矢量。

换言之:光流用来指定时变图像中运动模式的运动速度,因为当物体在运动时,在图像上对应点的亮度模式也在运动。这种图像亮度模式的表观运动就是光流。

程序说明

// 程序描述:来自OpenCV安装目录下Samples文件夹中的官方示例程序-利用光流法进行运动目标检测
// 操作系统: Windows 10 64bit
// 开发语言: C++
// IDE 版 本:Visual Studio 2019
// OpenCV版本:4.20

/************************************************************************

  • Copyright© 2011 Yang Xian
  • All rights reserved.
    • File: opticalFlow.cpp
  • Brief: lk光流法做运动目标检测
  • Version: 1.0
  • Author: Yang Xian
  • Email: xyang2011@sinano.ac.cn
  • Date: 2011/11/18
  • History:
    ************************************************************************/

代码

复制代码
    #include <opencv2/video/video.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    #include <opencv2/core/core.hpp>
    #include <iostream>
    #include <cstdio>
    
    using namespace std;
    using namespace cv;
    
    //-----------------------------------【全局函数声明】-----------------------------------------
    //		描述:声明全局函数
    //-------------------------------------------------------------------------------------------------
    void tracking(Mat& frame, Mat& output);
    bool addNewPoints();
    bool acceptTrackedPoint(int i);
    
    //-----------------------------------【全局变量声明】-----------------------------------------
    //		描述:声明全局变量
    //-------------------------------------------------------------------------------------------------
    string window_name = "optical flow tracking";
    Mat gray;	// 当前图片
    Mat gray_prev;	// 预测图片
    vector<Point2f> points[2];	// point0为特征点的原来位置,point1为特征点的新位置
    vector<Point2f> initial;	// 初始化跟踪点的位置
    vector<Point2f> features;	// 检测的特征
    int maxCount = 500;	// 检测的最大特征数
    double qLevel = 0.01;	// 特征检测的等级
    double minDist = 10.0;	// 两特征点之间的最小距离
    vector<uchar> status;	// 跟踪特征的状态,特征的流发现为1,否则为0
    vector<float> err;
    
    //-----------------------------------【main( )函数】--------------------------------------------
    //		描述:控制台应用程序的入口函数,我们的程序从这里开始
    //-------------------------------------------------------------------------------------------------
    int main()
    {
    
    	Mat frame;
    	Mat result;
    
    	VideoCapture capture(0);
    
    	if (capture.isOpened())	// 摄像头读取文件开关
    	{
    		while (true)
    		{
    			capture >> frame;
    
    			if (!frame.empty())
    			{
    				tracking(frame, result);
    			}
    			else
    			{
    				printf(" --(!) No captured frame -- Break!");
    				break;
    			}
    
    			int c = waitKey(50);
    			if ((char)c == 27)
    			{
    				break;
    			}
    		}
    	}
    	return 0;
    }
    
    //-------------------------------------------------------------------------------------------------
    // function: tracking
    // brief: 跟踪
    // parameter: frame	输入的视频帧
    //			  output 有跟踪结果的视频帧
    // return: void
    //-------------------------------------------------------------------------------------------------
    void tracking(Mat& frame, Mat& output)
    {
    
    	//此句代码的OpenCV3版为:
    	cvtColor(frame, gray, COLOR_BGR2GRAY);
    	//此句代码的OpenCV2版为:
    	//cvtColor(frame, gray, CV_BGR2GRAY);
    
    	frame.copyTo(output);
    
    	// 添加特征点
    	if (addNewPoints())
    	{
    		goodFeaturesToTrack(gray, features, maxCount, qLevel, minDist);
    		points[0].insert(points[0].end(), features.begin(), features.end());
    		initial.insert(initial.end(), features.begin(), features.end());
    	}
    
    	if (gray_prev.empty())
    	{
    		gray.copyTo(gray_prev);
    	}
    	// l-k光流法运动估计
    	calcOpticalFlowPyrLK(gray_prev, gray, points[0], points[1], status, err);
    	// 去掉一些不好的特征点
    	int k = 0;
    	for (size_t i = 0; i < points[1].size(); i++)
    	{
    		if (acceptTrackedPoint(i))
    		{
    			initial[k] = initial[i];
    			points[1][k++] = points[1][i];
    		}
    	}
    	points[1].resize(k);
    	initial.resize(k);
    	// 显示特征点和运动轨迹
    	for (size_t i = 0; i < points[1].size(); i++)
    	{
    		line(output, initial[i], points[1][i], Scalar(0, 0, 255));
    		circle(output, points[1][i], 3, Scalar(0, 255, 0), -1);
    	}
    
    	// 把当前跟踪结果作为下一此参考
    	swap(points[1], points[0]);
    	swap(gray_prev, gray);
    
    	imshow(window_name, output);
    }
    
    //-------------------------------------------------------------------------------------------------
    // function: addNewPoints
    // brief: 检测新点是否应该被添加
    // parameter:
    // return: 是否被添加标志
    //-------------------------------------------------------------------------------------------------
    bool addNewPoints()
    {
    	return points[0].size() <= 10;
    }
    
    //-------------------------------------------------------------------------------------------------
    // function: acceptTrackedPoint
    // brief: 决定哪些跟踪点被接受
    // parameter:
    // return:
    //-------------------------------------------------------------------------------------------------
    bool acceptTrackedPoint(int i)
    {
    	return status[i] && ((abs(points[0][i].x - points[1][i].x) + abs(points[0][i].y - points[1][i].y)) > 2);
    }

运行效果

可调用摄像头视频,也可读取本地视频;下面是摄像头捕获视频,光流法追踪效果

在这里插入图片描述

全部评论 (0)

还没有任何评论哟~