-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathSfM2.m
66 lines (62 loc) · 1.67 KB
/
SfM2.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
function SfM2(one,two,visualize)
if nargin==3
visualize=true;
else
visualize=false;
end
[path,~,~]=fileparts(one);
[~,filename,~]=fileparts(path);
res_dir='result/';
untitle=0;
if strcmp(filename,'')
while true
filename=[res_dir,'untitled',untitle];
if exist(filename,'file')
untitle=untitle+1;
else
break;
end
end
end
f=fopen(fullfile(path,'intrinsic.new'));
data=textscan(f,'%f %f %f');
for i=1:3
col(:,i)=data{i};
end
img0 = imread(one);
img1 = imread(two);
ms=size(img0,2);
ds=1200;
if ms>ds
img0=imresize(img0,ds/ms);
img1=imresize(img1,ds/ms);
col=col*ds/ms;
end
intrinsic=col(1:3,:)';
%% match features
[mp1, mp2]=matchFeaturePoints(img0,img1,0.01);
% figure
% showMatchedFeatures(img0, img1, mp1, mp2);
%% Estimate F R t
[F, inliersIdx] = MSAC(mp1, mp2);
% Find epipolar inliers
inlierPoints1 = mp1(inliersIdx, :);
inlierPoints2 = mp2(inliersIdx, :);
% figure
% showMatchedFeatures(img0, img1, inlierPoints1, inlierPoints2);
[R, t] = motionFromF(F, intrinsic, inlierPoints1, inlierPoints2);
camMat0 = [eye(3); [0 0 0]]*intrinsic;
camMat1 = [R; -t*R]*intrinsic;
%% dense match
[mp1, mp2]=matchFeaturePoints(img0,img1,0.0001);
points3D = mytriangualation(mp1, mp2, camMat0, camMat1);
%% plot
cls = reshape(img0, [size(img0, 1) * size(img0, 2), 3]);
colorIdx = sub2ind([size(img0, 1), size(img0, 2)], round(mp1(:,2)),round(mp1(:, 1)));
ptCloud = pointCloud(points3D, 'Color', cls(colorIdx, :));
pcwrite(ptCloud,[res_dir,filename],'PLYFormat','ascii');
disp(['ply saved to ',res_dir,filename,'.ply']);
if visualize
figure
pcshow(ptCloud, 'VerticalAxis', 'y', 'VerticalAxisDir', 'down', 'MarkerSize', 45);
end