简体   繁体   English

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

[英]How to use normalized coordinates in a rectangle for OpenCV

I'm using the Microsoft Custom Vision service for object detection with the Python SDK. 我正在使用Microsoft Custom Vision服务通过Python SDK进行对象检测。 I'm able to make predictions and I'm trying to use the bounding box information that comes back from the prediction to overlay a rectangle on the image using OpenCV. 我能够做出预测,并且尝试使用从预测返回的边界框信息来使用OpenCV在图像上覆盖一个矩形。

However, I'm not sure how to exactly calculate from the normalized coordinates that come back from the Custom Vision service to the point vertexes that the OpenCV rectangle function takes in. 但是,我不确定如何从自定义视觉服务返回到OpenCV rectangle函数接受的点顶点的归一化坐标精确计算。

Here's an example of what comes back from the service as bounding box: 这是从服务作为边界框返回的示例:

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

Currently, I'm doing these calculations below. 目前,我正在下面进行这些计算。 The x and y values look like they're being calculated correctly, but I'm not sure how to calculate the second vertex. xy值看起来好像是正确计算的,但是我不确定如何计算第二个顶点。 The image shape was resized to (400, 400) . 图像形状调整为(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)

And here is the resulting image from the above code: 这是上面的代码生成的图像: 在此处输入图片说明

The first box looks like it's not going far enough, whereas the second box looks like it produced a rectangle going the opposite way of where it should. 第一个框看起来距离不够远,而第二个框看起来像它产生了一个矩形,正好相反。

Does anyone know how to calculate these correctly from normalized coordinates? 有谁知道如何从归一化坐标正确计算这些?

Arguments for rectangle in opencv-python are point_1 and point_2. 在opencv-python中,矩形的参数为point_1和point_2。 Like that: 像那样:

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) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM