如何判断一个边界框(矩形)是否位于另一个边界框(矩形)内部?

6

我正在尝试获取外框内部的盒子坐标。我已经使用重叠联合方法完成了此操作,并希望使用其他方法来实现。enter image description here

另外,请问如何比较这两个内部的盒子呢?


我不明白你的问题...如果你已经使用了交集...现在有什么问题吗? - Kevin Nguetchouang
@KevinNguetchouang 我想找其他的方法来做这件事。 - Nitesh
1个回答

7

通过比较边界框的左上角和右下角坐标以及内部框,很容易知道后者是否位于前者之内。

以下代码是一个简单的示例,只包含一个边界框和一个内部框:

# Bounding box
boundb = {
    'x': 150,
    'y': 150,
    'height': 50,
    'width': 100
}

# Inner box
innerb = {
    'x': 160,
    'y': 160,
    'height': 25,
    'width': 25
}

# If top-left inner box corner is inside the bounding box
if boundb['x'] < innerb['x'] and boundb['y'] < innerb['y']:
    # If bottom-right inner box corner is inside the bounding box
    if innerb['x'] + innerb['width'] < boundb['x'] + boundb['width'] \
            and innerb['y'] + innerb['height'] < boundb['y'] + boundb['height']:
        print('The entire box is inside the bounding box.')
    else:
        print('Some part of the box is outside the bounding box.')


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接