camera_utils.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. def loadCam(args, id, cam_info, resolution_scale):
  16. orig_w, orig_h = cam_info.image.size
  17. if args.resolution in [1, 2, 4, 8]:
  18. resolution = round(orig_w/(resolution_scale * args.resolution)), round(orig_h/(resolution_scale * args.resolution))
  19. else: # should be a type that converts to float
  20. global_down = orig_w/args.resolution
  21. scale = float(global_down) * float(resolution_scale)
  22. resolution = (int(orig_w / scale), int(orig_h / scale))
  23. resized_image_rgb = PILtoTorch(cam_info.image, resolution)
  24. gt_image = resized_image_rgb[:3, ...]
  25. loaded_mask = None
  26. if resized_image_rgb.shape[1] == 4:
  27. loaded_mask = resized_image_rgb[3:4, ...]
  28. return Camera(colmap_id=cam_info.uid, R=cam_info.R, T=cam_info.T,
  29. FoVx=cam_info.FovX, FoVy=cam_info.FovY,
  30. image=gt_image, gt_alpha_mask=loaded_mask,
  31. image_name=cam_info.image_name, uid=id)
  32. def cameraList_from_camInfos(cam_infos, resolution_scale, args):
  33. camera_list = []
  34. for id, c in enumerate(cam_infos):
  35. camera_list.append(loadCam(args, id, c, resolution_scale))
  36. return camera_list
  37. def camera_to_JSON(id, camera : Camera):
  38. Rt = np.zeros((4, 4))
  39. Rt[:3, :3] = camera.R.transpose()
  40. Rt[:3, 3] = camera.T
  41. Rt[3, 3] = 1.0
  42. W2C = np.linalg.inv(Rt)
  43. pos = W2C[:3, 3]
  44. rot = W2C[:3, :3]
  45. serializable_array_2d = [x.tolist() for x in rot]
  46. camera_entry = {
  47. 'id' : id,
  48. 'img_name' : camera.image_name,
  49. 'width' : camera.width,
  50. 'height' : camera.height,
  51. 'position': pos.tolist(),
  52. 'rotation': serializable_array_2d,
  53. 'fy' : fov2focal(camera.FovY, camera.height),
  54. 'fx' : fov2focal(camera.FovX, camera.width)
  55. }
  56. return camera_entry