-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgaborFeatures.m
98 lines (81 loc) · 2.96 KB
/
gaborFeatures.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
function featureVector = gaborFeatures(img,gaborArray,d1,d2)
% GABORFEATURES extracts the Gabor features of the image.
% It creates a column vector, consisting of the image's Gabor features.
% The feature vectors are normalized to zero mean and unit variance.
%
%
% Inputs:
% img : Matrix of the input image
% gaborArray : Gabor filters bank created by the function gaborFilterBank
% d1 : The factor of downsampling along rows.
% d1 must be a factor of n if n is the number of rows in img.
% d2 : The factor of downsampling along columns.
% d2 must be a factor of m if m is the number of columns in img.
%
% Output:
% featureVector : A column vector with length (m*n*u*v)/(d1*d2).
% This vector is the Gabor feature vector of an
% m by n image. u is the number of scales and
% v is the number of orientations in 'gaborArray'.
%
%
% Sample use:
%
% % img = imread('cameraman.tif');
% % gaborArray = gaborFilterBank(5,8,39,39); % Generates the Gabor filter bank
% % featureVector = gaborFeatures(img,gaborArray,4,4); % Extracts Gabor feature vector, 'featureVector', from the image, 'img'.
if (nargin ~= 4) % Check correct number of arguments
error('Use correct number of input arguments!')
end
if size(img,3) == 3 % Check if the input image is grayscale
img = rgb2gray(img);
end
img = double(img);
%% Filtering
% Filter input image by each Gabor filter
[u,v] = size(gaborArray);
gaborResult = cell(u,v);
for i = 1:u
for j = 1:v
gaborResult{i,j} = conv2(img,gaborArray{i,j},'same');
% J{u,v} = filter2(G{u,v},I);
end
end
%% Feature Extraction
% Extract feature vector from input image
[n,m] = size(img);
s = (n*m)/(d1*d2);
l = s*u*v;
featureVector = zeros(l,1);
c = 0;
for i = 1:u
for j = 1:v
c = c+1;
gaborAbs = abs(gaborResult{i,j});
gaborAbs = downsample(gaborAbs,d1);
gaborAbs = downsample(gaborAbs.',d2);
gaborAbs = reshape(gaborAbs.',[],1);
% Normalized to zero mean and unit variance. (if not applicable, please comment this line)
gaborAbs = (gaborAbs-mean(gaborAbs))/std(gaborAbs,1);
featureVector(((c-1)*s+1):(c*s)) = gaborAbs;
end
end
%% Show filtered images
% % Show real parts of Gabor-filtered images
% figure('NumberTitle','Off','Name','Real parts of Gabor filters');
% for i = 1:u
% for j = 1:v
% subplot(u,v,(i-1)*v+j)
% imshow(real(gaborResult{i,j}),[]);
% end
% end
%
%
% % Show magnitudes of Gabor-filtered images
% figure('NumberTitle','Off','Name','Magnitudes of Gabor filters');
% for i = 1:u
% for j = 1:v
% subplot(u,v,(i-1)*v+j)
% imshow(abs(gaborResult{i,j}),[]);
% end
% end