![]()
在这篇文章中,我们将介绍如何使用通过 MultiTracker 类实现的 OpenCV 的多对象跟踪 API。我们将共享C++ 和 Python 代码。
大多数计算机视觉和机器学习的初学者都学习对象检测。如果您是初学者,您可能会想为什么我们需要对象跟踪。我们不能只检测每一帧中的对象吗?
让我们来探究一下跟踪是有用的几个原因。
首先,当在视频帧中检测到多个对象(例如人)时,跟踪有助于跨帧建立对象的身份。
其次,在某些情况下,对象检测可能会失败,但仍可能跟踪对象,因为跟踪考虑了对象在前一帧中的位置和外观。
第三,一些跟踪算法非常快,因为它们做的是局部搜索,而不是全局搜索。因此,我们可以通过每n帧进行目标检测,并在中间帧中跟踪目标,从而为我们的系统获得很高的帧率。
那么,为什么不在第一次检测后无限期地跟踪对象呢?跟踪算法有时可能会丢失它正在跟踪的对象。例如,当对象的运动太大时,跟踪算法可能跟不上。许多现实世界的应用程序同时使用检测和跟踪。
在本教程中,我们只关注跟踪部分。我们想要跟踪的对象将通过拖动它们周围的包围框来指定。
OpenCV 中的 MultiTracker 类提供了多目标跟踪的实现。它是一个简单的实现,因为它独立处理跟踪对象,而不对跟踪对象进行任何优化。
让我们逐步查看代码,了解如何使用 OpenCV 的多目标跟踪 API。
2.1 第 1 步:创建单一对象跟踪器
多目标跟踪器只是单目标跟踪器的集合。我们首先定义一个函数,该函数接受一个跟踪器类型作为输入,并创建一个跟踪器对象。OpenCV有8种不同的跟踪器类型:BOOSTING, MIL, KCF,TLD, MEDIANFLOW, GOTURN, MOSSE, CSRT。
如果您想使用 GOTURN 跟踪器,请务必阅读这篇文章并下载 caffe 模型。
在下面的代码中,给定跟踪器类的名称,我们返回跟踪器对象。这将在稍后用于多目标跟踪器。
Python
C++
2.2 第 2 步:读取视频的第一帧
多目标跟踪器需要两个输入
给定这些信息,跟踪器在所有后续帧中跟踪这些指定对象的位置。 在下面的代码中,我们首先使用 VideoCapture 类加载视频并读取第一帧。这将在稍后用于初始化 MultiTracker。
Python
C++
2.3 第 3 步:在第一帧中定位对象
接下来,我们需要在第一帧中定位我们想要跟踪的对象。该位置只是一个边界框。 OpenCV 提供了一个名为 selectROI 的函数,该函数会d出一个 GUI 来选择边界框(也称为感兴趣区域 (ROI))。 在 C++ 版本中,selectROI 允许您获取多个边界框,但在 Python 版本中,它只返回一个边界框。所以,在 Python 版本中,我们需要一个循环来获取多个边界框。 对于每个对象,我们还选择一种随机颜色来显示边界框。 代码如下所示。
Python
C++
getRandomColors 函数相当简单
2.4 第 3 步:初始化 MultiTracker
到目前为止,我们已经读取了第一帧并获得了对象周围的边界框。这就是我们初始化多目标跟踪器所需的所有信息。
我们首先创建一个 MultiTracker 对象,并向其中添加与边界框一样多的单个对象跟踪器。在此示例中,我们使用 CSRT 单对象跟踪器,但您可以通过将下面的 trackerType 变量更改为本文开头提到的 8 个跟踪器之一来尝试其他跟踪器类型。 CSRT 跟踪器不是最快的,但在我们尝试的许多情况下它产生了最好的结果。
您还可以使用包裹在同一个 MultiTracker 中的不同跟踪器,但当然,这没什么意义。
MultiTracker 类只是这些单个对象跟踪器的包装器。正如我们从上一篇文章中知道的那样,单个对象跟踪器是使用第一帧初始化的,并且边界框指示我们想要跟踪的对象的位置。 MultiTracker 将此信息传递给它在内部包装的单个对象跟踪器。
Python
C++
2.5 第 4 步:更新 MultiTracker 并显示结果
最后,我们的 MultiTracker 已准备就绪,我们可以在新帧中跟踪多个对象。我们使用 MultiTracker 类的 update 方法来定位新框架中的对象。每个跟踪对象的每个边界框都使用不同的颜色绘制。
Python
C++
C++
Python
下面是camshift物体追踪代码,需要你用avi视频测试,matlab对avi视频格式要求比较严格。但是可以试试mmreader函数读取视频。
% Adam Kukucka
% Zach Clay
% Marcelo Molina
% CSE 486 Project 3
function [ trackmov probmov centers ] = camshift
% ******************************************************************
% initialize vari ables
% ******************************************************************
rmin = 0%min row value for search window
rmax = 0%max row value for search window
cmin = 0%min col value for search window
cmax = 0%max col value for search window
numofframes = 0%number of frames in the avi
threshold = 1%threshold for convergence
centerold = [0 0]%for convergence... previous center of window
centernew = [0 0]%for convergence... new center of window
% ******************************************************************
% Pre code... load movie and select initial frame
% ******************************************************************
% prompt user for avi file name
%%%%%user_entry = input('Please enter an avi filename: ','s')
% load the avi file... handle is M
%%%%M = aviread(user_entry)
M=aviread('8888.avi')
% get number of frames
[dontneed numberofframes] = size(M)
% initialize matrix to hold center coordinates
imagecenters = zeros(numberofframes, 2)
% extract the first frame from the avi
Frame1 = M(1,1)
Image1 = frame2im(Frame1)
%%% ********** images(:, :, numberofframes) = G(:,:)
% get search window for first frame
[ cmin, cmax, rmin, rmax ] = select( Image1 )
cmin = round(cmin)
cmax = round(cmax)
rmin = round(rmin)
rmax = round(rmax)
wsize(1) = abs(rmax - rmin)
wsize(2) = abs(cmax - cmin)
% create histogram
% translate to hsv
hsvimage = rgb2hsv(Image1)
% pull out the h
huenorm = hsvimage(:,:,1)
% scale to 0 to 255
hue = huenorm*255
% set unit type
hue=uint8(hue)
% Getting Histogram of Image:
histogram = zeros(256)
for i=rmin:rmax
for j=cmin:cmax
index = uint8(hue(i,j)+1)
%count number of each pixel
histogram(index) = histogram(index) + 1
end
end
% ******************************************************************
% Algorithm from pdf
% ******************************************************************
aviobj1 = avifile('example3.avi')
aviobj2 = avifile('example4.avi')
% for each frame
for i = 1:200
disp('Processing frame')
disp(i)
Frame = M(1, i)
I = frame2im(Frame)
% translate to hsv
hsvimage = rgb2hsv(I)
% pull out the h
huenorm = hsvimage(:,:,1)
% scale to 0 to 255
hue = huenorm*255
% set unit type
hue=uint8(hue)
[rows cols] = size(hue)
% choose initial search window
% the search window is (cmin, rmin) to (cmax, rmax)
% create a probability map
probmap = zeros(rows, cols)
for r=1:rows
for c=1:cols
if(hue(r,c) ~= 0)
probmap(r,c)= histogram(hue(r,c))
end
end
end
probmap = probmap/max(max(probmap))
probmap = probmap*255
count = 0
rowcenter = 0 % any number just so it runs through at least twice
colcenter = 0
rowcenterold = 30
colcenterold = 30
% Mean Shift for 15 iterations or until convergence(the center doesnt
% change)
while (((abs(rowcenter - rowcenterold) >2) &&(abs(colcenter - colcenterold) >2)) || (count <15) )
%for j = 1:5
%disp('meanshift')
% disp(j)
rmin = rmin - 7 %increase window size and check for center
rmax = rmax + 7
cmin = cmin - 7
cmax = cmax + 7
rowcenterold = rowcenter%save old center for convergence check
colcenterold = colcenter
[ rowcenter colcenter M00 ] = meanshift(I, rmin, rmax, cmin,...
cmax, probmap)
% given image (I), search window(rmin rmax cmin cmax)
% returns new center (colcenter, rowcenter) for window and
% zeroth moment (Moo)
% redetermine window around new center
rmin = round(rowcenter - wsize(1)/2)
rmax = round(rowcenter + wsize(1)/2)
cmin = round(colcenter - wsize(2)/2)
cmax = round(colcenter + wsize(2)/2)
wsize(1) = abs(rmax - rmin)
wsize(2) = abs(cmax - cmin)
count = count + 1
end
% mark center on image
%save image
G = .2989*I(:,:,1)...
+.5870*I(:,:,2)...
+.1140*I(:,:,3)
trackim=G
%make box of current search window on saved image
for r= rmin:rmax
trackim(r, cmin) = 255
trackim(r, cmax) = 255
end
for c= cmin:cmax
trackim(rmin, c) = 255
trackim(rmax, c) = 255
end
aviobj1 = addframe(aviobj1,trackim)
aviobj2 = addframe(aviobj2,probmap)
%create image movie, and probability map movie
trackmov(:,:,i)= trackim(:,:)
probmov(:,:,i) = probmap(:,:)
% save center coordinates as an x, y by doing col, row
centers(i,:) = [colcenter rowcenter]
% Set window size = 2 * (Moo/256)^1/2
windowsize = 2 * (M00/256)^.5
% get side length ... window size is an area so sqrt(Area)=sidelength
sidelength = sqrt(windowsize)
% determine rmin, rmax, cmin, cmax
rmin = round(rowcenter-sidelength/2)
rmax = round(rowcenter+sidelength/2)
cmin = round(colcenter-sidelength/2)
cmax = round(colcenter+sidelength/2)
wsize(1) = abs(rmax - rmin)
wsize(2) = abs(cmax - cmin)
end
% end for loop
% Adam Kukucka
% Zach Clay
% Marcelo Molina
% CSE 486 Project 3
function [ rowcenter colcenter M00 ] = meanshift(I, rmin, rmax, cmin,...
cmax, probmap)
%inputs
% rmin, rmax, cmin, cmax are the coordiantes of the window
% I is the image
%outputs
% colcenter rowcenter are the new center coordinates
% Moo is the zeroth mean
% **********************************************************************
% initialize
% **********************************************************************
M00 = 0%zeroth mean
M10 = 0%first moment for x
M01 = 0%first moment for y
histdim = (0:1:255)% dimensions of histogram... 0 to 255, increment by 1
[rows cols] = size(I)
cols = cols/3% **********************8
% **********************************************************************
% Main code
% **********************************************************************
% determine zeroth moment
for c = cmin:cmax
for r = rmin:rmax
M00 = M00 + probmap(r, c)
end
end
% determine first moment for x(col) and y(row)
for c = cmin:cmax
for r = rmin:rmax
M10 = M10 + c*probmap(r,c)
M01 = M01 + r*probmap(r,c)
end
end
% determine new centroid
% x is cols
colcenter = M10/M00
% y is rows
rowcenter = M01/M00
% Adam Kukucka
% Zach Clay
% Marcelo Molina
% CSE 486 Project 3
function [ cmin, cmax, rmin, rmax ] = select( I )
%UNTITLED1 Summary of this function goes here
% Detailed explanation goes here
% for array... x is cols, y is rows
image(I)
k = waitforbuttonpress
point1 = get(gca,'CurrentPoint') %mouse pressed
rectregion = rbbox
point2 = get(gca,'CurrentPoint')
point1 = point1(1,1:2) % extract col/row min and maxs
point2 = point2(1,1:2)
lowerleft = min(point1, point2)
upperright = max(point1, point2)
cmin = round(lowerleft(1))
cmax = round(upperright(1))
rmin = round(lowerleft(2))
rmax = round(upperright(2))
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)