matlab计算iou
时间: 2023-08-23 17:40:17 浏览: 138
计算两个矩形之间的IOU面积和重叠率
5星 · 资源好评率100%
可以使用以下代码计算两个矩形框之间的交并比(IoU):
```
function iou = bbox_iou(bbox1, bbox2)
% bbox1和bbox2分别是两个矩形框的坐标,格式为[xmin, ymin, xmax, ymax]
if ~isempty(bbox1) && ~isempty(bbox2)
bbox1 = double(bbox1);
bbox2 = double(bbox2);
intersect_bbox = [max(bbox1(1), bbox2(1)), max(bbox1(2), bbox2(2)), min(bbox1(3), bbox2(3)), min(bbox1(4), bbox2(4))];
w = intersect_bbox(3) - intersect_bbox(1) + 1;
h = intersect_bbox(4) - intersect_bbox(2) + 1;
if w > 0 && h > 0
intersect_area = w * h;
bbox1_area = (bbox1(3) - bbox1(1) + 1) * (bbox1(4) - bbox1(2) + 1);
bbox2_area = (bbox2(3) - bbox2(1) + 1) * (bbox2(4) - bbox2(2) + 1);
iou = intersect_area / (bbox1_area + bbox2_area - intersect_area);
else
iou = 0;
end
else
iou = 0;
end
end
```
其中,bbox1和bbox2分别是两个矩形框的坐标,格式为[xmin, ymin, xmax, ymax]。返回值iou为交并比。
阅读全文