-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathvisualindex_query.m
98 lines (84 loc) · 2.96 KB
/
visualindex_query.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 [ids, scores, result] = visualindex_query(model, im, varargin)
% VISUALINDEX_QUERY Search index for matching images
% [IDS, SCORES] = VISUALINDEX_QUERY(MODEL, IM) searches the index
% MODEL for images matching the query image IM. It returns a list of
% imge IDS and SCORES by descending confidence.
%
% The function first matches images based on the visual words
% histograms and then geometrically verifies the top
% MODEL.RERANKDEPTH results, reranking those. The function also
% returns MATCHES, an array of structures with the geometric
% verification `certificates' for these top matches.
%
% [IDS, SCORES, RESULT] = VISUALINDEX_QUERY(...) returns an
% additional structure RESULT useful for visualization (use
% VISUALINDEX_SHOW()).
%
% Options:
%
% GeometricVerification:: true
% Swtich geometric verification on or off.
% Author: Andrea Vedaldi
opts.box = [] ;
opts.geometricVerification = true ;
opts = vl_argparse(opts, varargin) ;
% reranking depth cannot be larger than the number of indexed images
depth = min(model.rerankDepth, numel(model.index.ids)) ;
% extract the features, visual words, and histogram for the query images
if numel(im) > 1
% im is an image; extract features
[frames, descrs] = visualindex_get_features(model, im) ;
words = visualindex_get_words(model, descrs) ;
else
% im is an index in the database
frames = model.index.frames{im} ;
words = model.index.words{im} ;
end
% crop by the box, if any
if ~isempty(opts.box)
ok = frames(1,:) >= opts.box(1) & ...
frames(1,:) <= opts.box(3) & ...
frames(2,:) >= opts.box(2) & ...
frames(2,:) <= opts.box(4) ;
frames = frames(:, ok) ;
words = words(ok) ;
end
% get histogram
histogram = visualindex_get_histogram(model, words) ;
% compute histogram-based score
scores = histogram' * model.index.histograms ;
[scores, perm] = sort(scores, 'descend') ;
ids = model.index.ids(perm) ;
H = cell(1,depth) ;
matches = cell(1,depth) ;
result.ids = ids ;
result.scores = scores ;
result.matches = cell(1,depth) ;
result.H = cell(1,depth) ;
result.query = im ;
result.queryFrames = frames ;
if ~opts.geometricVerification, return ; end
% update top scores using geometric verification
words2 = model.index.words(perm(1:depth)) ;
frames2 = model.index.frames(perm(1:depth)) ;
parfor t = 1:depth
% find the features that are mapped to the same visual words
[drop,m1,m2] = intersect(words,words2{t}) ;
matches{t} = [m1(:)';m2(:)'] ;
%im2=imread(model.index.names{perm(t)});
%figure(101) ; clf ; plotMatches(im,im2,frames,frames2,matches{t}) ;
[inliers, H{t}] = geometricVerification(frames, frames2{t}, matches{t}) ;
if numel(inliers) >= 6
scores(t) = scores(t) + numel(inliers) ;
end
matches{t} = matches{t}(:, inliers) ;
end
% rerank
[scores(1:depth), perm] = sort(scores(1:depth), 'descend') ;
ids(1:depth) = ids(perm) ;
matches = matches(perm) ;
H = H(perm) ;
result.ids = ids ;
result.scores = scores ;
result.matches = matches ;
result.H = H ;