1 | %HAUSDM Hausdorff distance between datasets of image blobs |
---|
2 | % |
---|
3 | % [DH,DM] = hausdm(A,B) |
---|
4 | % |
---|
5 | % Computes a Hausdorff distance matrix between the sets of binary images |
---|
6 | % A and B, or datasets containing them as features. |
---|
7 | % If A and B are image sets and |
---|
8 | % size(A) is [may,max,na] |
---|
9 | % size(B) is [mby,mbx,nb] |
---|
10 | % then DH is the Hausdorff distance matrix, size(DH) = [na,nb], between |
---|
11 | % these sets. DM is the Modified Hausdorff distance matrix. |
---|
12 | % Preferably na <= nb (faster). |
---|
13 | % |
---|
14 | % See M.-P. Dubuisson and A.K. Jain, Modified Hausdorff distance for object |
---|
15 | % matching, Proceedings 12th IAPR International Conference on Pattern |
---|
16 | % Recognition (Jerusalem, October 9-13, 1994), vol. 1, IEEE, Piscataway, |
---|
17 | % NJ, USA,94CH3440-5, 1994, 566-568. |
---|
18 | % |
---|
19 | % $Id: hausdm.m,v 1.1 2005/04/25 05:54:36 duin Exp $ |
---|
20 | |
---|
21 | % Copyright: R.P.W. Duin, duin@ph.tn.tudelft.nl |
---|
22 | % Faculty of Applied Physics, Delft University of Technology |
---|
23 | % P.O. Box 5046, 2600 GA Delft, The Netherlands |
---|
24 | |
---|
25 | function [dh,dm] = hausdm(A,B) |
---|
26 | |
---|
27 | if isdataset(A) && isdataset(B) |
---|
28 | [dh,dm] = hausdm(data2im(A),data2im(B)); |
---|
29 | dh = setdata(A,dh); |
---|
30 | dm = setdata(A,dm); |
---|
31 | return |
---|
32 | end |
---|
33 | |
---|
34 | [dummy,dummy,na] = size(A); |
---|
35 | [dummy,dummy,nb] = size(B); |
---|
36 | dh = zeros(na,nb); |
---|
37 | dm = zeros(na,nb); |
---|
38 | for i=1:na |
---|
39 | a = A(:,:,i); |
---|
40 | J = find(any(a)); |
---|
41 | J = [min(J):max(J)]; |
---|
42 | K = find(any(a')); |
---|
43 | K = [min(K):max(K)]; |
---|
44 | a = double(a(K,J)); |
---|
45 | if ~isempty(a(:)) |
---|
46 | a = bord(a,0); |
---|
47 | end |
---|
48 | ca = contourc(a,[0.5,0.5]); |
---|
49 | J = find(ca(1,:) == 0.5); |
---|
50 | ca(:,[J J+1]) =[]; |
---|
51 | ca = ca - repmat([1.5;1.5],1,size(ca,2)); |
---|
52 | ca = ca/max(ca(:)); |
---|
53 | ca = ca - repmat(max(ca,[],2)/2,1,size(ca,2)); |
---|
54 | for j = 1:nb |
---|
55 | b = B(:,:,j); |
---|
56 | J = find(any(b)); |
---|
57 | J = [min(J):max(J)]; |
---|
58 | K = find(any(b')); |
---|
59 | K = [min(K):max(K)]; |
---|
60 | b = double(b(K,J)); |
---|
61 | if ~isempty(b(:)) |
---|
62 | b = bord(b,0); |
---|
63 | end |
---|
64 | cb = contourc(b,[0.5,0.5]); |
---|
65 | J = find(cb(1,:) == 0.5); |
---|
66 | cb(:,[J J+1]) =[]; |
---|
67 | cb = cb - repmat([1.5;1.5],1,size(cb,2)); |
---|
68 | cb = cb/max(cb(:)); |
---|
69 | cb = cb - repmat(max(cb,[],2)/2,1,size(cb,2)); |
---|
70 | dab = sqrt(distm(ca',cb')); |
---|
71 | dh(i,j) = max(max(min(dab)),max(min(dab'))); |
---|
72 | dm(i,j) = max(mean(min(dab)),mean(min(dab'))); |
---|
73 | end |
---|
74 | end |
---|
75 | |
---|
76 | % C = bord(A,n,m) |
---|
77 | % Puts a border of width m (default m=1) around image A |
---|
78 | % and gives it value n. If n = NaN: mirror image values. |
---|
79 | function C = bord(A,n,m); |
---|
80 | %ipcontr(0); |
---|
81 | if nargin == 2; m=1; end |
---|
82 | [x,y] = size(A); |
---|
83 | if m > min(x,y) |
---|
84 | mm = min(x,y); |
---|
85 | C = bord(A,n,mm); |
---|
86 | C = bord(C,n,m-mm); |
---|
87 | return |
---|
88 | end |
---|
89 | if isnan(n) |
---|
90 | C = [A(:,m:-1:1),A,A(:,y:-1:y-m+1)]; |
---|
91 | C = [C(m:-1:1,:);C;C(x:-1:x-m+1,:)]; |
---|
92 | else |
---|
93 | bx = ones(x,m)*n; |
---|
94 | by = ones(m,y+2*m)*n; |
---|
95 | C = [by;[bx,A,bx];by]; |
---|
96 | end |
---|
97 | return |
---|