tensorflow圖像基本處理

tensorflow庫提供的專門的圖片處理庫,如下只是部分示例,更多函數請參照源碼‘\tensorflow_api\v1\image__init__.py’python

加載圖像

方式1:api

使用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中,隊列不單單是一種數據結構,更提供多線程機制session

方法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()來調整圖片大小多線程

函數原型:dom

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()ui

函數原型:線程

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()

對角線翻轉圖像

函數原型:3d

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()

相關文章
相關標籤/搜索