camera_spherical.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import numpy as np
  2. import logging
  3. log = logging.getLogger(__name__)
  4. class Intrinsic_Spherical:
  5. def __init__(self, width, height):
  6. if (height * 2) != width:
  7. log.error("the ERP image width {} is not two time of height {}".format(width, height))
  8. self.width = width
  9. self.height = height
  10. def ima2cam(self, ima_points):
  11. ima_points_x = ima_points[0]
  12. ima_points_y = ima_points[1]
  13. # 1) point location to uv coordinates
  14. cam_points_x = (ima_points_x - self.width / 2.0) / self.width
  15. cam_points_y = (ima_points_y - self.height / 2.0) / self.width
  16. return [cam_points_x, cam_points_y]
  17. def bearing(self, ima_points):
  18. cam_points = self.ima2cam(ima_points)
  19. lon = cam_points[0] * 2.0 * np.pi
  20. lat = -cam_points[1] * 2.0 * np.pi
  21. lon = torch.from_numpy(lon)
  22. lat = torch.from_numpy(lat)
  23. bearing_x = torch.cos(lat) * torch.sin(lon)
  24. bearing_y = torch.cos(lat) * torch.cos(lon)
  25. bearing_z = torch.sin(lat)
  26. bearing_x = bearing_x.to(torch.float64)
  27. bearing_y = bearing_y.to(torch.float64)
  28. bearing_z = bearing_z.to(torch.float64)
  29. return [bearing_x, bearing_y, bearing_z]
  30. def cam2ima(self, cam_points):
  31. cam_points_x = cam_points[0]
  32. cam_points_y = cam_points[1]
  33. ima_points_x = cam_points_x * self.width + self.width / 2.0
  34. ima_points_y = cam_points_y * self.width + self.height / 2.0
  35. return [ima_points_x, ima_points_y]
  36. def project(self, cam_3dpoints):
  37. cam_3dpoints_x = cam_3dpoints[0]
  38. cam_3dpoints_y = cam_3dpoints[1]
  39. cam_3dpoints_z = cam_3dpoints[2]
  40. lon = torch.atan2(cam_3dpoints_x, cam_3dpoints_y)
  41. lat = torch.atan2(cam_3dpoints_z, torch.hypot(cam_3dpoints_x, cam_3dpoints_y))
  42. cam_2dpoints_x = lon / (2.0 * np.pi)
  43. cam_2dpoints_y = -lat / (2.0 * np.pi)
  44. return self.cam2ima([cam_2dpoints_x, cam_2dpoints_y])
  45. class Intrinsic_Spherical_NP:
  46. def __init__(self, width, height):
  47. if (height * 2) != width:
  48. log.error("the ERP image width {} is not two time of height {}".format(width, height))
  49. self.width = width
  50. self.height = height
  51. def ima2cam(self, ima_points):
  52. ima_points_x = ima_points[0]
  53. ima_points_y = ima_points[1]
  54. # 1) point location to uv coordinates
  55. cam_points_x = (ima_points_x - self.width / 2.0) / self.width
  56. cam_points_y = (ima_points_y - self.height / 2.0) / self.width
  57. return [cam_points_x, cam_points_y]
  58. def bearing(self, ima_points):
  59. cam_points = self.ima2cam(ima_points)
  60. lon = cam_points[0] * 2.0 * np.pi
  61. lat = -cam_points[1] * 2.0 * np.pi
  62. bearing_x = np.cos(lat) * np.sin(lon)
  63. bearing_y = np.cos(lat) * np.cos(lon)
  64. bearing_z = np.sin(lat)
  65. return [bearing_x, bearing_y, bearing_z]
  66. def cam2ima(self, cam_points):
  67. cam_points_x = cam_points[0]
  68. cam_points_y = cam_points[1]
  69. ima_points_x = cam_points_x * self.width + self.width / 2.0
  70. ima_points_y = cam_points_y * self.width + self.height / 2.0
  71. return [ima_points_x, ima_points_y]
  72. def project(self, cam_3dpoints):
  73. cam_3dpoints_x = cam_3dpoints[0]
  74. cam_3dpoints_y = cam_3dpoints[1]
  75. cam_3dpoints_z = cam_3dpoints[2]
  76. lon = np.arctan2(cam_3dpoints_x, cam_3dpoints_y)
  77. lat = np.arctan2(cam_3dpoints_z, np.hypot(cam_3dpoints_x, cam_3dpoints_y))
  78. cam_2dpoints_x = lon / (2.0 * np.pi)
  79. cam_2dpoints_y = -lat / (2.0 * np.pi)
  80. return self.cam2ima([cam_2dpoints_x, cam_2dpoints_y])