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

How to use normalized coordinates in a rectangle for OpenCV

我正在使用 Microsoft 自定义视觉服务通过 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)

下面是上述代码生成的图像:

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

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

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)