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

tensorflow图像基本处理的更多相关文章

  1. TensorFlow图像预处理-函数

    更多的基本的API请参看TensorFlow中文社区:http://www.tensorfly.cn/tfdoc/api_docs/python/array_ops.html 下面是实验的代码,可以参 ...

  2. Tensorflow图像操作

    图像操作 图像基本概念 在图像数字化表示当中,分为黑白和彩色两种.在数字化表示图片的时候,有三个因素.分别是图片的长.图片的宽.图片的颜色通道数.那么黑白图片的颜色通道数为1,它只需要一个数字就可以表 ...

  3. TensorFlow 图像预处理(一) 图像编解码,图像尺寸调整

    from: https://blog.csdn.net/chaipp0607/article/details/73029923 TensorFlow提供了几类图像处理函数,下面介绍图像的编码与解码,图 ...

  4. TensorFlow图像预处理完整样例

    参考书 <TensorFlow:实战Google深度学习框架>(第2版) 以下TensorFlow程序完成了从图像片段截取,到图像大小调整再到图像翻转及色彩调整的整个图像预处理过程. #! ...

  5. 吴裕雄 python 神经网络——TensorFlow 图像预处理完整样例

    import numpy as np import tensorflow as tf import matplotlib.pyplot as plt def distort_color(image, ...

  6. TensorFlow框架(2)之TensorBoard详解

    为了更方便 TensorFlow 程序的理解.调试与优化,TensorFlow发布了一套叫做 TensorBoard 的可视化工具.你可以用 TensorBoard 来展现你的 TensorFlow ...

  7. AlexNet 网络详解及Tensorflow实现源码

    版权声明:本文为博主原创文章,未经博主允许不得转载. 1. 图片数据处理 2. 卷积神经网络 2.1. 卷积层 2.2. 池化层 2.3. 全链层 3. AlexNet 4. 用Tensorflow搭 ...

  8. 深度学习动手入门:GitHub上四个超棒的TensorFlow开源项目

    作者简介:akshay pai,数据科学工程师,热爱研究机器学习问题.Source Dexter网站创办人. TensorFlow是Google的开源深度学习库,你可以使用这个框架以及Python编程 ...

  9. 深度学习与计算机视觉(12)_tensorflow实现基于深度学习的图像补全

    深度学习与计算机视觉(12)_tensorflow实现基于深度学习的图像补全 原文地址:Image Completion with Deep Learning in TensorFlow by Bra ...

随机推荐

  1. AVL树 - 学习笔记

    2017-08-29 14:35:55 writer:pprp AVL树就是带有平衡条件的二叉查找树.每个节点的左子树和右子树高度相差最多为1的二叉查找树 空树的高度定为-1 对树的修正称为旋转 对内 ...

  2. jQuery实际案例⑤——仿京东侧边栏(楼层)

    楼层:①页面滑动到哪块儿“楼层”就显示到哪个:②点击某“楼层”页面滚动到对应的位置:③点击“返回”回到页面顶部 实现:①使用$(window).scroll(function(){ });  //监视 ...

  3. VS2017编译项目出现提示al.exe运行失败的解决方法

    VS2013中编译一切正常,用VS2017打开项目,某个类库出现al.exe运行失败的解决方法,事件查看器中这样描述 “C:\Program Files (x86)\Microsoft SDKs\Wi ...

  4. Android----Material Design之(FloatActionButton,CoordinatorLayout,CollapsingToolbarLayout,AppBarLayout,TabLayout等)

    Material Design 的一些UI 平常开发还是用的比较多的,以前没写,最近总结一下,写一篇博客,要求版本在5.0以上. 主要介绍了FloatActionButton,CoordinatorL ...

  5. 通用Mapper相关

    1.通用Mapper中,用@Table来映数据表与实体,其中 name:指定表的名称,例如@Table(name="ls_post") catalog: 指定数据库名称,默认为当前 ...

  6. flask学习(十二):for循环遍历

    一. 字典的遍历 语法和python一样,可以使用items().keys().values().iteritems().iterkeys().itervalues() {% for k, v in ...

  7. nyoj1007——欧拉求和

    GCD 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 The greatest common divisor GCD(a,b) of two positive in ...

  8. 017——数组(十七) asort ksort rsort arsort krsort

    <?php /** * 数组 asort ksort rsort arsort krsort */ //asort()对数组按值排序,保留键名: /*$arr=array( 'bbs_url'= ...

  9. 【zzuli-2276】跳一跳

    题目描述 今天跳跳去公园游玩,第一个游戏就难倒了跳跳,游戏规则是跳跳站在一个面积无限大的矩形土地上,开始时跳跳在左上角(即第一行第一列),每一次跳跳都可以选择一个右下方格子,并瞬间跳过去(如从下图中的 ...

  10. git重要命令

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...