Tensorflow图像处理(一):图像大小调整

借助matplotlib.pyplot工具可以可视化图像。

首先读取一张图片并显示出来。

import matplotlib.pyplot as plt
import tensorflow as tf

image_raw_data = tf.gfile.FastGFile('00000.jpg', 'rb').read()

with tf.Session() as sess:
    img_data = tf.image.decode_jpeg(image_raw_data)
    plt.imshow(img_data.eval())
    plt.show()

一、resize

通过调用tf.image.resize_images函数来调整图像大小。

resized = tf.image.resize_images(img_data, (200, 200), method=0)
print(sess.run(tf.shape(resized)))
resized = tf.cast(resized, tf.int32)
plt.imshow(resized.eval())
plt.show()

其中,

method=0:双线性插值法(Bilinear interpolation);

method=1:最近邻法(Nearest neighbor interpolation);

method=2:双三次插值法(Bicubic interpolation);

method=3:面积插值法(Area interpolation)。

二、crop and pad

调用tf.image.resize_image_with_crop_or_pad函数可以裁剪或者填充图像。如果原始图像的尺寸大于目标图像,name会自动截取原始图像居中的部分。如果目标图像大于原始图像,会自动在原始图像的四周填充全0背景。

croped = tf.image.resize_image_with_crop_or_pad(img_data, 300, 400)
padded = tf.image.resize_image_with_crop_or_pad(img_data, 700, 900)
plt.imshow(croped.eval())
plt.show()
plt.imshow(padded.eval())
plt.show()

下面两个函数截取或者填充图像中指定的区域。

crop_bounding_box = tf.image.crop_to_bounding_box(img_data, 100, 200, 200, 200)
plt.imshow(crop_bounding_box.eval())
plt.show()

pad_bounding_box = tf.image.pad_to_bounding_box(img_data, 100, 100, 950, 650)
plt.imshow(pad_bounding_box.eval())
plt.show()

三、按比例调整

central_cropped = tf.image.central_crop(img_data, 0.5)
plt.imshow(central_cropped.eval())
plt.show()

猜你喜欢

转载自blog.csdn.net/heiheiya/article/details/81288444