Repository navigation
Expand file tree
/
Copy pathMultipleStitch.m
More file actions
186 lines (159 loc) · 7.4 KB
/
Copy pathMultipleStitch.m
File metadata and controls
186 lines (159 loc) · 7.4 KB
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
function Pano = MultipleStitch( IMAGES, TRANS, fileName )
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%MultipleStitch
% This function stitches multiple images together and outputs the panoramic stitched image
% with a chain of input images and its corresponding transformations.
%
% Given a chain of images:
% I1 -> I2 -> I3 -> ... -> Im
% and its corresponding transformations:
% T1 transforms I1 to I2
% T2 transforms I2 to I3
% ....
% Tm-1 transforms Im-1 to Im
%
% We choose the middle image as the reference image, and the outputed
% panorama is in the same coordinate system as the reference image.
%
% For this part, all the image stitching code has been provided to you.
% The main task for you is to fill in the code so that current
% transformations are used when we produce the final panorama.
%
% Originally, we have
% I1 -> I2 -> ... -> Iref -> ... -> Im-1 -> Im
% When we fix Iref as the final coordinate system, we want all other
% images transformed to Iref. You are responsible for finding the current
% transformations used under this circumstances.
%
% INPUT:
% IMAGES: 1 * m cell array, each cell contains an image
% TRANS: 1 * (m-1) cell array, each cell i contains an affine
% transformation matrix that transforms Ii to Ii+1.
% fileName: the output file name.
%
% OUTPUT:
% Pano: the final panoramic image.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ~exist('fileName', 'var'),
fileName = 'pano.jpg';
end
if length(IMAGES) ~= length(TRANS)+1,
error('Number of images does not match the number of transformations.');
end
%% Outbounds of panorama image
outBounds = zeros(2,2);
outBounds(1,:) = Inf;
outBounds(2,:) = -Inf;
%% Choose reference image Iref
refIdx = ceil(median(1:length(IMAGES)));
%% Estimate the largest possible panorama size
[nrows, ncols, ~] = size(IMAGES{1});
nrows = length(IMAGES) * nrows;
ncols = length(IMAGES) * ncols;
% imageToRefTrans is a 1 x m cell array where imageToRefTrans{i} gives the
% affine transformation from IMAGES{i} to the reference image
% IMAGES{refIdx}. Your task is to fill in this array.
imageToRefTrans = cell(1, length(IMAGES));
% Initialize imageToRefTrans to contain the identity transform.
for idx = 1:length(imageToRefTrans)
imageToRefTrans{idx} = eye(3);
end
%% Find the correct transformations used for images on the left side of Iref
for idx = refIdx-1:-1:1,
imageToRefTrans{idx} = makeTransformToReferenceFrame(TRANS, idx, refIdx);
T = imageToRefTrans{idx};
tmpBounds = findbounds(maketform('affine', T'), [1 1; ncols nrows]);
outBounds(1,:) = min(outBounds(1,:),tmpBounds(1,:));
outBounds(2,:) = max(outBounds(2,:),tmpBounds(2,:));
end
%% Find the correct transformations used for images on the right side of Iref
for idx = refIdx + 1 : length(imageToRefTrans),
imageToRefTrans{idx} = makeTransformToReferenceFrame(TRANS, idx, refIdx);
T = imageToRefTrans{idx};
T(3, :) = [0, 0, 1]; % Fix rounding errors in the last row.
tmpBounds = findbounds(maketform('affine', T'), [1 1; ncols nrows]);
outBounds(1,:) = min(outBounds(1,:),tmpBounds(1,:));
outBounds(2,:) = max(outBounds(2,:),tmpBounds(2,:));
end
%% Stitch the Iref image.
XdataLimit = round(outBounds(:,1)');
YdataLimit = round(outBounds(:,2)');
Pano = imtransform( im2double(IMAGES{refIdx}), maketform('affine', eye(3)), 'bilinear', ...
'XData', XdataLimit, 'YData', YdataLimit, ...
'FillValues', NaN, 'XYScale',1);
%% Transform the images from the left side of Iref using the correct transformations you computed
for idx = refIdx-1:-1:1,
T = imageToRefTrans{idx};
Tform = maketform('affine', T');
AddOn = imtransform(im2double(IMAGES{idx}), Tform, 'bilinear', ...
'XData', XdataLimit, 'YData', YdataLimit, ...
'FillValues', NaN, 'XYScale',1);
result_mask = ~isnan(Pano(:,:,1));
temp_mask = ~isnan(AddOn(:,:,1));
add_mask = temp_mask & (~result_mask);
for c = 1 : size(Pano,3),
cur_im = Pano(:,:,c);
temp_im = AddOn(:,:,c);
cur_im(add_mask) = temp_im(add_mask);
Pano(:,:,c) = cur_im;
end
end
%% Transform the images from the right side of Iref using the correct transformations you computed
for idx = refIdx + 1 : length(imageToRefTrans),
T = imageToRefTrans{idx};
T(3, :) = [0, 0, 1]; % Fix rounding errors in the last row.
Tform = maketform('affine', T');
AddOn = imtransform(im2double(IMAGES{idx}), Tform, 'bilinear', ...
'XData', XdataLimit, 'YData', YdataLimit, ...
'FillValues', NaN, 'XYScale',1);
result_mask = ~isnan(Pano(:,:,1));
temp_mask = ~isnan(AddOn(:,:,1));
add_mask = temp_mask & (~result_mask);
for c = 1 : size(Pano,3),
cur_im = Pano(:,:,c);
temp_im = AddOn(:,:,c);
cur_im(add_mask) = temp_im(add_mask);
Pano(:,:,c) = cur_im;
end
end
%% Cropping the final panorama to leave out black spaces.
[I, J] = ind2sub([size(Pano, 1), size(Pano, 2)], find(~isnan(Pano(:, :, 1))));
upper = max(min(I)-1, 1);
lower = min(max(I)+1, size(Pano, 1));
left = max(min(J)-1, 1);
right = min(max(J)+1, size(Pano, 2));
Pano = Pano(upper:lower, left:right,:);
imwrite(Pano, fileName);
end
function T = makeTransformToReferenceFrame(i_To_iPlusOne_Transform, currentFrameIndex, refFrameIndex)
%makeTransformToReferenceFrame
% INPUT:
% i_To_iPlusOne_Transform: this is a cell array where
% i_To_iPlusOne_Transform{i} contains the 3x3 homogeneous transformation
% matrix that transforms a point in frame i to the corresponding point in
% frame i+1
%
% currentFrameIndex: index of the current coordinate frame in i_To_iPlusOne_Transform
% refFrameIndex: index of the reference coordinate frame
%
% OUTPUT:
% T: A 3x3 homogeneous transformation matrix that would convert a point
% in the current frame into the corresponding point in the reference
% frame. For example, if the current frame is 2 and the reference frame
% is 3, then T = i_To_iPlusOne_Transform{2}. If the current frame and
% reference frame are not adjacent, T will need to be calculated.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% YOUR CODE HERE: Calculate T as defined above. %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% HINT 1: There are two separate cases to consider: currentFrameIndex <
% refFrameIndex (this is the easier case), and currentFrameIndex >
% refFrameIndex (this is the harder case).
% HINT 2: You can use the pinv function to invert a transformation.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% END OF YOUR CODE %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end