-
Notifications
You must be signed in to change notification settings - Fork 0
/
svm_image_classification.m
85 lines (62 loc) · 2.41 KB
/
svm_image_classification.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
RootTrainPath = 'D:\Skripsi\image_train\';
RootTestPath = 'D:\Skripsi\image_test\';
RootTrain = dir(RootTrainPath);
RootTest = dir(RootTestPath);
X_train = [];
Y_train = [];
index_file = 0;
index_folder = 0;
for i = 1:length(RootTrain)
FolderName = RootTrain(i).name
if(FolderName ~= '.' | FolderName ~= '..')
index_folder = index_folder + 1;
FolderPath = strcat(RootTrainPath, FolderName, "\");
File = dir(FolderPath);
for j = 1:length(File)
FileName = File(j).name;
if(FileName == '.')
else
index_file = index_file + 1;
FilePath = strcat(FolderPath, FileName);
image = rgb2gray(imresize(imread(FilePath), [256 342]));
X_train(index_file, :) = (image(:));
Y_train(index_file) = index_folder;
end
end
end
end
X_test = [];
Y_test = [];
index_folder = 0;
index_file = 0;
file_test_name = [];
for i = 1:length(RootTest)
FolderName = RootTest(i).name
if(FolderName ~= '.' | FolderName ~= '..')
% printf(strcat('Extracting feature from ', FolderName));
index_folder = index_folder + 1;
FolderPath = strcat(RootTestPath, FolderName, "\");
File = dir(FolderPath);
for j = 1:length(File)
FileName = File(j).name;
if(FileName == '.')
else
index_file = index_file + 1;
FilePath = strcat(FolderPath, FileName);
image = rgb2gray(imresize(imread(FilePath), [256 342]));
X_test(index_file, :) = image(:);
Y_test(index_file) = index_folder;
end
end
end
end
save('SVM-Full_TrainFeatures.mat', 'X_train')
save('SVM-Full_TrainLabels.mat', 'Y_train')
save('SVM-Full_TestFeatures.mat', 'X_test')
save('SVM-Full_TestLabels.mat', 'Y_test')
% 'linear' | 'gaussian' | 'rbf' | 'polynomial'
SVMModel = fitcecoc(X_train, Y_train);
[Y_predict] = predict(SVMModel, X_test);
ConfusionMatrix = confusionmat(Y_test, Y_predict);
confusionchart(ConfusionMatrix)
save('confusionchart-SVM-Full.mat', 'ConfusionMatrix');