boxes_transform_flip.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import numpy as np
  2. from .boxes_utils import assert_and_normalize_shape
  3. def flip_boxes(boxes, x_center=0, y_center=0, direction='h'):
  4. """
  5. Args:
  6. boxes: (N, 4+K)
  7. x_center: array-like whose shape is (), (1,), (N,), (1, 1) or (N, 1)
  8. y_center: array-like whose shape is (), (1,), (N,), (1, 1) or (N, 1)
  9. direction: str
  10. """
  11. assert direction in ['x', 'h', 'horizontal',
  12. 'y', 'v', 'vertical',
  13. 'o', 'b', 'both']
  14. boxes = np.asarray(boxes, np.float32)
  15. ret_boxes = boxes.copy()
  16. x_center = np.asarray(x_center, np.float32)
  17. y_center = np.asarray(y_center, np.float32)
  18. x_center = assert_and_normalize_shape(x_center, boxes.shape[0])
  19. y_center = assert_and_normalize_shape(y_center, boxes.shape[0])
  20. if direction in ['o', 'b', 'both', 'x', 'h', 'horizontal']:
  21. ret_boxes[:, 0] = 2 * x_center - boxes[:, 2]
  22. ret_boxes[:, 2] = 2 * x_center - boxes[:, 0]
  23. if direction in ['o', 'b', 'both', 'y', 'v', 'vertical']:
  24. ret_boxes[:, 1] = 2 * y_center - boxes[:, 3]
  25. ret_boxes[:, 3] = 2 * y_center - boxes[:, 1]
  26. return ret_boxes
  27. def fliplr_boxes(boxes, x_center=0, y_center=0):
  28. """
  29. Args:
  30. boxes: (N, 4+K)
  31. x_center: array-like whose shape is (), (1,), (N,), (1, 1) or (N, 1)
  32. y_center: array-like whose shape is (), (1,), (N,), (1, 1) or (N, 1)
  33. """
  34. boxes = np.asarray(boxes, np.float32)
  35. ret_boxes = boxes.copy()
  36. x_center = np.asarray(x_center, np.float32)
  37. y_center = np.asarray(y_center, np.float32)
  38. x_center = assert_and_normalize_shape(x_center, boxes.shape[0])
  39. y_center = assert_and_normalize_shape(y_center, boxes.shape[0])
  40. ret_boxes[:, 0] = 2 * x_center - boxes[:, 2]
  41. ret_boxes[:, 2] = 2 * x_center - boxes[:, 0]
  42. return ret_boxes
  43. def flipud_boxes(boxes, x_center=0, y_center=0):
  44. """
  45. Args:
  46. boxes: (N, 4+K)
  47. x_center: array-like whose shape is (), (1,), (N,), (1, 1) or (N, 1)
  48. y_center: array-like whose shape is (), (1,), (N,), (1, 1) or (N, 1)
  49. """
  50. boxes = np.asarray(boxes, np.float32)
  51. ret_boxes = boxes.copy()
  52. x_center = np.asarray(x_center, np.float32)
  53. y_center = np.asarray(y_center, np.float32)
  54. x_center = assert_and_normalize_shape(x_center, boxes.shape[0])
  55. y_center = assert_and_normalize_shape(y_center, boxes.shape[0])
  56. ret_boxes[:, 1] = 2 * y_center - boxes[:, 3]
  57. ret_boxes[:, 3] = 2 * y_center - boxes[:, 1]
  58. return ret_boxes
  59. def transpose_boxes(boxes, x_center=0, y_center=0):
  60. """
  61. Args:
  62. boxes: (N, 4+K)
  63. x_center: array-like whose shape is (), (1,), (N,), (1, 1) or (N, 1)
  64. y_center: array-like whose shape is (), (1,), (N,), (1, 1) or (N, 1)
  65. """
  66. boxes = np.asarray(boxes, np.float32)
  67. ret_boxes = boxes.copy()
  68. x_center = np.asarray(x_center, np.float32)
  69. y_center = np.asarray(y_center, np.float32)
  70. x_center = assert_and_normalize_shape(x_center, boxes.shape[0])
  71. y_center = assert_and_normalize_shape(y_center, boxes.shape[0])
  72. shift = x_center - y_center
  73. ret_boxes[:, 0] = boxes[:, 1] + shift
  74. ret_boxes[:, 1] = boxes[:, 0] - shift
  75. ret_boxes[:, 2] = boxes[:, 3] + shift
  76. ret_boxes[:, 3] = boxes[:, 2] - shift
  77. return ret_boxes
  78. def flip_boxes_in_image(boxes, image_width, image_height, direction='h'):
  79. """
  80. Args:
  81. boxes: (N, 4+K)
  82. image_width: int
  83. image_width: int
  84. direction: str
  85. References:
  86. `core.bbox.bbox_flip` in mmdetection
  87. `datasets.pipelines.RandomFlip.bbox_flip` in mmdetection
  88. """
  89. x_center = (image_width - 1) * 0.5
  90. y_center = (image_height - 1) * 0.5
  91. ret_boxes = flip_boxes(boxes, x_center, y_center, direction=direction)
  92. return ret_boxes
  93. def rot90_boxes_in_image(boxes, image_width, image_height, n=1):
  94. """Rotate boxes counter-clockwise by 90 degrees.
  95. References:
  96. np.rot90
  97. cv2.rotate
  98. tf.image.rot90
  99. """
  100. n = n % 4
  101. if n == 0:
  102. ret_boxes = boxes.copy()
  103. elif n == 1:
  104. ret_boxes = transpose_boxes(boxes)
  105. ret_boxes = flip_boxes_in_image(ret_boxes, image_width, image_height, 'v')
  106. elif n == 2:
  107. ret_boxes = flip_boxes_in_image(boxes, image_width, image_height, 'o')
  108. else:
  109. ret_boxes = transpose_boxes(boxes)
  110. ret_boxes = flip_boxes_in_image(ret_boxes, image_width, image_height, 'h');
  111. return ret_boxes