colmap_loader.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #
  2. # Copyright (C) 2023, Inria
  3. # GRAPHDECO research group, https://team.inria.fr/graphdeco
  4. # All rights reserved.
  5. #
  6. # This software is free for non-commercial, research and evaluation use
  7. # under the terms of the LICENSE.md file.
  8. #
  9. # For inquiries contact george.drettakis@inria.fr
  10. #
  11. import numpy as np
  12. import collections
  13. import struct
  14. CameraModel = collections.namedtuple(
  15. "CameraModel", ["model_id", "model_name", "num_params"])
  16. Camera = collections.namedtuple(
  17. "Camera", ["id", "model", "width", "height", "params"])
  18. BaseImage = collections.namedtuple(
  19. "Image", ["id", "qvec", "tvec", "camera_id", "name", "xys", "point3D_ids"])
  20. Point3D = collections.namedtuple(
  21. "Point3D", ["id", "xyz", "rgb", "error", "image_ids", "point2D_idxs"])
  22. CAMERA_MODELS = {
  23. CameraModel(model_id=0, model_name="SIMPLE_PINHOLE", num_params=3),
  24. CameraModel(model_id=1, model_name="PINHOLE", num_params=4),
  25. CameraModel(model_id=2, model_name="SIMPLE_RADIAL", num_params=4),
  26. CameraModel(model_id=3, model_name="RADIAL", num_params=5),
  27. CameraModel(model_id=4, model_name="OPENCV", num_params=8),
  28. CameraModel(model_id=5, model_name="OPENCV_FISHEYE", num_params=8),
  29. CameraModel(model_id=6, model_name="FULL_OPENCV", num_params=12),
  30. CameraModel(model_id=7, model_name="FOV", num_params=5),
  31. CameraModel(model_id=8, model_name="SIMPLE_RADIAL_FISHEYE", num_params=4),
  32. CameraModel(model_id=9, model_name="RADIAL_FISHEYE", num_params=5),
  33. CameraModel(model_id=10, model_name="THIN_PRISM_FISHEYE", num_params=12)
  34. }
  35. CAMERA_MODEL_IDS = dict([(camera_model.model_id, camera_model)
  36. for camera_model in CAMERA_MODELS])
  37. CAMERA_MODEL_NAMES = dict([(camera_model.model_name, camera_model)
  38. for camera_model in CAMERA_MODELS])
  39. def qvec2rotmat(qvec):
  40. return np.array([
  41. [1 - 2 * qvec[2]**2 - 2 * qvec[3]**2,
  42. 2 * qvec[1] * qvec[2] - 2 * qvec[0] * qvec[3],
  43. 2 * qvec[3] * qvec[1] + 2 * qvec[0] * qvec[2]],
  44. [2 * qvec[1] * qvec[2] + 2 * qvec[0] * qvec[3],
  45. 1 - 2 * qvec[1]**2 - 2 * qvec[3]**2,
  46. 2 * qvec[2] * qvec[3] - 2 * qvec[0] * qvec[1]],
  47. [2 * qvec[3] * qvec[1] - 2 * qvec[0] * qvec[2],
  48. 2 * qvec[2] * qvec[3] + 2 * qvec[0] * qvec[1],
  49. 1 - 2 * qvec[1]**2 - 2 * qvec[2]**2]])
  50. def rotmat2qvec(R):
  51. Rxx, Ryx, Rzx, Rxy, Ryy, Rzy, Rxz, Ryz, Rzz = R.flat
  52. K = np.array([
  53. [Rxx - Ryy - Rzz, 0, 0, 0],
  54. [Ryx + Rxy, Ryy - Rxx - Rzz, 0, 0],
  55. [Rzx + Rxz, Rzy + Ryz, Rzz - Rxx - Ryy, 0],
  56. [Ryz - Rzy, Rzx - Rxz, Rxy - Ryx, Rxx + Ryy + Rzz]]) / 3.0
  57. eigvals, eigvecs = np.linalg.eigh(K)
  58. qvec = eigvecs[[3, 0, 1, 2], np.argmax(eigvals)]
  59. if qvec[0] < 0:
  60. qvec *= -1
  61. return qvec
  62. class Image(BaseImage):
  63. def qvec2rotmat(self):
  64. return qvec2rotmat(self.qvec)
  65. def read_next_bytes(fid, num_bytes, format_char_sequence, endian_character="<"):
  66. """Read and unpack the next bytes from a binary file.
  67. :param fid:
  68. :param num_bytes: Sum of combination of {2, 4, 8}, e.g. 2, 6, 16, 30, etc.
  69. :param format_char_sequence: List of {c, e, f, d, h, H, i, I, l, L, q, Q}.
  70. :param endian_character: Any of {@, =, <, >, !}
  71. :return: Tuple of read and unpacked values.
  72. """
  73. data = fid.read(num_bytes)
  74. return struct.unpack(endian_character + format_char_sequence, data)
  75. def read_points3D_text(path):
  76. """
  77. see: src/base/reconstruction.cc
  78. void Reconstruction::ReadPoints3DText(const std::string& path)
  79. void Reconstruction::WritePoints3DText(const std::string& path)
  80. """
  81. xyzs = None
  82. rgbs = None
  83. errors = None
  84. num_points = 0
  85. with open(path, "r") as fid:
  86. while True:
  87. line = fid.readline()
  88. if not line:
  89. break
  90. line = line.strip()
  91. if len(line) > 0 and line[0] != "#":
  92. num_points += 1
  93. xyzs = np.empty((num_points, 3))
  94. rgbs = np.empty((num_points, 3))
  95. errors = np.empty((num_points, 1))
  96. count = 0
  97. with open(path, "r") as fid:
  98. while True:
  99. line = fid.readline()
  100. if not line:
  101. break
  102. line = line.strip()
  103. if len(line) > 0 and line[0] != "#":
  104. elems = line.split()
  105. xyz = np.array(tuple(map(float, elems[1:4])))
  106. rgb = np.array(tuple(map(int, elems[4:7])))
  107. error = np.array(float(elems[7]))
  108. xyzs[count] = xyz
  109. rgbs[count] = rgb
  110. errors[count] = error
  111. count += 1
  112. return xyzs, rgbs, errors
  113. def read_points3D_binary(path_to_model_file):
  114. """
  115. see: src/base/reconstruction.cc
  116. void Reconstruction::ReadPoints3DBinary(const std::string& path)
  117. void Reconstruction::WritePoints3DBinary(const std::string& path)
  118. """
  119. with open(path_to_model_file, "rb") as fid:
  120. num_points = read_next_bytes(fid, 8, "Q")[0]
  121. xyzs = np.empty((num_points, 3))
  122. rgbs = np.empty((num_points, 3))
  123. errors = np.empty((num_points, 1))
  124. for p_id in range(num_points):
  125. binary_point_line_properties = read_next_bytes(
  126. fid, num_bytes=43, format_char_sequence="QdddBBBd")
  127. xyz = np.array(binary_point_line_properties[1:4])
  128. rgb = np.array(binary_point_line_properties[4:7])
  129. error = np.array(binary_point_line_properties[7])
  130. track_length = read_next_bytes(
  131. fid, num_bytes=8, format_char_sequence="Q")[0]
  132. track_elems = read_next_bytes(
  133. fid, num_bytes=8*track_length,
  134. format_char_sequence="ii"*track_length)
  135. xyzs[p_id] = xyz
  136. rgbs[p_id] = rgb
  137. errors[p_id] = error
  138. return xyzs, rgbs, errors
  139. def read_intrinsics_text(path):
  140. """
  141. Taken from https://github.com/colmap/colmap/blob/dev/scripts/python/read_write_model.py
  142. """
  143. cameras = {}
  144. with open(path, "r") as fid:
  145. while True:
  146. line = fid.readline()
  147. if not line:
  148. break
  149. line = line.strip()
  150. if len(line) > 0 and line[0] != "#":
  151. elems = line.split()
  152. camera_id = int(elems[0])
  153. model = elems[1]
  154. assert model == "PINHOLE", "While the loader support other types, the rest of the code assumes PINHOLE"
  155. width = int(elems[2])
  156. height = int(elems[3])
  157. params = np.array(tuple(map(float, elems[4:])))
  158. cameras[camera_id] = Camera(id=camera_id, model=model,
  159. width=width, height=height,
  160. params=params)
  161. return cameras
  162. def read_extrinsics_binary(path_to_model_file):
  163. """
  164. see: src/base/reconstruction.cc
  165. void Reconstruction::ReadImagesBinary(const std::string& path)
  166. void Reconstruction::WriteImagesBinary(const std::string& path)
  167. """
  168. images = {}
  169. with open(path_to_model_file, "rb") as fid:
  170. num_reg_images = read_next_bytes(fid, 8, "Q")[0]
  171. for _ in range(num_reg_images):
  172. binary_image_properties = read_next_bytes(
  173. fid, num_bytes=64, format_char_sequence="idddddddi")
  174. image_id = binary_image_properties[0]
  175. qvec = np.array(binary_image_properties[1:5])
  176. tvec = np.array(binary_image_properties[5:8])
  177. camera_id = binary_image_properties[8]
  178. image_name = ""
  179. current_char = read_next_bytes(fid, 1, "c")[0]
  180. while current_char != b"\x00": # look for the ASCII 0 entry
  181. image_name += current_char.decode("utf-8")
  182. current_char = read_next_bytes(fid, 1, "c")[0]
  183. num_points2D = read_next_bytes(fid, num_bytes=8,
  184. format_char_sequence="Q")[0]
  185. x_y_id_s = read_next_bytes(fid, num_bytes=24*num_points2D,
  186. format_char_sequence="ddq"*num_points2D)
  187. xys = np.column_stack([tuple(map(float, x_y_id_s[0::3])),
  188. tuple(map(float, x_y_id_s[1::3]))])
  189. point3D_ids = np.array(tuple(map(int, x_y_id_s[2::3])))
  190. images[image_id] = Image(
  191. id=image_id, qvec=qvec, tvec=tvec,
  192. camera_id=camera_id, name=image_name,
  193. xys=xys, point3D_ids=point3D_ids)
  194. return images
  195. def read_intrinsics_binary(path_to_model_file):
  196. """
  197. see: src/base/reconstruction.cc
  198. void Reconstruction::WriteCamerasBinary(const std::string& path)
  199. void Reconstruction::ReadCamerasBinary(const std::string& path)
  200. """
  201. cameras = {}
  202. with open(path_to_model_file, "rb") as fid:
  203. num_cameras = read_next_bytes(fid, 8, "Q")[0]
  204. for _ in range(num_cameras):
  205. camera_properties = read_next_bytes(
  206. fid, num_bytes=24, format_char_sequence="iiQQ")
  207. camera_id = camera_properties[0]
  208. model_id = camera_properties[1]
  209. model_name = CAMERA_MODEL_IDS[camera_properties[1]].model_name
  210. width = camera_properties[2]
  211. height = camera_properties[3]
  212. num_params = CAMERA_MODEL_IDS[model_id].num_params
  213. params = read_next_bytes(fid, num_bytes=8*num_params,
  214. format_char_sequence="d"*num_params)
  215. cameras[camera_id] = Camera(id=camera_id,
  216. model=model_name,
  217. width=width,
  218. height=height,
  219. params=np.array(params))
  220. assert len(cameras) == num_cameras
  221. return cameras
  222. def read_extrinsics_text(path):
  223. """
  224. Taken from https://github.com/colmap/colmap/blob/dev/scripts/python/read_write_model.py
  225. """
  226. images = {}
  227. with open(path, "r") as fid:
  228. while True:
  229. line = fid.readline()
  230. if not line:
  231. break
  232. line = line.strip()
  233. if len(line) > 0 and line[0] != "#":
  234. elems = line.split()
  235. image_id = int(elems[0])
  236. qvec = np.array(tuple(map(float, elems[1:5])))
  237. tvec = np.array(tuple(map(float, elems[5:8])))
  238. camera_id = int(elems[8])
  239. image_name = elems[9]
  240. elems = fid.readline().split()
  241. xys = np.column_stack([tuple(map(float, elems[0::3])),
  242. tuple(map(float, elems[1::3]))])
  243. point3D_ids = np.array(tuple(map(int, elems[2::3])))
  244. images[image_id] = Image(
  245. id=image_id, qvec=qvec, tvec=tvec,
  246. camera_id=camera_id, name=image_name,
  247. xys=xys, point3D_ids=point3D_ids)
  248. return images
  249. def read_colmap_bin_array(path):
  250. """
  251. Taken from https://github.com/colmap/colmap/blob/dev/scripts/python/read_dense.py
  252. :param path: path to the colmap binary file.
  253. :return: nd array with the floating point values in the value
  254. """
  255. with open(path, "rb") as fid:
  256. width, height, channels = np.genfromtxt(fid, delimiter="&", max_rows=1,
  257. usecols=(0, 1, 2), dtype=int)
  258. fid.seek(0)
  259. num_delimiter = 0
  260. byte = fid.read(1)
  261. while True:
  262. if byte == b"&":
  263. num_delimiter += 1
  264. if num_delimiter >= 3:
  265. break
  266. byte = fid.read(1)
  267. array = np.fromfile(fid, np.float32)
  268. array = array.reshape((width, height, channels), order="F")
  269. return np.transpose(array, (1, 0, 2)).squeeze()