比较两个边界框和其他的Matlab。

时间:2022-02-22 14:17:29

I have two the co-ordinates of two bounding boxes, one of them is the groundtruth and the other is the result of my work. I want to evaluate the accuracy of mine against the groundtruth one. So I'm asking if anyone has any suggestions

我有两个边界框的坐标,其中一个是groundtruth,另一个是我工作的结果。我想要评估一下我的准确性。所以我问大家有没有什么建议。

The bounding box details is saved in this format [x,y,width,height]

绑定框的详细信息以这种格式保存[x,y,width,height]

6 个解决方案

#1


15  

Edit: I have corrected the mistake pointed by other users.

编辑:我已经纠正了其他用户的错误。

I am assuming you are detecting some object and you are drawing a bounding box around it. This comes under widely studied/researched area of object detection. The best method of evaluating the accuracy will be calculating intersection over union. This is taken from the PASCAL VOC challenge, from here. See here for visuals.

我假设你在探测某个物体,你在它周围画一个包围框。这是在被广泛研究的对象检测领域。计算精度的最佳方法是计算与联合的交点。这是从PASCAL VOC挑战,从这里。在这里看到的视觉效果。

If you have a bounding box detection and a ground truth bounding box, then the area of overlap between them should be greater than or equal to 50%. Suppose the ground truth bounding box is gt=[x_g,y_g,width_g,height_g] and the predicted bounding box is pr=[x_p,y_p,width_p,height_p] then the area of overlap can be calculated using the formula:

如果你有一个包围盒检测和一个地面的真理包围盒,那么它们之间的重叠区域应该大于或等于50%。假设ground truth包围盒是gt=[x_g,y_g,width_g,height_g],预测的边界框是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.

I hope its clear for you now.

我希望你现在明白了。

#2


5  

You should compute intersection and union, then the Jaccard index (intersection/union) is a value between 0 and 1 (1 mean perfect match, 0 mean perfect mismatch).

您应该计算交集和union,然后Jaccard索引(交集/union)是0到1之间的值(1表示完美匹配,0表示完全不匹配)。

#3


4  

Try intersection over Union

试着十字路口在联盟

Intersection over Union is an evaluation metric used to measure the accuracy of an object detector on a particular dataset.

在一个特定的数据集上,一个用于测量对象检测器的精度的评估指标是Union over Union。

More formally, in order to apply Intersection over Union to evaluate an (arbitrary) object detector we need:

