You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.2 KiB

6 years ago
  1. # Copyright 2017 The TensorFlow Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. """Operations for [N, 4] numpy arrays representing bounding boxes.
  16. Example box operations that are supported:
  17. * Areas: compute bounding box areas
  18. * IOU: pairwise intersection-over-union scores
  19. """
  20. import numpy as np
  21. def area(boxes):
  22. """Computes area of boxes.
  23. Args:
  24. boxes: Numpy array with shape [N, 4] holding N boxes
  25. Returns:
  26. a numpy array with shape [N*1] representing box areas
  27. """
  28. return (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1])
  29. def intersection(boxes1, boxes2):
  30. """Compute pairwise intersection areas between boxes.
  31. Args:
  32. boxes1: a numpy array with shape [N, 4] holding N boxes
  33. boxes2: a numpy array with shape [M, 4] holding M boxes
  34. Returns:
  35. a numpy array with shape [N*M] representing pairwise intersection area
  36. """
  37. [y_min1, x_min1, y_max1, x_max1] = np.split(boxes1, 4, axis=1)
  38. [y_min2, x_min2, y_max2, x_max2] = np.split(boxes2, 4, axis=1)
  39. all_pairs_min_ymax = np.minimum(y_max1, np.transpose(y_max2))
  40. all_pairs_max_ymin = np.maximum(y_min1, np.transpose(y_min2))
  41. intersect_heights = np.maximum(
  42. np.zeros(all_pairs_max_ymin.shape),
  43. all_pairs_min_ymax - all_pairs_max_ymin)
  44. all_pairs_min_xmax = np.minimum(x_max1, np.transpose(x_max2))
  45. all_pairs_max_xmin = np.maximum(x_min1, np.transpose(x_min2))
  46. intersect_widths = np.maximum(
  47. np.zeros(all_pairs_max_xmin.shape),
  48. all_pairs_min_xmax - all_pairs_max_xmin)
  49. return intersect_heights * intersect_widths
  50. def iou(boxes1, boxes2):
  51. """Computes pairwise intersection-over-union between box collections.
  52. Args:
  53. boxes1: a numpy array with shape [N, 4] holding N boxes.
  54. boxes2: a numpy array with shape [M, 4] holding N boxes.
  55. Returns:
  56. a numpy array with shape [N, M] representing pairwise iou scores.
  57. """
  58. intersect = intersection(boxes1, boxes2)
  59. area1 = area(boxes1)
  60. area2 = area(boxes2)
  61. union = np.expand_dims(area1, axis=1) + np.expand_dims(
  62. area2, axis=0) - intersect
  63. return intersect / union
  64. def ioa(boxes1, boxes2):
  65. """Computes pairwise intersection-over-area between box collections.
  66. Intersection-over-area (ioa) between two boxes box1 and box2 is defined as
  67. their intersection area over box2's area. Note that ioa is not symmetric,
  68. that is, IOA(box1, box2) != IOA(box2, box1).
  69. Args:
  70. boxes1: a numpy array with shape [N, 4] holding N boxes.
  71. boxes2: a numpy array with shape [M, 4] holding N boxes.
  72. Returns:
  73. a numpy array with shape [N, M] representing pairwise ioa scores.
  74. """
  75. intersect = intersection(boxes1, boxes2)
  76. areas = np.expand_dims(area(boxes2), axis=0)
  77. return intersect / areas