简体   繁体   English

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

[英]how to find overlapping and inner bounding boxes from a outer bounding box?

I am trying to annotate a image and here is the one bounding box details from a annotation I have made,我正在尝试注释图像,这是我所做的注释中的one边界框详细信息,

Original Image - The annotations are based on this document 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]]}

Now I have five other small bounding boxes which are also annotated by me,现在我还有另外五个小边界框,它们也由我注释,

[{'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]]}
 ]

I am looking for a way to find how many of the above 5 bounding box coordinates overlap or come inside the first main bounding box that I have at the very top.我正在寻找一种方法来查找上述 5 个边界框坐标中有多少重叠或进入我位于最顶部的第一个主边界框内。

I also need an option to select how much percent of overlapping to consider it as overlap.我还需要一个选项 select 有多少重叠百分比将其视为重叠。 Let's say if two boxes are slightly touching I don't want them.假设如果两个盒子有轻微的接触,我不想要它们。 Atleast 10-15% of bbox should be insider to consider it overlapping or inside.至少 10-15% 的 bbox 应该是内部的,以将其视为重叠或内部。

Can anyone help me out on how I can achieve that?任何人都可以帮助我如何实现这一目标吗?

Desired Output:所需的 Output:

In my example, these two below smaller bounding boxes are inside or overlapping with the main bounding box.在我的示例中,这两个较小的边界框位于主边界框内部或与主边界框重叠。

[{'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]]}]

The function I used to create points for bounding box from center points, width, height is this,我用来从center points, width, height创建边界框points的 function 是这样的,

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]]

You can check to see if two boxes have any overlap by checking if any of the corners of one box fall within the bounds of the other box (you have to do this vice-versa as well).您可以通过检查一个框的任何角是否落在另一个框的范围内来检查两个框是否有任何重叠(反之亦然)。

# 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;

Edit:编辑:

Sorry, to clarify: I'm thinking of boxes as four points [top-left, top-right, bottom-right, bottom-left] and I assume the boxes aren't rotated or anything (I think that's a safe assumption for most annotations).抱歉,澄清一下:我将框视为四个点 [左上、右上、右下、左下],我假设这些框没有旋转或任何东西(我认为这是一个安全的假设大多数注释)。

Edit 2:编辑2:

Here's an example of how to use this.这是一个如何使用它的示例。 Remember: I don't know all of the different ways you're going to use this.请记住:我不知道您将使用它的所有不同方式。 You'll have to modify it for your own use if you find something that isn't working.如果您发现某些内容不起作用,则必须对其进行修改以供自己使用。

# 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");

Edit 3:编辑3:

WOOPS.哎呀。 I just thought of a case where this function doesn't work.我只是想到了这个 function 不起作用的情况。 Please disregard this answer for now.请暂时忽略这个答案。 I don't have much time left today to work on this, If I come up with another answer later I'll post it.我今天没有太多时间来解决这个问题,如果我稍后想出另一个答案,我会发布它。 but for now you shouldn't use this, Sorry.但现在你不应该使用这个,对不起。 I should've been more diligent with checking edge cases.我应该更努力地检查边缘情况。

Edit 4:编辑4:

Ok, I looked up an actual algorithm this time instead of trying to make one on the spot.好的,这次我查找了一个实际的算法,而不是试图当场制作一个。

# 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");

Here's the source I pulled the equation from https://www.geeksforgeeks.org/find-two-rectangles-overlap/这是我从https 中提取方程式的来源://www.geeksforgeeks.org/find-two-rectangles-overlap/

That was pretty embarrassing, I should've just used an established algorithm to begin with.那太尴尬了,我应该一开始就使用一个既定的算法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 numpy 数组中获取不在内部边界框内但在外部边界框之间的所有 3d 点? - How do I get all 3d points in a numpy array that are not within a inner bounding box, but between the outer bounding box? 给定具有多个边界框的图像,我如何仅突出显示完全在另一个边界框内的那些边界框? - Given an image with several bounding boxes, how do I highlight only those bounding boxes that are completely inside another bounding box? 找到边界框的中心 - Find center of bounding box 如何删除与其他边界框重叠 90% 的框 - How to remove box overlapping 90% with other bounding box 如何在网格中找到边界框的占用率? - How to find occupancy of bounding box in a grid? 如何有效地找到一组点的边界框? - How to efficiently find the bounding box of a collection of points? 如何在Python中找到旋转边界框的坐标? - How to find coordinate of a rotated bounding box in Python? 使用边界框列表从图像中裁剪多个边界框 - Crop multiple bounding boxes from image with list of bounding boxes 如何从边界框获取图像 - How to get image from bounding box 如何将边界框合并为一个 - How to merge the bounding boxes into one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM