static Ptr create(const TrackerMIL::Params ¶meters);
CV_WRAP static Ptr create();
struct CV_EXPORTS Params
{
PARAMS();
//采样器的参数
float samplerInitInRadius; //初始收集正面实例的半径
int samplerInitMaxNegNum; //初始使用负样本
float samplerSearchWinSize; //搜索窗口的大小
float samplerTrackInRadius; //在跟踪期间收集正面实例的半径
int samplerTrackMaxPosNum; //在追踪期间使用正面样本
int samplerTrackMaxNegNum; //在跟踪期间使用的负样本
int featureSetNumFeatures; //特征
void read(const FileNode&fn);
void write(FileStorage&fs)const;
};
static Ptr create(const TrackerBoosting::Params ¶meters);
CV_WRAP static Ptr create();
struct CV_EXPORTS Params
{
PARAMS();
int numClassifiers; //在OnlineBoosting算法中使用的分类器的数量
float samplerOverlap; //搜索区域参数
float samplerSearchFactor; //搜索区域参数
int iterationInit; //初始迭代
int featureSetNumFeatures; //特征
//从文件读取参数
void read(const FileNode&fn);
//从文件写入参数
void write(FileStorage&fs)const;
};
首先获取视频的第一帧,通过点击左键框选选择要跟踪的目标,点击右键确认并使用MIL开始跟踪.(从实际情况看来,算法对过程中有遮挡的情况跟踪能力较差.)
(环境:Ubuntu16.04+QT5.8+opencv3.3.1)
#include
#include
#include
#include
using namespace cv;
void draw_rectangle(int event, int x, int y, int flags, void*);
Mat firstFrame;
Point previousPoint, currentPoint;
Rect2d bbox;
int main(int argc, char *argv[])
{
VideoCapture capture;
Mat frame;
frame = capture.open("/home/w/mycode/QT/img/runners.avi");
if(!capture.isOpened())
{
printf("can not open ...\n");
return -1;
}
//获取视频的第一帧,并框选目标
capture.read(firstFrame);
if(!firstFrame.empty())
{
namedWindow("output", WINDOW_AUTOSIZE);
imshow("output", firstFrame);
setMouseCallback("output", draw_rectangle, 0);
waitKey();
}
//使用TrackerMIL跟踪
Ptr tracker= TrackerMIL::create();
//Ptr tracker= TrackerTLD::create();
//Ptr tracker = TrackerKCF::create();
//Ptr tracker = TrackerMedianFlow::create();
//Ptr tracker= TrackerBoosting::create();
capture.read(frame);
tracker->init(frame,bbox);
namedWindow("output", WINDOW_AUTOSIZE);
while (capture.read(frame))
{
tracker->update(frame,bbox);
rectangle(frame,bbox, Scalar(255, 0, 0), 2, 1);
imshow("output", frame);
if(waitKey(20)=='q')
return 0;
}
capture.release();
destroyWindow("output");
return 0;
}
//框选目标
void draw_rectangle(int event, int x, int y, int flags, void*)
{
if (event == EVENT_LBUTTONDOWN)
{
previousPoint = Point(x, y);
}
else if (event == EVENT_MOUSEMOVE && (flags&EVENT_FLAG_LBUTTON))
{
Mat tmp;
firstFrame.copyTo(tmp);
currentPoint = Point(x, y);
rectangle(tmp, previousPoint, currentPoint, Scalar(0, 255, 0, 0), 1, 8, 0);
imshow("output", tmp);
}
else if (event == EVENT_LBUTTONUP)
{
bbox.x = previousPoint.x;
bbox.y = previousPoint.y;
bbox.width = abs(previousPoint.x-currentPoint.x);
bbox.height = abs(previousPoint.y-currentPoint.y);
}
else if (event == EVENT_RBUTTONUP)
{
destroyWindow("output");
}
}
实验对比发现:KCF速度最快,MedianFlow的速度也较快,对于无遮挡情况跟踪效果较好;TLD对部分遮挡处理的效果最好,处理时间相对较慢.
MIL对部分遮挡的处理效果:
KCF对部分遮挡的处理效果:
TLD对部分遮挡的处理效果:
opencv::Tracker Algorithms