-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathvisualindex_build.m
74 lines (68 loc) · 2.6 KB
/
visualindex_build.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 model = visualindex_build(images, varargin)
% VISUALINDEX_BUILD Initialize a visual index from a set of images
% MODEL = VISUALDINDEX_BUILD(IMAGES) builds an index using the
% specified list of images to construct the visual word vocabulary
% and compuring the TF-IDF weights.
%
% VISUALINDEX_BUILD(..., 'numWords', K, 'maxNumKMeansIterations', T)
% allows specifying the number of visual words and K-means
% iterations used to build the visual words vocabulary.
%
% MODEL is a structure with the following fields
%
% model.vocab.size:
% Size of the visual vocabulary (number of visual wrods).
%
% model.vocab.centers:
% Visual words (quantized SIFT features).
%
% model.vocab.tree:
% KD-Tree index of the visual words (see VL_KDTREEBUILD()).
%
% model.vocab.weights:
% TF-IDF weights of the visual words.
%
% model.index.frames:
% model.index.words:
% Cell array of frames (keypoints) and quantized descriptors of
% the SIFT features of each image.
%
% model.index.histograms
% Cell array of histogram of visual words for each image.
% Author: Andrea Vedaldi
opts.numWords = 1e4 ;
opts.rootSift = true ;
opts.maxNumKMeansIterations = 20 ;
opts.rerankDepth = 40 ;
opts = vl_argparse(opts, varargin) ;
randn('state',0) ;
rand('state',0) ;
model.rerankDepth = opts.rerankDepth ;
model.rootSift = opts.rootSift ;
model.vocab.size = opts.numWords ;
model.vocab.centers = [] ;
model.vocab.tree = struct ;
model.vocab.weights = ones(opts.numWords,1) ;
model.index = struct('ids',[],'frames',[],'words',[],'histograms',[],'names',[]) ;
%% Sample descriptors from example images
num = opts.numWords * 20 ;
numPerImage = ceil(num / length(images)) ;
descrs = cell(1, length(images)) ;
parfor i = 1:length(images)
fprintf('%s: extracting features from %s (%d of %d)\n', ...
mfilename, images{i}, i, numel(images)) ;
im = imread(images{i}) ;
[drop, descrs{i}] = visualindex_get_features(model, im) ;
descrs{i} = vl_colsubset(descrs{i}, numPerImage, 'uniform') ;
end
descrs = cat(2, descrs{:}) ;
%% Run ANN k-means to construct the visual word vocabulary
vl_twister('state',0) ;
model.vocab.centers = vl_kmeans(descrs, opts.numWords, ...
'Verbose', ...
'Initialization', 'randsel', ...
'Algorithm', 'ANN', ...
'MaxNumIterations', opts.maxNumKMeansIterations, ...
'NumTrees', 8, ...
'MaxNumComparisons', 1000) ;
model.vocab.tree = vl_kdtreebuild(model.vocab.centers, 'NumTrees', 8) ;