当前位置:   article > 正文

如何计算两个矩形框的IoU_mmcv box_iou_rotated

mmcv box_iou_rotated

由于最近跟别人说我搞过目标检测,然后被问到一个问题,如何计算两个矩形框的IoU?  

一开始我回答说可以采用OpenCV的&运算和|运算来计算IoU,但他要我不采用OpenCV 的接口,自己写。

当时我回答的比较乱,分4种情况进行讨论2个矩形框之间的位置关系,最后他告诉我说,根本不需要讨论,采用max(),min()就可以实现。囧。

大致做法是先求出2个矩形框的交集,交集也是矩形框或空,计算相交部分面积再除以并集的面积即可。

关键在于怎么不分情况讨论就计算出交集部分的面积。

摘取SSD中bbox_util.cpp一部分代码如下:

  1. void IntersectBBox(const NormalizedBBox& bbox1, const NormalizedBBox& bbox2,
  2. NormalizedBBox* intersect_bbox) {
  3. if (bbox2.xmin() > bbox1.xmax() || bbox2.xmax() < bbox1.xmin() ||
  4. bbox2.ymin() > bbox1.ymax() || bbox2.ymax() < bbox1.ymin()) {
  5. // Return [0, 0, 0, 0] if there is no intersection.
  6. intersect_bbox->set_xmin(0);
  7. intersect_bbox->set_ymin(0);
  8. intersect_bbox->set_xmax(0);
  9. intersect_bbox->set_ymax(0);
  10. } else {
  11. intersect_bbox->set_xmin(std::max(bbox1.xmin(), bbox2.xmin()));
  12. intersect_bbox->set_ymin(std::max(bbox1.ymin(), bbox2.ymin()));
  13. intersect_bbox->set_xmax(std::min(bbox1.xmax(), bbox2.xmax()));
  14. intersect_bbox->set_ymax(std::min(bbox1.ymax(), bbox2.ymax()));
  15. }
  16. }
  1. float JaccardOverlap(const NormalizedBBox& bbox1, const NormalizedBBox& bbox2,
  2. const bool normalized) {
  3. NormalizedBBox intersect_bbox;
  4. IntersectBBox(bbox1, bbox2, &intersect_bbox);
  5. float intersect_width, intersect_height;
  6. if (normalized) {
  7. intersect_width = intersect_bbox.xmax() - intersect_bbox.xmin();
  8. intersect_height = intersect_bbox.ymax() - intersect_bbox.ymin();
  9. } else {
  10. intersect_width = intersect_bbox.xmax() - intersect_bbox.xmin() + 1;
  11. intersect_height = intersect_bbox.ymax() - intersect_bbox.ymin() + 1;
  12. }
  13. if (intersect_width > 0 && intersect_height > 0) {
  14. float intersect_size = intersect_width * intersect_height;
  15. float bbox1_size = BBoxSize(bbox1);
  16. float bbox2_size = BBoxSize(bbox2);
  17. return intersect_size / (bbox1_size + bbox2_size - intersect_size);
  18. } else {
  19. return 0.;
  20. }
  21. }

这个交集的计算采用max,min就计算出来了,真是巧妙啊。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/114127
推荐阅读
  

闽ICP备14008679号