-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsfmmv_ex.m
212 lines (163 loc) · 6.67 KB
/
sfmmv_ex.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
clear all;
close all;
clc;
%% Use the |imageSet| object to get a list of all image file names in a
% directory.
imageDir = fullfile(toolboxdir('vision'), 'visiondata', ...
'structureFromMotion');
imSet = imageSet(imageDir);
%Display the images.
figure
montage(imSet.ImageLocation, 'Size', [3, 2]);
title('Input Image Sequence');
%Convert the images to grayscale.
images = cell(1, imSet.Count);
for i = 1:imSet.Count
I = read(imSet, i);
images{i} = rgb2gray(I);
end
%Load camera parameters (created using Camera Calibrator app)
load(fullfile(imageDir, 'cameraParams.mat'));
%% Undistort the first image and Create a View Set Containing the First View
I = undistortImage(images{1}, cameraParams);
% figure; imshow(I);
%Detect features. Increasing 'NumOctaves' helps detect large-scale
% features in high-resolution images. Use an ROI to eliminate spurious
% features around the edges of the image.
border = 50;
roi = [border, border, size(I, 2)- 2*border, size(I, 1)- 2*border];
prevPoints = detectSURFFeatures(I, 'NumOctaves', 8, 'ROI', roi);
%Extract features. Using 'Upright' features improves matching, as long as
% the camera motion involves little or no in-plane rotation.
prevFeatures = extractFeatures(I, prevPoints, 'Upright', true);
%Create an empty viewSet object to manage the data associated with each
% view.
vSet = viewSet;
%Add the first view. Place the camera associated with the first view
% and the origin, oriented along the Z-axis.
viewId = 1;
vSet = addView(vSet, viewId, 'Points', prevPoints, 'Orientation', eye(3),...
'Location', [0 0 0]);
%% Add the Rest of the Views
for i = 2:numel(images)
% Undistort the current image.
I = undistortImage(images{i}, cameraParams);
% Detect, extract and match features.
currPoints = detectSURFFeatures(I, 'NumOctaves', 8, 'ROI', roi);
currFeatures = extractFeatures(I, currPoints, 'Upright', true);
indexPairs = matchFeatures(prevFeatures, currFeatures, ...
'MaxRatio', .7, 'Unique', true);
% Select matched points.
matchedPoints1 = prevPoints(indexPairs(:, 1));
matchedPoints2 = currPoints(indexPairs(:, 2));
% Estimate the camera pose of current view relative to the previous view.
% The pose is computed up to scale, meaning that the distance between
% the cameras in the previous view and the current view is set to 1.
% This will be corrected by the bundle adjustment.
[relativeOrient, relativeLoc, inlierIdx] = helperEstimateRelativePose(...
matchedPoints1, matchedPoints2, cameraParams);
% Add the current view to the view set.
vSet = addView(vSet, i, 'Points', currPoints);
% Store the point matches between the previous and the current views.
vSet = addConnection(vSet, i-1, i, 'Matches', indexPairs(inlierIdx,:));
% Get the table containing the previous camera pose.
prevPose = poses(vSet, i-1);
prevOrientation = prevPose.Orientation{1};
prevLocation = prevPose.Location{1};
% Compute the current camera pose in the global coordinate system
% relative to the first view.
orientation = prevOrientation * relativeOrient;
location = prevLocation + relativeLoc * prevOrientation;
vSet = updateView(vSet, i, 'Orientation', orientation, ...
'Location', location);
% Find point tracks across all views.
tracks = findTracks(vSet);
% Get the table containing camera poses for all views.
camPoses = poses(vSet);
% Triangulate initial locations for the 3-D world points.
xyzPoints = triangulateMultiview(tracks, camPoses, cameraParams);
% Refine the 3-D world points and camera poses.
[xyzPoints, camPoses, reprojectionErrors] = bundleAdjustment(xyzPoints, ...
tracks, camPoses, cameraParams, 'FixedViewId', 1, ...
'PointsUndistorted', true);
% Store the refined camera poses.
vSet = updateView(vSet, camPoses);
prevFeatures = currFeatures;
prevPoints = currPoints;
end
%% Display camera poses
% Display camera poses.
camPoses = poses(vSet);
figure;
helperPlotCameras(camPoses);
% Exclude noisy 3-D points.
goodIdx = (reprojectionErrors < 5);
xyzPoints = xyzPoints(goodIdx, :);
% Display the 3-D points.
pcshow(xyzPoints, 'VerticalAxis', 'y', 'VerticalAxisDir', 'down', ...
'MarkerSize', 45);
grid on;
% Specify the viewing volume.
loc1 = camPoses.Location{1};
xlim([loc1(1)-5, loc1(1)+4]);
ylim([loc1(2)-5, loc1(2)+4]);
zlim([loc1(3)-1, loc1(3)+20]);
camorbit(0, -30);
title('Refined Camera Poses');
%% Compute Dense Reconstruction
% Read and undistort the first image
I = undistortImage(images{1}, cameraParams);
% Detect corners in the first image.
prevPoints = detectMinEigenFeatures(I, 'MinQuality', 0.001);
% Create the point tracker object to track the points across views.
tracker = vision.PointTracker('MaxBidirectionalError', 1, 'NumPyramidLevels', 6);
% Initialize the point tracker.
prevPoints = prevPoints.Location;
initialize(tracker, prevPoints, I);
% Store the dense points in the view set.
vSet = updateConnection(vSet, 1, 2, 'Matches', zeros(0, 2));
vSet = updateView(vSet, 1, 'Points', prevPoints);
% Track the points across all views.
for i = 2:numel(images)
% Read and undistort the current image.
I = undistortImage(images{i}, cameraParams);
% Track the points.
[currPoints, validIdx] = step(tracker, I);
% Clear the old matches between the points.
if i < numel(images)
vSet = updateConnection(vSet, i, i+1, 'Matches', zeros(0, 2));
end
vSet = updateView(vSet, i, 'Points', currPoints);
% Store the point matches in the view set.
matches = repmat((1:size(prevPoints, 1))', [1, 2]);
matches = matches(validIdx, :);
vSet = updateConnection(vSet, i-1, i, 'Matches', matches);
end
% Find point tracks across all views.
tracks = findTracks(vSet);
% Find point tracks across all views.
camPoses = poses(vSet);
% Triangulate initial locations for the 3-D world points.
xyzPoints = triangulateMultiview(tracks, camPoses,...
cameraParams);
% Refine the 3-D world points and camera poses.
[xyzPoints, camPoses, reprojectionErrors] = bundleAdjustment(...
xyzPoints, tracks, camPoses, cameraParams, 'FixedViewId', 1, ...
'PointsUndistorted', true);
%% Display Dense Reconstruction
% Display the refined camera poses.
figure;
helperPlotCameras(camPoses);
% Exclude noisy 3-D world points.
goodIdx = (reprojectionErrors < 5);
% Display the dense 3-D world points.
pcshow(xyzPoints(goodIdx, :), 'VerticalAxis', 'y', 'VerticalAxisDir', 'down', ...
'MarkerSize', 45);
grid on;
% Specify the viewing volume.
loc1 = camPoses.Location{1};
xlim([loc1(1)-5, loc1(1)+4]);
ylim([loc1(2)-5, loc1(2)+4]);
zlim([loc1(3)-1, loc1(3)+20]);
camorbit(0, -30);
title('Dense Reconstruction');