gpt4 book ai didi

python - 如何操纵跟踪器区域使其变为正方形?

转载 作者:行者123 更新时间:2023-12-02 16:23:39 25 4
gpt4 key购买 nike

我目前正在从事有关相机跟踪应用程序的项目。当我不得不更改检测到的对象的区域时,发现的麻烦很小。该区域必须转换为正方形,以便在显示到屏幕上时变得更加合适和真实。
这是我正在处理的几行代码。

for cam, coll in _colls.items():
# get all track
tracks = coll.getAllTracks() #tracks is a dictionary that stores the details of object that being detected.

for x in range(len(tracks)):
# get the selected area for croping
edge = tracks[x]['box']

crop_img = frame[int(edge[0]):int(edge[2]), int(edge[1]):int(edge[3])]
# edge[0] = ymin
# edge[1] = xmin
# edge[2] = ymax
# edge[3] = xmax
基于该代码,我得到了一个检测物体的区域。
enter image description here
问题是当我得到边缘时,它并不总是呈正方形,它也可以呈矩形。应该操纵它的样子,以便从那个边缘点开始,我可以像下面的图片那样创建一个正方形吗?
enter image description here

最佳答案

您必须计算区域的宽度和高度。计算宽度和高度之间的差异,并扩大该区域的较小一侧。不重要:

left, right, top, bottom = edge[0], edge[1], edge[2], edge[3]

width = right - left
height = bottom - top
delta = width - height

if delta > 0:
top -= delta / 2
bottom += delta / 2
else
left -= -delta / 2
right += -delta / 2

crop_img = frame[int(left):int(top), int(right):int(bottom)]

关于python - 如何操纵跟踪器区域使其变为正方形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62926589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com