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