gpt4 book ai didi

python - 如何在 OpenCV 的矩形中使用归一化坐标

转载 作者:太空宇宙 更新时间:2023-11-03 13:58:52 25 4
gpt4 key购买 nike

我使用 Microsoft Custom Vision 服务通过 Python SDK 进行对象检测。我能够做出预测,并且我正在尝试使用从预测返回的边界框信息,使用 OpenCV 在图像上叠加一个矩形。

但是,我不确定如何从自定义视觉服务返回的归一化坐标到 OpenCV rectangle 函数接收的点顶点进行精确计算。

这是从服务作为边界框返回的示例:

{'left': 0.146396145,
'top': 0.0305180848,
'width': 0.373975337,
'height': 0.570280433}

目前,我正在下面进行这些计算。 xy 值看起来计算正确,但我不确定如何计算第二个顶点。图像形状已调整为 (400, 400)

for pred in predictions:
x = int(pred.bounding_box.left * img.shape[0])
y = int(pred.bounding_box.top * img.shape[1])

width = int(pred.bounding_box.width * img.shape[0])
height = int(pred.bounding_box.height * img.shape[1])

img = cv2.rectangle(img, (x,y), (width,height), (0,0,255), 2)

下面是上述代码生成的图像: enter image description here

第一个框看起来还不够远,而第二个框看起来生成了一个与应有位置相反的矩形。

有谁知道如何根据归一化坐标正确计算这些值?

最佳答案

opencv-python 中矩形的参数是 point_1 和 point_2。像那样:

for pred in predictions:
x = int(pred.bounding_box.left * img.shape[0])
y = int(pred.bounding_box.top * img.shape[1])

x2 = x + int(pred.bounding_box.width * img.shape[0])
y2 = y + int(pred.bounding_box.height * img.shape[1])

img = cv2.rectangle(img, (x,y), (x2,y2), (0,0,255), 2)

关于python - 如何在 OpenCV 的矩形中使用归一化坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51747822/

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