tensorflow庫提供的專門的圖片處理庫,以下只是部分示例,更多函數請參照源碼‘\tensorflow_api\v1\image_init_.py’
加載圖像
方式1:
使用tf.gfile.GFile以二進制方式讀jpg文件,然后通過tf.image.decode_jpeg進行解碼
注函數都返回tensor張量,需在session中運行
import tensorflow as tf
import matplotlib.pyplot as plt
image_raw = tf.gfile.GFile('./image/cat/cat.jpg','rb').read()
with tf.Session() as sess:
image_data = tf.image.decode_jpeg(image_raw)
plt.imshow(image_data.eval())
plt.show()
上面的方法不太適合讀取批量數據,批量讀取可以采用另一種方式,把圖像看成一個文件,用隊列的方式進行讀取,在tensorflow中,隊列不僅僅是一種數據結構,更提供多線程機制
方法2:批量讀取文件
path1 = './image/cat/cat.jpg'
file_queue = tf.train.string_input_producer([path1]) #創建輸入隊列
image_reader = tf.WholeFileReader()
_,image=image_reader.read(file_queue) #將完整的文件加載到內存
image = tf.image.decode_jpeg(image)
with tf.Session() as sess:
coord = tf.train.Coordinator() #協同啟動的線程
threads = tf.train.start_queue_runners(sess=sess,coord=coord) #啟動線程運行
plt.imshow(image.eval())
plt.show()
coord.request_stop() #通在所有的線程
coord.join(threads)
調整圖像大小
通過tf.image.resize_image()來調整圖片大小
函數原型:
tf.image.resize_images(
images,
size,
method=ResizeMethod.BILINEAR,
align_corners=False,
preserve_aspect_ratio=False)
參數:
method:圖片形狀調整方法,可以取下面的值
ResizeMethod.BILINEAR:默認方法,雙線性插值
ResizeMethod.NEAREST_NEIGHBOR:最近鄰插值
ResizeMethod.BICUBIC:雙三次插值
ResizeMethod.AREA:區域插值
align_corners:布爾型參數,默認為False,為True時,輸入張量和輸出張量的四個角的像素點的中心是對齊的,保留四個角的像素值
preserve_aspect_ratio:布爾型參數,默認為False,設置是否保持輸入圖片的長、寬比,如果設置為True,輸入圖像 images 的尺寸將調整為輸入 size 的大小,同時保持原始輸入圖片的長寬比。如果輸入 size 的比輸入圖像 images的尺寸大,將會按照比例放大輸入圖像 images
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
image_raw = tf.gfile.GFile('./image/cat/cat.jpg','rb').read()
with tf.Session() as sess:
image_data = tf.image.decode_jpeg(image_raw)
resized = tf.image.resize_images(image_data,[300,300],method=0)
plt.imshow(np.asarray(resized.eval(),dtype='uint8'))
plt.show()
剪切和填充圖像
tf.image.resize_image_with_crop_or_pad()
函數原型:
def resize_image_with_crop_or_pad(image, target_height, target_width):
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
image_raw = tf.gfile.GFile('./image/cat/cat.jpg','rb').read()
with tf.Session() as sess:
image_data = tf.image.decode_jpeg(image_raw)
resized = tf.image.resize_image_with_crop_or_pad(image_data,1000,1000)
plt.imshow(np.asarray(resized.eval(),dtype='uint8'))
plt.show()
對角線翻轉圖像
函數原型:
tf.image.transpose_image(image)
with tf.Session() as sess:
image_data = tf.image.decode_jpeg(image_raw)
resized = tf.image.transpose_image(image_data)
plt.imshow(np.asarray(resized.eval(),dtype='uint8'))
plt.show()
調整圖像色彩
def adjust_brightness(image, delta)
def random_brightness(image, max_delta, seed=None)
max_delta:最大差量
with tf.Session() as sess:
image_data = tf.image.decode_jpeg(image_raw)
brightness = tf.image.random_brightness(image_data,max_delta=0.4,seed=42)
plt.imshow(np.asarray(brightness.eval(),dtype='uint8'))
plt.show()
調整圖像色調飽和度
def adjust_hue(images, delta, name=None)
delta:差量
with tf.Session() as sess:
image_data = tf.image.decode_jpeg(image_raw)
adjust_hue = tf.image.adjust_hue(image_data,delta=0.4)
plt.imshow(np.asarray(adjust_hue.eval(),dtype='uint8'))
plt.show()