-
Notifications
You must be signed in to change notification settings - Fork 3
/
preComputeT_1.m
42 lines (32 loc) · 1.05 KB
/
preComputeT_1.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
%% Cluster the images in the training set using k-means
% Entire training set
files = dir('data/*.mat');
files = {files.name}';
m = numel(files); % number of training samples
A = cell(m,1);
for i = 1:m
A{i} = load(['data/' files{i}]);
end
width = size(A{1}.img,1); % image width
height = size(A{1}.img,2); % image height
num_pixels = width*height;
% X is the data matrix for kmeans
X = zeros(m,num_pixels);
for i = 1:m
temp = A{i}.img;
X(i,:) = (temp(:))';
end
% show only the final output
opts = statset('Display','iter');
% number of clusters
k = 5;
% first try euclidean distance (use 3 initializations)
%[idx, C] = kmeans(X,k,'Replicates',3,'Options',opts);
% then try Manhattan distance (use 3 initializations)
[idx, C] = kmeans(X,k,'Distance','cityblock','Replicates',3,'Options',opts);
% idx will be a m x 1 vector of cluster indices assigned to each image while C will be a m x num_pixels matrix of
% cluster centroids
% For euclidean distance
%save('clusters.mat','idx','C');
% For Manhattan distance
save('clustersM.mat','idx','C');