-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOlafClustering.m~
340 lines (265 loc) · 9.15 KB
/
OlafClustering.m~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
function [ ] = OlafClustering(texturedatabase, dimension, output)
% OLAFCLUSTERING is a clustering algorithm without a predefined amount of
% cluster
% "... Die Suche beginnt beispielsweise in der linken oberen Ecke
% eines 2D-Merkmalsraumes und wird systematisch fortgesetzt.
% So stoesst man irgendwann auf den ersten Lernvektor im -raum. Er bildet
% das Zentrum der 1. Klasse. Der naechste Lernvektor besitzt einen Abstand
% zu diesem Zentrum.
% Liegt er unterhalb einer Zurueckweisungsdistanz, so gehoert er zur selben
% Klasse. Diese einfache Suche setzt sich fort bis zu einem Lernvektor,
% dessen Distanz zum Zentrum der 1. Klasse die schon erwaehnte
% Zurueckweisungsdistanz ueberschreitet. Dann wird eine neue Klasse mit
% einem neuen Zentrum gegruendet. Da nun schon 2 Klassen existieren,
% muss die Distanz der noch folgenden Lernvektoren jeweils zu den beiden
% Klassenzentren berechnet werden. In gleicher Weise verfaehrt man weiter,
% bis saemtliche Lernvektoren zugeordnet sind. ..."
%% SOURCE:
% Hochmuth O.: Mustererkennung, Uebungsaufgabe 36,
% http://www.informatik.hu-berlin.de/~hochmuth/download/uebgsv2.shtml
% 12.04.2005
%% Changelog:
% [23.03.11] - Multidimension
% [20.03.11] - Bug fixed (b=a+1:length(dist))
% - deleted old functions, added 3d+ support,
% - removed old parameters and kicked brainz
% [10.03.11] - Something...
% [22.02.11] - Redesign
load(char(texturedatabase));
%% Change the current folder to the folder of this m-file
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
load(char(texturedatabase));%load database
[numImage numFeatures] = size(values);% get number of images and features present
allClusHeader = cell(1,1);
allClusValues = zeros(numImage,1);
pos = 0;%keeps track of position for the final result variable
%Generate the value vector (dimension independant)
%Generating current pair (n-dimensional)
% startPos = i;
% vector = dimensionRobot(dimension, values, startPos);
for i = 1:numFeatures
if(i+1>numFeatures)
break
end
for coljump=i+1:numFeatures
pos = pos +1;
%bei 3d kommt hier noch ne sufe dazu, bei 4d, noch eine usw.
% for coljump=i+1:numFeatures
v(:,1) = values(:,i);
v(:,2) = values(:,coljump);
dist = createDistanceMatrix(v);
zwd = calculateZWD(dist);
cluster = generateCluster(dist, zwd);
%store all cluster in allClus
featureString = strcat(header(i), '/', header(coljump)); % concats the current two features
allClusHeader(1,pos) = featureString;
allClusValues(:,pos) = cluster;
end
end
% And finally save those values
allClusClass(:,1) = class(:,:);
save (output, 'allClusHeader', 'allClusValues', 'allClusClass');
end
function recu(dim)
% abbruch: x = length und y auch
if dim == 0
else
a = dim;
for b=a+1:numFeatures
pos = pos +1;
v(:,1) = values(:,a);
v(:,2) = values(:,b);
dist = createDistanceMatrix(v);
zwd = calculateZWD(dist);
cluster = generateCluster(dist, zwd);
featureString = strcat(header(i), '/', header(coljump)); % concats the current two features
allClusHeader(1,pos) = featureString;
allClusValues(:,pos) = cluster;
recu(dim-1)
end
end
end
%
%
% [a b] = size(vector);
%
% switch dimension
% case 1
% return;
% case 2
%
% % Now move
% % for each feature...
% for i = 1:numFeatures
% if(i+1>numFeatures)
% break
% end
% valA = vector(:,i);
% for coljump=i+1:numFeatures
% pos = pos +1;
% valB= vector(:,coljump);
%
% dist = createDistanceMatrix(vector);
%
% % Calculate the Zurueckweisungsdistanz ;)
% zwd = calculateZWD(dist);
%
% % Generate Cluster
% % cluster = generateCluster(dist, zwd, numImage, valA, valB);
% cluster = generateCluster(dist, zwd);
%
% %store all cluster in allClus
% featureString = strcat(header(i), '/', header(coljump)); % concats the current two features
% allClusHeader(1,pos) = featureString;
% allClusValues(:,pos) = cluster;
% end
% end
% allClusClass(:,1) = class(:,:);
%
% case 3
%
%
% for i = 1:numFeatures
% if(i+1>numFeatures)
% break
% end
% valA = values(:,i);
% for coljump=i+1 :numFeatures
% valB= values(:,coljump);
% for coljump2=coljump+1:numFeatures
% valC= values(:,coljump2);
% pos = pos +1;
%
% % Create distance Matrix
% % dist = createDistanceMatrix(numImage, valA, valB, valC);
% dist = createDistanceMatrix(vector);
% % Calculate the Zurueckweisungsdistanz ;)
% zwd = calculateZWD(dist);
%
% % Generate Cluster
% cluster = generateCluster(dist, zwd, numImage);
%
% %store all cluster in allClus
% featureString = strcat(header(i), '/', header(coljump), '/', header(coljump2));
% allClusHeader(1,pos) = featureString;
% allClusValues(:,pos) = cluster;
% end
% end
% end
% allClusClass(:,1) = class(:,:);
% end
function [result]= dimensionRobot(dim, values,sp, v)
[numImage numFeatures] = size(values);
if nargin == 3
a = dim + 1 - sp;
a = sp + dim -1;%startposition + dimension
else
a = dim;
end
%Exit criteria
% if (dim == 0)
% if (a == 0)
if(sp==a)
v(:,a) = values(:,a);
result = v;
%now 'v' contains all vectors
% dist = createDistanceMatrix(numImage, v);
return;
end
% result(:,dim) = values(:,dim);
v(:,a) = values(:,a);
%v(:,dim) = values(:,dim);
% Create distance Matrix
% result = dimensionRobot(dim-1, values,sp, v);
result = dimensionRobot(a-1, values, sp, v);
end
function [zwd] = calculateZWD(dist)
% CALCULATEZWD calculates the Zureuckweisungsdistanz (zwd)
% first try: mean
% [VERIFIED]
[rows cols] = size(dist);
cunt = 0;
summa = 0;
for k=1:rows
for l=k+1:cols
if isnan(dist(k,l))
else
summa = summa + dist(k,l);
cunt = cunt + 1;
end
end
end
% V1: summa/cunt; 11/24
% V2: mean(realsum); 1/24
% V3: realsum/cunt;
realsum = sum(sum(dist));
zwd = realsum/cunt;
zwd = summa/cunt;
% zwd = realsum/cunt;
end
%% function [dist] = createDistanceMatrix(X, Y )
function [dist] = createDistanceMatrix(V)
% CREATEDISTANCEMATRIX creates a matrix and gather all distance values
% CREATEDISTANCEMATRIX(numImage, ValX, ValY, ValZ)
%create empty distance matrix
[numImage dim] = size(V);
dist = zeros(numImage, numImage);
% v1 = V(1,1) or V(1,1,1)...and so on for each dimension
% v2 = V(2,2) or V(2,2,2)
for a=1:numImage
for b=a+1:numImage
zw =0;
% x=V(a,:); -> x(1), x(2)
% y=V(b,:); -> y(1), y(2)
for d=1:dim
% ( x1 - y1 )^2
zw = zw + (V(a,d) - V(b,d))^2;
end
dist(a,b) = sqrt(zw);
end
end %for all images
end %function
%% Generate cluster
function [cluster] = generateCluster(dist, zwd)
%create cluster array and put all to cluster 1
cluster=zeros(length(dist),1);
klaus = zeros(length(dist),1); % Klaus is for clusters, so we need exactly #images classes
klaus(1) = 1;%first object will always be the first center
center = zeros(1,1);
center(1) = 1; %just tells which (in the following) i is already a center
for i=2:length(dist)%begin with the second and iterate over all (X Y)
found = false;
%Check for all cluster center, where the new datapoint belongs to
for j=1:length(center)
currentcenter = center(j);
numberOfCenter = length(center);
%so the current vector belongs to a group already clustered
if (dist(currentcenter,i) < zwd)%Zeile j, Spalte i
% if j == numberOfCenter %so no other centers left
klaus(i) = currentcenter;
found = true;
% pass on, if the cluster was already determined
% % TODO: maybe a better solution here:
% % I mean the cluster can be already detemined, but what if it
% % wasn't the best we could do?
% else
% %optimization: check for less distance towards all the other center
% klaus(i) = currentcenter;
% found = true;
%
% end
end
end
if (found == true)
continue; %if we already found a cluster, go to the next iteration
%does not match to any existing center
%create a new cluster
else
klaus(i) = max(klaus + 1);
center(length(center) + 1) = i;
end
end
cluster = klaus;
end