Reputation: 77
I want to call tf.image.resize_image_with_crop_or_pad(images,height,width)
to resize my input images. As my input images are all in form as 2-d numpy array of pixels, while the image input of resize_image_with_crop_or_pad
must be 3-d or 4-d tensor, it will cause an error. What should I do?
Upvotes: 2
Views: 3600
Reputation: 27070
Let's suppose that you got images
that's a [n, W, H]
numpy nd-array, in which n
is the number of images and W
and H
are the width
and the height
of the images.
Convert images to a tensor, in order to be able to use tensorflow functions:
tf_images = tf.constant(images)
Convert tf_images
to the image data format used by tensorflow (thus from n, W, H
to n, H, W
)
tf_images = tf.transpose(tf_images, perm=[0,2,1])
In tensorflow, every image has a depth channell, thus altough you're using grayscale images, we have to add the depth=1
channell.
tf_images = tf.expand_dims(tf_images, 2)
Now you can use tf.image.resize_image_with_crop_or_pad
to resize the batch (that how has a shape of [n, H, W, 1]
(4-d tensor)):
resized = tf.image.resize_image_with_crop_or_pad(tf_images,height,width)
Upvotes: 2