Ahmad
Ahmad

Reputation: 93

Crop image to bounding box in Tensorflow Object Detection API

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.

Upvotes: 9

Views: 10923

Answers (2)

ZKS
ZKS

Reputation: 2836

Below is working code from cropping and saving bounding box in in tensorflow

 for idx in range(len(bboxes)):
    if bscores[idx] >= Threshold:
      #Region of Interest
      y_min = int(bboxes[idx][0] * im_height)
      x_min = int(bboxes[idx][1] * im_width)
      y_max = int(bboxes[idx][2] * im_height)
      x_max = int(bboxes[idx][3] * im_width)

      class_label = category_index[int(bclasses[idx])]['name']
      class_labels.append(class_label)
      bbox.append([x_min, y_min, x_max, y_max, class_label, float(bscores[idx])])

      #Crop Image - Working Code
      cropped_image = tf.image.crop_to_bounding_box(image, y_min, x_min, y_max - y_min, x_max - x_min).numpy().astype(np.int32)

      # encode_jpeg encodes a tensor of type uint8 to string
      output_image = tf.image.encode_jpeg(cropped_image)
      # decode_jpeg decodes the string tensor to a tensor of type uint8
      #output_image = tf.image.decode_jpeg(output_image)

      score = bscores[idx] * 100

      file_name = tf.constant(OUTPUT_PATH+image_name[:-4]+'_'+str(idx)+'_'+class_label+'_'+str(round(score))+'%'+'_'+os.path.splitext(image_name)[1])

      writefile = tf.io.write_file(file_name, output_image)

Upvotes: 0

Ishant Mrinal
Ishant Mrinal

Reputation: 4918

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)

Upvotes: 10

Related Questions