tensorflow图像基本处理

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

猜你喜欢

转载自www.cnblogs.com/xiaobingqianrui/p/10796269.html