Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.

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.

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. The image shape was resized to (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: enter image description here

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?

like image 745
Jon Avatar asked Oct 24 '25 12:10

Jon


1 Answers

Arguments for rectangle in opencv-python are point_1 and 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) 
like image 106
Sane_or_not Avatar answered Oct 27 '25 01:10

Sane_or_not



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!