Tensorflow中图像的预处理

转自:http://blog.csdn.net/qq_31120801/article/details/75506127

TensorFlow图像预处理-函数

更多的基本的API请参看TensorFlow中文社区:http://www.tensorfly.cn/tfdoc/api_docs/python/array_ops.html

下面是实验的代码,可以参考,对应的图片是输出的结果:

复制代码
import tensorflow as tf
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
%matplotlib inline
path = '/home/ubuntu-mm/TensorFlow/Learning/D-TestJupyter/images/Train/Dogs.jpg'
file_queue = tf.train.string_input_producer([path])
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) #启动线程运行队列  
    sess.run(image)  
    coord.request_stop() #停止所有的线程  
    coord.join(threads)  
    image_uint8 = tf.image.convert_image_dtype(image, dtype = tf.uint8)
    plt.figure(1)
    plt.imshow(image_uint8.eval())
    print image_uint8.get_shape()
    resize_image1 = tf.image.resize_images(image_uint8,[400,300],method=tf.image.ResizeMethod.NEAREST_NEIGHBOR) #修改图片的尺寸
    resize_image2 = tf.image.resize_images(image_uint8,[400,300],method=1) #修改图片的尺寸 1代表的就是NEAREST_NEIGHBOR的方法
    central_crop = tf.image.central_crop(image_uint8, 0.6) #从图片中心开始裁剪图片,裁剪比例为60%
    bounding_crop = tf.image.crop_to_bounding_box(resize_image1, offset_height=100, offset_width=100, target_height=100, target_width=100) #设定裁剪的起始位置和终止位置进行裁剪
    pad = tf.image.pad_to_bounding_box(bounding_crop, offset_height=0, offset_width=0, target_height=105, target_width=105) #设定边缘对图像的边缘进行填充(填0)
    flip1 = tf.image.flip_left_right(resize_image1) #左右翻转图片
    flip2 = tf.image.flip_up_down(flip1) #上下翻转图片
    adjust_brightness = tf.image.adjust_brightness(resize_image1, 0.2) #调节图像的亮度为原来的20%
    adjust_saturation = tf.image.adjust_saturation(resize_image1, 0.4) #调节图像的饱和度为原来的40%
    adjust_hue = tf.image.adjust_hue(resize_image1, 0.7) #调节原来的H(灰度)为原来的70%
    image_float = tf.cast(resize_image1, dtype=tf.float32) 
    gray = tf.image.rgb_to_grayscale(image_float) #对图像的类型进行转换rgb-grayscale
    hsv = tf.image.rgb_to_hsv(image_float) #对图像进行hsv转换rgb-hsv
    imag_gray = tf.image.convert_image_dtype(gray, tf.uint8)
    imag_hsv = tf.image.convert_image_dtype(hsv, tf.uint8)
    sess.run([flip1, flip2])
    plt.figure(2)
    plt.imshow(resize_image1.eval())
    plt.figure(3)
    plt.imshow(resize_image2.eval())
    plt.figure(4)
    plt.imshow(central_crop.eval())
    plt.figure(5)
    plt.imshow(bounding_crop.eval())
    plt.figure(6)
    plt.imshow(pad.eval())
    plt.figure(7)
    plt.imshow(flip2.eval())
    plt.figure(8)
    plt.imshow(adjust_brightness.eval())
    plt.figure(9)
    plt.imshow(adjust_saturation.eval())
    plt.figure(10)
    plt.imshow(adjust_hue.eval())
    plt.figure(1)
    plt.imshow(imag_hsv.eval(), cmap=cm.hsv)
复制代码

                      原图                                               改变尺寸                                       改变尺寸                                  图像中心裁剪

                   图像边缘裁剪                                              图像边缘补0                                     图像水平垂直翻转

                图像亮度度调节                           图像饱和度变换                                 图像弧度调节H                              图像HSV显示

相关函数介绍:

1、tf.image.resize_images(image_uint8,[400,300],method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)

  函数的作用是修改图像的尺寸(缩放放大的形式),第一个参数是原始图像,第二个参数是输出图像的大小,第三个参数是缩放或放大的方法。

2、tf.image.central_crop(image_uint8, 0.6)

  函数的功能是裁剪图像,裁剪的中心式图像的中心位置,第一个参数是原始图像,第二个参数是裁剪的比例。

3、tf.image.crop_to_bounding_box(resize_image1, offset_height=100, offset_width=100, target_height=100, target_width=100)

  函数的功能是按照输入的参数的边缘来裁剪图像,第一个参数是原始的图像,第二个参数是裁剪的y轴起始位置,第三个是x轴起始位置,第4个参数和第5个参数是输出图像的尺寸大小。

4、tf.image.pad_to_bounding_box(bounding_crop, offset_height=0, offset_width=0, target_height=105, target_width=105)

  函数的功能是扩充图像的边缘,对图像的边缘进行补零的操作,第一个参数是原始图像,第二个参数和第三个参数是输出图像在原图上的起始位置,第4和5个参数是输出图像的大小,当输出图像超出了原始图像的大小时,就会将超出的部分进行补零的操作。

5、tf.image.flip_left_right(resize_image1)

  函数的功能是对图像进行水平方向的反转,参数1是原始图像。

6、tf.image.flip_up_down(flip1)

  函数的功能是对图像进行垂直方向的反转,参数1是原始图像。

7、tf.image.adjust_brightness(resize_image1, 0.2)

  函数的功能是调节原始图像的亮度值,第一个参数是原始图像,第二个参数是调节的比例。

8、tf.image.adjust_saturation(resize_image1, 0.4)

  函数的功能是调节原始图像的饱和度,第一个参数是原始图像,第二个参数是调节的比例。

9、tf.image.adjust_hue(resize_image1, 0.7)

  函数的功能是调节图像的灰度值(Hue),参数1是原始图像,参数2是调节的比例。

10、tf.image.rgb_to_grayscale(image_float)

  函数的功能是间输入的rgb格式的图像转换成grayscale的灰度图像,参数1是输入的原始图像。(注意输入图像的格式需要时浮点形式的float)

11、tf.image.rgb_to_hsv(image_float)

  函数的功能是间输入的图像转换成为hsv格式的图像,参数1是输入图像,输入的格式需要时浮点型的。

                                                             完!

猜你喜欢

转载自blog.csdn.net/yinxingtianxia/article/details/78131712