基於粒子濾波的目標追蹤


基於粒子濾波的目標追蹤

讀"K. Nummiaro, E. Koller-Meier, L. Van Gool. An adaptive color-based particle filter[J], Image and Vision Computing, 2002"筆記

粒子濾波中最重要的一個過程就是重要性重采樣, Sampling Importance Resampling (SIR).

這篇博客基於粒子濾波的物體跟蹤講的挺形象的 :) 。 這里拿過來記錄一下。

  1. 初始化階段
    基於粒子濾波的目標追蹤方法是一種生成式跟蹤方法,所以要有一個初始化的階段。對於第一幀圖像,人工標定出待檢測的目標,對該目標區域提出特征。論文里將圖像的HSV空間划分成不同的bins,然后統計直方圖。另外

To increase the reliability of the color distribution when boundary pixels belong to the background or get occluded, smaller weights are assigned to the pixels that are further away from the region center by employing a weighting function

是到區域中心的距離,所以直方圖分布可如下計算

是歸一化因子,是區域半徑,是示性函數,是將加權后的值添加到對應的區間內。

  1. 搜索階段 - 放狗
    現在已經知道了目標的特征,然后就在目標的周圍撒點(particle),即放狗。放狗的方法有很多。 如a)均勻的撒點;b)按高斯分布撒點,就是近的地方撒得多,遠的地方撒的少。論文里使用的是后一種方法。每一個粒子都計算所在區域內的顏色直方圖,如初始化提取特征一樣,然后對所有的相似度進行歸一化。文中相似性使用的是巴氏距離。

  2. 重要性重采樣
    根據第2階段獲得的相似度重新撒粒子,相似度高的粒子周圍多撒,相似度低的地方少撒。這個過程其實可以重復幾次,但為了檢測速度,重采樣一次也可以。

  3. 狀態轉移
    對上一階段多次重采樣后獲得的粒子計算下一時刻的位置。

是多元高斯分布變量。

  1. 觀測階段
    i. 在出計算概率顏色直方圖
    ii. 計算各個粒子和目標的巴氏距離
    iii. 更新每個粒子的權重

  2. 決策階段
    每個粒子都能夠獲得一個和目標的相似度,這個相似度體現了該區域是目標的置信度,可以把所有例子使用相似度加權后的結果作為目標可能的位置。


粒子濾波更新過程

enter description here

iteration step of the color-based particle filter.jpg


