如何从外部边界框中查找重叠和内部边界框?

2024-06-26 00:23:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试注释图像,下面是我所做注释的one边界框细节

Original Image-注释基于此文档图像

# annotation
{'x': 267.9,
 'y': 40.3,
 'height': 116.7,
 'width': 672.7,
 'center': [604.25, 98.65],
 'points': [[267.9, 40.3], [267.9, 157], [940.6, 157], [940.6, 40.3]]}

现在我还有另外五个小边界框,它们也是我注释的

[{'x': 290,
  'y': 66,
  'width': 309,
  'height': 72,
  'center': [444.5, 102.0],
  'points': [[290.0, 66.0], [290.0, 138.0], [599.0, 138.0], [599.0, 66.0]]},
 {'x': 626,
  'y': 68,
  'width': 295,
  'height': 72,
  'center': [773.5, 104.0],
  'points': [[626.0, 68.0], [626.0, 140.0], [921.0, 140.0], [921.0, 68.0]]},
 {'x': 960,
  'y': 69,
  'width': 20,
  'height': 71,
  'center': [970.0, 104.5],
  'points': [[960.0, 69.0], [960.0, 140.0], [980.0, 140.0], [980.0, 69.0]]},
 {'x': 1000,
  'y': 72,
  'width': 881,
  'height': 72,
  'center': [1440.5, 108.0],
  'points': [[1000.0, 72.0], [1000.0, 144.0], [1881.0, 144.0], [1881.0, 72.0]]},
 {'x': 1904,
  'y': 73,
  'width': 5,
  'height': 71,
  'center': [1906.5, 108.5],
  'points': [[1904.0, 73.0], [1904.0, 144.0], [1909.0, 144.0], [1909.0, 73.0]]}
 ]

我正在寻找一种方法,找出上面5个边界框坐标中有多少重叠或位于顶部的第一个主边界框内

我还需要一个选项来选择重叠的百分比。假设两个盒子稍微接触,我不想要它们。至少10%-15%的BBOX应该是内幕,以考虑它重叠或内部。

有谁能帮助我实现这个目标吗

所需输出:

在我的示例中,下面这两个较小的边界框位于主边界框内部或与主边界框重叠

[{'x': 290,
  'y': 66,
  'width': 309,
  'height': 72,
  'center': [444.5, 102.0],
  'points': [[290.0, 66.0], [290.0, 138.0], [599.0, 138.0], [599.0, 66.0]]},
 {'x': 626,
  'y': 68,
  'width': 295,
  'height': 72,
  'center': [773.5, 104.0],
  'points': [[626.0, 68.0], [626.0, 140.0], [921.0, 140.0], [921.0, 68.0]]}]

我用于从center points, width, height为边界框创建points的函数如下:

def convert_points(center_x, center_y, width, height):
    x = center_x
    y = center_y
    return [[x - width / 2, y - height / 2], [x - width / 2, y + height / 2],
            [x + width / 2, y + height / 2], [x + width / 2, y - height / 2]]

Tags: 方法文档图像image选项annotationwidthone
1条回答
网友
1楼 · 发布于 2024-06-26 00:23:30

您可以通过检查一个框的任何角是否落在另一个框的边界内来检查两个框是否有重叠(您也必须这样做,反之亦然)

# check if a point falls within bounds
def inBounds(point, tl, br):
    x, y = point;
    left, top = tl;
    right, bottom = br;
    if left < x and x < right and top < y and y < bottom:
        return True;
    return False;

# check if these two boxes have any overlap
def boxOverlap(box1, box2):
    # check box2 in box1
    tl = box1[0];
    br = box1[2];
    for point in box2:
        if inBounds(point, tl, br):
            return True;
    
    # check box1 in box2
    tl = box2[0];
    br = box2[2];
    for point in box1:
        if inBounds(point, tl, br):
            return True;

    # no overlap
    return False;

编辑:

抱歉,澄清一下:我认为方框是四个点[左上角、右上角、右下角、左下角],我假设方框没有旋转或其他任何东西(我认为对于大多数注释来说,这是一个安全的假设)

编辑2:

下面是一个如何使用它的示例。记住:我不知道你要用的所有不同的方法。如果你发现一些不起作用的东西,你必须修改它以供自己使用

# main box annotation
main_annot = {'x': 267.9,
 'y': 40.3,
 'height': 116.7,
 'width': 672.7,
 'center': [604.25, 98.65],
 'points': [[267.9, 40.3], [267.9, 157], [940.6, 157], [940.6, 40.3]]};

# other box annotation
other_annot = {'x': 290,
  'y': 66,
  'width': 309,
  'height': 72,
  'center': [444.5, 102.0],
  'points': [[290.0, 66.0], [290.0, 138.0], [599.0, 138.0], [599.0, 66.0]]};

if boxOverlap(main_annot['points'], other_annot['points']):
    print("OVERLAP DETECTED");
else:
    print("NO OVERLAP");

编辑3:

呜呜!我刚想到一个例子,这个函数不起作用。请暂时忽略这个答案。我今天没有多少时间来做这件事了。如果我以后再想出另一个答案,我会把它贴出来,但现在你不应该用这个。对不起,我应该更认真地检查边缘案例

编辑4:

好的,这次我查了一个实际的算法,而不是试图当场制作一个

# check if these two boxes have any overlap
def boxOverlap(box1, box2):
    # get corners
    tl1 = box1[0];
    br1 = box1[2];
    tl2 = box2[0];
    br2 = box2[2];

    # separating axis theorem
    # left/right
    if tl1[0] >= br2[0] or tl2[0] >= br1[0]:
        return False;

    # top/down
    if tl1[1] >= br2[1] or tl2[1] >= br1[1]:
        return False;

    # overlap
    return True;

# main box annotation
main_annot = {'x': 267.9,
 'y': 40.3,
 'height': 116.7,
 'width': 672.7,
 'center': [604.25, 98.65],
 'points': [[267.9, 40.3], [267.9, 157], [940.6, 157], [940.6, 40.3]]};

# other box annotation
other_annot = {'x': 290,
  'y': 66,
  'width': 309,
  'height': 72,
  'center': [444.5, 102.0],
  'points': [[290.0, 66.0], [290.0, 138.0], [599.0, 138.0], [599.0, 66.0]]};

if boxOverlap(main_annot['points'], other_annot['points']):
    print("OVERLAP DETECTED");
else:
    print("NO OVERLAP");

这是我从https://www.geeksforgeeks.org/find-two-rectangles-overlap/中提取方程的来源

这真是太尴尬了,我应该先用一个既定的算法

相关问题 更多 >