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','kcf', 'tld', 'medianflow', 'goturn', 'mosse'
tracker_type =
'mil'
tracker = cv2.multitracker_create(
)#獲得追蹤的初始化結果
# 建立視窗
cv2.namedwindow(
"tracking"
)#給視窗命名
video = cv2.videocapture(
"./pig/001.mp4"
)# 讀入第一幀
ok, frame = video.read()if
not ok:
print()
sys.exit(
)# 定義乙個bounding box
#多目標跟蹤,這裡定義3個box
box1 = cv2.selectroi(
"tracking"
, frame)
#在第一幀上進行選框操作
box2 = cv2.selectroi(
"tracking"
, frame)
box3 = cv2.selectroi(
"tracking"
, frame)
# 用第一幀初始化
ok = tracker.add(cv2.trackermil_create(
), frame, box1)
ok1 = tracker.add(cv2.trackermil_create(
), frame, box2)
ok2 = tracker.add(cv2.trackermil_create(
), frame, box3)
while
true
: ok, frame = video.read()if
not ok:
break
# 計時
timer = cv2.gettickcount(
)# 更新追蹤器tracker
ok, boxes = tracker.update(frame)
print
(ok, boxes)
# 計算fps
fps = cv2.gettickfrequency()/
(cv2.gettickcount(
)- timer)
for box in boxes:
# 畫bonding box
if ok:
p1 =
(int
(box[0]
),int(box[1]
))p2 =
(int
(box[0]
+ box[2]
),int(box[1]
+ box[3]
))cv2.rectangle(frame, p1, p2,
(255,0
,0),
2,1)
else
: cv2.puttext(frame,
"tracking failed detected",(
100,80)
, cv2.font_hershey_******x,
0.75,(
0,0,
255),2
)# 展示tracker型別
cv2.puttext(frame, tracker_type+
"tracker",(
100,20)
, cv2.font_hershey_******x,
0.75,(
50,170,50)
,2)# 展示fps
cv2.puttext(frame,
"fps:"
+str
(fps),(
100,50)
, cv2.font_hershey_******x,
0.75,(
50,170,50)
,2)# result
cv2.imshow(
"tracking"
, frame)
# exit
k = cv2.waitkey(1)
&0xff
if k ==27:
break
跟蹤結果:
OpenCV下的多目標跟蹤
opencv下的多目標跟蹤 flyfish 執行環境 qt 5.12.2 os ubuntu 18.04 opencv 4.1.0 opencv contrib 4.1.0 用滑鼠畫出你要跟蹤的目標,按enter鍵,畫下乙個框,按esc退出畫框開始執行跟蹤 將原例項 改造成使用攝像頭跟蹤 inclu...
基於OpenCV的多目標動態檢測與跟蹤
1 源 include stdafx.h include include include include include include include include include include include 跟蹤引數 const double mhi duration 0.5 最大跟蹤時間...
多目標跟蹤
目前參與的乙個專案是 足球事件檢索 事件是乙個 high level 概念,需要基於一些 low level 資訊來做。後者主要就是一些影象處理的東西了。兩個 level 之間,會有一些中間層的資訊,比如足球的走向。根據師兄的建議,可以考慮對足球場上的運動員進行多目標跟蹤,在跟蹤軌跡的基礎上抽取一些...