2009年2月7日

One dimension minimum Hausdorff distance

Hausdorff distance可以用來量測兩個sets之間的距離,如果將其中一個set的元素全部加(減)上t個單位,則兩個sets之間的Hausdorff distance可能會變小或變大。實際的應用,假設我們要做影像的比對,有一張影像為template,而另一張影像是準備要和template比對的圖片,若能夠讓兩張圖片儘量對齊,則能夠提高辨識的淮確度。

my meeting report

我在IPL Vol 106(2008)第一次看到這一篇A new algorithm for computing the minimum Hausdorff distance between two point sets on a line under translation時,看了很久還是不懂演算法實際上是如何運作的,於是追蹤下去追到了源頭的兩篇論文。

源頭是Huttenlocher在1990年發表的Computing the Mimimum Hausdorff Distance for Point Sets Under Translation,這一篇提出的算法提供最初的想法,把sets中每一個元素移動t單位的軌跡的圖形找出來,再找出所有軌跡極大值中的最小值即為所求,時間複雜度是O(mnlog(mn))。

第二篇論文是Rote在1991年發表的Computing the mimimum Hausdorff distance between two point sets on a line under translation也是沿用了Huttenlocker的概念,只是他使用了一個lower bound來減少搜尋解答時所要檢查的解答數量,這個演算法的時間複雜度已經是Optimal (O(m+n)log(m+n)),所以一直以來大家都使用這個算法。

2008年的這一篇論文也是Optimal algorithm,但是所需要檢查的解答數量又更少了,時間複雜度仍然是O((m+n)log(m+n)),但實驗的結果比Rote’s algorithm快了將近15倍。

以下是我用來畫軌跡圖的Scilab script:

  1. //one dimensional Hausdorff distance
  2. clear;
  3. function [dist] = hausdorff(A, B)
  4.   if(size(A, 'r') ~= 1 | size(B, 'r') ~= 1)
  5.     warning("must be one dimension array");
  6.     dist = [];
  7.     return;
  8.   end
  9.  
  10.   dist = max(compute_dist(A, B), compute_dist(B, A));
  11. endfunction
  12.  
  13. //compute distance from point to set
  14. function [dist] = compute_dist(A, B)
  15.   m = size(A, 'c');
  16.   n = size(B, 'c');
  17.    
  18.   for k=1:m
  19.     D = abs(B - A(k));
  20.     dist(k) = min(D);
  21.   end
  22.   dist = max(dist);
  23. endfunction
  24.  
  25. function [dist,dist2] = minHausdorff(A, B, t)
  26.     if (size(A, 'r') ~=1 | size(B, 'r') ~= 1 | size(t, 'r') ~=1)
  27.         warning("must be one dimension array");
  28.         dist=[];
  29.         return;
  30.     end
  31.  
  32.     m = size(A,'c');
  33.     n = size(B,'c');
  34.     len = size(t,'c');
  35.    
  36.     for i=1:m
  37.         for j=1:len
  38.             dist(i, j) = compute_dist(A(i)+t(j), B);
  39.         end
  40.     end
  41.     subplot(1, 2, 1);
  42.     xlabel("t");
  43.     plot(t, dist);
  44.    
  45.     for i=1:n
  46.         for j=1:len
  47.             dist2(i, j) = compute_dist(B(i), A+t(j));
  48.         end
  49.     end
  50.     subplot(1, 2, 2);
  51.     xlabel("t");
  52.     plot(t, dist2);
  53. endfunction
  54.  
  55.  
  56. A=[0, 0.5, 2, 3];
  57. B=[0 0.3 1];
  58. t=linspace(-3, 3, 500);
  59. minHausdorff(A, B, t);