赞
踩
由于最近跟别人说我搞过目标检测,然后被问到一个问题,如何计算两个矩形框的IoU?
一开始我回答说可以采用OpenCV的&运算和|运算来计算IoU,但他要我不采用OpenCV 的接口,自己写。
当时我回答的比较乱,分4种情况进行讨论2个矩形框之间的位置关系,最后他告诉我说,根本不需要讨论,采用max(),min()就可以实现。囧。
大致做法是先求出2个矩形框的交集,交集也是矩形框或空,计算相交部分面积再除以并集的面积即可。
关键在于怎么不分情况讨论就计算出交集部分的面积。
摘取SSD中bbox_util.cpp一部分代码如下:
- void IntersectBBox(const NormalizedBBox& bbox1, const NormalizedBBox& bbox2,
- NormalizedBBox* intersect_bbox) {
- if (bbox2.xmin() > bbox1.xmax() || bbox2.xmax() < bbox1.xmin() ||
- bbox2.ymin() > bbox1.ymax() || bbox2.ymax() < bbox1.ymin()) {
- // Return [0, 0, 0, 0] if there is no intersection.
- intersect_bbox->set_xmin(0);
- intersect_bbox->set_ymin(0);
- intersect_bbox->set_xmax(0);
- intersect_bbox->set_ymax(0);
- } else {
- intersect_bbox->set_xmin(std::max(bbox1.xmin(), bbox2.xmin()));
- intersect_bbox->set_ymin(std::max(bbox1.ymin(), bbox2.ymin()));
- intersect_bbox->set_xmax(std::min(bbox1.xmax(), bbox2.xmax()));
- intersect_bbox->set_ymax(std::min(bbox1.ymax(), bbox2.ymax()));
- }
- }
- float JaccardOverlap(const NormalizedBBox& bbox1, const NormalizedBBox& bbox2,
- const bool normalized) {
- NormalizedBBox intersect_bbox;
- IntersectBBox(bbox1, bbox2, &intersect_bbox);
- float intersect_width, intersect_height;
- if (normalized) {
- intersect_width = intersect_bbox.xmax() - intersect_bbox.xmin();
- intersect_height = intersect_bbox.ymax() - intersect_bbox.ymin();
- } else {
- intersect_width = intersect_bbox.xmax() - intersect_bbox.xmin() + 1;
- intersect_height = intersect_bbox.ymax() - intersect_bbox.ymin() + 1;
- }
- if (intersect_width > 0 && intersect_height > 0) {
- float intersect_size = intersect_width * intersect_height;
- float bbox1_size = BBoxSize(bbox1);
- float bbox2_size = BBoxSize(bbox2);
- return intersect_size / (bbox1_size + bbox2_size - intersect_size);
- } else {
- return 0.;
- }
- }
这个交集的计算采用max,min就计算出来了,真是巧妙啊。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。