forked from januhlenberg/Signa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassificationKNN.m
55 lines (42 loc) · 1.56 KB
/
ClassificationKNN.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
load('classesForIndex');
files = getAllFiles('C:\Users\Jan\Desktop\Training\CarImages\');
fileCount = length(files);
% Permutation
trainCount = int32(fileCount / 3 * 2);
trainIndizes = randperm(fileCount);
trainIndizes = trainIndizes(1:trainCount);
testIndizes = setdiff(1:fileCount,trainIndizes);
%
featureSize = length(featuresFromImage( imread(fullfile(char(files(1))))));
imageFeatures = zeros(fileCount,featureSize);
% Fill imageFeatures
for i=1:min(fileCount,length(carClasses))
path = fullfile(char(files(i)));
im = imread(path);
f = featuresFromImage(im);
imageFeatures(i,:) = f;
end
[wcoeff,~,~,~,explained] = pca(imageFeatures);
subImageFeatures = zeros(size(imageFeatures,1), ...
length(subFeatureSelection(imageFeatures(1,:),wcoeff)));
% Subfeature Selection
for i=1:size(imageFeatures,1)
subImageFeatures(i,:) = subFeatureSelection(imageFeatures(i,:),wcoeff);
end
X = double(subImageFeatures(trainIndizes,:));
Y = int32(carClasses(trainIndizes))';
XTest = double(subImageFeatures(testIndizes,:));
YTest = int32(carClasses(testIndizes))';
Mdl = fitcecoc(X,Y,'Learners','tree');
errorTrain = sum(abs(predict(Mdl,X) - Y) > 0) / double(trainCount)
errorTest = sum(abs(predict(Mdl,XTest) - YTest) > 0) / length(YTest)
function f = featuresFromImage(im)
s = [64 64];
f = [size(im,1);size(im,2);double(reshape(imresize(rgb2gray(im), s),s(1)*s(2),1))];
%f = [std(double(im(:)));mean(im(:));size(im,1);size(im,2)];
end
function f = subFeatureSelection(v,w)
tran = w'*v';
f = tran(1:90);
%f = v;
end