boxes_transform_translate.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import numpy as np
  2. from .boxes_utils import assert_and_normalize_shape
  3. def translate_boxes(boxes, x_shift=0, y_shift=0, copy=True):
  4. """translate boxes coordinates in x and y dimensions.
  5. Args:
  6. boxes: (N, 4+K)
  7. x_shift: array-like whose shape is (), (1,), (N,), (1, 1) or (N, 1)
  8. shift in x dimension
  9. y_shift: array-like whose shape is (), (1,), (N,), (1, 1) or (N, 1)
  10. shift in y dimension
  11. copy: bool
  12. References:
  13. `datasets.pipelines.RandomCrop` in mmdetection
  14. """
  15. boxes = np.array(boxes, dtype=np.float32, copy=copy)
  16. x_shift = np.asarray(x_shift, np.float32)
  17. y_shift = np.asarray(y_shift, np.float32)
  18. x_shift = assert_and_normalize_shape(x_shift, boxes.shape[0])
  19. y_shift = assert_and_normalize_shape(y_shift, boxes.shape[0])
  20. boxes[:, 0] += x_shift
  21. boxes[:, 1] += y_shift
  22. boxes[:, 2] += x_shift
  23. boxes[:, 3] += y_shift
  24. return boxes
  25. def adjust_boxes(boxes, x_min_shift, y_min_shift, x_max_shift, y_max_shift, copy=True):
  26. """
  27. Args:
  28. boxes: (N, 4+K)
  29. x_min_shift: array-like whose shape is (), (1,), (N,), (1, 1) or (N, 1)
  30. shift (x_min, y_min) in x dimension
  31. y_min_shift: array-like whose shape is (), (1,), (N,), (1, 1) or (N, 1)
  32. shift (x_min, y_min) in y dimension
  33. x_max_shift: array-like whose shape is (), (1,), (N,), (1, 1) or (N, 1)
  34. shift (x_max, y_max) in x dimension
  35. y_max_shift: array-like whose shape is (), (1,), (N,), (1, 1) or (N, 1)
  36. shift (x_max, y_max) in y dimension
  37. copy: bool
  38. """
  39. boxes = np.array(boxes, dtype=np.float32, copy=copy)
  40. x_min_shift = np.asarray(x_min_shift, np.float32)
  41. y_min_shift = np.asarray(y_min_shift, np.float32)
  42. x_max_shift = np.asarray(x_max_shift, np.float32)
  43. y_max_shift = np.asarray(y_max_shift, np.float32)
  44. x_min_shift = assert_and_normalize_shape(x_min_shift, boxes.shape[0])
  45. y_min_shift = assert_and_normalize_shape(y_min_shift, boxes.shape[0])
  46. x_max_shift = assert_and_normalize_shape(x_max_shift, boxes.shape[0])
  47. y_max_shift = assert_and_normalize_shape(y_max_shift, boxes.shape[0])
  48. boxes[:, 0] += x_min_shift
  49. boxes[:, 1] += y_min_shift
  50. boxes[:, 2] += x_max_shift
  51. boxes[:, 3] += y_max_shift
  52. return boxes
  53. def inflate_or_deflate_boxes(boxes, width_delta=0, height_delta=0, copy=True):
  54. """
  55. Args:
  56. boxes: (N, 4+K)
  57. width_delta: array-like whose shape is (), (1,), (N,), (1, 1) or (N, 1)
  58. height_delta: array-like whose shape is (), (1,), (N,), (1, 1) or (N, 1)
  59. copy: bool
  60. """
  61. boxes = np.array(boxes, dtype=np.float32, copy=copy)
  62. width_delta = np.asarray(width_delta, np.float32)
  63. height_delta = np.asarray(height_delta, np.float32)
  64. width_delta = assert_and_normalize_shape(width_delta, boxes.shape[0])
  65. height_delta = assert_and_normalize_shape(height_delta, boxes.shape[0])
  66. half_width_delta = width_delta * 0.5
  67. half_height_delta = height_delta * 0.5
  68. boxes[:, 0] -= half_width_delta
  69. boxes[:, 1] -= half_height_delta
  70. boxes[:, 2] += half_width_delta
  71. boxes[:, 3] += half_height_delta
  72. return boxes
  73. def inflate_boxes_to_square(boxes, copy=True):
  74. """Inflate boxes to square
  75. Args:
  76. boxes: (N, 4+K)
  77. copy: bool
  78. """
  79. boxes = np.array(boxes, dtype=np.float32, copy=copy)
  80. widths = boxes[:, 2] - boxes[:, 0]
  81. heights = boxes[:, 3] - boxes[:, 1]
  82. max_side_lengths = np.maximum(widths, heights)
  83. width_deltas = np.subtract(max_side_lengths, widths, widths)
  84. height_deltas = np.subtract(max_side_lengths, heights, heights)
  85. width_deltas *= 0.5
  86. height_deltas *= 0.5
  87. boxes[:, 0] -= width_deltas
  88. boxes[:, 1] -= height_deltas
  89. boxes[:, 2] += width_deltas
  90. boxes[:, 3] += height_deltas
  91. return boxes
  92. def deflate_boxes_to_square(boxes, copy=True):
  93. """Deflate boxes to square
  94. Args:
  95. boxes: (N, 4+K)
  96. copy: bool
  97. """
  98. boxes = np.array(boxes, dtype=np.float32, copy=copy)
  99. widths = boxes[:, 2] - boxes[:, 0]
  100. heights = boxes[:, 3] - boxes[:, 1]
  101. min_side_lengths = np.minimum(widths, heights)
  102. width_deltas = np.subtract(min_side_lengths, widths, widths)
  103. height_deltas = np.subtract(min_side_lengths, heights, heights)
  104. width_deltas *= 0.5
  105. height_deltas *= 0.5
  106. boxes[:, 0] -= width_deltas
  107. boxes[:, 1] -= height_deltas
  108. boxes[:, 2] += width_deltas
  109. boxes[:, 3] += height_deltas
  110. return boxes