-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRANSAC.m
More file actions
211 lines (156 loc) · 6.03 KB
/
RANSAC.m
File metadata and controls
211 lines (156 loc) · 6.03 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
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
%% Q2A
fprintf('Part A starting...\n');
% Setup VLFeat for temp use
run('vlfeat-0.9.21/toolbox/vl_setup');
% Load images
LI = imread("./img_inputs/parliament-left.jpg");
RI = imread("./img_inputs/parliament-right.jpg");
% converted to single and grayscale
leftImg = single(rgb2gray(LI));
rightImg = single(rgb2gray(RI));
% The number of matches to extract from SIFT result
pruneVal = 5000;
% The sift() function takes a bit of time to complete so I stored all of
% the workspace variables for quicker execution. If you want to see it run
% instead, simply comment out the load() command, and uncomment the sift()
% command
load('./data/Part2A');
%[leftF, rightF, prunedMatches] = sift(leftImg, rightImg, pruneVal);
%% Compute affine transformation
% From sample list, compute affine transformation
disp('Computing affine transformation estimate using RANSAC');
disp('This might take some time...');
tic;
affine = RansacAffineEst(prunedMatches,leftF,rightF,1000,1000,1);
disp('Done!');
toc;
% The parameters to calculate my affine (which was fed into the
% RansacAffineEst() function above) were reduced to speed up execution
% time. I loaded in a better estimate below which was calculated using more
% datapoints, and therefore took much longer to calculate.
load('./data/affine');
%% Image stitching
close all hidden;
% Transformation
T = [[affine(1) affine(2) affine(5)]' [affine(3) affine(4) affine(6)]' [0 0 1]'];
tform = affine2d(T);
RI = imread("./img_inputs/parliament-right.jpg");
LI = imread("./img_inputs/parliament-left.jpg");
LI = imwarp(LI,tform);
figure;
imshow(uint8(LI));
title("Warped left image");
figure;
imshow(uint8(RI));
title("Original right image");
%% Image stitching
% The stitch() function takes some time to process, so I preprocessed it
% and simply reloaded the variable here for quicker execution for the demo
% only. If you want to run the stitch() function, simply uncomment the
% function and comment out the load() function.
%SI = stitch(LI, RI, pruneVal);
load('./data/Part2A-SI');
figure;
imshow(uint8(SI));
title("Combined image");
fprintf('Part A done! Press enter to continue...\n\n');
pause;
%% Q2B-1
fprintf('Part B-1 starting...\n\n');
clear;
% Load images, converted to single and grayscale
leftImg = single(rgb2gray(imread("./img_inputs/Ryerson-left.jpg")));
rightImg = single(rgb2gray(imread("./img_inputs/Ryerson-right.jpg")));
% The number of matches to extract from SIFT result
pruneVal = 5000;
% The sift() function takes a bit of time to complete so I stored all of
% the workspace variables for quicker execution. If you want to see it run
% instead, simply comment out the load() command, and uncomment the sift()
% command
load('./data/Part2B');
%[leftF, rightF, prunedMatches] = sift(leftImg, rightImg, pruneVal);
%% Compute homography transformation
% From sample list, compute homography transformation
disp('Computing homography transformation estimate using RANSAC');
disp('This might take some time...');
tic;
homography = RansacHomoEst(prunedMatches,leftF,rightF,500,500,1.5);
disp('Done!');
toc;
% The parameters to calculate my homography (which was fed into the
% RansacHomoEst() function above) were reduced to speed up execution
% time. I loaded in a better estimate below which was calculated using more
% datapoints, and therefore took much longer to calculate.
load('./data/homography');
% Reshape column vector into 3x3 matrix
H = reshape(homography,[3,3]);
tform = projective2d(H);
RI = imread("./img_inputs/Ryerson-right.jpg");
LI = imread("./img_inputs/Ryerson-left.jpg");
%% Image stitching
% The stitch() function takes some time to process, so I preprocessed it
% and simply reloaded the variable here for quicker execution for the demo
% only. If you want to run the stitch() function, simply uncomment the
% function and comment out the load() function.
%SI = stitch(imwarp(LI,tform), RI, pruneVal);
load('./data/Part2B-SI');
figure;
imshow(uint8(imwarp(LI,tform)));
title("Warped left image");
figure;
imshow(uint8(RI));
title("Right image");
figure;
imshow(uint8(SI));
title("Combined image");
fprintf('Part B-1 done! Press enter to continue...\n\n');
pause;
%% Q2B-2
fprintf('Part B-2 starting...\n\n');
clear;
% Load images, converted to single and grayscale
leftImg = single(rgb2gray(imread("./img_inputs/campus-left.jpg")));
rightImg = single(rgb2gray(imread("./img_inputs/campus-right.jpg")));
% The number of matches to extract from SIFT result
pruneVal = 2500;
% The sift() function takes a bit of time to complete so I stored all of
% the workspace variables for quicker execution. If you want to see it run
% instead, simply comment out the load() command, and uncomment the sift()
% command
load('./data/Part2B-2');
%[leftF, rightF, prunedMatches] = sift(leftImg, rightImg, pruneVal);
%% Compute homography transformation
% From sample list, compute homography transformation
disp('Computing homography transformation estimate using RANSAC');
disp('This might take some time...');
tic;
homography = RansacHomoEst(prunedMatches,leftF,rightF,500,500,1);
disp('Done!');
toc;
% The parameters to calculate my homography (which was fed into the
% RansacHomoEst() function above) were reduced to speed up execution
% time. I loaded in a better estimate below which was calculated using more
% datapoints, and therefore took much longer to calculate.
load('./data/homography-2B-2');
% Reshape column vector into 3x3 matrix
H = reshape(homography,[3,3]);
tform = projective2d(H);
%% Image stitching
LI = imread("./img_inputs/campus-left.jpg");
RI = imread("./img_inputs/campus-right.jpg");
% The stitch() function takes some time to process, so I preprocessed it
% and simply reloaded the variable here for quicker execution for the demo
% only. If you want to run the stitch() function, simply uncomment the
% function and comment out the load() function.
%SI = stitch(imwarp(LI,tform), RI, pruneVal);
load('./data/Part2B-2-SI');
figure;
imshow(uint8(imwarp(LI,tform)));
title("Warped left image");
figure;
imshow(uint8(RI));
title("Right image");
figure;
imshow(uint8(SI));
title("Combined image");
fprintf('Part B done!\n\n');