筆者近期研究目標跟蹤,涉及到運動表觀模型的動態建模,需要進行仿射變換。我閱讀了L1-APG和IVT算法、ALSA算法、SCM算法中的仿射變換,發現這幾種算法的仿射變換實現代碼有非常大的差異。其中IVT算法的仿射變換似乎在各種視覺跟蹤算法中用得更多,但是代碼上沒有做很詳細的解釋,我查找各類原創的論文、博客,似乎也沒有講清楚IVT仿射變換代碼實現的基本原理。IVT中用到affparam2mat.m這個函數進行仿射矩陣的編程,但是我發現這個代碼和一般的仿射變換公式推導對不上號,於是我自己試圖去揣測作者的想法,完整地重建出這個作者仿射變換矩陣的設計過程。IVT這個仿射變換的關鍵在於,第一:它假定仿射變換之前,那個矩形的中心是在原點的,第二:IVT的仿射是兩步完成的,先做旋轉切變,然後再做平移;第三:IVT仿射變換中的A矩陣,是採取奇異值分解的方式進行的,需要用到旋轉角,切變角,尺度變換因子,高寬比這四個隨機變量。因此,這個體系就和L1-APG完全不同,具體設計細節如下:
通過上述推導,再回過頭來看affparam2mat.m這個代碼,就非常清晰了,我們來看看這個實現過程,與我的推導是一一對應的:
function q = affparam2mat(p) % function q = affparam2mat(p) % % input : p, a 'geometric' affine parameter; % output : q, a 2x3 matrix; % % The functions affparam2geom and affparam2mat convert a 'geometric' % affine parameter from/to a matrix form (2x3 matrix). % % affparam2mat converts 6 affine parameters (x, y, th, scale, aspect, skew) to a 2x3 matrix, % and affparam2geom does the inverse. % % p(6) : [dx dy sc th sr phi]' % q(6) : [q(1) q(3) q(4); q(2) q(5) q(6)] % % Reference "Multiple View Geometry in Computer Vision" by Richard % Hartley and Andrew Zisserman.
% Copyright (C) Jongwoo Lim and David Ross. All rights reserved. % Thanks to Jongwoo Lim and David Ross for this code. -- Wei Zhong.
sz = size(p); if (length(p(:)) == 6) p = p(:); end s = p(3,:); th = p(4,:); r = p(5,:); phi = p(6,:); cth = cos(th); sth = sin(th); cph = cos(phi); sph = sin(phi); ccc = cth.*cph.*cph; ccs = cth.*cph.*sph; css = cth.*sph.*sph; scc = sth.*cph.*cph; scs = sth.*cph.*sph; sss = sth.*sph.*sph; q(1,:) = p(1,:); q(2,:) = p(2,:); q(3,:) = s.*(ccc +scs +r.*(css -scs)); q(4,:) = s.*(r.*(ccs -scc) -ccs -sss); q(5,:) = s.*(scc -ccs +r.*(ccs +sss)); q(6,:) = s.*(r.*(ccc +scs) -scs +css); q = reshape(q, sz); |
這個代碼我想一定也困擾了很多做目標跟蹤的科研工作者,如果大家閱讀我推導過程中存在困惑,歡迎和筆者討論,我的QQ是:553702786。
閩南師範大學 陳穎頻