-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImages.cs
More file actions
141 lines (114 loc) · 3.31 KB
/
Images.cs
File metadata and controls
141 lines (114 loc) · 3.31 KB
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
using System.IO;
using System.Text;
using System.Collections.Generic;
namespace brab.colmap
{
public class Point2D
{
public double x, y;
public long Point3DId;
override public string ToString()
{
return $"Point2D: {x}, {y} {Point3DId}";
}
}
public class Image
{
public int Id;
public string Name;
public int CameraId;
public double tx, ty, tz;
/* rotation quaternion */
public double qw, qx, qy, qz;
public Point2D[] Points2D;
override public string ToString()
{
return $"Image: Id {Id} qvec {qw} {qx} {qy} {qz} " +
$"tvec {tx} {ty} {tz} camID {CameraId} '{Name}'";
}
}
public interface Images
{
IEnumerable<Image> GetImages();
}
public class ImagesTxt : Images
{
TextFile TxtFile;
public ImagesTxt(string path)
{
TxtFile = new TextFile(path);
}
public IEnumerable<Image> GetImages()
{
var iter = TxtFile.Lines().GetEnumerator();
while (iter.MoveNext())
{
yield return ParseLine.ImagePose(iter.Current);
iter.MoveNext(); /* skip 'points' line for now */
}
}
}
public class ImagesBin : Images
{
string FilePath;
public ImagesBin(string path)
{
FilePath = path;
}
public IEnumerable<Image> GetImages()
{
using (BinaryReader reader = new BinaryReader(File.Open(FilePath, FileMode.Open)))
{
var numImages = reader.ReadUInt64();
for (ulong i = 0; i < numImages; i += 1)
{
yield return ReadImage(reader);
}
}
}
Image ReadImage(BinaryReader reader)
{
var img = new Image();
img.Id = reader.ReadInt32();
img.qw = reader.ReadDouble();
img.qx = reader.ReadDouble();
img.qy = reader.ReadDouble();
img.qz = reader.ReadDouble();
img.tx = reader.ReadDouble();
img.ty = reader.ReadDouble();
img.tz = reader.ReadDouble();
img.CameraId = reader.ReadInt32();
img.Name = ReadImageName(reader);
img.Points2D = ReadPoints2D(reader);
return img;
}
string ReadImageName(BinaryReader reader)
{
var name = new StringBuilder();
byte b;
while ((b = reader.ReadByte()) != 0)
{
name.Append((char)b, 1);
}
return name.ToString();
}
Point2D ReadPoint2D(BinaryReader reader)
{
var point = new Point2D();
point.x = reader.ReadDouble();
point.y = reader.ReadDouble();
point.Point3DId = reader.ReadInt64();
return point;
}
Point2D[] ReadPoints2D(BinaryReader reader)
{
var numPoints = reader.ReadUInt64();
var points = new Point2D[numPoints];
for (ulong i = 0; i < numPoints; i += 1)
{
points[i] = ReadPoint2D(reader);
}
return points;
}
}
}