cameras.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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
  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. self.original_image = image.clamp(0.0, 1.0).cuda()
  29. self.image_width = self.original_image.shape[2]
  30. self.image_height = self.original_image.shape[1]
  31. if gt_alpha_mask is not None:
  32. self.original_image *= gt_alpha_mask.cuda()
  33. else:
  34. self.original_image *= torch.ones((1, self.image_height, self.image_width), device="cuda")
  35. self.zfar = 100.0
  36. self.znear = 0.01
  37. self.trans = trans
  38. self.scale = scale
  39. self.world_view_transform = torch.tensor(getWorld2View2(R, T, trans, scale)).transpose(0, 1).cuda()
  40. self.projection_matrix = getProjectionMatrix(znear=self.znear, zfar=self.zfar, fovX=self.FoVx, fovY=self.FoVy).transpose(0,1).cuda()
  41. self.full_proj_transform = (self.world_view_transform.unsqueeze(0).bmm(self.projection_matrix.unsqueeze(0))).squeeze(0)
  42. self.camera_center = self.world_view_transform.inverse()[3, :3]
  43. class MiniCam:
  44. def __init__(self, width, height, fovy, fovx, znear, zfar, world_view_transform, full_proj_transform):
  45. self.image_width = width
  46. self.image_height = height
  47. self.FoVy = fovy
  48. self.FoVx = fovx
  49. self.znear = znear
  50. self.zfar = zfar
  51. self.world_view_transform = world_view_transform
  52. self.full_proj_transform = full_proj_transform
  53. view_inv = torch.inverse(self.world_view_transform)
  54. self.camera_center = view_inv[3][:3]