**SAMF tracker代码阅读笔记(matlab)**如有错误,请不吝赐教
function [positions, rect_results, time] = tracker(video_path, img_files, pos, target_sz, ...
padding, kernel, lambda, output_sigma_factor, interp_factor, cell_size, ...
features, show_visualization)
简单的定义function
1.addpath('./utility');
2.temp = load('w2crs');
3.w2c = temp.w2crs;
1.添加脚本文件utility到工作路径
2.temp是一个返回值,load是将w2crs变量加载到工作区
3.w2crs,一个数据集 需要matlab分帧的话可以点击
链接: [link]https://download.csdn.net/download/CloudVans/20354273?spm=1001.2014.3001.5503
4.resize_image = (sqrt(prod(target_sz)) >= 100);
(sqrt(prod(target_sz))是追踪框尺寸
resize_image是图像缩放函数,后半部分是对角线尺寸≥是阈值
5. if resize_image
6. pos = floor(pos / 2);
7. target_sz = floor(target_sz / 2);
8. end
9. target_sz_back = target_sz;
10. window_sz = floor(target_sz * (1 + padding));
11. current_size =1;
target_sz是目标初始大小
window_sz是窗口的尺寸
pos是目标初始位置
padding:对目标附近附加跟踪区域
10.窗口大小,考虑填充
11.设置初始尺寸为1,但是后面没用到这个赋值
12.output_sigma = sqrt(prod(target_sz)) * output_sigma_factor / cell_size;
用空间带宽,像素大小,跟踪框尺寸计算高斯标签的带宽
13.yf = fft2(gaussian_shaped_labels(output_sigma, floor(window_sz / cell_size)));
fft2是傅里叶变换,用于返回矩阵
14.cos_window = hann(size(yf,1)) * hann(size(yf,2))';
生成汉宁窗cos_window,尺寸与yf相同,即floor(window_sz / cell_size),检测范围window_sz中cell的个数。
15.search_size = [1 0.985 0.99 0.995 1.005 1.01 1.015];
没读懂
16. if show_visualization
17. update_visualization = show_video(img_files, video_path, resize_image);
18. end
创建视频界面,对上一个可视化进行调整
show_video是将跟踪结果可视化,通过后面的几个参数生成新视频界面
time = 0;
用于计时
19.positions = zeros(numel(img_files), 2);
20.rect_results = zeros(numel(img_files), 4);
21.response = zeros(size(cos_window,1),size(cos_window,2),size(search_size,2);
22.szid = 1;
zeros函数用于返回全零矩阵
19.计算每帧目标的中心位置
20.结果矩阵
21.得到了一个回应的行矩阵
22.这个赋值没懂
23.for frame = 1:numel(img_files)
24.im = imread([video_path img_files{frame}]);
23.简单的for循环从第一帧到最后一帧
24.读取图片为im
25.if resize_image
26. im = imresize(im, 0.5);
27.end
将图片变成0.5倍返还给im,a= imresize(b, c);将b的大小变成c给a
28.tic()
计时开始
29. if frame > 1
30. for i=1:size(search_size,2)
31. tmp_sz = floor((target_sz * (1 + padding))*search_size(i));
32. param0 = [pos(2), pos(1), tmp_sz(2)/window_sz(2), 0,...
33. tmp_sz(1)/window_sz(2)/(window_sz(1)/window_sz(2)),0];
34. param0 = affparam2mat(param0);
35. patch = uint8(warpimg(double(im), param0, window_sz));
36. zf = fft2(get_features(patch, features, cell_size, cos_window,w2c));
37. switch kernel.type
38. case 'gaussian'
39. kzf = gaussian_correlation(zf, model_xf, kernel.sigma);
40. case 'polynomial'
41. kzf = polynomial_correlation(zf, model_xf, kernel.poly_a, kernel.poly_b);
42. case 'linear'
43. kzf = linear_correlation(zf, model_xf);
44. end
45. response(:,:,i) = real(ifft2(model_alphaf .* kzf));
46. end
从第二帧开始进入循环,计算分类器在所有班次的响应,回应快速检测方程
47.[vert_delta,tmp, horiz_delta] = find(response == max(response(:)), 1);
47. szid = floor((tmp-1)/(size(cos_window,2)))+1;
49.horiz_delta = tmp - ((szid -1)* size(cos_window,2));
没读懂,猜测是目标在最大响应值,峰值出现在左上角而不是中间的处理方法
48. if vert_delta > size(zf,1) / 2
49. vert_delta = vert_delta - size(zf,1);
50. end
51. if horiz_delta > size(zf,2) / 2
52. horiz_delta = horiz_delta - size(zf,2);
53. end
54. tmp_sz = floor((target_sz * (1 + padding))*search_size(szid));
55. current_size = tmp_sz(2)/window_sz(2);
56. pos = pos + current_size*cell_size * [vert_delta - 1, horiz_delta - 1];
57. end
垂直与水平的矫正
58. target_sz = target_sz * search_size(szid);
59. tmp_sz = floor((target_sz * (1 + padding)));
60. param0 = [pos(2), pos(1), tmp_sz(2)/window_sz(2), 0,...
61. tmp_sz(1)/window_sz(2)/(window_sz(1)/window_sz(2)),0];
62. param0 = affparam2mat(param0);
63. patch = uint8(warpimg(double(im), param0, window_sz));
64. xf = fft2(get_features(patch, features, cell_size, cos_window,w2c));
在新估计的目标位置获得用于训练的子窗口
65.switch kernel.type
66. case 'gaussian'
67. kf = gaussian_correlation(xf, xf, kernel.sigma);
68. case 'polynomial'
69. kf = polynomial_correlation(xf, xf, kernel.poly_a, kernel.poly_b);
70. case 'linear'
71. kf = linear_correlation(xf, xf);
72. end
73. alphaf = yf ./ (kf + lambda);
核岭回归,计算 alphas(在傅立叶域中)
74. if frame == 1
75. model_alphaf = alphaf;
76. model_xf = xf;
77. else
78. model_alphaf = (1 - interp_factor) * model_alphaf + interp_factor * alphaf;
79. model_xf = (1 - interp_factor) * model_xf + interp_factor * xf;
80. end
第一帧,用单个图像训练,后续帧用后续插值模型
81.positions(frame,:) = pos;
82.time = time + toc();
83.box = [pos([2,1]) - target_sz([2,1])/2, target_sz([2,1])];
84.rect_results(frame,:)=box;
保存位置和时间
85. if show_visualization
86. stop = update_visualization(frame, box);
87. if stop, break, end
88. drawnow
89. end
90. end
令结果可视化并跟新数据,处理回调>绘图
91. if resize_image
92. positions = positions * 2;
93. rect_results = rect_results*2;
94. end
最后再次调整图像大小