基於OpenCV的多目標動態檢測與跟蹤

2021-07-25 09:23:31 字數 2775 閱讀 6999

1、源**

#include "stdafx.h"

#include #include #include #include #include #include #include #include #include #include#include //跟蹤引數

const double mhi_duration = 0.5;//最大跟蹤時間

const double max_time_delta = 0.5;

const double min_time_delta = 0.05;

const int n = 3;

const int contour_max_aera = 100;//矩形面積

iplimage **buf = 0;

int last = 0;

int flag;

iplimage *mhi = 0; // mhi: motion history image

cvconnectedcomp *cur_comp, min_comp;

cvconnectedcomp comp;

cvmemstorage *storage;

cvpoint pt[4];

// dst – 檢測結果

void update_mhi(iplimage* img, iplimage* dst, int diff_threshold)

for (i = 0; i < n; i++)

cvreleaseimage(&mhi);

mhi = cvcreateimage(size, ipl_depth_32f, 1);

cvzero(mhi);

} cvcvtcolor(img, buf[last], cv_bgr2gray); //rgb->gray

idx1 = last;

idx2 = (last + 1) % n;

last = idx2;

// 做幀差

silh = buf[idx2];

cvabsdiff(buf[idx1], buf[idx2], silh); //兩幀差異

// 對差影象做二值化

cvthreshold(silh, silh, 30, 255, cv_thresh_binary); //src(x,y)>threshold ,dst(x,y) = max_value; 否則,dst(x,y)=0;

cvupdatemotionhistory(silh, mhi, timestamp, mhi_duration); //更新畫素點的運動歷史

cvcvtscale(mhi, dst, 255. / mhi_duration,

(mhi_duration - timestamp)*255. / mhi_duration);//timestamp是時間戳;mhi_duration,獲得的是當前時間

cvcvtscale(mhi, dst, 255. / mhi_duration, 0);

// 中值濾波,消除小的雜訊

cvsmooth(dst, dst, cv_median, 3, 0, 0, 0);

// 向下取樣,去掉雜訊

cvpyrdown(dst, pyr, 7);

cvdilate(pyr, pyr, 0, 1); // 做膨脹操作,消除目標的不連續空洞

cvpyrup(pyr, dst, 7);

// // 下面的程式段用來找到輪廓

// // create dynamic structure and sequence.

stor = cvcreatememstorage(0);

cont = cvcreateseq(cv_seq_eltype_point, sizeof(cvseq), sizeof(cvpoint), stor);

// 找到所有輪廓

cvfindcontours(dst, stor, &cont, sizeof(cvcontour),

// 直接使用contour中的矩形來畫輪廓

for (; cont; cont = cont->h_next)

else

}cvreleasememstorage(&stor);

cvreleaseimage(&pyr);

}int main(int argc, char** argv)

}update_mhi(image, motion, 6);

cvputtext(image, t_y, cvpoint(10, 25), &font, cv_rgb(255, 0, 0));

cvshowimage("motion", image);

if (cvwaitkey(10) >= 0)

break;

}cvreleasecapture(&capture);

cvdestroywindow("motion");

} }return 0;

}

2、實驗結果

基於Opencv的多目標跟蹤

python實現 import cv2 import sys major ver,minor ver,subminor ver cv2.version split print major ver,minor ver,subminor ver if name main 建立 boosting mil ...

OpenCV下的多目標跟蹤

opencv下的多目標跟蹤 flyfish 執行環境 qt 5.12.2 os ubuntu 18.04 opencv 4.1.0 opencv contrib 4.1.0 用滑鼠畫出你要跟蹤的目標,按enter鍵,畫下乙個框,按esc退出畫框開始執行跟蹤 將原例項 改造成使用攝像頭跟蹤 inclu...

opencv3之目標跟蹤(單目標 多目標)

對於剛入門的opencv玩家,提起目標跟蹤,馬上想起的就是camshift,但是camshift跟蹤往往達不到我們的跟蹤要求,包括穩定性和準確性。opencv3.1版本發行後,整合了多個跟蹤演算法,即tracker,大部分都是近年vot競賽榜上有名的演算法,雖然仍有缺陷存在,但效果還不錯。ps 我在...