cameras.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 torch
  12. from torch import nn
  13. import numpy as np
  14. from utils.graphics_utils import getWorld2View2, getProjectionMatrix
  15. class Camera(nn.Module):
  16. def __init__(self, colmap_id, R, T, FoVx, FoVy, image, gt_alpha_mask,
  17. image_name, uid,
  18. trans=np.array([0.0, 0.0, 0.0]), scale=1.0, data_device = "cuda"
  19. ):
  20. super(Camera, self).__init__()
  21. self.uid = uid
  22. self.colmap_id = colmap_id
  23. self.R = R
  24. self.T = T
  25. self.FoVx = FoVx
  26. self.FoVy = FoVy
  27. self.image_name = image_name
  28. try:
  29. self.data_device = torch.device(data_device)
  30. except Exception as e:
  31. print(e)
  32. print(f"[Warning] Custom device {data_device} failed, fallback to default cuda device" )
  33. self.data_device = torch.device("cuda")
  34. self.original_image = image.clamp(0.0, 1.0).to(self.data_device)
  35. self.image_width = self.original_image.shape[2]
  36. self.image_height = self.original_image.shape[1]
  37. if gt_alpha_mask is not None:
  38. self.original_image *= gt_alpha_mask.to(self.data_device)
  39. else:
  40. self.original_image *= torch.ones((1, self.image_height, self.image_width), device=self.data_device)
  41. self.zfar = 100.0
  42. self.znear = 0.01
  43. self.trans = trans
  44. self.scale = scale
  45. self.world_view_transform = torch.tensor(getWorld2View2(R, T, trans, scale)).transpose(0, 1).cuda()
  46. self.projection_matrix = getProjectionMatrix(znear=self.znear, zfar=self.zfar, fovX=self.FoVx, fovY=self.FoVy).transpose(0,1).cuda()
  47. self.full_proj_transform = (self.world_view_transform.unsqueeze(0).bmm(self.projection_matrix.unsqueeze(0))).squeeze(0)
  48. self.camera_center = self.world_view_transform.inverse()[3, :3]
  49. class MiniCam:
  50. def __init__(self, width, height, fovy, fovx, znear, zfar, world_view_transform, full_proj_transform):
  51. self.image_width = width
  52. self.image_height = height
  53. self.FoVy = fovy
  54. self.FoVx = fovx
  55. self.znear = znear
  56. self.zfar = zfar
  57. self.world_view_transform = world_view_transform
  58. self.full_proj_transform = full_proj_transform
  59. view_inv = torch.inverse(self.world_view_transform)
  60. self.camera_center = view_inv[3][:3]