如何在OpenCV中使用矩形中的规范化坐标

Jon*_*Jon 3 python opencv python-3.x microsoft-cognitive

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

但是,我不确定如何根据从Custom Vision服务返回到OpenCV rectangle函数接收的点顶点的归一化坐标准确地进行计算。

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

{'left': 0.146396145,
 'top': 0.0305180848,
 'width': 0.373975337,
 'height': 0.570280433}
Run Code Online (Sandbox Code Playgroud)

目前,我正在下面进行这些计算。该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)
Run Code Online (Sandbox Code Playgroud)

这是上面的代码生成的图像: 在此处输入图片说明

第一个框看起来距离不够远,而第二个框看起来像它产生了一个矩形,正好相反。

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

小智 5

在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) 
Run Code Online (Sandbox Code Playgroud)