首先,dlib的安装在这里就不多赘述了,Dlib是一个现代c++工具包,包含机器学习算法和工具,用于用c++创建复杂的软件来解决现实世界中的问题。它被广泛应用于工业和学术界,包括机器人、嵌入式设备、移动电话和大型高性能计算环境。官网链接http://dlib.net/
dlib实现目标跟踪是基于DSST算法的,简单的说就是尺度滤波器和空间滤波器相结合的一个算法。
以下是为OTB50写的调用例子,修改name和path就可以实现
import os
import cv2
import numpy as np
import dlib
def num_read_directory(directory_name):
filename1=os.listdir(directory_name)
return len(filename1)
def read_directory(directory_name,num):
filename1=os.listdir(directory_name)
img = cv2.imread(directory_name + "/" + filename1[num],cv2.IMREAD_COLOR)
return img
if __name__ == '__main__':
name="Biker"
path1="H:/tracking/OTB50/"+name+"/img/"
path2="H:/tracking/OTB50/"+name+"/groundtruth_rect.txt"
num_image=num_read_directory(path1)
print(num_image)
img=read_directory(path1,0)
print(img.shape)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter("H:/tracking/OTB50/"+name+"/DSST_out.avi",fourcc, 24, (img.shape[1],img.shape[0]))
f = open(path2)
frames=np.zeros([num_image,4])
j=0
for i in f:
frames[j,:]=i.split(',')
j+=1
print(i)
f.close()
tracker1 = dlib.correlation_tracker()
img = read_directory(path1,0)
img_first=img.copy()
cv2.rectangle(img_first,(int(frames[0,0]),int(frames[0,1])),(int(frames[0,0]+frames[0,2]),int(frames[0,1]+frames[0,3])),(0,0,255),1)
tracker1.start_track(img,dlib.rectangle(int(frames[0,0]),int(frames[0,1]),int(frames[0,0]+frames[0,2]),int(frames[0,1]+frames[0,3])))
"""
cv2.namedWindow('bak',cv2.WINDOW_AUTOSIZE)
cv2.imshow('bak',img_first)
k=cv2.waitKey(0)
"""
cv2.namedWindow('image',cv2.WINDOW_AUTOSIZE)
cv2.imshow('image', img_first)
for i in range(num_image-1):
image=read_directory(path1,i+1)
tracker1.update(image)
box1_predict = tracker1.get_position()
cv2.rectangle(image, (int(box1_predict.left()), int(box1_predict.top())),
(int(box1_predict.right()), int(box1_predict.bottom())), (0, 255, 255), 3)
cv2.imshow('image', image)
out.write(image)
c = cv2.waitKey(10) & 0xff
if c == 27: break
cv2.destroyAllWindows()
实现效果并不佳,一般适用于变化较小的环境。以下是结果的展示:
前期能追上,后边跟踪失效,一旦跟踪失效就再也找不到目标,这也是这一类跟踪器的弊端之一