camera_utils.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. from scene.cameras import Camera
  12. import numpy as np
  13. from utils.general_utils import PILtoTorch
  14. from utils.graphics_utils import fov2focal
  15. WARNED = False
  16. def loadCam(args, id, cam_info, resolution_scale):
  17. orig_w, orig_h = cam_info.image.size
  18. if args.resolution in [1, 2, 4, 8]:
  19. resolution = round(orig_w/(resolution_scale * args.resolution)), round(orig_h/(resolution_scale * args.resolution))
  20. else: # should be a type that converts to float
  21. if args.resolution == -1:
  22. if orig_w > 1600:
  23. global WARNED
  24. if not WARNED:
  25. print("[ INFO ] Encountered quite large input images (>1.6K pixels width), rescaling to 1.6K.\n "
  26. "If this is not desired, please explicitly specify '--resolution/-r' as 1")
  27. WARNED = True
  28. global_down = orig_w / 1600
  29. else:
  30. global_down = 1
  31. else:
  32. global_down = orig_w / args.resolution
  33. scale = float(global_down) * float(resolution_scale)
  34. resolution = (int(orig_w / scale), int(orig_h / scale))
  35. resized_image_rgb = PILtoTorch(cam_info.image, resolution)
  36. gt_image = resized_image_rgb[:3, ...]
  37. loaded_mask = None
  38. if resized_image_rgb.shape[1] == 4:
  39. loaded_mask = resized_image_rgb[3:4, ...]
  40. return Camera(colmap_id=cam_info.uid, R=cam_info.R, T=cam_info.T,
  41. FoVx=cam_info.FovX, FoVy=cam_info.FovY,
  42. image=gt_image, gt_alpha_mask=loaded_mask,
  43. image_name=cam_info.image_name, uid=id, data_device=args.data_device)
  44. def cameraList_from_camInfos(cam_infos, resolution_scale, args):
  45. camera_list = []
  46. for id, c in enumerate(cam_infos):
  47. camera_list.append(loadCam(args, id, c, resolution_scale))
  48. return camera_list
  49. def camera_to_JSON(id, camera : Camera):
  50. Rt = np.zeros((4, 4))
  51. Rt[:3, :3] = camera.R.transpose()
  52. Rt[:3, 3] = camera.T
  53. Rt[3, 3] = 1.0
  54. W2C = np.linalg.inv(Rt)
  55. pos = W2C[:3, 3]
  56. rot = W2C[:3, :3]
  57. serializable_array_2d = [x.tolist() for x in rot]
  58. camera_entry = {
  59. 'id' : id,
  60. 'img_name' : camera.image_name,
  61. 'width' : camera.width,
  62. 'height' : camera.height,
  63. 'position': pos.tolist(),
  64. 'rotation': serializable_array_2d,
  65. 'fy' : fov2focal(camera.FovY, camera.height),
  66. 'fx' : fov2focal(camera.FovX, camera.width)
  67. }
  68. return camera_entry