将图像边缘的边界框展开为正方形



我有一个函数,用于将图像中的边界框扩展为正方形。

def expand_bbox(bbox, img_shape):
h, w = bbox[2] - bbox[0], bbox[3] - bbox[1]
if h < w:
d = w - h
u = min(bbox[0], d // 2) 
v = d - u
bbox[0] -= u
bbox[2] += v
else:
d = h - w
u = min(bbox[1], d // 2)
v = d - u
bbox[1] -= u
bbox[3] += v
bbox[0] = max(bbox[0], 0)
bbox[1] = max(bbox[1], 0)
bbox[2] = min(bbox[2], img_shape[0]) 
bbox[3] = min(bbox[3], img_shape[1])
h, w = bbox[2] - bbox[0], bbox[3] - bbox[1]
assert h == w
inc = h // 2
inc = min(inc, bbox[0])
inc = min(inc, bbox[1])
inc = min(inc, img_shape[0] - bbox[2])
inc = min(inc, img_shape[1] - bbox[3])
bbox[0] -= inc
bbox[1] -= inc
bbox[2] += inc
bbox[3] += inc
h, w = bbox[2] - bbox[0], bbox[3] - bbox[1]
assert h == w
return bbox

它适用于边界框出现在图像内部,但无法扩展图像边缘中的框的情况(即,当边界框为[9791571080261],图像形状为(10801920,3(时(

我该怎么做才能确保边界框捕获沿图像边缘出现的边界框?

我很确定所有这些代码都会更漂亮,但如果你搜索一个工作代码,我会添加一些ifs:

# if increasing the upper edge position would go outside the img_shape, do not increase it, decrease bottom instead.
if bbox[2] + v > img_shape[0]:
bbox[0] -= u + v
else:
bbox[0] -= u
bbox[2] += v

宽度也是一样。

最新更新