utils.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # Copyright (c) 2024 PaddlePaddle 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. __all__ = ["get_neighbor_boxes_idx"]
  15. import numpy as np
  16. def get_neighbor_boxes_idx(src_boxes: np.ndarray, ref_box: np.ndarray) -> list:
  17. """
  18. Retrieve indices of source boxes that are neighbors to the reference box.
  19. Parameters:
  20. src_boxes (np.ndarray): An array of bounding boxes with shape (N, 4),
  21. where N is the number of boxes and each box is represented
  22. by [x1, y1, x2, y2].
  23. ref_box (np.ndarray): A single bounding box represented by [x1, y1, x2, y2].
  24. Returns:
  25. list: A list of indices of the source boxes that are close to the
  26. reference box based on the intersection area.
  27. """
  28. match_idx_list = []
  29. if len(src_boxes) > 0:
  30. x1 = np.maximum(ref_box[0], src_boxes[:, 0])
  31. y1 = np.maximum(ref_box[1], src_boxes[:, 1])
  32. x2 = np.minimum(ref_box[2], src_boxes[:, 2])
  33. y2 = np.minimum(ref_box[3], src_boxes[:, 3])
  34. pub_w = x2 - x1
  35. pub_h = y2 - y1
  36. match_idx = np.where((pub_w > 0) & (pub_h < 3) & (pub_h > -15))[0]
  37. match_idx_list.extend(match_idx)
  38. return match_idx_list