camera_utils.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from scene.cameras import Camera
  2. import numpy as np
  3. from utils.general_utils import PILtoTorch
  4. from utils.graphics_utils import fov2focal
  5. def loadCam(args, id, cam_info, resolution_scale):
  6. orig_w, orig_h = cam_info.image.size
  7. if args.resolution in [1, 2, 4, 8]:
  8. resolution = round(orig_w/(resolution_scale * args.resolution)), round(orig_h/(resolution_scale * args.resolution))
  9. else: # should be a type that converts to float
  10. global_down = orig_w/args.resolution
  11. scale = float(global_down) * float(resolution_scale)
  12. resolution = (int(orig_w / scale), int(orig_h / scale))
  13. resized_image_rgb = PILtoTorch(cam_info.image, resolution)
  14. gt_image = resized_image_rgb[:3, ...]
  15. loaded_mask = None
  16. if resized_image_rgb.shape[1] == 4:
  17. loaded_mask = resized_image_rgb[3:4, ...]
  18. return Camera(colmap_id=cam_info.uid, R=cam_info.R, T=cam_info.T,
  19. FoVx=cam_info.FovX, FoVy=cam_info.FovY,
  20. image=gt_image, gt_alpha_mask=loaded_mask,
  21. image_name=cam_info.image_name, uid=id)
  22. def cameraList_from_camInfos(cam_infos, resolution_scale, args):
  23. camera_list = []
  24. for id, c in enumerate(cam_infos):
  25. camera_list.append(loadCam(args, id, c, resolution_scale))
  26. return camera_list
  27. def camera_to_JSON(id, camera : Camera):
  28. Rt = np.zeros((4, 4))
  29. Rt[:3, :3] = camera.R.transpose()
  30. Rt[:3, 3] = camera.T
  31. Rt[3, 3] = 1.0
  32. W2C = np.linalg.inv(Rt)
  33. pos = W2C[:3, 3]
  34. rot = W2C[:3, :3]
  35. serializable_array_2d = [x.tolist() for x in rot]
  36. camera_entry = {
  37. 'id' : id,
  38. 'img_name' : camera.image_name,
  39. 'width' : camera.width,
  40. 'height' : camera.height,
  41. 'position': pos.tolist(),
  42. 'rotation': serializable_array_2d,
  43. 'fy' : fov2focal(camera.FovY, camera.height),
  44. 'fx' : fov2focal(camera.FovX, camera.width)
  45. }
  46. return camera_entry