forked from Huang15/Team-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ceshi.m
139 lines (103 loc) · 3.84 KB
/
ceshi.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
imageDir = fullfile('/MATLAB Drive/dslr_images_undistorted');
imds = imageDatastore(imageDir);
images = cell(1, numel(imds.Files));
num_img =numel(imds.Files);
Points =cell(1, num_img);
Features=cell(1, num_img);
% indexPairs=cell(1, num_img);
for i = 1:num_img
I = readimage(imds, i);
I= im2gray(I);
Points{i}= detectSIFTFeatures(I);
[Features{i}, Points{i}]= extractFeatures(I, Points{i});
end
% max_indexPairs=cell(2,num_img);
% maxnum_indexPairs =0;
% for i =1:num_img
% for j=1:num_img
% if j==i
% continue
% end
% indexPairs = matchFeatures(Features{i}, Features{j},MaxRatio=0.7);
% if numel(indexPairs) > numel(max_indexPairs{1,i})
% max_indexPairs{1,i}=indexPairs;
% max_indexPairs{2,i}=[i,j];
% % max_indexPairs{3,i}=numel(indexPairs);
% end
% end
% if numel(max_indexPairs{1,i}) > maxnum_indexPairs
% maxnum_indexPairs=numel(max_indexPairs{1,i});
% first_img =i;
% end
% end
imageSize =[4135 6208];
focalLength =[3408.59 3408.87];
principalPoint=[3117.24 2064.07];
intrinsics = cameraIntrinsics(focalLength,principalPoint,imageSize);
vSet = imageviewset;
viewId = 1;
vSet = addView(vSet, viewId, rigidtform3d, Points=Points{1});
for i = 2:numel(images)
max_Pairs=0;
for j =1:i-1
indexPairs = matchFeatures(Features{i-j}, Features{i}, Method="Approximate",...
MaxRatio=0.6, Unique=true);
if numel(indexPairs) >max_Pairs
max_Pairs=numel(indexPairs);
k=j;
indexPairs_k=indexPairs;
end
% if size(indexPairs,1) >= 500
% break
% end
% matchedPoints1 = Points{i-j}(indexPairs(:, 1));
% matchedPoints2 = Points{i}(indexPairs(:, 2));
% try
%
% [relPose, inlierIdx] = helperEstimateRelativePose(...
% matchedPoints1, matchedPoints2, intrinsics);
% catch ME
% continue
% end
end
matchedPoints1 = Points{i-k}(indexPairs_k(:, 1));
matchedPoints2 = Points{i}(indexPairs_k(:, 2));
[E, inlierIdx] = estimateEssentialMatrix(matchedPoints1.Location, matchedPoints2.Location,...
intrinsics);
inlierPoints1 = matchedPoints1.Location(inlierIdx, :);
inlierPoints2 = matchedPoints2.Location(inlierIdx, :);
[relPose, validPointFraction] = ...
estrelpose(E, intrinsics, inlierPoints1(1:2:end, :),...
inlierPoints2(1:2:end, :));
% [relPose, inlierIdx] = helperEstimateRelativePose(...
% matchedPoints1, matchedPoints2, intrinsics);
prevPose = poses(vSet, i-k).AbsolutePose;
currPose = rigidtform3d(prevPose.A*relPose.A);
vSet = addView(vSet, i, currPose, Points=Points{i});
vSet = addConnection(vSet, i-k, i, relPose, Matches=indexPairs_k(inlierIdx,:));
tracks = findTracks(vSet);
camPoses = poses(vSet);
xyzPoints = triangulateMultiview(tracks, camPoses, intrinsics);
[xyzPoints, camPoses, reprojectionErrors] = bundleAdjustment(xyzPoints, ...
tracks, camPoses, intrinsics, FixedViewId=1, ...
PointsUndistorted=true);
vSet = updateView(vSet, camPoses);
end
% Display camera poses.
camPoses = poses(vSet);
figure;
plotCamera(camPoses, Size=0.2);
hold on
% 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
hold off
% Specify the viewing volume.
loc1 = camPoses.AbsolutePose(1).Translation;
xlim([loc1(1)-5, loc1(1)+4]);
ylim([loc1(2)-5, loc1(2)+4]);
zlim([loc1(3)-1, loc1(3)+20]);
camorbit(0, -30);