pythoncomputer-visionobject-detectionmicrosoft-custom-vision

Cropping an image in python


I am working on an object detection project using Azure Custom Vision. An example of a bounding box I got is [0.053913698, 0.6198375, 0.09218301, 0.13308609].

The selected answer here is not going to suit my task because all the values are less than 0.

Can anyone please help?


Solution

  • Reason

    A bounding-box list tells you ["left", "top", "width", "height"], and each of them is in percent of the image original size.

    Solution

    Assumed that your image dimension is 800 x 600 (i.e., Image Width is 800, Image Height is 600). Therefore, what you need to do is to multiply the width and height to the corresponding values. Read the code below in Python:

    imageWidth  = 800
    imageHeight = 600
    bbx = {
        "left": 0.053913698,
        "top": 0.6198375,
        "width": 0.09218301,
        "height": 0.13308609
    }
    
    # top-left point position
    (x, y) = (bbx["left"]*imageWidth, bbx["top"]*imageHeight) 
    
    # bounding box's width and height
    bbxWidth  = bbx["width"]  * imageWidth
    bbxHeight = bbx["height"] * imageHeight
    

    You can use the above values (i.e., x, y, bbxWidth, and bbxHeight) to draw the bounding box on the original image.