-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.m
219 lines (166 loc) · 5.12 KB
/
main.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
%{
%% load and crop
imgs = Prj2TB.read_all_imgs('raw_img/FV5','','TIF');
imgs_cropped = Prj2TB.crop_images(imgs);
%% show
close all
pause(0.5); figure;
for i=1:length(imgs_cropped)
imshow(imgs_cropped{i});
title(num2str(i));
pause(3);
end
%}
%% clear and load cropped images
clear;
imgs = Prj2TB.read_all_imgs('.','','TIF');
close all
%% align all?
close all
imgs_tfed = {};
imgs_cumu = {};
ind_range = [1,2,5,6,7,8,9];
ind_range = [5,6,7,8,9];
% ind_range = [5,6];
imtermediate_plots = false;
count = 1;
img_base = imgs{ind_range(1)};
img_pool = uint32(imresize(img_base,1));
for i = ind_range(2:end)
count = count + 1;
if count>3 && imtermediate_plots
close all
end
this_img = Prj2TB.align(img_base, imgs{i}, imtermediate_plots);
size(this_img)
size(img_pool)
imgs_tfed{end+1} = this_img;
img_pool = img_pool + uint32(this_img);
imgs_cumu{end+1} = uint16(img_pool / count);
figure;
imshow(imgs_cumu{end});
title(sprintf('img number %i', i))
%{
center = flip([509, 780]);
box = floor([256 256]/2)*2;
lc = center - box/2 + 1;
uc = center + box/2;
this_img = imgs_cumu{end}(lc(1):uc(1), lc(2):uc(2), :);
filename = sprintf('progressive_%i.PNG',count);
imwrite(imresize(uint8(this_img ./ 2^8), 8, 'nearest'), filename);
%}
end
img_out = uint16(img_pool ./ length(ind_range));
%{
this_img = img_base(lc(1):uc(1), lc(2):uc(2), :);
filename = 'progressive_0.PNG';
imwrite(imresize(uint8(this_img ./ 2^8), 8, 'nearest'), filename);
%}
%% show and compare
figure; imshowpair(img_out, img_base, 'montage');
figure; imshowpair(img_out(lc(1):uc(1), lc(2):uc(2), :), img_base(lc(1):uc(1), lc(2):uc(2), :), 'montage');
%%
close all
imgA_raw = imgs{1};
imgB_raw = imgs{2};
pause(0.5); figure; imshow(imgA_raw);
title('original image A');
pause(0.5); figure; imshow(imgA_raw);
title('original image B');
imgA = imgA_raw(:,:,1);
imgB = imgB_raw(:,:,1);
ptThresh = 0.2;
pointsA = detectFASTFeatures(imgA, 'MinContrast', ptThresh);
pointsB = detectFASTFeatures(imgB, 'MinContrast', ptThresh);
pause(0.5); figure; imshow(imgA); hold on;
plot(pointsA);
title('Corners in A, red channel');
pause(0.5); figure; imshow(imgB); hold on;
plot(pointsB);
title('Corners in B, red channel');
%%
[featuresA, pointsA] = extractFeatures(imgA, pointsA);
[featuresB, pointsB] = extractFeatures(imgB, pointsB);
%%
indexPairs = matchFeatures(featuresA, featuresB);
pointsA = pointsA(indexPairs(:, 1), :);
pointsB = pointsB(indexPairs(:, 2), :);
%%
pause(0.5); figure; showMatchedFeatures(imgA, imgB, pointsA, pointsB);
legend('A', 'B');
title('features marked and paired between A and B')
%%
shift = pointsA.Location - pointsB.Location;
pause(0.5); figure;
plot(shift(:,1),shift(:,2), 'x')
grid on;
axis([-200 200 -200 200])
title('scatter plot of feature shifts for clustering')
axis square
pt_ref = [-16 * ones(length(shift), 1),-23 * ones(length(shift), 1)] ;
norm2sq = sum(transpose((shift - pt_ref).^2));
sel_ind = norm2sq < 20^2;
ptA_sel = pointsA(sel_ind);
ptB_sel = pointsB(sel_ind);
%% change to selected points
pointsA = ptA_sel;
pointsB = ptB_sel;
%%
[tform, pointsBm, pointsAm] = estimateGeometricTransform(...
pointsB, pointsA, 'affine');
imgBp = imwarp(imgB, tform, 'OutputView', imref2d(size(imgB)));
pointsBmp = transformPointsForward(tform, pointsBm.Location);
%%
pause(0.5); figure;
showMatchedFeatures(imgA, imgBp, pointsAm, pointsBmp);
legend('A', 'B');
title('image B shifted and overlayed with corners marked')
%%
imgA = imgA_raw;
imgB = imgB_raw;
% Extract scale and rotation part sub-matrix.
H = tform.T;
R = H(1:2,1:2);
% Compute theta from mean of two possible arctangents
theta = mean([atan2(R(2),R(1)) atan2(-R(3),R(4))]);
% Compute scale from mean of two stable mean calculations
scale = mean(R([1 4])/cos(theta));
% Translation remains the same:
translation = H(3, 1:2);
% Reconstitute new s-R-t transform:
HsRt = [[scale*[cos(theta) -sin(theta); sin(theta) cos(theta)]; ...
translation], [0 0 1]'];
tformsRT = affine2d(HsRt);
imgBold = imwarp(imgB, tform, 'OutputView', imref2d(size(imgB)));
imgBsRt = imwarp(imgB, tformsRT, 'OutputView', imref2d(size(imgB)));
pause(0.5); figure, clf;
imshowpair(imgBold,imgBsRt,'ColorChannels','red-cyan'), axis image;
title('Color composite of affine and s-R-t transform outputs');
%%
pause(0.5); figure;
showMatchedFeatures(imgA, imgBsRt, pointsAm, pointsBmp);
legend('A', 'B');
title('image B shifted and overlayed with corners marked')
%%
close all
im_merge = (uint32(imgA) + uint32(imgBsRt))./2;
im_merge = uint16(im_merge);
pause(0.5); figure;
imshow(imgA)
title('original image A');
pause(0.5); figure;
imshow(im_merge)
title('shifted and stacked with mean value');
img_out = {};
for i = {im_merge, imgA}
this_img = i{1};
img_size = size(this_img)
% center = floor(img_size(1:2)/2)
center = [180, 780];
box = floor([256 256]/2)*2;
lc = center - box/2
uc = center + box/2
img_out{end+1} = this_img(lc(1):uc(1), lc(2):uc(2), :);
end
pause(0.5); figure; imshowpair(img_out{1}, img_out{2}, 'montage');
title('256px cropped comparison bettwen stacked(L) and raw(R)');