更正式的是,为了在联合中应用交集来评估我们需要的(任意的)对象检测器:

  1. The ground-truth bounding boxes (i.e., the hand labeled bounding boxes from the testing set that specify where in the image our object is).
  2. 地真理包围盒(即:,从测试集的手标记包围盒,它指定我们的对象在图像中的位置。
  3. The predicted bounding boxes from our model.
  4. 从我们的模型预测的边界框。

Below I have included a visual example of a ground-truth bounding box versus a predicted bounding box:

下面我已经包含了一个地面真值包围盒的视觉例子和一个预测的包围盒:

比较两个边界框和其他的Matlab。

The predicted bounding box is drawn in red while the ground-truth (i.e., hand labeled) bounding box is drawn in green.

预测的边界框是用红色绘制的,而地面事实(即:(手标)包围盒以绿色绘制。

In the figure above we can see that our object detector has detected the presence of a stop sign in an image.

在上图中,我们可以看到我们的对象检测器检测到图像中有停止标志的存在。

Computing Intersection over Union can therefore be determined via:

因此,可以通过以下方法来确定计算交叉点:

比较两个边界框和其他的Matlab。

As long as we have these two sets of bounding boxes we can apply Intersection over Union.

只要我们有这两套包围盒我们就可以在联合上应用交集。

Here is the Python code

这是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

The gt and pred are

gt和pred是。

  1. gt : The ground-truth bounding box.
  2. 地面-真理包围盒。
  3. pred : The predicted bounding box from our model.
  4. pred:从我们的模型预测的边界框。

For more information, you can click this post

想了解更多信息,可以点击这个帖子。

#4


3  

The answer that has been marked best answer above is wrong.

上面给出的最好答案的答案是错误的。

The solution suggested by @Parag actually calculates the ratio of intersection area and area of the minimum covering rectangle. It should be the union area instead.

@Parag提出的解决方案实际上是计算最小覆盖矩形的交点面积和面积的比值。应该是联合区域。

So, the code would be

所以,代码是。

rect1 = [x1,y1,w1,h1]; 
rect2 = [x2,y2,w2,h2];
intersectionArea = rectint(rect1,rect2);
unionArea = w1*h1 + w2*h2 - intersectionArea;
overlap = intersectionArea/unionArea;

You can check this code to confirm the above (This code won the Pascal challenge).

您可以检查此代码以确认上述代码(此代码获得了Pascal挑战)。

#5


3  

All answers to the question suggest using intersection over union (IoU) metric. This is a vectorized version of IoU that can be used for multiple matching.

这个问题的所有答案都表明,使用交集(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

It calculates pairwise IoU for two sets of bounding boxes. If R1 and R2 each specify one rectangle, the output IoU is a scalar.

它计算两组包围盒的成对IoU。如果R1和R2指定一个矩形,输出IoU是一个标量。

R1 and R2 can also be matrices, where each row is a position vector ([x y w h]). IoU is then a matrix giving the IoU of all rectangles specified by R1 with all the rectangles specified by R2. That is, if R1 is n-by-4 and R2 is m-by-4, then IoU is an n-by-m matrix where IoU(i,j) is the IoU of the rectangles specified by the ith row of R1 and the jth row of R2.

R1和R2也可以是矩阵,每一行都是一个位置向量([x y wh])。IoU是一个矩阵,给出由R1指定的所有矩形的IoU和R2指定的所有矩形。也就是说,如果R1是n×4,而R2是m×4,那么IoU就是一个n×m矩阵,IoU(i,j)是由R1的第i行和R2的第j行所指定的矩形的IoU。

It also accepts a scalar parameter, treshold, to set the match output. Size of match is exactly like IoU. match(i,j) indicates if the rectangles specified by the ith row of R1 and the jth row of R2 match.

它还接受一个标量参数treshold来设置匹配输出。火柴的大小和我的一模一样。match(i,j)表示由R1的第i行和R2匹配的第j行指定的矩形。

For example,

例如,

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)

returns:

返回:

IoU =
     0         0    0.7738
     0    0.6596         0


match =
     0     0     1
     0     1     0

that means R1(1, :) and R1(2, :) match R2(3, :) and R2(2, :) respectively.

这意味着R1(1,:)和R1(2,:)匹配R2(3,:)和R2(2,:)。

比较两个边界框和其他的Matlab。

PS: At the time I'm posting this answer, there is a small mistake in Parag's answer (the accepted answer above). IMHO he/she calculates the union area in a wrong way. The unionCoords in his answer is actually the blue square in the picture below, and unionArea is its area, that obviously is not the union area of red and green squares.

PS:在我发布这个答案的时候,Parag的回答中有一个小错误(上面所接受的答案)。他/她以一种错误的方式计算了联邦地区。在他的回答中,unionCoords实际上是下图中的蓝色方块,而unionArea是它的区域,显然不是红色和绿色方块的联合区域。

比较两个边界框和其他的Matlab。

#6


0  

Just an extension to what @Parag S. Chandakkar said. I have edited his code to get overlap ratio matrix for many number of boxes.

这是@Parag S. Chandakkar所说的延伸。我已经编辑了他的代码,以得到许多盒子的重叠比率矩阵。

Incase you have you want to build function and directly use it to get overlap matrix (M,N) (each entry lies between [0,1]) for Box1(M,4) and Box2(N,4). (Box1 and Box2 contains(x,y,width,height) data of M and N boxes respectively).

如果您想要构建函数并直接使用它来得到重叠矩阵(M,N)(每个条目位于[0,1]之间),用于Box1(M,4)和Box2(N,4)。(Box1和Box2分别包含(x,y,width,height) M和N个盒子的数据)。

    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

This is kindof parallel function to bboxOverlapRatio (http://in.mathworks.com/help/vision/ref/bboxoverlapratio.html) but not available in R2014a or earlier.

这是bboxOverlapRatio (http://in.mathworks.com/help/vision/ref/bboxoverlapratio.html)的并行函数,但在R2014a或更早版本中是不可用的。

#1


15  

Edit: I have corrected the mistake pointed by other users.

编辑:我已经纠正了其他用户的错误。

I am assuming you are detecting some object and you are drawing a bounding box around it. This comes under widely studied/researched area of object detection. The best method of evaluating the accuracy will be calculating intersection over union. This is taken from the PASCAL VOC challenge, from here. See here for visuals.

我假设你在探测某个物体,你在它周围画一个包围框。这是在被广泛研究的对象检测领域。计算精度的最佳方法是计算与联合的交点。这是从PASCAL VOC挑战,从这里。在这里看到的视觉效果。

If you have a bounding box detection and a ground truth bounding box, then the area of overlap between them should be greater than or equal to 50%. Suppose the ground truth bounding box is gt=[x_g,y_g,width_g,height_g] and the predicted bounding box is pr=[x_p,y_p,width_p,height_p] then the area of overlap can be calculated using the formula:

如果你有一个包围盒检测和一个地面的真理包围盒,那么它们之间的重叠区域应该大于或等于50%。假设ground truth包围盒是gt=[x_g,y_g,width_g,height_g],预测的边界框是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.

I hope its clear for you now.

我希望你现在明白了。

#2


5  

You should compute intersection and union, then the Jaccard index (intersection/union) is a value between 0 and 1 (1 mean perfect match, 0 mean perfect mismatch).

您应该计算交集和union,然后Jaccard索引(交集/union)是0到1之间的值(1表示完美匹配,0表示完全不匹配)。

#3


4  

Try intersection over Union

试着十字路口在联盟

Intersection over Union is an evaluation metric used to measure the accuracy of an object detector on a particular dataset.

在一个特定的数据集上,一个用于测量对象检测器的精度的评估指标是Union over Union。

More formally, in order to apply Intersection over Union to evaluate an (arbitrary) object detector we need:

更正式的是,为了在联合中应用交集来评估我们需要的(任意的)对象检测器:

  1. The ground-truth bounding boxes (i.e., the hand labeled bounding boxes from the testing set that specify where in the image our object is).
  2. 地真理包围盒(即:,从测试集的手标记包围盒,它指定我们的对象在图像中的位置。
  3. The predicted bounding boxes from our model.
  4. 从我们的模型预测的边界框。

Below I have included a visual example of a ground-truth bounding box versus a predicted bounding box:

下面我已经包含了一个地面真值包围盒的视觉例子和一个预测的包围盒:

比较两个边界框和其他的Matlab。

The predicted bounding box is drawn in red while the ground-truth (i.e., hand labeled) bounding box is drawn in green.

预测的边界框是用红色绘制的,而地面事实(即:(手标)包围盒以绿色绘制。

In the figure above we can see that our object detector has detected the presence of a stop sign in an image.

在上图中,我们可以看到我们的对象检测器检测到图像中有停止标志的存在。

Computing Intersection over Union can therefore be determined via:

因此,可以通过以下方法来确定计算交叉点:

比较两个边界框和其他的Matlab。

As long as we have these two sets of bounding boxes we can apply Intersection over Union.

只要我们有这两套包围盒我们就可以在联合上应用交集。

Here is the Python code

这是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

The gt and pred are

gt和pred是。

  1. gt : The ground-truth bounding box.
  2. 地面-真理包围盒。
  3. pred : The predicted bounding box from our model.
  4. pred:从我们的模型预测的边界框。

For more information, you can click this post

想了解更多信息,可以点击这个帖子。

#4


3  

The answer that has been marked best answer above is wrong.

上面给出的最好答案的答案是错误的。

The solution suggested by @Parag actually calculates the ratio of intersection area and area of the minimum covering rectangle. It should be the union area instead.

@Parag提出的解决方案实际上是计算最小覆盖矩形的交点面积和面积的比值。应该是联合区域。

So, the code would be

所以,代码是。

rect1 = [x1,y1,w1,h1]; 
rect2 = [x2,y2,w2,h2];
intersectionArea = rectint(rect1,rect2);
unionArea = w1*h1 + w2*h2 - intersectionArea;
overlap = intersectionArea/unionArea;

You can check this code to confirm the above (This code won the Pascal challenge).

您可以检查此代码以确认上述代码(此代码获得了Pascal挑战)。

#5


3  

All answers to the question suggest using intersection over union (IoU) metric. This is a vectorized version of IoU that can be used for multiple matching.

这个问题的所有答案都表明,使用交集(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

It calculates pairwise IoU for two sets of bounding boxes. If R1 and R2 each specify one rectangle, the output IoU is a scalar.

它计算两组包围盒的成对IoU。如果R1和R2指定一个矩形,输出IoU是一个标量。

R1 and R2 can also be matrices, where each row is a position vector ([x y w h]). IoU is then a matrix giving the IoU of all rectangles specified by R1 with all the rectangles specified by R2. That is, if R1 is n-by-4 and R2 is m-by-4, then IoU is an n-by-m matrix where IoU(i,j) is the IoU of the rectangles specified by the ith row of R1 and the jth row of R2.

R1和R2也可以是矩阵,每一行都是一个位置向量([x y wh])。IoU是一个矩阵,给出由R1指定的所有矩形的IoU和R2指定的所有矩形。也就是说,如果R1是n×4,而R2是m×4,那么IoU就是一个n×m矩阵,IoU(i,j)是由R1的第i行和R2的第j行所指定的矩形的IoU。

It also accepts a scalar parameter, treshold, to set the match output. Size of match is exactly like IoU. match(i,j) indicates if the rectangles specified by the ith row of R1 and the jth row of R2 match.

它还接受一个标量参数treshold来设置匹配输出。火柴的大小和我的一模一样。match(i,j)表示由R1的第i行和R2匹配的第j行指定的矩形。

For example,

例如,

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)

returns:

返回:

IoU =
     0         0    0.7738
     0    0.6596         0


match =
     0     0     1
     0     1     0

that means R1(1, :) and R1(2, :) match R2(3, :) and R2(2, :) respectively.

这意味着R1(1,:)和R1(2,:)匹配R2(3,:)和R2(2,:)。

比较两个边界框和其他的Matlab。

PS: At the time I'm posting this answer, there is a small mistake in Parag's answer (the accepted answer above). IMHO he/she calculates the union area in a wrong way. The unionCoords in his answer is actually the blue square in the picture below, and unionArea is its area, that obviously is not the union area of red and green squares.

PS:在我发布这个答案的时候,Parag的回答中有一个小错误(上面所接受的答案)。他/她以一种错误的方式计算了联邦地区。在他的回答中,unionCoords实际上是下图中的蓝色方块,而unionArea是它的区域,显然不是红色和绿色方块的联合区域。

比较两个边界框和其他的Matlab。

#6


0  

Just an extension to what @Parag S. Chandakkar said. I have edited his code to get overlap ratio matrix for many number of boxes.

这是@Parag S. Chandakkar所说的延伸。我已经编辑了他的代码,以得到许多盒子的重叠比率矩阵。

Incase you have you want to build function and directly use it to get overlap matrix (M,N) (each entry lies between [0,1]) for Box1(M,4) and Box2(N,4). (Box1 and Box2 contains(x,y,width,height) data of M and N boxes respectively).

如果您想要构建函数并直接使用它来得到重叠矩阵(M,N)(每个条目位于[0,1]之间),用于Box1(M,4)和Box2(N,4)。(Box1和Box2分别包含(x,y,width,height) M和N个盒子的数据)。

    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

This is kindof parallel function to bboxOverlapRatio (http://in.mathworks.com/help/vision/ref/bboxoverlapratio.html) but not available in R2014a or earlier.

这是bboxOverlapRatio (http://in.mathworks.com/help/vision/ref/bboxoverlapratio.html)的并行函数,但在R2014a或更早版本中是不可用的。