Crop image to bounding box in Tensorflow Object De

2020-05-28 04:15发布

How can I crop an image to the bounding box in Tensorflow? I am using the Python API.

From the documentation,

tf.image.crop_to_bounding_box(image, offset_height, offset_width, target_height, target_width)

Crops an image to a specified bounding box.

This op cuts a rectangular part out of image. The top-left corner of the returned image is at offset_height, offset_width in image, and its lower-right corner is at offset_height + target_height, offset_width + target_width.

I can get the coordinates of a bounding box in normalized coordinates as,

    ymin = boxes[0,i,0]
    xmin = boxes[0,i,1]
    ymax = boxes[0,i,2]
    xmax = boxes[0,i,3]

and convert these to absolute coordinates,

    (xminn, xmaxx, yminn, ymaxx) = (xmin * im_width, xmax * im_width, ymin * im_height, ymax * im_height)

However I cant figure out how to use these coordinates in the crop_to_bounding_box function.

标签: tensorflow
1条回答
淡お忘
2楼-- · 2020-05-28 04:40

Since we consider x as horizontal and y as vertical, following would crop the image with specified box.

cropped_image = tf.image.crop_to_bounding_box(image, yminn, xminn, 
                                       ymaxx - yminn, xmaxx - xminn)
查看更多
登录 后发表回答