matlab代碼實現

  1. % REFERENCE: 
  2. % An adaptive color-based particle filter [J]. Image and Vision 
  3. % Computing, 21 (2003) 99-110 
  4. % 
  5. % note: 
  6. % the structure of sample 
  7. % {1} x,  
  8. % {2} y, 
  9. % {3} vx, 
  10. % {4} vy, 
  11. % {5} hx, 
  12. % {6} hy, 
  13. % {7} a. 
  14. % and the selected region is an ellipse. 
  15. % These functions are programmed for RGB images. 
  16.  
  17. function [sample,initHist]=ACPF(image,initSample,N,targetHist,bin) 
  18. % INPUTS: 
  19. % image - current frame 
  20. % initSample - init region to be detected 
  21. % N - the numbers of candidates 
  22. % targetHist - the histgram of the target 
  23. % bins -the element represents the number of bins in each channel 
  24. % OUTPUTS: 
  25. % sample - detected region 
  26.  
  27. % predefined parameters  
  28. global curFrame; % current frame 
  29. global velocityTurb; % the turbulence of the velocity 
  30. global scaleTurb; % the turbulence of the scale 
  31. global bins; % the element represents the number of bins in each channel 
  32. global deltaT; % the time margin of consecutive frames 
  33. global SIGMA2; % the variance of the Gaussian specifing the weight in function: observe 
  34. global shiftTurb; 
  35.  
  36. curFrame=image; 
  37. velocityTurb=4
  38. scaleTurb=4
  39. shiftTurb=40
  40. bins=bin; 
  41. deltaT=0.05
  42. SIGMA2=0.02; % 2*sigma^2 
  43. tolerance=initSample{3}*deltaT+scaleTurb; 
  44. A=eye(7); 
  45. beta=0.9; % the similarity ratio between new histgram and the origin histgram 
  46. alpha=0.1; % the learning factor of object histgram 
  47.  
  48. [initHist,sampleSet,weight]=initialization(initSample,N); 
  49.  
  50. tempHist=sqrt(initHist.*targetHist); 
  51. rho=sum(tempHist,1); 
  52. PIE=exp(-(1-rho)/SIGMA2); 
  53. fprintf('similarity:%4f\n',PIE); 
  54. if PIE>beta 
  55. initHist=alpha*initHist+(1-alpha)*targetHist; 
  56. else 
  57. initHist=targetHist; 
  58. end 
  59.  
  60. MAXITER=2
  61. iter=0
  62. oldsample=initSample; 
  63. while iter<MAXITER 
  64. sampleSet=reselect(sampleSet,weight); 
  65. sampleSet=propagate(sampleSet,A); 
  66. weight=observe(sampleSet,initHist); 
  67. sample=estimate(sampleSet,weight); 
  68. if different(sample,oldsample)<tolerance 
  69. break
  70. end 
  71. oldsample=sample; 
  72. iter=iter+1
  73. end 
  74. % draw the scatter dots ------------------------ 
  75. for i=1:N 
  76. tempSample=sampleSet{i}
  77. plot(tempSample{1},tempSample{2},'o','MarkerSize',5,'MarkerFaceColor','g'); 
  78. hold on 
  79. end 
  80. plot(sample{1},sample{2},'o','MarkerSize',5,'MarkerFaceColor','r'); 
  81. hold on 
  82. hold off 
  83. % ------------------------------------------------- 
  84. end 
  85.  
  86. function err=different(sample1,sample2) 
  87. % calculate the difference between sample1 and sample2 
  88. err=0
  89. for i=1:7 
  90. err=err+abs(sample1{i}-sample2{i}); 
  91. end 
  92. end 
  93.  
  94. function [initHist, sampleSet,weight]=initialization(initSample,N) 
  95. % Initializing the target in each frame at the beginning of 
  96. % tracking process. 
  97. % INPUTS: 
  98. % initSample -the sample which will be tracked in current frame 
  99. % N -the length of the sampleSets 
  100. % channel, respectively. 
  101. % OUTPUTS: 
  102. % initHist -the histgram of the target model 
  103. % sampleSet -the set of candidate samples 
  104. % weight -the initial weight of each sample in the sampleSet  
  105. global velocityTurb; 
  106. global scaleTurb; 
  107. global shiftTurb; 
  108.  
  109. sampleSet=cell(N,1); 
  110. weight=zeros(N,1); 
  111. numSamples=0; % the number of initialized samples 
  112. while numSamples<N 
  113. randoms=randn(7,1); 
  114. sampleTemp{1}=round(initSample{1}+randoms(1)*shiftTurb); 
  115. sampleTemp{2}=round(initSample{2}+randoms(2)*shiftTurb); 
  116. sampleTemp{3}=initSample{3}+randoms(3)*velocityTurb; 
  117. sampleTemp{4}=initSample{4}+randoms(4)*velocityTurb; 
  118. sampleTemp{5}=round(initSample{5}+randoms(5)*scaleTurb); 
  119. sampleTemp{6}=round(initSample{6}+randoms(6)*scaleTurb); 
  120. sampleTemp{7}=scaleTurb; 
  121. if isValidate(sampleTemp)==1 
  122. numSamples=numSamples+1
  123. sampleSet{numSamples}=sampleTemp; 
  124. weight(numSamples)=1/N; 
  125. end 
  126. end 
  127. initHist=calColorHist(initSample); 
  128.  
  129. end 
  130.  
  131. function colorHist=calColorHist(sample) 
  132. % calculating the color histgram of the region selected by the sample  
  133. % note: 
  134. % the weight function if r<1 then k(r)=1-r^2 else k(r)=0 
  135. global curFrame; 
  136. global bins; 
  137. x=sample{1}-sample{5}
  138. y=sample{2}-sample{6}
  139. region=curFrame(y:y+2*sample{6},x:x+2*sample{5},:); 
  140. [m,n,~]=size(region); 
  141. tempBins=zeros(bins); 
  142. margin=1.1 ./bins; 
  143. for i=1:m 
  144. for j=1:n 
  145. r=double(region(i,j,1)); 
  146. g=double(region(i,j,2)); 
  147. b=double(region(i,j,3)); 
  148. tempxy=(i-double(sample{6}))^2/(double(sample{6}))^2+1.0*(j-double(sample{5}))^2/(double(sample{5}))^2
  149. if tempxy<=1 
  150. tempBins(fix(r/margin(1)+1), fix(g/margin(2)+1), fix(b/margin(3)+1))=... 
  151. tempBins(fix(r/margin(1)+1), fix(g/margin(2)+1), fix(b/margin(3)+1))+... 
  152. 1-tempxy;  
  153. end 
  154. end 
  155. end 
  156. colorHist=tempBins(:); 
  157. colorHist=colorHist./sum(colorHist,1); 
  158. end 
  159.  
  160. function sampleSet=reselect(sampleSet,weight) 
  161. % reselect process. 
  162. % new samples are selected according the corresponding weight 
  163.  
  164. % following the steps in the referred paper. 
  165. N=size(weight,1); 
  166. cumulativeW=zeros(N,1); 
  167. cumulativeW(1)=weight(1); 
  168. % calculate the normalized cumulative probabilities 
  169. for i=2:N 
  170. cumulativeW(i)=cumulativeW(i-1)+weight(i); 
  171. end 
  172. cumulativeW=cumulativeW./cumulativeW(N); 
  173. index=zeros(N,1); 
  174. for i=1:N 
  175. % generate a uniformly distributed random number r belong [0,1] 
  176. r=rand(1); 
  177. [~,ind]=find(cumulativeW'>=r); 
  178. if isempty(ind) 
  179. ind=N; 
  180. end 
  181. index(i)=ind(1); 
  182. end 
  183. tempSampleSet=cell(N,1); 
  184. for i=1:N 
  185. tempSampleSet{i}=sampleSet{index(i)}
  186. end 
  187. sampleSet=tempSampleSet; 
  188. end 
  189.  
  190. function sampleSet=propagate(sampleSet, A ) 
  191. global deltaT; 
  192. global velocityTurb; 
  193. global scaleTurb; 
  194. global shiftTurb; 
  195.  
  196. MINWIDTH=10
  197. MINHEIGHT=15
  198. N=length(sampleSet); 
  199. numSamples=0
  200. while numSamples<N 
  201. tempSample=sampleSet{numSamples+1}
  202. % convernient for expand to high order model A 
  203. tempVector=zeros(7,1); 
  204. for j=1:7 
  205. tempVector(j)=tempSample{j}
  206. end 
  207. tempVector=A*tempVector; 
  208. randoms=0.7*randn(7,1); 
  209. tempSample{1}=round(tempVector(1)+deltaT*tempSample{3}+randoms(1)*shiftTurb); 
  210. tempSample{2}=round(tempVector(2)+deltaT*tempSample{4}+randoms(2)*shiftTurb); 
  211. tempSample{3}=tempVector(3)+randoms(3)*velocityTurb; 
  212. tempSample{4}=tempVector(4)+randoms(4)*velocityTurb; 
  213. tempSample{5}=max(round(tempVector(5)+randoms(5)*scaleTurb),MINWIDTH); 
  214. tempSample{6}=max(round(tempVector(6)+randoms(6)*scaleTurb),MINHEIGHT); 
  215. tempSample{7}=scaleTurb; 
  216. if isValidate(tempSample)==1 
  217. numSamples=numSamples+1
  218. sampleSet{numSamples}=tempSample; 
  219. end 
  220. end 
  221. end 
  222.  
  223. function weight=observe(sampleSet,colorHist) 
  224. % calculate the color distribution of each sample in sampleSet 
  225. % return the weight 
  226. global SIGMA2; 
  227. N=length(sampleSet); 
  228. weight=zeros(N,1); 
  229. for i=1:N 
  230. sampleHist=calColorHist(sampleSet{i}); 
  231. tempHist=sqrt(sampleHist.*colorHist); 
  232. rho=sum(tempHist,1);% Bhattacharyya cofficient 
  233. weight(i)=exp(-(1-rho)/SIGMA2); 
  234. end 
  235. weight=weight./sum(weight,1); 
  236. end 
  237.  
  238. function sample=estimate(sampleSet,weight) 
  239. % estimate the mean state of the set 
  240. N=length(sampleSet); 
  241. sample=cell(7,1); 
  242. for i=1:7 
  243. sample{i}=0
  244. for j=1:N 
  245. tempSample=sampleSet{j}
  246. sample{i}=sample{i}+weight(j)*tempSample{i}
  247. end 
  248. end 
  249. sample{1}=round(sample{1}); 
  250. sample{2}=round(sample{2}); 
  251. sample{5}=round(sample{5}); 
  252. sample{6}=round(sample{6}); 
  253. end 
  254.  
  255. function validate=isValidate(sample) 
  256. % change the validation of the sample 
  257. % if the coordinate of the corner exceed the image edge,then the sample is not rightful 
  258. global curFrame; 
  259. [m,n,~]=size(curFrame); 
  260. x=sample{1}
  261. y=sample{2}
  262. hx=sample{5}
  263. hy=sample{6}
  264. validate=0; %false 
  265. if fix(x-hx-0.5)>=1 && ceil(x+hx+0.5)<=n && fix(y-hy-0.5)>=1 && ceil(y+hy+0.5)<=m 
  266. validate=1
  267. end 
  268.  
  269. end 
  270.  

參考文獻


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM