bbox_utils.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Copyright (c) 2019 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. from __future__ import absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. import logging
  18. import numpy as np
  19. __all__ = ["bbox_overlaps", "box_to_delta"]
  20. logger = logging.getLogger(__name__)
  21. def bbox_overlaps(boxes_1, boxes_2):
  22. '''
  23. bbox_overlaps
  24. boxes_1: x1, y, x2, y2
  25. boxes_2: x1, y, x2, y2
  26. '''
  27. assert boxes_1.shape[1] == 4 and boxes_2.shape[1] == 4
  28. num_1 = boxes_1.shape[0]
  29. num_2 = boxes_2.shape[0]
  30. x1_1 = boxes_1[:, 0:1]
  31. y1_1 = boxes_1[:, 1:2]
  32. x2_1 = boxes_1[:, 2:3]
  33. y2_1 = boxes_1[:, 3:4]
  34. area_1 = (x2_1 - x1_1 + 1) * (y2_1 - y1_1 + 1)
  35. x1_2 = boxes_2[:, 0].transpose()
  36. y1_2 = boxes_2[:, 1].transpose()
  37. x2_2 = boxes_2[:, 2].transpose()
  38. y2_2 = boxes_2[:, 3].transpose()
  39. area_2 = (x2_2 - x1_2 + 1) * (y2_2 - y1_2 + 1)
  40. xx1 = np.maximum(x1_1, x1_2)
  41. yy1 = np.maximum(y1_1, y1_2)
  42. xx2 = np.minimum(x2_1, x2_2)
  43. yy2 = np.minimum(y2_1, y2_2)
  44. w = np.maximum(0.0, xx2 - xx1 + 1)
  45. h = np.maximum(0.0, yy2 - yy1 + 1)
  46. inter = w * h
  47. ovr = inter / (area_1 + area_2 - inter)
  48. return ovr
  49. def box_to_delta(ex_boxes, gt_boxes, weights):
  50. """ box_to_delta """
  51. ex_w = ex_boxes[:, 2] - ex_boxes[:, 0] + 1
  52. ex_h = ex_boxes[:, 3] - ex_boxes[:, 1] + 1
  53. ex_ctr_x = ex_boxes[:, 0] + 0.5 * ex_w
  54. ex_ctr_y = ex_boxes[:, 1] + 0.5 * ex_h
  55. gt_w = gt_boxes[:, 2] - gt_boxes[:, 0] + 1
  56. gt_h = gt_boxes[:, 3] - gt_boxes[:, 1] + 1
  57. gt_ctr_x = gt_boxes[:, 0] + 0.5 * gt_w
  58. gt_ctr_y = gt_boxes[:, 1] + 0.5 * gt_h
  59. dx = (gt_ctr_x - ex_ctr_x) / ex_w / weights[0]
  60. dy = (gt_ctr_y - ex_ctr_y) / ex_h / weights[1]
  61. dw = (np.log(gt_w / ex_w)) / weights[2]
  62. dh = (np.log(gt_h / ex_h)) / weights[3]
  63. targets = np.vstack([dx, dy, dw, dh]).transpose()
  64. return targets