Hausdorff Distance(豪斯多夫距離)
參考博客:
http://cgm.cs.mcgill.ca/~godfried/teaching/cg-projects/98/normand/main.html
理解:
Hausdorff距離是描述兩組點集之間相似程度的一種量度,它是兩個點集之間距離的一種定義形式。
假設有兩組集合A={a1,…,ap},B={b1,…,bq},則這兩個點集合之間的Hausdorff距離定義為
H(A,B)=max(h(A,B),h(B,A)) (1)
其中,
h(A,B)=max(a∈A)min(b∈B)‖a-b‖ (2)
h(B,A)=max(b∈B)min(a∈A)‖b-a‖ (3)
‖·‖是點集A和B點集間的距離范式(如:L2或Euclidean距離).
這里,式(1)稱為雙向Hausdorff距離,是Hausdorff距離的最基本形式;式(2)中的h(A,B)和h(B,A)分別稱為從A集合到B集合和從B集合到A集合的單向Hausdorff距離.即h(A,B)實際上首先對點集A中的每個點ai到距離此點ai最近的B集合中點bj之間的距離‖ai-bj‖進行排序,然后取該距離中的最大值作為h(A,B)的值.h(B,A)同理可得.
由式(1)知,雙向Hausdorff距離H(A,B)是單向距離h(A,B)和h(B,A)兩者中的較大者,它度量了兩個點集間的最大不匹配程度.
以下是參考文獻的摘錄:
1.Introduction
When talking about distances, we usually mean the shortest : for instance, if a point X is said to be at distance D of a polygon P, we generally assume that D is the distance from X to the nearest point of P..
The same logic applies for polygons : if two polygons A and B are at some distance from each other, we commonly understand that distance as the shortest one between any point of A and any point of B.
Formally, this is called a minimin function, because the distance D between A and B is given by :
即 A集合中的任一點ai 到集合B中的任意點的最短的距離di,然后在這些距離di中選擇距離最短的,即作為兩個集合A與B的距離。
That definition of distance between polygons can become quite unsatisfactory for some applications (並不適用於描述兩個多邊形的位置關系); let's see for example fig. 1. We could say the triangles are close to each other considering their shortest distance, shown by their red vertices. However, we would naturally expect that a small distance between these polygons means that no point of one polygon is far from the other polygon. In this sense, the two polygons shown in fig. 1 are not so close, as their furthest points, shown in blue, could actually be very far away from the other polygon. Clearly, the shortest distance is totally independent of each polygonal shape.
另一個例子:
Another example is given by fig. 2, where we have the same two triangles at the same shortest distance than in fig. 1, but in different position. It's quite obvious that the shortest distance concept carries very low informative content, as the distance value did not change from the previous case, while something did change with the objects.
兩個多邊形的最短距離提供的信息很少。 如下圖,與圖1同樣最短距離的兩個多邊形的位置卻不同。
因此:
定義Hausdoff Distance 距離可以捕捉兩個多邊形的細微之處, 因此要忽略其最短距離。
2. Hausdoff Distance 定義
More formally, Hausdorff distance from set A to set B is a maximin function, defined as
where a and b are points of sets A and B respectively, and d(a, b) is any metric between these points ; for simplicity, we'll take d(a, b) as the Euclidian distance between a and b. If for instance A and B are two sets of points, a brute force algorithm would be :
即: 即 A集合中的任一點ai 到集合B中的任意點的最短的距離di,然后在這些距離di中選擇距離最長(遠)的,即作為兩個集合A與B之間的Hausdoff Distance。
Brute force algorithm : 1. h = 0 2. for every point ai of A, 2.1 shortest = Inf ; 2.2 for every point bj of B dij = d (ai , bj ) if dij < shortest then shortest = dij 2.3 if shortest > h then // 記錄每個ai 到B中的所有點中最小的 h = shortest
算法的時間復雜度是:O(nm)
·This algorithm obviously runs in O(n m) time, with n and m the number of points in each set.
注意:
(1)Hausdoff Distance 是非對稱的, 即單向的。是由方向的。h(A,B) 稱為forward Hausdoff Distance, h(B, A)稱為Backward Hausdoff Distance。
(2)如果A, B是多邊形,而不是離散的點集, Hausdoff Distance同樣適用。 即H(A, B)適應於所有多邊形,線段定義的點集。
(3)Hausdoff Distance給出了兩個多邊形相互接近的程度, 即,一個多邊形(點集)到另一個多邊形(點集)最大距離。這與最短距離的定義不同,因為其僅適用於每個多邊形的一個點,而不考慮多邊形的所有其他點。
(4)直觀的理解就是:Hausdoff Distance 對兩個多邊形的位置距離敏感,而不敏感於多邊形位置之間的最短距離。
如下圖: 兩個多邊形制件的Hausdoff Distance 是截然不同的。
3. Hausdoff Distance的計算
假定:
(1)假定兩個多邊形是簡單的凸多邊形;
(2)兩個多邊形制件不相接處,即相交,也不互相包含。
基礎概念:
(1)單調鏈: 鏈條C(連續的邊)如果是在D方向上是單調的, 則與D方向正交的直線L 與C的交點只有一個,則說明C在D向是單調的。
直線L通過多邊形P中的某個點, 如果多邊形P的內部區域完全在L的一側, 則該L是一條支撐線
注意: 支撐線L是切片的泛化形式。
引理:
we assume two points a and b that belong respectively to polygons A and B, such that :
d (a, b) = h (A, B)
即: a是多邊形A(點集)中距離多邊形B最遠的點。 而b是多邊形B(點集)中距離多邊形A最近的點。
引理(1.1)The perpendicular to ab at a is a supporting line of A, and A is on the same side as B relative to that line.
垂直於兩個點a,b且在定a位置的多邊形A的支撐線,則相對於該支撐線,則B也在同側。
因為:從Hausdoff Distance定義中,則點a 出的直線必然是多邊形A的“支撐線” ,且,As illustrated below, if a is the furthest point of A relative to b, then a circle C centered at b and of radius ab will completely enclose A. Because C contains all points of A, then its tangent to a is a supporting line of A.
引理(1.2):The perpendicular to ab at b is a supporting line of B, and a and B are on different sides relative to that line
垂直於ab 且位於b點的垂線是B多邊形的支撐線。 且 a 和B在該線的不同側。
證明:If b is the closest point of B from a, then a circle C of radius abcentered at a contains only one point of B, namely b. The tangent to C is thus a supporting line of B.
引理(2)There is a vertex x of A such that the distance from x to B is equal to h (A, B).
A 中存在一點x,且x 到B的距離與 h(A,B)相等。
證明:a line going from a vertex b of a triangle abc to some point that belongs to the opposite side ß is always shorter than one of ab or cb, or both.
引理(3)
4. 算法:
因為引理2,則沒有必要計算出開始多邊形的每一個點, 僅僅計算多邊形的“頂點”即可。
情況一:
注意: 算法最近點只能是目標多邊形的頂點,或者是垂直於目標多邊形某條邊的垂點z。因此,要檢查是否存在最近的點。
Function z = CheckForClosePoint (a, b1 , b2 ) :
Compute the position z where the line that passes through b1 and b2 crosses its perpendicular through a ;
計算垂直於穿過b1,b2的直線,且經過點a的位置c。
過程: 如果z是b1,b2中間的點,則返回z, 否則再計算b2點處的直線P(出至於直線a,b2)是否是多邊形B的“支撐線”,如果是,則返回b2,否則返回Null。
if z is between b1 b2 then return z ; else compute at b2 a line P perpendicular to the line ab2 ; if P is a supporting line of B then return b2 ; else return NULL.
主程序:
開始計算主程序,
注意:所有多邊形的頂點序列都是通過“逆時針”的順序列出。
Algorithm for computing h(A, B) :
(枚舉ai 到B中的最近距離di) 1. From a1, find the closest point b1 and compute d1 = d ( a1, b1 ) 2. h(A, B) = d1 3. for each vertex ai of A,
(因為是逆時針依次遍歷A中的點集序列,而bi則根據ai+1情況進行相應移動) 3.1 if ai+1 is to the left of aibi find bi+1 , scanning B counterclockwise with CheckForClosePoint from bi if ai+1 is to the right of aibi find bi+1 , scanning B clockwise with CheckForClosePoint from bi if ai+1 is anywhere on aibi bi+1 = bi
(更新當前距離di+1) 3.2 Compute di+1 = d (ai+1 , bi+1 )
(始終取值為每次更新的最大值) 3.3 h (A, B) = max { h (A, B), di+1 }
因為:多邊形是簡單凸多邊形,且逆時針,在遍歷的時候ai, ai+1 必然會發生“轉向”,這個過程有三種情況
(1)ai+1 在 aibi 直線的左邊, 則遍歷ai+1趨勢繼續“單調”, 因為為了找到最近的點, 則要【逆時針移動】B中當前點bi的下一個點bi+1 的距離判斷(check)此點bi+1;
(2)ai+1 在 aibi 直線上, 則遍歷ai+1即將不在“單調”,此刻必然ai +1 到bi的距離發生變化,為了找到最近的點,則此時將bi+1 視為bi,需要重新更新距離;
(3)ai+1 在 aibi 直線的右邊, 與情況(1)相反, 則,為了找到最近的點,需要【順時針移動】B中的bi的下一個bi+1點,然后cheke此點bi+1”
5. 復雜度分析
If polygons A and B respectively have n and m vertices, then :
- Step 1 can clearly be done in O(m) time ;
- Step 2 takes constant time O(1) ;
- Step 3 will be executed (n-1) times, that is O(n) ;
- Step 3.1 will not be executed in total more than O(2m). This is a consequence of lemma 3, which guarantees that polygon B can not be scanned more than twice ;
- Steps 3.2 and 3.3 are done in constant time O(1) ;
So the algorithm for computing h(A, B) takes :
O(m) + O(n) + O(2m) = O(n+m)
To find H(A, B), the algorithm needs to executed twice ; the total complexity for computing Hausdorff distance then stays linear to O(n+m).
6. 案例分析
Hausdorff distance 應用是圖像匹配,如圖像分析,機器人的視覺導航,電腦輔助手術,
Basically, the Hausdorff metric will serve to check if a template image is present in a test image ; the lower the distance value, the best the match.That method gives interesting results, even in presence of noise or occlusion (when the target is partially hidden).
We want to find if the small image is present, and where, in the large image. The first step is to extract the edges of both images, so to work with binary sets of points, lines or polygons :
Edge extraction is usually done with one of the many edge detectors known in image processing, such as Canny edge detector, Laplacian, Sobel, etc. After applyingRucklidge's algorithm that minimizes Hausdorff distance between two images, the computer found a best match :
For this example, at least 50 % of the template points had to lie within 1 pixel of a test image point, and vice versa. Some scaling and skew were also allowed, to prevent rejection due to a different viewing angle of the template in the test image (these images and results come from Michael Leventon's pages). Other algorithms might allow more complicated geometric transformations for registering the template on the test image.
an online demo is definitely beyond the scope of this Web project ! So here are some Web resources about image matching with Hausdorff distance :
- Hausdorff-based image comparison
A vision project that uses the generalized Hausdorff distance
to locate a template image in an indoor scene. - Hausdorff distance for object tracking and classification
Hausdorff distance used with a multi-layer perceptron
for recognizing and tracking cars in a road scene. - Computer-vision-enabled ophthalmic augmented reality environment
An interesting application of Hausdorff distance for merging
partially overlapping images into a single image. - Comparing 2D images of the 3D world
Conference material by Daniel Huttenlocher on object recognition.
Briefly introduces some problems of image analysis, and compares
the generalized Hausdorff distance with more traditional methods.
endl;