source: distools/edgedistm.m @ 41

Last change on this file since 41 was 10, checked in by bduin, 14 years ago
File size: 3.5 KB
Line 
1%EDGEDISTM  Distance Matrix between Images based on Their Edges
2%
3%   D = EDGEDISTM (A,B,DIST,K)
4%
5% INPUT
6%   A     Matrix of N binary images
7%   B     Matrix of M binary images
8%   DIST  Distance measure (optional; default: 'MHAUS')
9%         'HAUS'  - Hausdorff distance
10%         'MHAUS' - modified Hausdorff distance
11%         'R75'   - based on the 75th quantile
12%         'R90'   - based on the 90th quantile
13%   K     Number of rows in an image if images are stored as row vectors
14%         (optional; default: sqrt(size(A,1)))
15%
16% OUTPUT
17%   D     NxM dissimilarity matrix or dataset
18%
19% DESCRIPTION
20% Computes the distance matrix D between two sets of binary images, A and B.
21% First edges are extracted, then a distance defined by DIST is computed.
22% If the size of A is N x P and the size of B is M x P, then images are
23% stored as row vectors. P should be such that P = K*L, where K and L are
24% image sizes.
25%
26% If the size of A is Kx x LX x N and the size of B is KY x LY x M, then
27% images are stored as 2D arrays.
28%
29% If A and B are datasets, then D is a dataset as well with the labels defined
30% by the labels of A and the feature labels defined by the labels of B. If A is
31% not a dataset, but a matrix of doubles, then D is also a matrix of doubles.
32
33% Copyright: Robert P.W. Duin, r.p.w.duin@prtoos.org, Elzbieta Pekalska, ela.pekalska@googlemail.com
34% Faculty EWI, Delft University of Technology and
35% School of Computer Science, University of Manchester
36
37
38
39function D = edgedistm (A,B,dist,k)
40
41if nargin < 3,
42  dist = 'MHAUS';
43else
44  dist = upper(dist);
45end
46
47if nargin < 2,
48  B = A;
49end
50
51switch ndims(A)
52  case 2,
53    isda = isdataset(A);
54    isdb = isdataset(B);
55    a = +A;
56    b = +B;
57    [ma, na] = size(a);
58
59    if nargin < 4,
60      k = round(sqrt(na));
61    end
62    l = round(na/k);
63
64    if (k * l) ~= na,
65      error ('Sizes of the images are incorrect.');
66    end
67
68    [mb, nb] = size(b);
69    if na ~= nb,
70      error ('Number of features should be the same.');
71    end
72
73    D = zeros(ma,mb);
74    for i=1:ma
75      for j=1:mb
76        [Ia,Ja] = find (edge (reshape (a(i,:),k,l),'canny'));
77        [Ib,Jb] = find (edge (reshape (b(j,:),k,l),'canny'));
78        D(i,j)  = edist([Ia Ja],[Ib Jb],dist);
79      end
80    end
81
82  % Set object labels and feature labels
83  if xor(isda, isdb),
84    prwarning(1,'One matrix is a dataset and the other not. ')
85  end
86  if isda,
87    if isdb,
88      D = setdata(A,D,getlab(B));
89    else
90      D = setdata(A,D);
91    end
92    D.name = 'Distance matrix';
93    if ~isempty(A.name)
94      D.name = [D.name ' for ' A.name];
95    end
96  end
97
98  case 3,
99    [ka,la,ma] = size(a);
100    [kb,lb,mb] = size(b);
101
102    D = zeros(ma,mb);
103    for i=1:ma
104      for j=1:mb
105        [Ia,Ja] = find (edge (a(:,:,i),'canny'));
106        [Ib,Jb] = find (edge (b(:,:,j),'canny'));
107        D(i,j)  = edist ([Ia Ja],[Ib Jb],dist);
108      end
109    end
110
111  otherwise
112    error('The number of matrix''s dimensions should be 2 or 3.');
113end
114
115D(find(D < eps)) = 0;
116return
117
118
119
120
121
122
123
124
125function dd = edist (aa,bb,dist)
126dab   = sqrt(distm(aa,bb));
127minab = min(dab);
128
129dba   = sqrt(distm(bb,aa));
130minba = min(dba);
131
132switch dist,
133  case 'haus',
134    dAB = max(minab);
135    dBA = max(minba);
136
137  case 'mhaus'
138    dAB = mean(minab);
139    dBA = mean(minba);
140
141  case {'r75','r90'}
142    if strcmp(dist,'r75')
143      r = 75;
144    else
145      r = 90;
146    end;
147    minab = sort(minab);
148    dAB   = minab(round (r/ma));
149    minba = sort(minba);
150    dBA   = minba(round (r/mb));
151  otherwise
152    error ('Wrong distance measure.')
153end
154
155dd = max(dBA, dAB);
156return
Note: See TracBrowser for help on using the repository browser.