Search code examples
tensorflowobject-detectionbounding-box

tf.image.crop_and_resize() returns broken cropped image


I'm trying to capture the cropped image using bounding boxes from Faster R-CNN implemented by TensorFlow API. (especially, I followed and customized this tutorial from tensorflow)

My code following the tutorial above is below:

for image_path in TEST_IMAGE_PATHS[0:1]:
            image = Image.open(image_path)
            image_np = load_image_into_numpy_array(image)
            image_np_expanded = np.expand_dims(image_np, axis=0)

            (_image_tensor,_boxes, scores, classes, num) = sess.run(
              [image_tensor,detection_boxes, detection_scores, detection_classes, num_detections],
              feed_dict={image_tensor: image_np_expanded})

            test = tf.image.crop_and_resize(image=image_np_expanded,
                                                             boxes=[[0.27640104,0.2573258,0.57859987,0.7340185]],
                                                             box_ind=[0],
                                                             crop_size=[50,50])
            plt.figure()
            plt.imshow(image_np)

            plt.figure()
            plt.imshow(test[0].eval())

when the code above is executed, the result shows the picture below: enter image description here

As you can see, the second one which is the cropped image is broken. The value of bounding box is the first value of the variable '_boxes' which is "_boxes[0]"

Is there anything I'm missing? I'm stuck with this issue.


Solution

  • It seems that tf.image.crop_and_resize expects pixel values in the range [0,1]. Changing your code to

    test = tf.image.crop_and_resize(image=image_np_expanded/255., ...)
    

    solved the problem for me.