通过比较边界框的左上角和右下角坐标以及内部框,很容易知道后者是否位于前者之内。
以下代码是一个简单的示例,只包含一个边界框和一个内部框:
# 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.')