def get_bbox_threshold(x, y, img, mag=None, init_size=None, threshold=0.5):
"""
更新位置并返回结果。
Args:
x:
y:
img: 图像。
mag:
init_size:
threshold:
Returns: None or List[int]: [x1, x2, y1, y2]
更新后的位置。
"""
# 初始化边框
if (x < 0 or x > img.shape[1]) or (y < 0 or y > img.shape[0]):
return None
if init_size is None:
init_size = max(img.shape[0], img.shape[1]) // 10 # 设置默认的初始化大小为图像宽度或高度的十分之一
half_init_size = init_size // 2
x1 = max(x - half_init_size, 0)
y1 = max(y - half_init_size, 0)
x2 = min(x + half_init_size, img.shape[1] - 1)
y2 = min(y + half_init_size, img.shape[0] - 1)
location = [x1, x2, y1, y2]
left, right, up, bottom = False, False, False, False
# 获取每个边的真实边界
while True:
# side_len = min(x2 - x1, y2 - y1)
#
# mag_conditions = [
# (24 < mag and side_len >= 24),
# (22 < mag <= 24 and side_len >= 36),
# (20 < mag <= 22 and side_len >= 48),
# (18 < mag <= 20 and side_len >= 72),
# (16 < mag <= 18 and side_len >= 120),
# (14 < mag <= 16 and side_len >= 192),
# (12 < mag <= 14 and side_len >= 264),
# (10 < mag <= 12 and side_len >= 512)
# ]
#
# if any(mag_conditions):
# break
# 提取边界线
left_line = img[y1:y2, x1].tolist()
right_line = img[y1:y2, x2].tolist()
up_line = img[y1, x1:x2].tolist()
bottom_line = img[y2, x1:x2].tolist()
# 计算边界线含零率
left_ratio = left_line.count(0) / len(left_line)
right_ratio = right_line.count(0) / len(right_line)
up_ratio = up_line.count(0) / len(up_line)
bottom_ratio = bottom_line.count(0) / len(bottom_line)
if left_ratio > threshold and not left:
location[0] = x1
if right and (x - location[0]) > (location[1] - x + 2):
location[0] = x - (location[1] - x + 2)
left = True
if right_ratio > threshold and not right:
location[1] = x2
if left and (location[1] - x) > (x - location[0] + 2):
location[1] = x + (x - location[0] + 2)
right = True
if up_ratio > threshold and not up:
location[2] = y1
if bottom and (y - location[2]) > (location[3] - y + 2):
location[2] = y - (location[3] - y + 2)
up = True
if bottom_ratio > threshold and not bottom:
location[3] = y2
if up and (location[3] - y) > (y - location[2] + 2):
location[3] = y + (y - location[2] + 2)
bottom = True
if left and right and up and bottom:
break
# 检查左边界和右边界是否需要调整
if not left:
x1 = max(0, x1 - 1)
left = x1 == 0
if not right:
x2 = min(img.shape[1] - 1, x2 + 1)
right = x2 == (img.shape[1] - 1)
# 检查上边界和下边界是否需要调整
if not up:
y1 = max(0, y1 - 1)
up = y1 == 0
if not bottom:
y2 = min(img.shape[0] - 1, y2 + 1)
bottom = y2 == (img.shape[0] - 1)
return location这段代码如何修改让它实现自动框选目标的功能,对不同大小的目标使用不同大小的目标框