-
Notifications
You must be signed in to change notification settings - Fork 16
/
extract_FV_features.m
113 lines (90 loc) · 2.83 KB
/
extract_FV_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
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
function feats = extract_FV_features(opts,images,GMM,PCA)
nWords = length(images);
feats = zeros(opts.FVdim,nWords,'single');
parfor i=1:length(images)
fprintf('Extracting FV representation from image %d\n',i);
im = images{i};
[height,width] = size(im);
im = im2single(im);
% get PHOW features
descrs = [];
frames = [];
if height>1 && width>1
[frames, descrs] = vl_phow(im, opts.phowOpts{:}) ;
descrs = descrs / 255;
end
if isempty(descrs)
continue;
end
if opts.doMinibox == 0
% XY at GT coordinate space
fx = single(frames(1,:)/width-0.5);
fy = single(frames(2,:)/height-0.5);
else
% XY at word coordinate space
bb = DoBB(im);
w = bb(2)-bb(1)+1;
h = bb(4)-bb(3)+1;
cx = round(bb(1)+w/2);
cy = round(bb(3)+h/2);
fx = single((frames(1,:)-cx)/w);
fy = single((frames(2,:)-cy)/h);
end
xy = [fx; fy];
descrs = [descrs; xy];
[descrs,frames] = normalizeSift(opts,descrs,frames);
feats(:,i) = single(getImageDescriptorFV(opts, GMM, PCA, descrs));
end
feats(isnan(feats)) = 0;
end
% -------------------------------------------------------------------------
function fv = getImageDescriptorFV(opts, GMM, PCA, descrs)
% -------------------------------------------------------------------------
% Project into PCA space
xy = descrs(opts.SIFTDIM+1:end,:);
descrs=bsxfun(@minus, descrs(1:opts.SIFTDIM,:), PCA.mean);
descrs=PCA.eigvec'*descrs;
descrs = [descrs; xy];
% Extracts FV representation using the GMM
fv = vl_fisher(descrs, GMM.mu, GMM.sigma, GMM.we, 'Improved');
end
function X = normFV(X)
% -------------------------------------------------------------------------
X = sign(X).*sqrt(abs(X));
X = bsxfun(@rdivide, X, sqrt(sum(X.*X)));
X(isnan(X)) = 0;
end
function [descrs_normalized,frames_normalized] = normalizeSift(opts,descrs,frames)
% -------------------------------------------------------------------------
descrs_normalized = descrs;
xy = descrs_normalized(opts.SIFTDIM+1:end,:);
descrs_normalized = descrs_normalized(1:opts.SIFTDIM,:);
% Remove empty ones
idx = find(sum(descrs_normalized)==0);
descrs_normalized(:,idx)=[];
if nargin < 3
frames_normalized = [];
else
frames_normalized = frames;
frames_normalized(:,idx) = [];
end
% Square root:
descrs_normalized = sqrt(descrs_normalized);
% 1/4 norm
X = sum(descrs_normalized.*descrs_normalized).^-0.25;
descrs_normalized = bsxfun(@times, descrs_normalized,X);
xy(:,idx) = [];
descrs_normalized = [descrs_normalized; xy];
descrs_normalized(isnan(descrs_normalized))=0;
end
function im = adjustImage(im)
imOrig = im;
im = im2bw(im);
[h,w] = size(im);
x = find(im==0);
w1 = ceil(min(x)/h);
w2 = floor(max(x)/h);
h1 = min(mod(x,h))+1;
h2 = max(mod(x,h))-1;
im = imOrig(h1:h2,w1:w2);
end