-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkmeans.m
More file actions
86 lines (77 loc) · 2.64 KB
/
kmeans.m
File metadata and controls
86 lines (77 loc) · 2.64 KB
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
close all; clear all; clc
% for reproducibility
rng('default');
cd('C:\Users\Bazooka\Desktop\EECE5644_Machine_Learn_Pattern_Recogntn\project\trainImages\');
train_img_files = dir('*.tif');
cd('C:\Users\Bazooka\Desktop\EECE5644_Machine_Learn_Pattern_Recogntn\project\trainResults\');
train_gt_files = dir('*.tif');
cd('/..')
% Cropped AerialImageDataset contains 18,001 images. Randomly choose N
N = 20;
radius = 20;
train_image = cell(N, 1);
ground_true = cell(N, 1);
X = randperm(length(train_img_files), N);
train_img_files = train_img_files(X);
train_gt_files = train_gt_files(X);
n = zeros(N, 1);
K = 8;
tic;
accuracy = [];
for i = 1:N
img = im2double(imread(strcat('C:\Users\Bazooka\Desktop\EECE5644_Machine_Learn_Pattern_Recogntn\project\trainImages\', train_img_files(i).name)));
gt = im2double(imread(strcat('C:\Users\Bazooka\Desktop\EECE5644_Machine_Learn_Pattern_Recogntn\project\trainResults\', train_gt_files(i).name)));
new_I = reshape(img,size(img,1)*size(img,2),3);
c_center = new_I( ceil(rand(K,1)*size(new_I,1)) ,:);
% Distances and Labels
DAL = zeros(size(new_I,1),K+2);
% K-means Iteration
KMI = 8;
for n = 1:KMI
for i = 1:size(new_I,1)
for j = 1:K
DAL(i,j) = norm(new_I(i,:) - c_center(j,:));
end
% 1:K are Distance from Cluster Centers 1:K
[Distance, labels] = min(DAL(i,1:K));
% K+1 is Cluster Label
DAL(i,K+1) = labels;
% K+2 is Minimum Distance
DAL(i,K+2) = Distance;
end
for i = 1:K
% Cluster K Points
A = (DAL(:,K+1) == i);
% New Cluster Centers
c_center(i,:) = mean(new_I(A,:));
% If CENTS(i,:) Is Nan Then Replace It With Random Point
if sum(isnan(c_center(:))) ~= 0
% Find Nan Centers
NC = find(isnan(c_center(:,1)) == 1);
for Ind = 1:size(NC,1)
c_center(NC(Ind),:) = new_I(randi(size(new_I,1)),:);
end
end
end
end
X = zeros(size(new_I));
for i = 1:K
idx = find(DAL(:,K+1) == i);
X(idx,:) = repmat(c_center(i,:),size(idx,1),1);
end
%seg_I = reshape(X,size(I,1),size(I,2),3);
seg_I = round(rgb2gray(reshape(X,size(img,1),size(img,2),3)));
match=0;
[row, col, hig] = size(img);
for i=1:row
for j=1:col
if gt(i,j) == seg_I(i,j)
match=match+1;
end
end
end
match=match/(row*col);
accuracy = [accuracy;match];
fprintf('correctness = %f\n', match*100)
end
toc