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

Nit*_*sh 6 python opencv bounding-box

我试图获取外盒内的盒子(坐标)。我已经使用交集并集方法完成了,我希望其他方法也能这样做。在此输入图像描述

另外,你能告诉我如何比较这两个内盒吗?

Jos*_*ano 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.')

Run Code Online (Sandbox Code Playgroud)