如何计算Python OpenCV中边界盒的质心

发布于 2025-01-21 15:14:27 字数 733 浏览 2 评论 0原文

我正在做推理和检测手,我需要计算检测到的手的边界框的质心。

以下是它的坐标:

left = 215 
bottom = 447 
right = 325 
top = 367

这就是我现在绘制矩形

cv2.rectangle(frame, (int(obj.Left), int(obj.Bottom)), (int(obj.Right), int(obj.Top)), (0, 0, 255), 2)

以计算边界框的质心的方式,我使用以下代码线:

w = int(obj.Right - obj.Left)
h = int(obj.Bottom - obj.Top)
cx = int(obj.Top + h/2)
cy = int(obj.Left + w/2)
print(cx, cy)

因此,首先,我正在计算边界框的宽度和高度。然后将高度和宽度除以2,然后将它们分别添加到顶部和左侧以计算质心。但这似乎无法正常工作,因为您可以看到白点(以红色圆圈为代表)远离被检测到的手。

我该如何改进?

I am doing inference and detecting hands and I need to calculate the centroid of the bounding box of the detected hand.

Below is its coordinates:

left = 215 
bottom = 447 
right = 325 
top = 367

and this is how I am drawing the rectangle

cv2.rectangle(frame, (int(obj.Left), int(obj.Bottom)), (int(obj.Right), int(obj.Top)), (0, 0, 255), 2)

Now in order to calculate the centroid of the bounding box, I am using below lines of code:

w = int(obj.Right - obj.Left)
h = int(obj.Bottom - obj.Top)
cx = int(obj.Top + h/2)
cy = int(obj.Left + w/2)
print(cx, cy)

So first I am calculating the width and height of the bounding box. Then dividing the height and width by 2 and adding them to top and left respectively to calculate the centroid. But this doesn't seems to be working as you can see the white dot (represented in red circle) is far away from detected hand.

enter image description here

How can I improve it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文