简体   繁体   中英

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?

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 (ie, 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 (ie, x , y , bbxWidth , and bbxHeight ) to draw the bounding box on the original image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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