Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. """
  2. (*)~---------------------------------------------------------------------------
  3. Pupil - eye tracking platform
  4. Copyright (C) 2012-2019 Pupil Labs
  5.  
  6. Distributed under the terms of the GNU
  7. Lesser General Public License (LGPL v3.0).
  8. See COPYING and COPYING.LESSER for license details.
  9. ---------------------------------------------------------------------------~(*)
  10. """
  11.  
  12. import json
  13. import os
  14.  
  15. import cv2
  16. import numpy as np
  17.  
  18. export_dir = "/cluster/users/Ching/datasets/PI_extrinsic"
  19. path = os.path.join(export_dir, "matrix_diff_to_golden.json")
  20.  
  21.  
  22. class Transformation:
  23. def __init__(self):
  24. with open(path, "r") as file:
  25. data = json.load(file)
  26. self._matrix_diff_to_golden = {
  27. scene_sn: np.array(transformation_matrix)
  28. for scene_sn, transformation_matrix in data.items()
  29. }
  30.  
  31. def calculate_points_2d_undist_golden(self, points_3d_world, scene_sn):
  32. """
  33. This function calculate N undistorted points in golden headset coordinate
  34. based on the given frame and scene camera
  35.  
  36. :param points_3d_world: detected 3d gaze target points (unit: mm),
  37. shape: (N x 3)
  38. :param scene_sn: serial number of the scene camera
  39. :return: undistorted points in golden coordinate, shape: (N x 2)
  40.  
  41. Example:
  42. >>> points = [[0.0, 0.0, 1000.0], [-1000.0, 1000.0, 2000.0]]
  43. >>> calculate_points_2d_undist_golden(points, "krxdw")
  44. """
  45.  
  46. points_3d_world = np.array(points_3d_world, dtype=np.float64)
  47.  
  48. points_3d_world_golden = self._transform_points_3d_world_to_golden(
  49. points_3d_world, scene_sn
  50. )
  51.  
  52. points_2d_undist_golden = _normalize_points(points_3d_world_golden)
  53. return points_2d_undist_golden
  54.  
  55. def _transform_points_3d_world_to_golden(self, points_3d_world, scene_sn):
  56. transform_matrix = self._matrix_diff_to_golden[scene_sn]
  57. points_3d_world_golden = _transform_points(transform_matrix, points_3d_world)
  58. return points_3d_world_golden
  59.  
  60.  
  61. def _transform_points(transform_matrix, points_3d_cam1):
  62. """
  63. Transform 3d points from cam1 coordinate to cam2 coordinate
  64.  
  65. :param points_3d_cam1: 3d points in cam1 coordinate, shape: (N x 3)
  66. :param transform_matrix: transform_matrix of cam2 in cam1 coordinate,
  67. shape: (4, 4)
  68. :return: 3d points in cam2 coordinate, shape: (N x 3)
  69. """
  70.  
  71. points_3d_cam1.shape = -1, 3
  72. points_3d_cam1_h = cv2.convertPointsToHomogeneous(points_3d_cam1).reshape(-1, 4)
  73.  
  74. points_3d_cam2_h = np.matmul(transform_matrix, points_3d_cam1_h.T).T
  75. points_3d_cam2 = cv2.convertPointsFromHomogeneous(points_3d_cam2_h)
  76. points_3d_cam2.shape = -1, 3
  77. return points_3d_cam2
  78.  
  79.  
  80. def _inverse_extrinsic(extrinsic):
  81. rotation_ext, translation_ext = _split_extrinsic(extrinsic)
  82. rotation_inv = -rotation_ext
  83. translation_inv = np.matmul(
  84. -cv2.Rodrigues(rotation_ext.copy())[0].T, translation_ext
  85. )
  86. return _merge_extrinsic(rotation_inv, translation_inv)
  87.  
  88.  
  89. def _convert_extrinsic_to_matrix(extrinsic):
  90. rotation, translation = _split_extrinsic(extrinsic)
  91. extrinsic_matrix = np.eye(4, dtype=np.float64)
  92. extrinsic_matrix[0:3, 0:3] = cv2.Rodrigues(rotation.copy())[0]
  93. extrinsic_matrix[0:3, 3] = translation
  94. return extrinsic_matrix
  95.  
  96.  
  97. def _split_extrinsic(extrinsic):
  98. extrinsic = np.array(extrinsic.copy(), dtype=np.float64)
  99. assert extrinsic.size == 6
  100. rotation = extrinsic.ravel()[0:3]
  101. translation = extrinsic.ravel()[3:6]
  102. return rotation, translation
  103.  
  104.  
  105. def _merge_extrinsic(rotation, translation):
  106. assert rotation.size == 3 and translation.size == 3
  107. extrinsic = np.concatenate((rotation.ravel(), translation.ravel()))
  108. return extrinsic
  109.  
  110.  
  111. def _normalize_points(points_3d):
  112. return points_3d[:, :-1] / points_3d[:, -1][:, np.newaxis]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement