-
Notifications
You must be signed in to change notification settings - Fork 14
/
encodeImage.m
74 lines (65 loc) · 2.33 KB
/
encodeImage.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
function psi = encodeImage(encoder, im, cache)
% ENCODEIMAGE Applies an encoder to an image
% PSI = ENCODEIMAGE(ENCODER, IM) applies the specified ENCODER
% to image IM, reurning a corresponding code vector PSI.
%
% IM can be an image, the path to an image, or a cell array of
% the same, to operate on multiple images.
%
% ENCODEIMAGE(ENCODER, IM, CACHE) utilizes the specified CACHE
% directory to store encodings for the given images. The cache
% is used only if the images are specified as file names.
% Author: Andrea Vedaldi
if ~iscell(im), im = {im} ; end
if nargin <= 2, cache = [] ; end
psi = cell(1,numel(im)) ;
if numel(im) > 1
%par
for i = 1:numel(im)
fprintf('%05d of %05d\n', i, numel(im)) ;
psi{i} = processOne(encoder, im{i}, cache) ;
end
elseif numel(im) == 1
psi{1} = processOne(encoder, im{1}, cache) ;
end
psi = cat(2, psi{:}) ;
% --------------------------------------------------------------------
function psi = processOne(encoder, im, cache)
% --------------------------------------------------------------------
if isstr(im)
if ~isempty(cache)
psi = getFromCache(im, cache) ;
if ~isempty(psi), return ; end
end
fprintf('encoding image %s\n', im) ;
end
psi = encodeOne(encoder, im) ;
if isstr(im) & ~isempty(cache)
storeToCache(im, cache, psi) ;
end
% --------------------------------------------------------------------
function psi = encodeOne(encoder, im)
% --------------------------------------------------------------------
im = standardizeImage(im) ;
im_ = bsxfun(@minus, 255*im, encoder.averageColor) ;
res = vl_simplenn(encoder.net, im_) ;
psi = mean(reshape(res(end).x, [], size(res(end).x,3)), 1)' ;
% --------------------------------------------------------------------
function psi = getFromCache(name, cache)
% --------------------------------------------------------------------
[drop, name] = fileparts(name) ;
cachePath = fullfile(cache, [name '.mat']) ;
if exist(cachePath, 'file')
data = load(cachePath) ;
psi = data.psi ;
else
psi = [] ;
end
% --------------------------------------------------------------------
function storeToCache(name, cache, psi)
% --------------------------------------------------------------------
[drop, name] = fileparts(name) ;
cachePath = fullfile(cache, [name '.mat']) ;
vl_xmkdir(cache) ;
data.psi = psi ;
save(cachePath, '-STRUCT', 'data') ;