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

tensorflow图像基本处理的更多相关文章
- TensorFlow图像预处理-函数
更多的基本的API请参看TensorFlow中文社区:http://www.tensorfly.cn/tfdoc/api_docs/python/array_ops.html 下面是实验的代码,可以参 ...
- Tensorflow图像操作
图像操作 图像基本概念 在图像数字化表示当中,分为黑白和彩色两种.在数字化表示图片的时候,有三个因素.分别是图片的长.图片的宽.图片的颜色通道数.那么黑白图片的颜色通道数为1,它只需要一个数字就可以表 ...
- TensorFlow 图像预处理(一) 图像编解码,图像尺寸调整
from: https://blog.csdn.net/chaipp0607/article/details/73029923 TensorFlow提供了几类图像处理函数,下面介绍图像的编码与解码,图 ...
- TensorFlow图像预处理完整样例
参考书 <TensorFlow:实战Google深度学习框架>(第2版) 以下TensorFlow程序完成了从图像片段截取,到图像大小调整再到图像翻转及色彩调整的整个图像预处理过程. #! ...
- 吴裕雄 python 神经网络——TensorFlow 图像预处理完整样例
import numpy as np import tensorflow as tf import matplotlib.pyplot as plt def distort_color(image, ...
- TensorFlow框架(2)之TensorBoard详解
为了更方便 TensorFlow 程序的理解.调试与优化,TensorFlow发布了一套叫做 TensorBoard 的可视化工具.你可以用 TensorBoard 来展现你的 TensorFlow ...
- AlexNet 网络详解及Tensorflow实现源码
版权声明:本文为博主原创文章,未经博主允许不得转载. 1. 图片数据处理 2. 卷积神经网络 2.1. 卷积层 2.2. 池化层 2.3. 全链层 3. AlexNet 4. 用Tensorflow搭 ...
- 深度学习动手入门:GitHub上四个超棒的TensorFlow开源项目
作者简介:akshay pai,数据科学工程师,热爱研究机器学习问题.Source Dexter网站创办人. TensorFlow是Google的开源深度学习库,你可以使用这个框架以及Python编程 ...
- 深度学习与计算机视觉(12)_tensorflow实现基于深度学习的图像补全
深度学习与计算机视觉(12)_tensorflow实现基于深度学习的图像补全 原文地址:Image Completion with Deep Learning in TensorFlow by Bra ...
随机推荐
- jni使用问题总结
参考: https://blog.csdn.net/fred_lzy/article/details/53159138 https://blog.csdn.net/avi3/article/detai ...
- git-svn — 让git和svn协同工作
git-svn — 让git和svn协同工作 svn作为一个优秀源码版本的管理工具,可以适合绝大多数项目.但是因为它的采用中心化管理,不可避免的存在本地代码的备份和版本管理问题.也就是说对于尚未或暂 ...
- Chrome自动化搭建
工具安装: 1.selenium-java-2.53.0-srcs.jar 2.selenium-java-2.53.0.jar 3.下载chromedriver.exe(下载地址:http://ch ...
- WIN10局域网共享,经常出现无法访问的现象的解决方法
WIN10的局域网共享是真操蛋啊,用着好好的,重启了一下,突然就不好用了,其他人无法访问我的电脑,百度了半天也没有找到能解决的访问,文章中说的都试过了,该开启的也都开启了,防火墙也关了,没用,后来想着 ...
- 15.并发容器之ConcurrentLinkedQueue
1.ConcurrentLinkedQueue简介 在单线程编程中我们会经常用到一些集合类,比如ArrayList,HashMap等,但是这些类都不是线程安全的类.在面试中也经常会有一些考点,比如Ar ...
- 【SystemTap】 Linux下安装使用SystemTap源码安装SystemTap
转自 http://blog.csdn.net/zklth/article/details/6248558 文章 http://blog.csdn.net/zklth/archive/2010/09/ ...
- Ansible 手册系列 一(介绍)
一 介绍 Ansible 是一个配置管理和应用部署工具,功能类似于目前业界的配置管理工具 Chef,Puppet,Saltstack.Ansible 是通过 Python 语言开发.Ansible 平 ...
- python __new__和__init__
转载:http://www.cnblogs.com/tuzkee/p/3540293.html 1 2 3 4 5 6 7 8 class A(object): def __init__(se ...
- log4cpp总结
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- eureka-7-多网卡下的ip选择
目前没有需求,后面需要的话,再补充 只是简单使用的话,只需要指定ip即可 eureka.instance.ip-address:127.0.0.1