-
Notifications
You must be signed in to change notification settings - Fork 0
/
featureExtraction.m
156 lines (133 loc) · 4.2 KB
/
featureExtraction.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
function feature = featureExtraction(I, featureType, filterType, kb, T)
% featureExtraction - Extract SIFT or MLBP features
%
% Syntax: feature = featureExtraction(I, featureType, filterType, kb)
%
% Extract features for each patches and concatenate them.
% Available feature type: 'MLBP' or 'SIFT'
% Available filter type: 'dog', 'csdn' or 'gaussian'
% kb is an optional parameter which is a alpha*n dim integer vector.
randomSubspaces = (nargin >= 4);
%% Change representation of featureType and filterType
if isnumeric(featureType) %number presentation
m = featureType;
if featureType ~= filterType
throw(MException('testing: illegal arguments',...
'featureType should be equal to filterType when use numberic representation \n'));
end
[featureType,filterType] = num2string(m);
end
%% Read result directly
if size(I, 1) == 1 && size(I, 2) == 1
ftype = [filterType, featureType];
% load('featureVectors.mat');
if randomSubspaces
if mod(m, 2) == 0 %even, SIFT
featureDim = 128;
else %odd, MLBP
featureDim = 236;
end
feature = NaN(featureDim*size(kb,1),1);
for k = 1 : size(kb)
startIndex = (k - 1) * featureDim + 1;
TstartIndex = (kb(k) - 1) * featureDim + 1;
feature(startIndex:startIndex + featureDim - 1) =...
T{m}(TstartIndex:TstartIndex + featureDim - 1,I);
end
else
feature = T{m}(:,I);
end
return;
end
%% Filter
switch filterType
case 'dog'
[I,~,~] = imageFiltering(I);
case 'csdn'
[~,I,~] = imageFiltering(I);
case 'gaussian'
[~,~,I] = imageFiltering(I);
end
%% Split image to patches
[patches,nx,ny] = patching(I, 16);
N = nx * ny;
%% Extract features
if (randomSubspaces)
switch featureType
case 'MLBP'
feature = zeros(236,size(kb,1));
case 'SIFT'
feature = zeros(128,size(kb,1));
end
for i = 1 : size(kb,1)
switch featureType
case 'MLBP'
feature(:,i) = transpose(mlbp(patches(:,:,kb(i))));
case 'SIFT'
[f,~] = sift(patches(:,:,kb(i)));
num = size(f,1);
if num == 0
f = zeros(1, 128);
else
f = sum(f, 2);
end
feature(:,i) = transpose(f);
% k = k + num;
end
end
else
switch featureType
case 'MLBP'
feature = zeros(236,size(patches,3));
case 'SIFT'
feature = zeros(128,size(patches,3));
% k = 1;
end
for i = 1 : size(patches,3)
switch featureType
case 'MLBP'
feature(:,i) = transpose(mlbp(patches(:,:,i)));
case 'SIFT'
[f,~] = sift(patches(:,:,i));
f = sum(f, 2);
% num = size(f,1);
% if num == 0
% f = zeros(1, 128);
% else
% f = sum(f, 1);
% end
feature(:,i) = f;
% k = k + num;
end
end
end
% if featureType == 'SIFT'
% feature = feature(:,1:k-1);
% end
%% Reshape feature matrix to a vector and normalize
feature = reshape(feature, [], 1);
feature = feature ./ sum(feature);
end
%%---------------------Subfunction------------------------%%
function [featureType, filterType] = num2string(num)
switch(num)
case 1
featureType = 'MLBP';
filterType = 'csdn';
case 2
featureType = 'SIFT';
filterType = 'csdn';
case 3
featureType = 'MLBP';
filterType = 'dog';
case 4
featureType = 'SIFT';
filterType = 'dog';
case 5
featureType = 'MLBP';
filterType = 'gaussian';
case 6
featureType = 'SIFT';
filterType = 'gaussian';
end
end