基于模板匹配的目标跟踪(OpenCV)
基于VS2010+ OpenCV2。代码可以读入视频,也可以读摄像头,两者的选择只需要在代码中稍微修改即可。对于视频来说,运行会先显示第一帧,然后我们用鼠标框选要跟踪的目标,然后跟踪器开始跟踪每一帧。对摄像头来说,就会一直采集图像,然后我们用鼠标框选要跟踪的目标,接着跟踪器开始跟踪后面的每一帧。具体代码如下:
#include <opencv2/opencv.hpp> using namespace cv;
using namespace std; // Global variables
Rect box;
bool drawing_box = false;
bool gotBB = false; // bounding box mouse callback
void mouseHandler(int event, int x, int y, int flags, void *param){
switch( event ){
case CV_EVENT_MOUSEMOVE:
if (drawing_box){
box.width = x-box.x;
box.height = y-box.y;
}
break;
case CV_EVENT_LBUTTONDOWN:
drawing_box = true;
box = Rect( x, y, 0, 0 );
break;
case CV_EVENT_LBUTTONUP:
drawing_box = false;
if( box.width < 0 ){
box.x += box.width;
box.width *= -1;
}
if( box.height < 0 ){
box.y += box.height;
box.height *= -1;
}
gotBB = true;
break;
}
} // tracker: get search patches around the last tracking box,
// and find the most similar one
void tracking(Mat frame, Mat &model, Rect &trackBox)
{
Mat gray;
cvtColor(frame, gray, CV_RGB2GRAY); Rect searchWindow;
searchWindow.width = trackBox.width * 3;
searchWindow.height = trackBox.height * 3;
searchWindow.x = trackBox.x + trackBox.width * 0.5 - searchWindow.width * 0.5;
searchWindow.y = trackBox.y + trackBox.height * 0.5 - searchWindow.height * 0.5;
searchWindow &= Rect(0, 0, frame.cols, frame.rows); Mat similarity;
matchTemplate(gray(searchWindow), model, similarity, CV_TM_CCOEFF_NORMED); double mag_r;
Point point;
minMaxLoc(similarity, 0, &mag_r, 0, &point);
trackBox.x = point.x + searchWindow.x;
trackBox.y = point.y + searchWindow.y;
model = gray(trackBox);
} int main(int argc, char * argv[])
{
VideoCapture capture;
capture.open("david.mpg");
bool fromfile = true;
//Init camera
if (!capture.isOpened())
{
cout << "capture device failed to open!" << endl;
return -1;
}
//Register mouse callback to draw the bounding box
cvNamedWindow("Tracker", CV_WINDOW_AUTOSIZE);
cvSetMouseCallback("Tracker", mouseHandler, NULL ); Mat frame, model;
capture >> frame;
while(!gotBB)
{
if (!fromfile)
capture >> frame; imshow("Tracker", frame);
if (cvWaitKey(20) == 'q')
return 1;
}
//Remove callback
cvSetMouseCallback("Tracker", NULL, NULL ); Mat gray;
cvtColor(frame, gray, CV_RGB2GRAY);
model = gray(box); int frameCount = 0; while (1)
{
capture >> frame;
if (frame.empty())
return -1;
double t = (double)cvGetTickCount();
frameCount++; // tracking
tracking(frame, model, box); // show
stringstream buf;
buf << frameCount;
string num = buf.str();
putText(frame, num, Point(20, 20), FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 0, 255), 3);
rectangle(frame, box, Scalar(0, 0, 255), 3);
imshow("Tracker", frame); t = (double)cvGetTickCount() - t;
cout << "cost time: " << t / ((double)cvGetTickFrequency()*1000.) << endl; if ( cvWaitKey(1) == 27 )
break;
} return 0;
}
基于模板匹配的目标跟踪(OpenCV)的更多相关文章
- 开源项目(9-0)综述--基于深度学习的目标跟踪sort与deep-sort
基于深度学习的目标跟踪sort与deep-sort https://github.com/Ewenwan/MVision/tree/master/3D_Object_Detection/Object_ ...
- opencv如何用模板匹配寻找目标
首先使用: MatchTemplate 比较模板和重叠的图像区域 void cvMatchTemplate( const CvArr* image, const CvArr* templ, CvArr ...
- 基于 MeanShift 算法的目标跟踪问题研究
参考:http://www.cnblogs.com/tornadomeet/archive/2012/03/15/2398769.html MeanShift 算法作为一种基于特征的跟踪方法,基本思想 ...
- 使用Opencv中matchTemplate模板匹配方法跟踪移动目标
模板匹配是一种在图像中定位目标的方法,通过把输入图像在实际图像上逐像素点滑动,计算特征相似性,以此来判断当前滑块图像所在位置是目标图像的概率. 在Opencv中,模板匹配定义了6种相似性对比方式: C ...
- 基于MeanShift的目标跟踪算法及实现
这次将介绍基于MeanShift的目标跟踪算法,首先谈谈简介,然后给出算法实现流程,最后实现了一个单目标跟踪的MeanShift算法[matlab/c两个版本] csdn贴公式比较烦,原谅我直接截图了 ...
- OpenCV——模板匹配
minMaxLoc函数: void minMaxLoc( const Mat& src, double* minVal, double* maxVal=0, Point* minLoc=0, ...
- Video Target Tracking Based on Online Learning—深度学习在目标跟踪中的应用
摘要 近年来,深度学习方法在物体跟踪领域有不少成功应用,并逐渐在性能上超越传统方法.本文先对现有基于深度学习的目标跟踪算法进行了分类梳理,后续会分篇对各个算法进行详细描述. 看上方给出的3张图片,它们 ...
- 【目标跟踪】相关滤波算法之MOSSE
简要 2010年David S. Bolme等人在CVPR上发表了<Visual Object Tracking using Adaptive Correlation Filters>一文 ...
- 时序分析:DTW算法(基于模板)
对时序对象进行分析,使用KMP算法可以分析速率不变的模式,参考时序分析:欧式空间轨迹模式识别.使用基于模板匹配的方法,对于速率发生变化的模式,需要用新的对速率要求松散的方法,DTW方法为一种广泛使用的 ...
随机推荐
- 牛客寒假算法基础集训营2 【处女座与复读机】DP最小编辑距离【模板题】
链接:https://ac.nowcoder.com/acm/contest/327/G来源:牛客网 一天,处女座在牛客算法群里发了一句“我好强啊”,引起无数的复读,可是处女座发现复读之后变成了“处女 ...
- java实现上传文件夹
我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 首先我们需要了解的是上传文件三要素: 1.表单提交方式:post (get方式提交有大小 ...
- Non-standard serial port baud rate setting
////combuad_recv.cpp #include <stdio.h> /*标准输入输出定义*/ #include <stdlib.h> /*标准函数库定义*/ #in ...
- Poj 2887 Big String(块状数组)
Big String Time Limit: 1000MS Memory Limit: 131072K Description You are given a string and supposed ...
- jmeter正则表达式提取多个值
1.返回的数据截图,需要获取customerId.customerName的值 2.把jmeter查看结果树返回的数据放在 Regester,正则表达式写 : "customerId&quo ...
- Nginx之共享内存与slab机制
1. 共享内存 在 Nginx 里,一块完整的共享内存以结构体 ngx_shm_zone_t 来封装,如下: typedef struct ngx_shm_zone_s ngx_shm_zone_t; ...
- ArcGIS中国工具3.0正式发布
ArcGIS中国工具3.0正式发布,新功能有 1. 支持面积分割(见4.6),见https://weibo.com/tv/v/HsM2ksYY3?fid=1034:4368578107884427 ...
- Bootstrap4从入门到精通视频教程
一.布局 0.课件1.Bootstrap介绍_栅格系统2.禁用响应式_响应式分界点 二.内容 3.排版_代码4.图片_图片框5.表格 三.公共样式 6.边框_浮动7.颜色_Display显示属性8.文 ...
- 查找与排序算法(Searching adn Sorting)
1,查找算法 常用的查找算法包括顺序查找,二分查找和哈希查找. 1.1 顺序查找(Sequential search) 顺序查找: 依次遍历列表中每一个元素,查看是否为目标元素.python实现代码如 ...
- 【Java】给整数加上千分位分隔符
package com.testEmp; import java.text.DecimalFormat; public class NumberFormat { public static void ...