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. java 连接 redis集群时报错:Could not get a resource from the pool

    由于弄这个的时候浪费了太多的时间,所以才记录下这个错,给大伙参考下 检查了一下,配置啥的都没问题的,但在redis集群机器上就可以,错误如下: Exception in thread "ma ...

  2. windows组策略屏蔽

    点“IP安全策略,在本地机器”——>创建IP安全策略---->下一步---->名称随便写,如输入阻止,然后一直点下一步,出现提示点是,一直到完成,这个时候就创建了一个名为“阻止”的策 ...

  3. 利用Object.defineProperty实现Vue数据双向绑定

    body部分很简单,一个输入框和一个展示的div <div> <p>你好,<input id='nickName'></p> <div id=&q ...

  4. Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 离散化+逆元

    E. Mike and Geometry Problem time limit per test 3 seconds memory limit per test 256 megabytes input ...

  5. linux安装数据库删除

    https://blog.csdn.net/qq_40550973/article/details/80721014 卸载mysql .快速删除 yum remove mysql mysql-serv ...

  6. Mac 终端命令行报错 -bash: vi: command not found

    我遇到的问题与这个类似,但是我的问题也是用该博文作者方法进行中断才解决的,在此表示感谢. 前段时间在 Mac 下使用终端遇到了这个问题: appledeMacBook-Air:~ air$ vi .b ...

  7. 关于java.lang.Exception:No tests found matching的一系列解决方法

    问题描述: java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=yahaa], {ExactMatcher ...

  8. brew || yarn 软件包管理工具

    1.brew || yarn 软件包管理工具

  9. HDU 4669 Mutiples on a circle 不知道该归为哪一类。

    题意:给你N个珠宝和一个K,每个珠宝上面都有数字,这个珠宝做成项链,把珠宝上的数字拼起来如果可以整除掉K,那么久说这个数字为wonderful value,问你有多少种方案可以组成WONDERFUL ...

  10. List根据某个字段(属性)去重

    有时候自带的list.Distinct()去重并不能满足魔门的要求,比如以下情况 如果testList的Name相同则视为重复,则可以如下实现,比写循环语句简洁多了 testList.Where((x ...