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. e.target和e.currentTarget区别

    直接上代码: body里: <div id="father"> father <div id="son"> son </div&g ...

  2. node.js 之 http 架设

    Node.js 安装配置 下载node.js安装mis 打开:cmd cd到node.js安装目录下 输入nodejs --version 显示版本号,证明安装成功 在其根目录下建server.js ...

  3. bootstrap自定义——栅格列数修改

    从下载的bootstrap文件中找到less文件夹里面的variables.less,然后可以找到栅格列数进行修改 然后执行一下bootstrap.less,通过命令行,切换到其所在的目录D:\03 ...

  4. angularjs1 自定义图片查看器(可旋转、放大、缩小、拖拽)

    笔记: angularjs1 制作自定义图片查看器(可旋转.放大.缩小.拖拽) 2018-01-12 更新  可以在我的博客  查看我 已经封装好的 纯 js写的图片查看器插件    博客链接 懒得把 ...

  5. 关于SpringMVC中两种映射器不能共存的解决

    首先大家都知道SpringMVC有两种映射器: org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping 和 org.spri ...

  6. Git的基础学习

    https://www.w3cschool.cn/git/git-install-setup.html 一.安装 Git官网下载,安装,操作步骤可以百度 二.用户信息配置 配置个人用户名称和邮箱地址: ...

  7. HLSL实现简单的图像处理功能

    由于对于dxva2解码得到的数据不宜copy回内存给CPU处理,所以最好的办法是在GPU上直接进行处理.D3D的像素着色器能够对像素直接进行操作,实现点运算极其简单方便,简单的卷积运算效果也非常好.但 ...

  8. Java解析XML格式串(JDOM解析)

    import java.io.IOException; import java.io.StringReader; import java.util.List; import org.jdom.Docu ...

  9. IE中的if语句--实现不同版本IE浏览器不同html代码 (转)

    在很多html代码中常常可以看到很多的IE的条件语句,有时候很不明白他们的意思,其实这是为了能与低版本浏览器实现更好的兼容,例如在制作纯CSS的级联菜单时,由于在IE6中只有<a>支持伪类 ...

  10. Ti IPNC Web网页之ActiveX控件

    Ti IPNC Web网页之ActiveX控件 本篇介绍关于TI IPNC网页中播放器相关的东西. gStudio工程中添加播放器并控制播放器 打开IPNC网页时首先会自动下载ActiveX控件并安装 ...