-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathReadFiles.jl
56 lines (46 loc) · 1.72 KB
/
ReadFiles.jl
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
# Extracting the datasets given at https://grail.cs.washington.edu/projects/bal/
"""
readfile(filename::String; T::Type=Float64)
Read the .txt.bzip2 file in Data/filename and extract the data,
returns the matrices observed 2D points, cameras and points
and the vectors of camera indices and points indices.
"""
function readfile(filename::String; T::Type = Float64)
f = Bzip2DecompressorStream(open(filename))
# We get the first line of the file
ncams, npnts, nobs = map(x -> parse(Int, x), split(readline(f)))
@debug "$filename: reading" ncams npnts nobs
cam_indices = Vector{Int}(undef, nobs)
pnt_indices = Vector{Int}(undef, nobs)
pt2d = Vector{T}(undef, 2 * nobs)
# x0 = [X₁, X₂, ..., Xnpnts, C₁, C₂, ..., Cncam]
x0 = Vector{T}(undef, 3 * npnts + 9 * ncams)
# read nobs lines of the form
for i = 1:nobs
# cam_index point_index xcoord ycoord
cam, pnt, x, y = split(readline(f))
cam_indices[i] = parse(Int, cam) + 1 # make indices start at 1
pnt_indices[i] = parse(Int, pnt) + 1
pt2d[2 * i - 1] = parse(T, x)
pt2d[2 * i] = parse(T, y)
end
# read 9 camera parameters, one per line, for each camera
for i = 1:ncams
for j = 1:7
if j == 7
# Cam_param = (rx, ry, rz, tx, ty, tz, k1, k2, f)
x0[3 * npnts + 9 * (i - 1) + 9] = parse(T, readline(f))
x0[3 * npnts + 9 * (i - 1) + 7] = parse(T, readline(f))
x0[3 * npnts + 9 * (i - 1) + 8] = parse(T, readline(f))
else
x0[3 * npnts + 9 * (i - 1) + j] = parse(T, readline(f))
end
end
end
# read npts 3d points, one coordinate per line
for k = 1:(3 * npnts)
x0[k] = parse(T, readline(f))
end
close(f)
return cam_indices, pnt_indices, pt2d, x0, ncams, npnts, nobs
end