我有两个边界框的坐标,一个是groundtruth,另一个是我工作的结果。我想根据真实情况评估我的准确性。所以想问问大家有没有什么建议
边界框细节以这种格式保存[x,y,width,height]
我有两个边界框的坐标,一个是groundtruth,另一个是我工作的结果。我想根据真实情况评估我的准确性。所以想问问大家有没有什么建议
边界框细节以这种格式保存[x,y,width,height]
编辑:我已经纠正了其他用户指出的错误。
我假设您正在检测某个对象,并且您正在围绕它绘制一个边界框。这属于广泛研究/研究的对象检测领域。评估准确性的最佳方法是计算交集而不是联合。这取自 PASCAL VOC 挑战,从这里。请参阅此处了解视觉效果。
如果你有一个bounding box detection和一个ground truth bounding box,那么它们之间的重叠区域应该大于或等于50%。假设ground truth bounding box是gt=[x_g,y_g,width_g,height_g]
,预测的bounding box是,pr=[x_p,y_p,width_p,height_p]
那么重叠区域可以使用公式计算:
intersectionArea = rectint(gt,pr); %If you don't have this function then write a simple one for yourself which calculates area of intersection of two rectangles.
unionArea = (width_g*height_g)+(width_p*height_p)-intersectionArea;
overlapArea = intersectionArea/unionArea; %This should be greater than 0.5 to consider it as a valid detection.
我希望你现在清楚了。
尝试在 Union 上的交集
Intersection over Union 是一种评估指标,用于衡量目标检测器在特定数据集上的准确性。
更正式地说,为了应用 Intersection over Union 来评估(任意)对象检测器,我们需要:
下面我包含了一个真实边界框与预测边界框的视觉示例:
预测的边界框用红色绘制,而真实的(即手工标记的)边界框用绿色绘制。
在上图中,我们可以看到我们的物体检测器已经检测到图像中存在停车标志。
因此,计算联合上的交集可以通过以下方式确定:
只要我们有这两组边界框,我们就可以应用 Intersection over Union。
这是Python代码
# import the necessary packages
from collections import namedtuple
import numpy as np
import cv2
# define the `Detection` object
Detection = namedtuple("Detection", ["image_path", "gt", "pred"])
def bb_intersection_over_union(boxA, boxB):
# determine the (x, y)-coordinates of the intersection rectangle
xA = max(boxA[0], boxB[0])
yA = max(boxA[1], boxB[1])
xB = min(boxA[2], boxB[2])
yB = min(boxA[3], boxB[3])
# compute the area of intersection rectangle
interArea = (xB - xA + 1) * (yB - yA + 1)
# compute the area of both the prediction and ground-truth
# rectangles
boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)
boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)
# compute the intersection over union by taking the intersection
# area and dividing it by the sum of prediction + ground-truth
# areas - the interesection area
iou = interArea / float(boxAArea + boxBArea - interArea)
# return the intersection over union value
return iou
gt
和pred
是_
gt
:真实边界框。pred
:我们模型的预测边界框。更多信息,您可以点击此帖子
您应该计算交集和并集,然后Jaccard 索引(交集/并集)是介于 0 和 1 之间的值(1 表示完全匹配,0 表示完全不匹配)。
该问题的所有答案都建议使用联合交集 (IoU) 度量。这是 IoU 的矢量化版本,可用于多个roi匹配。
function [IoU, match] = rectIoU(R1, R2, treshold)
I = rectint(R1, R2);
A1 = R1(:, 3).*R1(:, 4);
A2 = R2(:, 3).*R2(:, 4);
U = bsxfun(@plus, A1, A2')-I;
IoU = I./U;
if nargout > 1
if nargin<3
treshold = 0.5;
end
match = IoU>treshold;
end
end
它计算两组边界框的成对 IoU。如果R1
和R2
each 指定一个矩形,则输出IoU
是一个标量。
R1
也R2
可以是矩阵,其中每一行是一个位置向量 ( [x y w h]
)。IoU
然后是一个矩阵,给出由 指定的所有矩形与由 指定的所有矩形的R1
IoU R2
。也就是说,如果R1
isn-by-4
和R2
is m-by-4
,则IoU
是一个n-by-m
矩阵,其中是 的第 行和 的第 行IoU(i,j)
指定的矩形的 IoU 。i
R1
j
R2
它还接受一个标量参数treshold
, 来设置match
输出。大小match
一模一样IoU
。指示由第th 行和第 th 行match(i,j)
指定的矩形是否匹配。i
R1
j
R2
例如,
R1 = [0 0 1 1; 2 1 1 1];
R2 = [-.5 2 1 1; flipud(R1)];
R2 = R2+rand(size(R2))*.4-.2;
[IoU, match] = rectIoU(R1, R2, 0.4)
返回:
IoU =
0 0 0.7738
0 0.6596 0
match =
0 0 1
0 1 0
这意味着R1(1, :)
和R1(2, :)
匹配R2(3, :)
和R2(2, :)
分别。
PS:
在我发布此答案时,Parag 的答案(上面接受的答案)中有一个小错误。恕我直言,他们以错误的方式计算联合区域。他们回答中的unionCoords
其实就是下图中的蓝色方块,unionArea
是它的面积,显然不是红绿矩形的联合面积。
上面标记为最佳答案的答案是错误的。
@Parag 建议的解决方案实际上是计算相交面积与最小覆盖矩形面积的比值。它应该是联合区域。
所以,代码将是
rect1 = [x1,y1,w1,h1];
rect2 = [x2,y2,w2,h2];
intersectionArea = rectint(rect1,rect2);
unionArea = w1*h1 + w2*h2 - intersectionArea;
overlap = intersectionArea/unionArea;
您可以检查此代码以确认上述内容(此代码赢得了 Pascal 挑战)。
只是对@Parag S. Chandakkar 所说的内容的扩展。我已经编辑了他的代码以获得许多盒子的重叠率矩阵。
如果您想构建函数并直接使用它来获取 Box1(M,4) 和 Box2(N,4) 的重叠矩阵 (M,N)(每个条目位于 [0,1] 之间)。(Box1 和 Box2 分别包含 M 和 N 个盒子的 (x,y,width,height) 数据)。
function result= overlap_matrix(box1,box2)
[m,y1]=size(box1);
[n,y2]=size(box2);
result=zeros(m,n,'double');
for i = 1:m
for j=1:n
gt=box1(i,:);
pr=box2(j,:);
x_g=box1(i,1);
y_g=box1(i,2);
width_g=box1(i,3);
height_g=box1(i,4);
x_p=box2(j,1);
y_p=box2(j,2);
width_p=box2(j,3);
height_p=box2(j,4);
intersectionArea=rectint(gt,pr);
unionCoords=[min(x_g,x_p),min(y_g,y_p),max(x_g+width_g-1,x_p+width_p-1),max(y_g+height_g-1,y_p+height_p-1)];
unionArea=(unionCoords(3)-unionCoords(1)+1)*(unionCoords(4)-unionCoords(2)+1);
overlapArea=intersectionArea/unionArea;
result(i,j)=overlapArea;
end
end
这是 bboxOverlapRatio ( http://in.mathworks.com/help/vision/ref/bboxoverlapratio.html ) 的一种并行函数,但在 R2014a 或更早版本中不可用。