简体   繁体   中英

Use `tf.image.resize_image_with_crop_or_pad` to resize numpy array

I want to use tf.image.resize_image_with_crop_or_pad on a Numpy array of shape (100,100,2) to get it cropped or padded to a target shape (h,w,2) .

However, when I do:

img = resize_image_with_crop_or_pad(img, target_height, target_width)
img = np.array(img)

img.shape evaluates to () , which is not what I expected. How do I turn the output of this function into a properly shaped numpy array?

img = resize_image_with_crop_or_pad(img_tensor, target_height, target_width)
with tf.Session as sess:
    img_output = sess.run(img)

Now img_output is a numpy array, but note that img has to be a tf.Tensor of shape [1, height, width, channels] so you might do this beforehand, suggesting your input image is already a numpy array:

img_input = np.expand_dims(img_input, 0)
img_tensor = tf.convert_to_tensor(img_input)

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