-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathK_MultiMatchRound.m
114 lines (95 loc) · 4.01 KB
/
K_MultiMatchRound.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
114
function [affines, scores, matchConfigs] = K_MultiMatchRound(img, tpl, params)
% Input: I, input image; T, template for matching
% Output: AA, array of Affine transformations; scores, corresponding match score for each affine
% 1. Downsample _I_ and _T_ w.r.t. some rules (same scale or to a proper scale and then adjust the scale range).
% 2. Use BBS to approximately locate possible matches. Generate a mask _M_, and only search where _M_==1 in the following steps.
% 2*. Run multiple BBS rounds with different scaling of _T_ may help constraint scale range.
% 3. Generate configs for searching.
% 4. Search for the 1st match _A1_.
% 4*. If _M_ consists of disjoint regions, then search each region for a best match.
% 5. Set _M(A1(T))=0_ and search for the 2nd match, and so on; stop when score is below a criterion.
% 6. Output the result.
% 暂时不考虑mask!
%% 0. Init
[BBSParams, matchParams] = CheckAllParams(params);
affines = [];
scores = [];
matchConfigs = [];
%% 1. Downsample _I_ and _T_ w.r.t. some rules
[img, tpl, IResize, TResize] = AdjustSize(img, tpl, BBSParams.pz);
matchParams.resizeFactor = IResize/TResize;
%% 2. Use BBS to approximately locate possible matches. Generate a mask _M_
[M] = BBSPreprocess(img, tpl, BBSParams);
img = MakeOdd(img);
tpl = MakeOdd(tpl);
M = MakeOdd(M);
szI = size(img);
szT = size(tpl);
%% 3. Generate configs for searching.
% M = ones(size(M)); % debug
[initConfigs, gridSize, matchParams] = InitializeConfigs(M, matchParams, szI, szT);
%% 4. Search for the 1st match _A1_.
matchRound = 1;
fprintf('First match:\n');
[A, score, config, accept] = FindOneMatch(img, tpl, M, initConfigs, matchParams);
config
fprintf('Match #%d score (distance) = %.4f\n\n', matchRound, score);
% A is not a matrix, it is a struct!
if accept == 0
fprintf('First match score (%.4f) too low, halt!\n', score);
return;
else
affines = [affines, A];
scores = [scores, score];
matchConfigs = [matchConfigs; config];
end
%% 5. Set _M(A1(T))=0_ and search for the 2nd match, and so on.
curAccept = 1;
matchRound = matchRound + 1;
while(curAccept && matchRound<=8)
fprintf('Match #%d:\n', matchRound);
[M, initConfigs] = MaskNewMatch(M, tpl, initConfigs, A);
[A, score, config, accept] = FindOneMatch(img, tpl, M, initConfigs, matchParams, scores); % scores can be used as threshold
curAccept = accept;
config
fprintf('Match #%d score (distance) = %.4f\n\n', matchRound, score);
if(~curAccept)
fprintf('Stop at match #%d!\n', matchRound);
else
affines = [affines, A];
scores = [scores, score];
matchConfigs = [matchConfigs; config];
end
matchRound = matchRound + 1;
end
%% 6. Output the result.
% 似乎不用做什么。
% 不对,得把大小还原回去……
affines = RecoverResizedAffines(affines, matchConfigs, IResize, TResize);
end
function [bbsparams, matchparams] = CheckAllParams(params)
% BBSParams: gamma, gz, rect(?)
% matchParams:
% epsilon, delta, photometricInvariance;
% searchRange: minTx, maxTx, minTy, maxTy, minRotation, maxRotation, minScale, maxScale
bbsparams = [];
matchparams = [];
if isfield(params, 'BBSParams'); bbsparams = params.BBSParams; end
if isfield(params, 'matchParams'); matchparams = params.matchParams; end
if(~isfield(bbsparams, 'gamma')); bbsparams.gamma = 2; end
if(~isfield(bbsparams, 'pz')); bbsparams.pz = 3; end
if(~isfield(matchparams, 'epsilon')); matchparams.epsilon = 0.15; end
if(~isfield(matchparams, 'delta')); matchparams.delta = 0.25; end
if(~isfield(matchparams, 'photometricInvariance')); matchparams.photometricInvariance = 0; end
% searchRange will be initialized in InitializeConfigs()
if(~isfield(matchparams, 'searchRange')); matchparams.searchRange = []; end
end
function [affs] = RecoverResizedAffines(affs, configs, imgresize, tplresize)
configs(:, 1:2) = configs(:, 1:2) / imgresize; % tx, ty
configs(:, 4:5) = configs(:, 4:5) / (imgresize/tplresize); % sx, sy
for i = 1:length(affs)
% aff = affs(i);
affmat = CreateAffineTransformation(configs(i,:));
affs(i) = maketform('affine', affmat');
end
end