读取图片:
image_string = tf.io.read_file(filename)
转化图片的格式
image_decoded = tf.image.decode_jpeg(image_string)
图像的resize,注意这里是通过下采样来处理的。
image_resized = tf.image.resize(image_decoded, [224,224],method=tf.image.ResizeMethod.BILINEAR) / 255.0
如果要查看你现在预处理的样子,可以用这个
with tf.Session() as sess:
img_ = image_resized.eval()
plt.figure(1)
plt.imshow(img_)
plt.show()
如果你要进行padding的话:要注意啦,offset_height的话,是你的上面要填充多少,然后target_height是你最终的填充高度。所以下面的话就是我最终的高度是224,但我实际上只有126,所以还需要98,所以我上下需要49,这样我的图片就在中间,所以呢我就上下填充啦。
image_padding = tf.image.pad_to_bounding_box(image_resized,offset_height=49,offset_width=0,target_height=224,target_width=224)