opencv3/C++ 使用Tracker實(shí)現(xiàn)簡(jiǎn)單目標(biāo)跟蹤
簡(jiǎn)介
MIL: TrackerMIL 以在線方式訓(xùn)練分類器將對(duì)象與背景分離;多實(shí)例學(xué)習(xí)避免魯棒跟蹤的漂移問題.
OLB: TrackerBoosting 基于AdaBoost算法的在線實(shí)時(shí)對(duì)象跟蹤.分類器在更新步驟中使用周圍背景作為反例以避免漂移問題.
MedianFlow: TrackerMedianFlow 跟蹤器適用于非常平滑和可預(yù)測(cè)的運(yùn)動(dòng),物體在整個(gè)序列中可見.
TLD: TrackerTLD 將長(zhǎng)期跟蹤任務(wù)分解為跟蹤,學(xué)習(xí)和檢測(cè).跟蹤器在幀之間跟蹤對(duì)象.探測(cè)器本地化所觀察到的所有外觀,并在必要時(shí)糾正跟蹤器.學(xué)習(xí)估計(jì)檢測(cè)器的錯(cuò)誤并進(jìn)行更新以避免再出現(xiàn)這些錯(cuò)誤.追蹤器能夠處理快速運(yùn)動(dòng),部分遮擋,物體缺失等情況.
KCF: TrackerKCF 使用目標(biāo)周圍區(qū)域的循環(huán)矩陣采集正負(fù)樣本,利用脊回歸訓(xùn)練目標(biāo)檢測(cè)器,并成功的利用循環(huán)矩陣在傅里葉空間可對(duì)角化的性質(zhì)將矩陣的運(yùn)算轉(zhuǎn)化為向量的Hadamad積,即元素的點(diǎn)乘,大大降低了運(yùn)算量,提高了運(yùn)算速度,使算法滿足實(shí)時(shí)性要求.
部分相關(guān)API:
TrackerMIL
static Ptr<TrackerMIL> create(const TrackerMIL::Params ¶meters); CV_WRAP static Ptr<TrackerMIL> create();
struct CV_EXPORTS Params
{
PARAMS();
//采樣器的參數(shù)
float samplerInitInRadius; //初始收集正面實(shí)例的半徑
int samplerInitMaxNegNum; //初始使用負(fù)樣本
float samplerSearchWinSize; //搜索窗口的大小
float samplerTrackInRadius; //在跟蹤期間收集正面實(shí)例的半徑
int samplerTrackMaxPosNum; //在追蹤期間使用正面樣本
int samplerTrackMaxNegNum; //在跟蹤期間使用的負(fù)樣本
int featureSetNumFeatures; //特征
void read(const FileNode&fn);
void write(FileStorage&fs)const;
};
TrackerBoosting
static Ptr<TrackerBoosting> create(const TrackerBoosting::Params ¶meters); CV_WRAP static Ptr<TrackerBoosting> create();
struct CV_EXPORTS Params
{
PARAMS();
int numClassifiers; //在OnlineBoosting算法中使用的分類器的數(shù)量
float samplerOverlap; //搜索區(qū)域參數(shù)
float samplerSearchFactor; //搜索區(qū)域參數(shù)
int iterationInit; //初始迭代
int featureSetNumFeatures; //特征
//從文件讀取參數(shù)
void read(const FileNode&fn);
//從文件寫入?yún)?shù)
void write(FileStorage&fs)const;
};
示例
首先獲取視頻的第一幀,通過點(diǎn)擊左鍵框選選擇要跟蹤的目標(biāo),點(diǎn)擊右鍵確認(rèn)并使用MIL開始跟蹤.(從實(shí)際情況看來,算法對(duì)過程中有遮擋的情況跟蹤能力較差.)
(環(huán)境:Ubuntu16.04+QT5.8+opencv3.3.1)
#include <opencv2/opencv.hpp>
#include <opencv2/video.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/tracking/tracker.hpp>
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;
}
//獲取視頻的第一幀,并框選目標(biāo)
capture.read(firstFrame);
if(!firstFrame.empty())
{
namedWindow("output", WINDOW_AUTOSIZE);
imshow("output", firstFrame);
setMouseCallback("output", draw_rectangle, 0);
waitKey();
}
//使用TrackerMIL跟蹤
Ptr<TrackerMIL> tracker= TrackerMIL::create();
//Ptr<TrackerTLD> tracker= TrackerTLD::create();
//Ptr<TrackerKCF> tracker = TrackerKCF::create();
//Ptr<TrackerMedianFlow> tracker = TrackerMedianFlow::create();
//Ptr<TrackerBoosting> 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;
}
//框選目標(biāo)
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");
}
}



實(shí)驗(yàn)對(duì)比發(fā)現(xiàn):KCF速度最快,MedianFlow的速度也較快,對(duì)于無遮擋情況跟蹤效果較好;TLD對(duì)部分遮擋處理的效果最好,處理時(shí)間相對(duì)較慢.
部分遮擋處理效果
MIL對(duì)部分遮擋的處理效果:


以上這篇opencv3/C++ 使用Tracker實(shí)現(xiàn)簡(jiǎn)單目標(biāo)跟蹤就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C語言實(shí)現(xiàn)掃雷小游戲(擴(kuò)展版可選擇游戲難度)
游戲目標(biāo)是找出所有沒有地雷的方格,完成游戲;要是按了有地雷的方格,游戲失敗;玩家可標(biāo)記雷的位置,游戲以完成時(shí)間來評(píng)高低,并且用戶可以選擇游戲難度2019-10-10
C++中l(wèi)ist的使用方法及常用list操作總結(jié)
這篇文章主要介紹了C++中l(wèi)ist的使用方法及常用list操作總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-06-06
利用C語言實(shí)踐OOP,以及new,delete的深入分析
本篇文章是對(duì)用C語言實(shí)踐OOP,new,delete進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C語言如何利用ASCII碼表統(tǒng)計(jì)字符串每個(gè)字符出現(xiàn)的次數(shù)
這篇文章主要介紹了C語言如何利用ASCII碼表統(tǒng)計(jì)字符串每個(gè)字符出現(xiàn)的次數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
C++實(shí)現(xiàn)分?jǐn)?shù)計(jì)算器
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)分?jǐn)?shù)計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
淺析char 指針變量char *=p 這個(gè)語句的輸出問題
下面小編就為大家?guī)硪黄獪\析char 指針變量char *=p 這個(gè)語句的輸出問題。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-05-05

