-
Notifications
You must be signed in to change notification settings - Fork 56
/
bundleAdjustment2D3DRobustFile.m
67 lines (51 loc) · 1.68 KB
/
bundleAdjustment2D3DRobustFile.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
function [cameraRtC2W,pointCloud] = bundleAdjustment2D3DRobustFile(cameraRtC2W,pointCloud,pointObserved, pointObservedValue, K, weight, mode)
[camID,ptsID,valID] = find(pointObserved);
nCam=uint32(size(cameraRtC2W,3));
nPts=uint32(size(pointCloud,2));
nObs=uint32(length(valID));
fx=K(1,1);
fy=K(2,2);
px=K(1,3);
py=K(2,3);
cameraRtW2C = zeros(3,4,size(cameraRtC2W,3));
% transform the cameraRt
for cameraID=1:size(cameraRtC2W,3)
cameraRtW2C(:,:,cameraID) = transformCameraRt(cameraRtC2W(:,:,cameraID));
end
fname_inout = tempname;
fname_in = [fname_inout '.in'];
fname_out = [fname_inout '.out'];
fin = fopen(fname_in, 'wb');
fwrite(fin, nCam, 'uint32');
fwrite(fin, nPts, 'uint32');
fwrite(fin, nObs, 'uint32');
fwrite(fin, fx, 'double');
fwrite(fin, fy, 'double');
fwrite(fin, px, 'double');
fwrite(fin, py, 'double');
fwrite(fin, cameraRtW2C, 'double');
fwrite(fin, pointCloud, 'double');
% write observation
ptsObservedIndex = uint32([camID,ptsID]-1)';
ptsObservedValue = pointObservedValue(:,valID);
fwrite(fin, ptsObservedIndex, 'uint32');
fwrite(fin, ptsObservedValue, 'double');
fclose(fin);
cmd = sprintf('./ba2D3D %d %f %s %s', mode, weight, fname_in, fname_out);
fprintf('%s\n',cmd);
system(cmd);
% read the result back;
fout = fopen(fname_out, 'rb');
nCam=fread(fout,1,'uint32');
nPts=fread(fout,1,'uint32');
cameraRtW2C = fread(fout,12*nCam,'double');
pointCloud = fread(fout,3*nPts,'double');
fclose(fout);
cameraRtW2C = reshape(cameraRtW2C,3,4,[]);
pointCloud = reshape(pointCloud,3,[]);
% transform the cameraRt back
for cameraID=1:size(cameraRtW2C,3)
cameraRtC2W(:,:,cameraID) = transformCameraRt(cameraRtW2C(:,:,cameraID));
end
delete(fname_in);
delete(fname_out);