-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainSIFTclassifier.m
52 lines (44 loc) · 1.33 KB
/
trainSIFTclassifier.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
%% Data
path = '~/Desktop/coins/regions';
sloth = io.readsloth('~/Desktop/coins/401/labels.json');
display('Extracting regions');
tic
io.extractregions(sloth, path, 'FileStructure', 'ClassFolders', ...
'ReadFcn' , @(x) imresize(x, [320 320]));
imds = imageDatastore(path, 'IncludeSubfolders', true, 'LabelSource', 'foldernames');
toc
%% Training
k = 500;
imageCount = length(imds.Files);
descriptors = {imageCount};
descriptorsPerImage = zeros(imageCount, 1);
% Get SIFT descriptors from all images
display('Obtain SIFT descriptors');
tic
for i = 1:imageCount
I = single(rgb2gray(readimage(imds, i)));
[f, d] = vl_sift(I);
descriptors{i} = d;
descriptorsPerImage(i) = size(d, 2);
end
D = horzcat(descriptors{1:imageCount});
toc
display('K-Means');
% Use k-means to cluster descriptors
tic
[c, a] = vl_kmeans(single(D), k, 'Initialization', 'plusplus');
toc
% Assign descriptors of each image to clusters to get feature vectors
features = zeros(imageCount, k);
descCounter = 0;
for j = 1:imageCount
for i = 1:descriptorsPerImage(j)
bin = a(descCounter + i);
features(j, bin) = features(j, bin) + 1;
end
descCounter = descCounter + descriptorsPerImage(j);
end
% Optional - transform to binary features, might be better for SVM
features = sign(features);
% SVM
%classifier = fitcecoc(features, imds.Labels);