-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.lua
423 lines (391 loc) · 14.6 KB
/
init.lua
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
require 'torch'
require 'xlua'
require 'dok'
require 'sys'
require 'image'
sfm = {}
-- c lib:
require 'libsfm'
sfm.cnp = 6 -- 3 rot params + 3 trans params
sfm.pnp = 3 -- euclidean 3D points
sfm.mnp = 2 -- x,y projection in image
-- takes an already created motstruct,vmask,initrot and imgproj
function sfm.sba_expert (...)
local _, motstruct, nframes, n3Dpts, vmask, initrot,
imgproj, calibration = xlua.unpack(
{...},
'sfm.sba_expert',
'Computes structure from motion using sba-1.6',
{arg='motstruct', type='torch.Tensor',
help='matrix of nFrames x 3 motion parameters and nPts x 3 structure parameters', req=true},
{arg="nframes", type='number',
help='number of frames in set', req=true},
{arg="n3Dpts", type='number',
help='number of 3D points in set', req=true},
{arg="vmask", type='torch.CharTensor',
help='nframes x n2Dpts', req=true},
{arg='initrot', type='torch.Tensor',
help='matrix of camera rotations ncams x 4', req=true},
{arg='imgproj', type='torch.Tensor',
help='projection of points back into frames n3Dpts x 2', req=true},
{arg='calibration', type='torch.Tensor',
help='matrix of intrinsic camera parameters'}
)
motstruct.libsfm.sba_driver(motstruct,nframes,n3Dpts,vmask,
initrot,imgproj, calibration)
end
function sfm.sba(...)
local _, projections, vmask, cameras, points3D, calibration =
xlua.unpack(
{...},
'sfm.sba',
'Computes structure from motion using sba-1.6',
{arg='projections', type='torch.Tensor',
help='projection of points back into frames n2Dpts x 2', req=true},
{arg="vmask", type='torch.CharTensor',
help='nframes x n2Dpts', req=true},
{arg='cameras', type='torch.Tensor',
help='matrix of camera rotations and translation ncams x DOF'},
{arg='points3D', type='torch.Tensor',
help='list of points npts x 3'},
{arg='calibration', type='torch.Tensor',
help='matrix of intrinsic camera parameters (3x3)'}
)
end
function sfm.project3D(point,calibration)
local point3D = torch.Tensor(3):fill(1)
-- xd = (xp - cc1)/fc1 -alpha_c*yd
-- yd = (yp - cc2)/fc2
-- assume alpha_c is zero for now
point3D[2] =
(point[2] - calibration[2][3] )/calibration[2][2]
point3D[1] =
(point[1] - calibration[1][3])/calibration[1][1]
return point3D
end
-- accepts points in a n3Dpoints x nframes x 2 (x,y) matrix, builds
-- the necessary data structures to pass this to sba returns the
-- motion, structure and calibration parameters computed
function sfm.sba_points (...)
local _,projections,calibration = dok.unpack (
{...},
'sfm.sba_points',
'creates all necessary structs for sparse bundle adjust, returns motion, structure(3D points) and calibration computed',
{arg='projections', type='torch.Tensor | table',
help='n3Dpoints x nframes x 2', req=true},
{arg='calibration', type='torch.Tensor',
help='calibration matrix for camera (3x3)'}
)
local cnp = 6
local mnp = 2
local pnp = 3
if not calibration then
calibration=torch.Tensor(3,3):fill(0)
-- make many assumptions
-- from: http://phototour.cs.washington.edu/focal.html
-- focal length in pixels =
-- (image width in pixels) *
-- (focal length in mm) / (CCD width in mm)
-- cnp = cnp + 7
local focalmm = 5.4
local CCDmm = 5.27
local width = 640
local height = 480
local focalpx = width * focalmm/CCDmm
calibration[1][1] = focalpx
calibration[2][2] = focalpx
calibration[3][3] = 1
calibration[1][3] = width/2
calibration[2][3] = height/2
end
local n3Dpts = 0
local nframes = 0
local n2Dproj = 0
if type(projections) == 'table' then
for i = 1,#projections do
n3Dpts = n3Dpts + projections[i]:size(1)
nframes = nframes + projections[i]:size(2)
n2Dproj = projections[i]:size(1) * projections[i]:size(2)
end
else
n3Dpts = projections:size(1)
nframes = projections:size(2)
n2Dproj = n3Dpts * nframes
end
local vmask = torch.CharTensor(n3Dpts,nframes):fill(0)
local motstruct = torch.Tensor(nframes*cnp + n3Dpts*pnp):fill(0)
local motion = motstruct:narrow(1,1,nframes*cnp):resize(nframes,cnp)
local structure = motstruct:narrow(1,nframes*cnp+1,n3Dpts*pnp):resize(n3Dpts,pnp)
local fullquatz = 4
local initrot = torch.Tensor(nframes,fullquatz):fill(0)
-- set initial rotation estimate to quaterion zero {1,0,0,0}
initrot:select(2,1):fill(1)
local imgprojs = torch.Tensor(n2Dproj,mnp)
local cproj = 1
-- FIXME make this work for tables also
for i = 1,n3Dpts do
-- approximate 3D based on projection using first point
local pt3d = sfm.project3D(projections[i][1],calibration)
structure[i]:copy(pt3d)
for j = 1,nframes do
imgprojs[j]:copy(projections[i][j])
vmask[i][j] = 1
end
end
print("calling sba_driver")
motstruct.libsfm.sba_driver(motstruct,nframes,n3Dpts,vmask,
initrot,imgprojs, calibration)
return motion,structure,calibration
end
-- opens a points file in the format of the sba demo files
function sfm.pointsfile_get_npts(pointsfname,points_df)
local must_close = false
if not points_df then
print("opening "..pointsfname)
points_df = torch.DiskFile(pointsfname)
must_close = true
end
points_df:seek(1)
local header = points_df:readString("*l")
local n3Dpts=sys.execute("wc -l "..pointsfname):match("%d+")
n3Dpts=n3Dpts-1
local n2Dprojs = 0
for i = 1,n3Dpts do
points_df:readDouble()
points_df:readDouble()
points_df:readDouble()
n2Dprojs = n2Dprojs + points_df:readInt()
-- read to end of line
points_df:readString("*l")
end
if must_close then
print("closing "..pointsfname)
points_df:close()
end
return n3Dpts,n2Dprojs
end
function sfm.pointsfile_get_points(points_df,
n3Dpts,pnp,
n2Dprojs,mnp,
nframes,cnp)
local vmask = torch.CharTensor(n3Dpts,nframes):fill(0)
local motstruct = torch.Tensor(nframes*cnp + n3Dpts*pnp)
local motion = motstruct:narrow(1,1,nframes*cnp)
motion:resize(nframes,cnp)
-- now reread the points file
points_df:seek(1)
points_df:readString("*l") -- skip header
local structure = motstruct:narrow(1,nframes*cnp+1,n3Dpts*pnp)
structure:resize(n3Dpts,pnp)
local imgprojs = torch.Tensor(n2Dprojs,mnp)
local cproj = 1
for i = 1,n3Dpts do
structure[i][1] = points_df:readDouble()
structure[i][2] = points_df:readDouble()
structure[i][3] = points_df:readDouble()
local nframes = points_df:readInt()
for j = 1,nframes do
local fnum = points_df:readInt()
imgprojs[cproj][1] = points_df:readDouble()
imgprojs[cproj][2] = points_df:readDouble()
vmask[i][fnum+1]=1
cproj = cproj+1
end
end
return motstruct,structure,imgprojs,vmask
end
function sfm.load_points_file(pointsfname,nframes,cnp,pnp,mnp)
print("opening "..pointsfname)
local points_df = torch.DiskFile(pointsfname)
local n3Dpts, n2Dprojs = sfm.pointsfile_get_npts(pointsfname,points_df)
local motstruct,structure,imgprojs,vmask =
sfm.pointsfile_get_points(points_df, n3Dpts, pnp,
n2Dprojs,mnp, nframes,cnp)
points_df:close()
return n3Dpts,n2Dprojs,motstruct,structure,imgprojs,vmask
end
function sfm.load_camera_file(camerafname,filecnp)
print("opening "..camerafname)
local nframes=tonumber(sys.execute("wc -l "..camerafname):match("%d+"))
if not filecnp then
filecnp = 7
end
local camera = torch.Tensor(nframes,filecnp)
local camera_df = torch.DiskFile(camerafname)
camera:copy(torch.Tensor(camera_df:readDouble(nframes*(filecnp))))
camera_df:close()
return camera,nframes
end
function sfm.load_calibration_file(calibfname)
local calibration=torch.Tensor(3,3)
local calib_df = torch.DiskFile(calibfname)
print("opening "..calibfname)
calibration:copy(torch.Tensor(calib_df:readDouble(9)))
calib_df:close()
return calibration
end
function sfm.sba_test_projection ()
-- test my projection function
local camerafname = sys.concat(sys.fpath(), "7cams.txt")
local pointsfname = sys.concat(sys.fpath(), "7pts.txt")
local calibfname = sys.concat(sys.fpath(), "calib.txt")
sfm.cnp = 6
sfm.pnp = 3
sfm.mnp = 2
local cnp = sfm.cnp
local pnp = sfm.pnp
local mnp = sfm.mnp
local calibration = sfm.load_calibration_file(calibfname)
local camera,nframes = sfm.load_camera_file(camerafname)
local n3Dpts,n2Dprojs,motstruct,structure,imgprojs,vmask =
sfm.load_points_file(pointsfname,nframes,cnp,pnp,mnp)
return calibration,structure,imgprojs,vmask
end
function sfm.sba_testme ()
-- similar to the expert case but the initrot and and vmask are
-- computed int the sfm.sba() function
local camerafname = sys.concat(sys.fpath(), "7cams.txt")
local pointsfname = sys.concat(sys.fpath(), "7pts.txt")
local calibfname = sys.concat(sys.fpath(), "calib.txt")
sfm.cnp = 6
sfm.pnp = 3
sfm.mnp = 2
local cnp = sfm.cnp
local pnp = sfm.pnp
local mnp = sfm.mnp
local filecnp = 7
local calibration = sfm.load_calibration_file(calibfname)
local camera,nframes = sfm.load_camera_file(camerafname)
local n3Dpts,n2Dprojs,motstruct,structure,imgprojs,vmask =
sfm.load_points_file(pointsfname,nframes,cnp,pnp,mnp)
local motion = motstruct:narrow(1,1,nframes*cnp)
motion:resize(nframes,cnp)
local fullquatz = 4
local initrot = torch.Tensor(nframes,fullquatz):fill(0)
-- go from quaternions in camera (7 params) to 3 rotation and 3
-- position (6 params) in motion (quat2vec)
for i = 1,nframes do
local mag = math.sqrt(camera[i][1] * camera[i][1] +
camera[i][2] * camera[i][2] +
camera[i][3] * camera[i][3] +
camera[i][4] * camera[i][4])
local sg = 1
if camera[i][1] < 0 then sg = -1 end
mag = sg/mag
motion[i][1] = camera[i][2]*mag
motion[i][2] = camera[i][3]*mag
motion[i][3] = camera[i][4]*mag
-- translation
motion[i][4] = camera[i][5]
motion[i][5] = camera[i][6]
motion[i][6] = camera[i][7]
end
-- it seems so f*cking broken to move back and forth between
-- quaterion mode and non-quaterion
for i = 1,nframes do
initrot[i][2] = motion[i][1]
initrot[i][3] = motion[i][2]
initrot[i][4] = motion[i][3]
initrot[i][1] = math.sqrt(1
- initrot[i][2]*initrot[i][2]
- initrot[i][3]*initrot[i][3]
- initrot[i][4]*initrot[i][4])
end
sfm.sba_expert(motstruct,nframes,n3Dpts,vmask,initrot,imgprojs,calibration)
end
function sfm.sba_testme_expert ()
local camerafname = sys.concat(sys.fpath(), "7cams.txt")
local pointsfname = sys.concat(sys.fpath(), "7pts.txt")
local calibfname = sys.concat(sys.fpath(), "calib.txt")
print("opening "..camerafname)
local nframes=tonumber(sys.execute("wc -l "..camerafname):match("%d+"))
sfm.cnp = 6
sfm.pnp = 3
sfm.mnp = 2
local cnp = sfm.cnp
local pnp = sfm.pnp
local mnp = sfm.mnp
local filecnp = 7
local camera = torch.Tensor(nframes,filecnp)
local camera_df = torch.DiskFile(camerafname)
camera:copy(torch.Tensor(camera_df:readDouble(nframes*(filecnp))))
camera_df:close()
print("opening "..pointsfname)
local n3Dpts=sys.execute("wc -l "..pointsfname):match("%d+")
n3Dpts=n3Dpts-1
local n2Dprojs = 0
local points_df = torch.DiskFile(pointsfname)
local header = points_df:readString("*l")
for i = 1,n3Dpts do
points_df:readDouble()
points_df:readDouble()
points_df:readDouble()
n2Dprojs = n2Dprojs + points_df:readInt()
-- read to end of line
points_df:readString("*l")
end
local vmask = torch.CharTensor(n3Dpts,nframes):fill(0)
local imgpts = torch.Tensor(n2Dprojs * mnp)
local motstruct = torch.Tensor(nframes*cnp + n3Dpts*pnp)
local motion = motstruct:narrow(1,1,nframes*cnp)
motion:resize(nframes,cnp)
local fullquatz = 4
local initrot = torch.Tensor(nframes,fullquatz):fill(0)
-- go from quaternions in camera (7 params) to 3 rotation and 3
-- position (6 params) in motion (quat2vec)
-- it seems so f*cking broken to move back and forth between
-- quaterion mode and non-quaterion
for i = 1,nframes do
local mag = math.sqrt(camera[i][1] * camera[i][1] +
camera[i][2] * camera[i][2] +
camera[i][3] * camera[i][3] +
camera[i][4] * camera[i][4])
local sg = 1
if camera[i][1] < 0 then sg = -1 end
mag = sg/mag
motion[i][1] = camera[i][2]*mag
motion[i][2] = camera[i][3]*mag
motion[i][3] = camera[i][4]*mag
-- translation
motion[i][4] = camera[i][5]
motion[i][5] = camera[i][6]
motion[i][6] = camera[i][7]
end
for i = 1,nframes do
-- in quaternion mode the rotation parameters start in 2nd column
initrot[i][2] = motion[i][1]
initrot[i][3] = motion[i][2]
initrot[i][4] = motion[i][3]
initrot[i][1] = math.sqrt(1
- initrot[i][2]*initrot[i][2]
- initrot[i][3]*initrot[i][3]
- initrot[i][4]*initrot[i][4])
end
-- now reread the points file
points_df:seek(1)
points_df:readString("*l") -- skip header
local structure = motstruct:narrow(1,nframes*cnp+1,n3Dpts*pnp)
structure:resize(n3Dpts,pnp)
local imgprojs = torch.Tensor(n2Dprojs,mnp)
local cproj = 1
for i = 1,n3Dpts do
structure[i][1] = points_df:readDouble()
structure[i][2] = points_df:readDouble()
structure[i][3] = points_df:readDouble()
local nframes = points_df:readInt()
for j = 1,nframes do
local fnum = points_df:readInt()
imgprojs[cproj][1] = points_df:readDouble()
imgprojs[cproj][2] = points_df:readDouble()
vmask[i][fnum+1]=1
cproj = cproj+1
end
end
points_df:close()
local calibration=torch.Tensor(3,3)
local calib_df = torch.DiskFile(calibfname)
print("opening "..calibfname)
calibration:copy(torch.Tensor(calib_df:readDouble(9)))
calib_df:close()
sfm.sba_expert(motstruct,nframes,n3Dpts,vmask,initrot,imgprojs,calibration)
end