rotate.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import numpy as np
  2. from scipy import ndimage as spimage
  3. from view.python_core.misc import class_mixer
  4. class BaseTransform(object):
  5. def __init__(self):
  6. super().__init__()
  7. def transpose(self, frame_data: np.ndarray):
  8. return frame_data
  9. def transform_frame_size(self, frame_size):
  10. return frame_size
  11. def flip_x(self, frame_data: np.ndarray):
  12. return frame_data
  13. def flip_y(self, frame_data: np.ndarray):
  14. return frame_data
  15. def transform(self, frame_data: np.ndarray):
  16. temp = self.transpose(frame_data)
  17. temp1 = self.flip_x(temp)
  18. temp2 = self.flip_y(temp1)
  19. return temp2
  20. class Transpose(BaseTransform):
  21. def __init__(self):
  22. super().__init__()
  23. def transpose(self, frame_data: np.ndarray):
  24. return frame_data.swapaxes(0, 1)
  25. def transform_frame_size(self, frame_size):
  26. return frame_size[1], frame_size[0]
  27. class MirrorX(BaseTransform):
  28. def __init__(self):
  29. super().__init__()
  30. def flip_x(self, frame_data: np.ndarray):
  31. return np.fliplr(frame_data)
  32. class MirrorY(BaseTransform):
  33. def __init__(self):
  34. super().__init__()
  35. def flip_y(self, frame_data: np.ndarray):
  36. return np.flipud(frame_data)
  37. def get_frame_rotator(rotate, reverse):
  38. classes2mix = []
  39. if rotate == 0:
  40. pass
  41. elif rotate == 1:
  42. classes2mix += [Transpose, MirrorX]
  43. elif rotate == 2:
  44. classes2mix += [MirrorY, MirrorX]
  45. elif rotate == 3:
  46. classes2mix += [Transpose, MirrorY]
  47. elif rotate == 4:
  48. classes2mix += [Transpose]
  49. elif rotate == 5:
  50. classes2mix += [MirrorY]
  51. elif rotate == 6:
  52. classes2mix += [Transpose, MirrorX, MirrorY]
  53. elif rotate == 7:
  54. classes2mix += [MirrorX]
  55. if reverse:
  56. if MirrorY in classes2mix:
  57. classes2mix.remove(MirrorY)
  58. else:
  59. classes2mix += [MirrorY]
  60. if not classes2mix:
  61. return BaseTransform()
  62. else:
  63. return class_mixer(*classes2mix)()
  64. # def rotate_IDL(frame_data, direction):
  65. # """
  66. # reimplementation of the function ROTATE of IDL (http://www.harrisgeospatial.com/docs/ROTATE.html)
  67. # :param image: numpy.ndarray
  68. # :param value: int in [0, 7]
  69. # :return: numpy.ndarray
  70. # """
  71. #
  72. # assert direction in range(8), f"Invalid value {direction} for direction"
  73. #
  74. # direction_transpose_enumerate = [False, False, False, False, True, True, True, True]
  75. # direction_rotation_enumerate = [0, 90, 180, 270, 0, 90, 180, 270]
  76. #
  77. # frame_data_output = frame_data.copy()
  78. # if direction_transpose_enumerate[direction]:
  79. #
  80. # frame_data_output = np.swapaxes(frame_data_output, 0, 1)
  81. #
  82. # frame_data_output = spimage.rotate(frame_data_output, -direction_rotation_enumerate[direction],
  83. # reshape=True)
  84. #
  85. # return frame_data_output
  86. #
  87. #
  88. # def get_frame_rotater(flags):
  89. #
  90. # def frame_rotater(frame_data):
  91. #
  92. # # apply rotation/transposition (see for interpretation of "mv_rotateImage"
  93. # # http://www.harrisgeospatial.com/docs/ROTATE.html)
  94. # rotated_frame_data = rotate_IDL(frame_data, flags["mv_rotateImage"])
  95. #
  96. # # flip the frame vertically if required.
  97. # if flags["mv_reverseIt"]:
  98. # rotated_flipped_frame_data = np.fliplr(rotated_frame_data)
  99. # else:
  100. # rotated_flipped_frame_data = rotated_frame_data
  101. #
  102. # return rotated_flipped_frame_data
  103. #
  104. # return frame_rotater