Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crop image to bounding box in Tensorflow Object Detection API

Tags:

tensorflow

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.

like image 556
Ahmad Avatar asked Aug 19 '17 12:08

Ahmad


1 Answers

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)
like image 139
Ishant Mrinal Avatar answered Oct 22 '22 06:10

Ishant Mrinal