-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathK_MultiMatchFeaturePoints.m
294 lines (253 loc) · 11.6 KB
/
K_MultiMatchFeaturePoints.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
function [affines, scores, matchConfigs, timeList, fullErrors, nFeatMatch, nOtherMatch] = K_MultiMatchFeaturePoints(img_in, tpl_in, params)
% Use *SURF* to first generate several good _configs_ clusters and then
% process them one by one (expand--evaluate--expand--...).
% 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
useBigImages = 1; % use original images or subsampled images for matching (default=1)
useClustering = 0; % use DBSCAN clustering or not (default=0)
[BBSParams, matchParams] = CheckAllParams(params);
maxMatchRounds = params.nMatches;
useBBS = params.useBBS;
useSURF = params.useSURF;
timeList.BBSTime = 0;
timeList.featureTime = 0;
timeList.clusteringTime = 0;
timeList.featureMatchTime = [];
timeList.otherMatchTime = [];
fullErrors = [];
%% 1. Downsample _I_ and _T_ w.r.t. some rules
[img, tpl, IResize, TResize] = AdjustSize(img_in, tpl_in, BBSParams.pz);
resizeFactor = IResize/TResize;
matchParams.resizeFactor = IResize/TResize;
matchParams.useBigImages = useBigImages;
%% 2. Use BBS to approximately locate possible matches. Generate a mask _M_
if(useBBS)
[M, timeList.BBSTime] = BBSPreprocess(img, tpl, BBSParams);
figure, imshow(M);
else
M = ones(size(img, 1), size(img, 2));
end
if useBigImages
img = MakeOdd(img_in);
tpl_in = imresize(tpl_in, 1/resizeFactor);
tpl = MakeOdd(tpl_in);
szI = size(img);
szT = size(tpl);
M = imresize(M, [szI(1), szI(2)]);
else
szI = size(img);
szT = size(tpl);
end
%% 3. Use SURF features to further restrain the rotation and scale search range
% the high-resolution version of the images are needed to get more features
if(useSURF)
[restrictedSearchRange, resM, scaleRotRange, ~, ~, timeList.featureTime] = SearchRangeFromFeatureVoting(img_in, tpl_in, M);
matchParams.searchRangeByFeature = restrictedSearchRange;
else
restrictedSearchRange = [];
end
%% 4. Generate configs for searching.
% M = ones(size(M)); % debug
[initConfigs, gridSize, matchParams] = InitializeConfigs(M, matchParams, szI, szT);
% 可以先initialize所有configs,但先根据feature得到的结果来搜索,用搜索结果去mask这些initConfigs。
%% 5. Search for matches guided by features
curAccept = 1;
matchRound = 1;
nFeatMatch = 0;
nOtherMatch = 0;
affines = [];
scores = []; %zeros(maxMatchRounds, 1);
matchConfigs = zeros(maxMatchRounds, 6);
% matchRound = matchRound + 1;
A = [];
deltaFact = 1.511;
optMat = [];
% restrictedSearchRange = [];
scalerotRanges = [];
acceptFeatureRange = [];
for i = 1:size(restrictedSearchRange,1)
[M, initConfigs] = MaskNewMatch(M, tpl, initConfigs, A);
featSR = restrictedSearchRange(i,:);
if ~useBigImages
featSR(1:2) = featSR(1:2) * resizeFactor;
featSR(5:8) = featSR(5:8) * IResize;
end
sr.minScale = featSR(1); % * resizeFactor;
sr.maxScale = featSR(2); % * resizeFactor;
sr.minRotation = featSR(3);
sr.maxRotation = featSR(4);
sr.minTx = featSR(5); % * IResize;
sr.maxTx = featSR(6); % * IResize;
sr.minTy = featSR(7); % * IResize;
sr.maxTy = featSR(8); % * IResize;
curMatchParams = matchParams;
curMatchParams.searchRange = sr;
[curInitConfigs, curGridSize, curMatchParams] = InitializeConfigs(M, curMatchParams, szI, szT);
if isempty(curInitConfigs)
fprintf('Feature based match #%d not found because of no configs!!\n', matchRound);
acceptFeatureRange = [acceptFeatureRange, 0];
continue;
end
[A, score, config, accept, curtime] = FindOneMatch(img, tpl, M, curInitConfigs, curMatchParams, scores);
timeList.featureMatchTime = [timeList.featureMatchTime, curtime];
curAccept = accept;
acceptFeatureRange = [acceptFeatureRange, accept];
config
fprintf('Match #%d score (distance) = %.4f\n\n', matchRound, score);
if(~curAccept)
fprintf('Feature based match #%d not found because of not accepted!!\n', matchRound);
else
scalerotRanges = [scalerotRanges; featSR];
affines = [affines, A];
scores = [scores, score];
matchConfigs(matchRound, :) = config;
% [optError,fullError,overlapError] = MatchingResult(tpl,img,A.tdata.T',optMat,sprintf('example %d', matchRound));
% fprintf('example %d - optError: %.4f (%.2f GLs), fullError: %.4f (%.2f GLs), overlapError: %.1f%%\n',...
% matchRound, optError,256*optError,fullError,256*fullError,100*overlapError);
% fullErrors = [fullErrors, fullError];
matchRound = matchRound + 1;
end
end
nFeatMatch = matchRound - 1;
% draw feature voting search ranges
% figure, imshow(img_in); hold on
% for j = 1:length(acceptFeatureRange)
% if(acceptFeatureRange(j))
% rectangle('Position', [restrictedSearchRange(j,5) + size(img_in,2)/2, ...
% restrictedSearchRange(j,7) + size(img_in,1)/2, restrictedSearchRange(j,6) - restrictedSearchRange(j,5), ...
% restrictedSearchRange(j,8) - restrictedSearchRange(j,7) ], 'EdgeColor', 'g', 'LineWidth', 1.5);
% else
% rectangle('Position', [restrictedSearchRange(j,5) + size(img_in,2)/2, ...
% restrictedSearchRange(j,7) + size(img_in,1)/2, restrictedSearchRange(j,6) - restrictedSearchRange(j,5), ...
% restrictedSearchRange(j,8) - restrictedSearchRange(j,7) ], 'EdgeColor', 'y', 'LineWidth', 1.5);
% end
% end
if ~isempty(scalerotRanges)
allmins = min(scalerotRanges(:,1));
allmaxs = max(scalerotRanges(:,2));
allminr = min(scalerotRanges(:,3));
allmaxr = max(scalerotRanges(:,4));
scaleRotRange = [allmins, allmaxs, allminr, allmaxr]
else
scaleRotRange = [];
end
if ~isempty(scaleRotRange)
if useBigImages
matchParams.searchRange.minScale = scaleRotRange(:,1);
matchParams.searchRange.maxScale = scaleRotRange(:,2);
else
matchParams.searchRange.minScale = scaleRotRange(:,1) * matchParams.resizeFactor;
matchParams.searchRange.maxScale = scaleRotRange(:,2) * matchParams.resizeFactor;
end
matchParams.searchRange.minRotation = scaleRotRange(:,3);
matchParams.searchRange.maxRotation = scaleRotRange(:,4);
end
%% 6. Set _M(A1(T))=0_ and search for the remaining matches.
if(nFeatMatch >= maxMatchRounds)
fprintf('\n!!!!!!!!ENDING because enough feature matches!!!!!!!!!!\n');
fprintf('nFeatMatch = %d, nOtherMatch = 0\n', nFeatMatch);
return;
end
[initConfigs, gridSize, matchParams] = InitializeConfigs(M, matchParams, szI, szT);
% assess initConfigs and find good ones in it to start further matching
[goodConfigs, configClassList, numClasses, allDistances, initConfigs, initTime] = FindGoodInitConfigs(img, tpl, M, initConfigs, matchParams, useClustering);
timeList.otherMatchTime = [timeList.otherMatchTime, initTime];
newMatchRound = 1;
if ~useClustering
initGoodConfigs = goodConfigs(configClassList == 1, :);
curConfigs = ExpandConfigsRandom(initGoodConfigs, matchParams.steps, 1, 80, deltaFact);
curConfigs = BoundConfigsTrSc(curConfigs, matchParams.bounds);
end
curAccept = 1;
while(curAccept && matchRound<=maxMatchRounds)
if useClustering
fprintf('\nMatch #%d, %d goodConfigs in this class: \n', matchRound, nnz(configClassList == matchRound));
[M, initConfigs] = MaskNewMatch(M, tpl, initConfigs, A);
initGoodConfigs = goodConfigs(configClassList == newMatchRound, :);
curConfigs = ExpandConfigsRandom(initGoodConfigs, matchParams.steps, 1, 80, deltaFact);
curConfigs = BoundConfigsTrSc(curConfigs, matchParams.bounds);
newMatchRound = newMatchRound + 1;
else
[M, curConfigs] = MaskNewMatch(M, tpl, curConfigs, A);
fprintf('\n--->>>Match #%d\n', matchRound);
if(size(curConfigs, 1) < 50) % 补充一下资源
[initGoodConfigs, ~, ~, allDistances, initConfigs, initTime] = ...
FindGoodInitConfigs(img, tpl, M, initConfigs, matchParams, useClustering, allDistances);
curConfigs = ExpandConfigsRandom(initGoodConfigs, matchParams.steps, 1, 80, deltaFact);
curConfigs = BoundConfigsTrSc(curConfigs, matchParams.bounds);
end
end
[A, score, config, accept, curtime] = FindOneMatch(img, tpl, M, curConfigs, matchParams, scores); % scores can be used as threshold
timeList.otherMatchTime = [timeList.otherMatchTime, curtime];
curAccept = accept;
config
fprintf('Match #%d score (distance) = %.4f\n\n', matchRound, score);
if(~curAccept)
fprintf('Stop at match #%d!\n', matchRound);
break;
else
affines = [affines, A];
scores = [scores, score];
matchConfigs(matchRound, :) = config;
% [optError,fullError,overlapError] = MatchingResult(tpl,img,A.tdata.T',optMat,sprintf('example %d', matchRound));
% fprintf('example %d - optError: %.4f (%.2f GLs), fullError: %.4f (%.2f GLs), overlapError: %.1f%%\n',...
% matchRound, optError,256*optError,fullError,256*fullError,100*overlapError);
% fullErrors = [fullErrors, fullError];
end
matchRound = matchRound + 1;
end
nOtherMatch = matchRound - nFeatMatch - 1;
%% 7. Output the result.
% 似乎不用做什么。
% 不对,得把大小还原回去……
if ~useBigImages
affines = RecoverResizedAffines(affines, matchConfigs, IResize, TResize);
end
fprintf('\n!!!!!!!!ENDING!!!!!!!!!!\n');
fprintf('nFeatMatch = %d, nOtherMatch = %d\n', nFeatMatch, nOtherMatch);
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
function [] = DisplayRestrictedSR(restrictedSearchRange, img_in)
figure, imshow(img_in);
for j=1:size(restrictedSearchRange,1)
rectangle('Position', [restrictedSearchRange(j,5) + size(img_in,2)/2, ...
restrictedSearchRange(j,7) + size(img_in,1)/2, restrictedSearchRange(j,6) - restrictedSearchRange(j,5), ...
restrictedSearchRange(j,8) - restrictedSearchRange(j,7) ]);
end
end