-
Notifications
You must be signed in to change notification settings - Fork 1
/
SIFT_features.m
79 lines (67 loc) · 2.11 KB
/
SIFT_features.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
%Script to extract SIFT features from images (which must all be in
%the same folder)
%
%This script relies on the vlfeat library, available on
%http://www.vlfeat.org/install-matlab.html.
%
%Authors: Vincent Bazinet
%%
%SOME CONSTANT PARAMETERS
run('vlfeat-0.9.21/toolbox/vl_setup'); %Setup the vlfeat library
path = 'my_new_folder_2'
myFolderInfo = dir(path); %Must specify folder
%%
%FEATURE EXTRACTOR
descriptors1 = [];
descriptors2 = [];
descriptors3 = [];
descriptors4 = [];
%For each images, get SIFT descriptors, at each resolution
for i=3:numel(myFolderInfo)
ii=i
imgInfo = myFolderInfo(i);
img = imgInfo.name;
path = join([path,'/',img]);
img = imread(path);
I = single(rgb2gray(img));
for k=1:4
[f,d] = vl_sift(I);
if k==1
descriptors1 = [descriptors1, d];
end
if k==2
descriptors2 = [descriptors2, d];
end
if k==3
descriptors3 = [descriptors3, d];
end
if k==4
descriptors4 = [descriptors4, d];
end
I = imresize(I,0.50);
end
end
%%
%COMPUTE CENTROIDS OF CLUSTERS
[centers1_200, ~] = vl_kmeans(single(descriptors1), 500,'Verbose','NumRepetitions',3);
[centers2_200, ~] = vl_kmeans(single(descriptors2), 500,'Verbose','NumRepetitions',3);
[centers3_200, ~] = vl_kmeans(single(descriptors3), 500,'Verbose','NumRepetitions',3);
[centers4_200, ~] = vl_kmeans(single(descriptors4), 500,'Verbose','NumRepetitions',3);
%%
%CREATE FEATURES
features500 = zeros(numel(myFolderInfo)-2,501);
for i=3:numel(myFolderInfo)
ii=i
imgInfo = myFolderInfo(i);
img = imgInfo.name;
path = join([path,'/',img]);
img = imread(path);
I = single(rgb2gray(img));
kdtree1_500 = vl_kdtreebuild(single(centers1_500));
[f,d] = vl_sift(I);
for m=1:size(d,2)
[index500, ~] = vl_kdtreequery(kdtree1_500, single(centers1_500), single(d(:,m)));
features500(i-2,index500) = features500(i-2,index500)+1;
end
features500(i-2,501) = nnz(features500(i-2,:));
end