原OTB并没有包含KCF,所以这里以KCF为例子,介绍如何将自己的算法加入到OTB中,
KCF的源码可以去http://www.robots.ox.ac.uk/~joao/circulant/下载
点击matlab code 即可下载matlab版本的KCF。
下载完毕后,解压缩文件,并把KCF的matlab源码放到benchmark的文件夹trackers中。如图
然后打开util文件夹中的configTrackers.m,并将KCF跟踪算法添加到trackers1这个结构体中。添加的形式如下:
trackers1={
struct('name','SRDCF','namePaper','SRDCF'),...
struct('name','KCF','namePaper','KCF'),...
struct('name','DSST','namePaper','DSST'),...
struct('name','Struck','namePaper','Struck')};
说明一下configTrackers.m文件中放的是所有的跟踪算法。算法对比的添加删除都是在此完成。
就是需要写一个将KCF的输入输出对接到benchmark中的脚本run_tracker.m文件,打开trackers文件夹中的其他算法时会发现每个算法中都会有 run_(这里是算法名字).m 文件,例如CT中的 run_CT.m
参照其它算法 例如:run_CT.m对比Runtracker.m文件 在第一行的输入和最后几行的输出进行统一,
function results=run_CT(seq, res_path, bSaveImage)
results.res=res;
results.type='rect';
results.fps=(seq.len-1)/duration;
disp(['fps: ' num2str(results.fps)])
PS:其实,写的run_().m接口主要就是特别注意以上信息。其中seq包含的主要是测试序列的各种信息。
res_path 和 bSaveImage主要是保存的图片,即BB框画框图。
再详细点说,就是在KCF的文件夹下新建一个run_KCF.m
然后将run_tracker.m的代码复制过来并修改之。
%第一行保证OTB能找到你的算法
function results=run_XX(seq, res_path, bSaveImage)
**
**
rects = [positions(:,2) - target_sz(2)/2, positions(:,1) - target_sz(1)/2];
rects(:,3) = target_sz(2);
rects(:,4) = target_sz(1);
fps = numel(img_files) / time;
results.type = 'rect';
results.res = rects;%each row is a rectangle
results.fps = fps;
%**最后的这几行保证能将result返回给OTB**
这里贴出我根据自己的需要精简删除修改后的run_KCF.m。
(PS:这里的[positions , time] = tracker()其实就是调用KCF了,里面的各种参数可能你用不上,就要进行修改调整)
**
**
最应该注意的地方就是原算法中seq 以及第一帧pos的获取的方式,将原算法中这两个参数的获取方式改成由你的function results=run_XX(seq, res_path, bSaveImage)中的seq 提供,而不是由原算法通过别的函数提供。差不多就是这个意思吧
function results = run_KCF(seq, res_path, bSaveImage)
kernel.type = 'gaussian';
padding = 1.5; %extra area surrounding the target
lambda = 1e-4; %regularization
output_sigma_factor = 0.1; %spatial bandwidth (proportional to target)
interp_factor = 0.02;
kernel.sigma = 0.5;
kernel.poly_a = 1;
kernel.poly_b = 9;
features.hog = true;
features.gray = false;
features.hog_orientations = 9;
cell_size = 4;
show_visualization = 0;
%%%%%这里我就将seq传给了seq_KCF (其实不用,这个多余了。)
%%%%%%然后直接通过调用关系,就可以将seq中的值传给(这里的seq是从OTB的main函数中来的)
%%%%%target_sz(目标大小)pos(目标位置)img_files(后续frames的地址)
seq_KCF = seq;
target_sz = seq_KCF.init_rect(1,[4,3]);
pos = seq_KCF.init_rect(1,[2,1]) + floor(target_sz/2);
img_files = seq_KCF.s_frames;
video_path = [];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% 上面的这些参数设置其实为tracker( );里面的各种参数进行设置服务,
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%这里的[positions , time] = tracker()其实就是调用KCF了,里面的各种参数可能你用不上,就要进行修改调整。
[positions , time] = tracker(video_path, img_files, pos, target_sz, ...
padding, kernel, lambda, output_sigma_factor, interp_factor, ...
cell_size, features,show_visualization);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if bSaveImage
imwrite(frame2im(getframe(gcf)),[res_path num2str(frame) '.jpg']);
end
%return results to benchmark, in a workspace variable
%下面这些处理就是为了将上面得到的[positions , time]变成OTB接受的返回值形式
rects = [positions(:,2) - target_sz(2)/2, positions(:,1) - target_sz(1)/2];
rects(:,3) = target_sz(2);
rects(:,4) = target_sz(1);
fps = numel(img_files) / time;
results.type = 'rect';
results.res = rects;%each row is a rectangle
results.fps = fps;
end
最后就是运行main_running.m函数。当然少不了类型的设置是TRE还是SRE,运行完main_running.m函数,得到的结果是在文件夹results中的results_SRE_CVPR13和results_TRE_CVPR13。(保存位置取决于你自己设置的位置)
首先跑一遍mian_running.m,让工作区有subS这个数据就好。如图
这时候就可以打开run_KCF.m了,把第一行和最后一行注释掉。
并修改seq_KCF = subS。
这样就可以用加断点的方式,如在下面这个代码处
[positions , time] = tracker(video_path, img_files, pos, target_sz, ...
padding, kernel, lambda, output_sigma_factor, interp_factor, ...
cell_size, features,show_visualization);
加断点运行判断有什么问题。
下面贴出完整的调试用run_KCF
%function results = run_KCF(seq, res_path, bSaveImage)
kernel_type = 'gaussian';
kernel.type = kernel_type;
padding = 1.5; %extra area surrounding the target
lambda = 1e-4; %regularization
output_sigma_factor = 0.1; %spatial bandwidth (proportional to target)
interp_factor = 0.02;
kernel.sigma = 0.5;
kernel.poly_a = 1;
kernel.poly_b = 9;
features.hog = true;
features.gray = false;
features.hog_orientations = 9;
cell_size = 4;
show_visualization = 0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%修改这里%%%%%%%%%%%%%%%%%%5
seq_KCF = subS;
% seq_KCF = seq;
target_sz = seq_KCF.init_rect(1,[4,3]);
pos = seq_KCF.init_rect(1,[2,1]) + floor(target_sz/2);
img_files = seq_KCF.s_frames;
video_path = [];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[positions , time] = tracker(video_path, img_files, pos, target_sz, ...
padding, kernel, lambda, output_sigma_factor, interp_factor, ...
cell_size, features,show_visualization);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if bSaveImage
imwrite(frame2im(getframe(gcf)),[res_path num2str(frame) '.jpg']);
end
%return results to benchmark, in a workspace variable
rects = [positions(:,2) - target_sz(2)/2, positions(:,1) - target_sz(1)/2];
rects(:,3) = target_sz(2);
rects(:,4) = target_sz(1);
fps = numel(img_files) / time;
results.type = 'rect';
results.res = rects;%each row is a rectangle
results.fps = fps;
% end