//--------------------------------------【程序说明】-------------------------------------------
// 程序说明:《OpenCV3编程入门》OpenCV2版书本配套示例程序09
// 程序描述:来自OpenCV安装目录下Samples文件夹中的官方示例程序-利用光流法进行运动目标检测
// 开发测试所用操作系统: Windows 7 64bit
// 开发测试所用IDE版本:Visual Studio 2010
// 开发测试所用OpenCV版本: 2.4.9
// 2014年11月 Revised by @浅墨_毛星云
//------------------------------------------------------------------------------------------------
/************************************************************************
* Copyright(c) 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;
//--------------------------------【help( )函数】----------------------------------------------
// 描述:输出帮助信息
//-------------------------------------------------------------------------------------------------
static void help()
{
//输出欢迎信息和OpenCV版本
cout <<"\n\n\t\t\t非常感谢购买《OpenCV3编程入门》一书!\n"
<<"\n\n\t\t\t此为本书OpenCV2版的第9个配套示例程序\n"
<< "\n\n\t\t\t 当前使用的OpenCV版本为:" << CV_VERSION
<<"\n\n ----------------------------------------------------------------------------" ;
}
//-----------------------------------【main( )函数】--------------------------------------------
// 描述:控制台应用程序的入口函数,我们的程序从这里开始
//-------------------------------------------------------------------------------------------------
int main()
{
Mat frame;
Mat result;
VideoCapture capture("1.avi");
help();
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;
}
}
}
system("pause");
return 0;
}
//-------------------------------------------------------------------------------------------------
// function: tracking
// brief: 跟踪
// parameter: frame 输入的视频帧
// output 有跟踪结果的视频帧
// return: void
//-------------------------------------------------------------------------------------------------
void tracking(Mat &frame, Mat &output)
{
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());
}
//printf("s= %d",points[0]);
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);
}
没有合适的资源?快使用搜索试试~ 我知道了~
用光流法进行运动目标检测
共30个文件
tlog:10个
pdb:2个
manifest:2个
2星 需积分: 30 33 下载量 84 浏览量
2016-10-14
16:55:58
上传
评论 2
收藏 15.98MB RAR 举报
温馨提示
这个代码可以直接使用。打开,下载,然后可以替换自己的视频文件就可以正常运行了。
资源推荐
资源详情
资源评论
收起资源包目录
【9】用光流法进行运动目标检测.rar (30个子文件)
【9】用光流法进行运动目标检测
9_用光流法进行运动目标检测.sln 981B
9_用光流法进行运动目标检测
9_用光流法进行运动目标检测.vcxproj 4KB
1.avi 7.09MB
9_OpticalFlow.cpp 6KB
Debug
vc100.idb 347KB
CL.write.1.tlog 420B
9_用光流法进行运动目标检测.lastbuildstate 89B
CL.read.1.tlog 12KB
mt.read.1.tlog 336B
9_用光流法进行运动目标检测.exe.intermediate.manifest 381B
mt.command.1.tlog 502B
cl.command.1.tlog 832B
9_用光流法进行运动目标检测.vcxprojResolveAssemblyReference.cache 707B
link.write.1.tlog 706B
9_OpticalFlow.obj 390KB
link.command.1.tlog 3KB
link.read.1.tlog 9KB
mt.write.1.tlog 336B
9_用光流法进行运动目标检测.write.1.tlog 0B
9_用光流法进行运动目标检测.log 4KB
vc100.pdb 532KB
9_用光流法进行运动目标检测.vcxproj.filters 951B
9_用光流法进行运动目标检测.vcxproj.user 143B
9_用光流法进行运动目标检测.sdf 11.14MB
ipch
9_用光流法进行运动目标检测-d2cf1a6c
9_用光流法进行运动目标检测-d0a8acea.ipch 43MB
Debug
9_用光流法进行运动目标检测.pdb 1.32MB
9_用光流法进行运动目标检测.exe 105KB
9_用光流法进行运动目标检测.ilk 776KB
9_用光流法进行运动目标检测.exe.manifest 406B
9_用光流法进行运动目标检测.suo 16KB
共 30 条
- 1
资源评论
- sarila092019-11-07没有源代码,不能直接运行
- lugangmail20062017-02-19这个完全没有源代码啊!只有一个书配的简单源码!
千山暮雪13153
- 粉丝: 3
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功