-
Notifications
You must be signed in to change notification settings - Fork 5
/
naz_sift.m
325 lines (266 loc) · 10.9 KB
/
naz_sift.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
clear; clc; close all; addpath(genpath('.\')); % ind_ = @(A,r,c) A(r,c);
iptsetpref('ImshowBorder','tight'); %??
Consts; Params;
params.seg.featureSet = consts.BFT_RGBD;
params.debug_visible = 'on';
params.debug_fig = false;
conf.imgFile = '%s/img%06d_stg%d_a';
conf.imgJuncFile = '%s/img%06d_stg%d_b';
conf.imgSiftMatchFile = '%s/img%06d_stg%d_rt%1.1f';
conf.imgSiftHomoFile = '%s/img%06d_stg%d_t%1.1f_rt%1.1f';
%NB! Constants must be modified inside Consts.m only, don't modify them
%here, otherwise annoying bugs will come out and bug you.
%%% NB! Do not change this line, change sample size only by changing the
%%% range of consts.useNdx in Consts.m
conf.sampleSize = length(consts.useNdx);
conf.sampleStage = [5 4 3 2 1];
conf.imgGap = 20; % size of gap between the images
conf.juncMarker = 'oy';
conf.siftMarker = 'oy';
conf.markerSize = 3;
conf.t = {
[0.6 0.3], ...
[0.6 0.4], ...
[0.4 0.4], ...
[0.7 0.4], ...
[0.4 0.2], ...
[0.3 0.2] ...
};
% TODO to skip already computed figs
for t_ = conf.t
rt = t_{1}(1);
t = t_{1}(2);
for sampleStage = conf.sampleStage
% create subdir according to Sample size and Stage chosen
matchDir = sprintf('%ssample_%d/stage_%d/', consts.matchDir, conf.sampleSize, sampleStage);
if exist(matchDir, 'dir')~=7
mkdir(matchDir);
end
% TODO verify if classifier and corresponding hierarchical segmentation exists for given sample size
for setInd = 1:length(consts.matchImgId)
idSet = consts.matchImgId{setInd}; % set of image IDs for cuurent match group (from consts.matchImgId cell array)
imgNum = length(idSet); % number of images in chosen set
% CONSIDERRING ONLY PAIRS OF IMAGES (BY NOW)
if imgNum < 2 || imgNum > 2; continue; end;
fprintf('Processing image pairs: %d<->%d, stage %d, rt = %1.1f, t = %1.1f ...', ...
idSet(1), idSet(2), sampleStage, rt, t);
imRgb = cell(imgNum,1); % RGB images
im = cell(imgNum, 1); % greyscale images
x = cell(imgNum, 1); % junction x-coordinates
y = cell(imgNum, 1); % junction y-coordinates
D = cell(imgNum, 1); % sift descriptors
juncsIm = cell(imgNum, 1);
edgesIm = cell(imgNum, 1);
for i = 1:imgNum
load(sprintf(consts.imageRgbFilename, idSet(i)), 'imgRgb');
load(sprintf(consts.boundaryInfoPostMerge, conf.sampleSize, params.seg.featureSet, sampleStage, idSet(i)), 'boundaryInfo');
edgesIm{i} = boundaryInfo.edges.fragments; % edges from watershed segmentation
juncsIm{i} = boundaryInfo.junctions.position; % round means top-right pixel from junction point
imgRgb = double(imgRgb)/255; % im2double
%MARKER round!
% extracting image and its junctions coordinates
imRgb{i} = imgRgb;
im{i} = rgb2gray(imRgb{i});
x{i} = round(juncsIm{i}(:,1));
y{i} = round(juncsIm{i}(:,2));
conf.sizeX = size(imRgb{i},1);
conf.sizeY = size(imRgb{i},2);
end
clear imgRgb boundaryInfo;
conf.imgGapStub = 0*ones(conf.sizeX, conf.imgGap, 3); % 1 == maximum intensity (255)
pairedImRgb = [imRgb{1} conf.imgGapStub imRgb{2}];
% show images
% --------------------------------------------------
filename = sprintf([conf.imgFile '.png'], matchDir, idSet(1), sampleStage);
if ~exist(filename, 'file')
h_img = figure('Visible','off');%params.debug_visible);
imshow(pairedImRgb); axis image; axis off; title(sprintf('Images #%d #%d', idSet(1), idSet(2)));
if params.debug; print(h_img, '-dpng', sprintf(filename, matchDir, idSet(1), sampleStage) ); end;
end
% show detected points
% --------------------------------------------------
filename = sprintf([conf.imgJuncFile '.png'], matchDir, idSet(1), sampleStage);
if ~exist(filename, 'file')
h_pnts = figure('Visible',params.debug_visible);
imshow(pairedImRgb); axis image; axis off; hold on;
title(sprintf('Images #%d #%d, stage%d', idSet(1), idSet(2), sampleStage));
plot(juncsIm{1}(:,1),juncsIm{1}(:,2), conf.juncMarker, 'MarkerSize', conf.markerSize);
plot(juncsIm{2}(:,1)+conf.sizeY+conf.imgGap, juncsIm{2}(:,2), conf.juncMarker, 'MarkerSize', conf.markerSize);
for i = 1:imgNum
if i==1
shift = 0;
else
shift = conf.sizeY + conf.imgGap;
end
edges = edgesIm{i};
for k = 1:length(edges)
plot(edges{k}(:,1)+shift, edges{k}(:,2), 'r', 'LineWidth', 0.5);
end
end
%if params.debug; print(h_pnts, '-dpng', sprintf('%s/img%06d_b.png', matchDir, idSet(1)) ); end;
if params.debug; saveas(h_pnts, filename, 'png'); end
if params.debug_fig; saveas(h_pnts, filename, 'fig'); end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Extract descriptors (heavily blurred 21xb1 patches)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
filename = sprintf([conf.imgSiftHomoFile '.png'], matchDir, idSet(1), sampleStage, t, rt);
if ~exist(filename, 'file')
for i = 1:imgNum
[D{i}, x{i}, y{i}] = ext_desc(im{i}, x{i}, y{i});
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Compute tentative matches between image 1 (a) and 2 (b)
% by matching local features
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%rt = 0.6; % 1NN/2NN distance ratio threshold (between 0 and 1)
D2 = dist2(D{1}',D{2}'); % compute pair-wise distances between descriptors
[Y,I] = sort(D2,2); % sort distances
rr = Y(:,1)./Y(:,2); % compute D. Lowes' 1nn/2nn ratio test
inD12 = find(rr<rt); % take only points with a 1nn/2nn ratio below 0.8
I = I(inD12); % select matched points
xat = x{1}(inD12);
yat = y{1}(inD12);
xbt = x{2}(I);
ybt = y{2}(I);
% show all tentative matches
h_match = figure('Visible',params.debug_visible);
imshow(pairedImRgb); axis image; axis off; hold on;
title( sprintf('Tentative correspondences: img #%d #%d, stage%d (rt=%1.1f)', idSet(1), idSet(2), sampleStage, rt) );
for i = 1:imgNum
if i==1
shift = 0;
else
shift = conf.sizeY + conf.imgGap;
end
edges = edgesIm{i};
for k = 1:length(edges)
plot(edges{k}(:,1)+shift, edges{k}(:,2), 'r', 'LineWidth', 0.5);
end
end
plot(xat,yat,conf.siftMarker, 'MarkerSize', conf.markerSize);
plot(xbt+conf.sizeY+conf.imgGap,ybt,conf.siftMarker, 'MarkerSize', conf.markerSize);
hl = line([xat; xbt+conf.sizeY+conf.imgGap],[yat; ybt],'color','g');
if params.debug; saveas(h_match, sprintf([conf.imgSiftMatchFile '.png'], matchDir, idSet(1), sampleStage, rt), 'png'); end
if params.debug_fig; saveas(h_match, sprintf([conf.imgSiftMatchFile '.fig'], matchDir, idSet(1), sampleStage, rt), 'fig'); end
%if length(D)<5; continue; end;
try
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Robustly fit homography
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Specify the inlier threshold (in noramlized image co-ordinates)
%t = 0.3;
[Hab, inliers] = ransacfithomography([xat; yat], [xbt; ybt], t);
% show inliers
h_homo = figure('Visible',params.debug_visible); clf; clf;
imshow(pairedImRgb); axis image; axis off; hold on;
title(sprintf('Homography (ransac inliers): img #%d #%d, stg%d (rt=%1.1f, t=%1.1f)', idSet(1), idSet(2), sampleStage, rt, t));
for i = 1:imgNum
if i==1
shift = 0;
else
shift = conf.sizeY + conf.imgGap;
end
edges = edgesIm{i};
for k = 1:length(edges)
plot(edges{k}(:,1)+shift, edges{k}(:,2), 'r', 'LineWidth', 0.5);
end
end
hl = line([xat(inliers); xbt(inliers)+conf.sizeY+conf.imgGap],[yat(inliers); ybt(inliers)],'color','g');
plot(xat(inliers),yat(inliers),conf.siftMarker, 'MarkerSize', conf.markerSize);
plot(xbt(inliers)+conf.sizeY+conf.imgGap,ybt(inliers), conf.siftMarker, 'MarkerSize', conf.markerSize);
if params.debug; saveas(h_homo, filename, 'png'); end
if params.debug_fig; saveas(h_homo, filename, 'fig'); end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Visualize homography
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% vgg_gui_H(imc,imb,Hcb);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Warp and composite images
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % % figure(5); clf;
% % % bbox=[-400 1200 -200 700] % image space for mosaic
% % % % warp image b to mosaic image using an identity homogrpahy
% % % % Image b is chosen as the reference frame
% % % iwb = vgg_warp_H(imRgb{2}, eye(3), 'linear', bbox);
% % % imshow(iwb); axis image;
% % %
% % % % warp image 1 to the reference mosaic frame (image 2)
% % % figure(6); clf;
% % % iwa = vgg_warp_H(imRgb{1}, Hab, 'linear', bbox); % warp image a to the mosaic image
% % % imshow(iwa); axis image;
% % % imagesc(double(max(iwb,iwa))); % combine images into a common mosaic (take maximum value of the two images)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Estimate homography between images 3 and 2.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Based on the code above, write code to:
% 1. Compute tentative matches between images 2 and 3
% 2. Robustly fit homography Hcb
% 3. Re-estimate homography from inliers
% ----
catch ME
fprintf('Exception in homography: %s\n', ME.message);
end
else
fprintf(' pair computed already.\n');
end
if imgNum < 3
%pause;
close all;
continue
end;
rt = 0.5; % 1NN/2NN distance ratio threshold (between 0 and 1)
D2 = dist2(Dc',Db'); % compute pair-wise distances between descriptors
[Y,I] = sort(D2,2); % sort distances
rr = Y(:,1)./Y(:,2); % compute D. Lowes' 1nn/2nn ratio test
inD12 = find(rr<rt); % take only points with a 1nn/2nn ratio below 0.8
I = I(inD12); % select matched points
xct = x{3}(inD12);
yct = y{3}(inD12);
xbt = x{2}(I);
ybt = y{2}(I);
% show all tentative matches
figure(1); clf;
imagesc(imargb); hold on;
plot(xat,yat,'+g');
hl = line([xct; xbt],[yct; ybt],'color','y');
title('Tentative correspondences');
axis off;
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Robustly fit homography
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Specify the inlier threshold (in noramlized image co-ordinates)
t = 0.3;
[Hcb, inliers] = ransacfithomography([xct; yct], [xbt; ybt], t);
% show inliers
figure(4); clf;
imagesc(imargb); hold on;
hl = line([xct(inliers); xbt(inliers)],[yct(inliers); ybt(inliers)]);
set(hl,'color','y');
plot(xct(inliers),yct(inliers),'+g');
title('Inliers');
figure(5); clf;
bbox=[-400 1200 -200 700]; % image space for mosaic
% warp image b to mosaic image using an identity homogrpahy
% Image b is chosen as the reference frame
iwb = vgg_warp_H(imbrgb, eye(3), 'linear', bbox);
imshow(iwb); axis image;
% warp image 1 to the reference mosaic frame (image 2)
figure(6); clf;
iwc = vgg_warp_H(imcrgb, Hcb, 'linear', bbox); % warp image a to the mosaic image
imshow(iwc); axis image;
imagesc(double(max(iwb,iwc))); % combine images into a common mosaic (take maximum value of the two images)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Final warping and compositing
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure(7); clf;
iwc = vgg_warp_H(imcrgb, Hcb, 'linear', bbox); % warp image c to mosaic image
imshow(iwc); axis image;
figure(8); clf;
imagesc(max(iwc,double(max(iwb,iwa)))); % combine images into a common mosaic
axis image; axis off;
% pause;
close all;
end
end
end