import tensorflow as tf
import matplotlib.pyplot as plt image_raw_data = tf.gfile.GFile("./picture.jpg", "rb").read()
with tf.Session() as sess:
"""
图像编码解码处理
"""
# 解码过程
img_data = tf.image.decode_jpeg(image_raw_data)
print(img_data.eval()) # 编码过程
encoded_image = tf.image.encode_jpeg(img_data)
with tf.gfile.GFile('./output.jpg', 'wb') as f:
f.write(encoded_image.eval()) """
图像大小调整
"""
# 将图片数据转化为实数类型,将0-255的像素值转化为0.0-1.0之间的实数
img_data = tf.image.convert_image_dtype(img_data, dtype=tf.float32) # 第一个参数:原始图像;第二个参数:调整后的图像大小;第三个参数:method参数给出调整图像大小的算法
resized = tf.image.resize_images(img_data, [300, 300], method=0) # 目标尺寸小于原始尺寸,居中截取;否者填充
croped = tf.image.resize_image_with_crop_or_pad(img_data, 500, 300) plt.figure()
plt.subplot(2, 2, 1)
plt.imshow(img_data.eval())
plt.subplot(2, 2, 2)
plt.imshow(resized.eval())
plt.subplot(2, 1, 2)
plt.imshow(croped.eval()) """
图像翻转
"""
# 上下翻转
flipped_up_down = tf.image.flip_up_down(img_data)
# 50%的概率上下翻转
flipped1 = tf.image.random_flip_up_down(img_data) # 左右翻转
flipped_left_right = tf.image.flip_left_right(img_data)
# 50%的概率左右翻转
flipped2 = tf.image.random_flip_left_right(img_data) # 沿对角线高翻转
transpose_image = tf.image.transpose_image(img_data) plt.figure()
plt.subplot(2, 2, 1)
plt.imshow(flipped_up_down.eval())
plt.subplot(2, 2, 2)
plt.imshow(flipped_left_right.eval())
plt.subplot(2, 2, 3)
plt.imshow(transpose_image.eval())
plt.subplot(2, 2, 4)
plt.imshow(flipped2.eval()) """
图像色彩调整
"""
# 亮度 -0.5
adjusted = tf.image.adjust_brightness(img_data, -0.5)
# 将其值截取在0.0-1.0之间,防止像素实数值超出0.0-1.0的范围
adjusted_down = tf.clip_by_value(adjusted, 0.0, 1.0)
# 亮度 +0.5
adjusted_up = tf.image.adjust_brightness(img_data, 0.5)
# 在[-max_delta, max_delta]的范围之间随机调整图像亮度
adjusted_random = tf.image.random_brightness(img_data, 0.2) # 对比度 x0.5
adjusted1 = tf.image.adjust_contrast(img_data, 0.5)
# 对比度 增加5倍
adjusted2 = tf.image.adjust_contrast(img_data, 5)
# 在[lower, upper]的范围内随机调整图像的对比度
adjusted3 = tf.image.random_contrast(img_data, 2, 4) # 色相 +0.6
adjusted_hue1 = tf.image.adjust_hue(img_data, 0.6)
# 色相 -0.6
adjusted_hue2 = tf.image.adjust_hue(img_data, -0.6)
# 在[-max_delta, max_delta]的范围内随机调整图像的色相,max_delta取值范围[0, 0.5]
adjusted_hue3 = tf.image.random_hue(img_data, 0.3) # 饱和度 -5
adjust_saturation1 = tf.image.adjust_saturation(img_data, -5)
# 饱和度 +5
adjust_saturation2 = tf.image.adjust_saturation(img_data, 5)
# 在[lower, upper]的范围之间随机调整图像的饱和度,lower必须是非负值
adjust_saturation3 = tf.image.random_saturation(img_data, 0, 4)
plt.figure()
plt.subplot(4, 2, 1)
plt.imshow(adjusted_down.eval())
plt.subplot(4, 2, 2)
plt.imshow(adjusted_up.eval())
plt.subplot(4, 2, 3)
plt.imshow(adjusted1.eval())
plt.subplot(4, 2, 4)
plt.imshow(adjusted2.eval())
plt.subplot(4, 2, 5)
plt.imshow(adjusted_hue1.eval())
plt.subplot(4, 2, 6)
plt.imshow(adjusted_hue2.eval())
plt.subplot(4, 2, 7)
plt.imshow(adjust_saturation1.eval())
plt.subplot(4, 2, 8)
plt.imshow(adjust_saturation2.eval()) # 图像数值标准化,均值为0,方差为1
adjust_standardization = tf.image.per_image_standardization(img_data)
plt.figure()
plt.imshow(adjust_standardization.eval()) plt.show()

TensorFlow学习之 图像预处理的更多相关文章

  1. tensorflow学习笔记——图像数据处理

    喜欢摄影的盆友都知道图像的亮度,对比度等属性对图像的影响是非常大的,相同物体在不同亮度,对比度下差别非常大.然而在很多图像识别问题中,这些因素都不应该影响最后的结果.所以本文将学习如何对图像数据进行预 ...

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

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

  3. 『TensorFlow』第九弹_图像预处理_不爱红妆爱武装

    部分代码单独测试: 这里实践了图像大小调整的代码,值得注意的是格式问题: 输入输出图像时一定要使用uint8编码, 但是数据处理过程中TF会自动把编码方式调整为float32,所以输入时没问题,输出时 ...

  4. TensorFlow学习笔记(五)图像数据处理

    目录: 一.TFRecord输入数据格式 1.1 TFrecord格式介绍 1.2 TFRecord样例程序 二.图像数据处理 2.1TensorFlow图像处理函数 2.2图像预处理完整样例 三.多 ...

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

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

  6. tensorflow学习笔记——多线程输入数据处理框架

    之前我们学习使用TensorFlow对图像数据进行预处理的方法.虽然使用这些图像数据预处理的方法可以减少无关因素对图像识别模型效果的影响,但这些复杂的预处理过程也会减慢整个训练过程.为了避免图像预处理 ...

  7. TensorFlow学习笔记——LeNet-5(训练自己的数据集)

    在之前的TensorFlow学习笔记——图像识别与卷积神经网络(链接:请点击我)中了解了一下经典的卷积神经网络模型LeNet模型.那其实之前学习了别人的代码实现了LeNet网络对MNIST数据集的训练 ...

  8. tensorflow学习笔记——使用TensorFlow操作MNIST数据(1)

    续集请点击我:tensorflow学习笔记——使用TensorFlow操作MNIST数据(2) 本节开始学习使用tensorflow教程,当然从最简单的MNIST开始.这怎么说呢,就好比编程入门有He ...

  9. Tensorflow学习笔记No.5

    tf.data卷积神经网络综合应用实例 使用tf.data建立自己的数据集,并使用CNN卷积神经网络实现对卫星图像的二分类问题. 数据下载链接:https://pan.baidu.com/s/141z ...

随机推荐

  1. Spring中三个重要概念 IOC AOP Bean

    Spring中三个重要概念 IOC AOP Bean 首先讲解一下Spring框架,以及为什么要使用Spring 框架? spring 是一个很好的容器框架, 是轻量级的IoC和AOP的容器框架,主要 ...

  2. rgbdslam 源代码的实现

    经过一番努力,终于跑通了felix.endres的rgbd slam v2 源码,中间遇到挺多问题.总结如下: (1) 关于SiftGPU问题:ERROR: SiftGPU cannot be com ...

  3. 2019-1-19 object祖宗类的equals重写

    package com.test; /** * object祖宗类的equals重写 * @author Mr.kemi *2019-1-19 */ public class Equals { pri ...

  4. Groovy中String转换Gstring用于动态插值

    知识点是Groovy中的模板引擎 GStringTemplateEngine 第一个例子: def binding = [ firstname : "Grace", lastnam ...

  5. js数组的forEach方法能不能修改数组的值

    如果要使用数组的forEach()方法对其改值时,需要直接通过arr[i]这种方式来更改. 请看下面代码: // 数组改值 let arr = [1,3,5,7,9]; arr.forEach(fun ...

  6. dsu on tree(CF600E Lomsat gelral)

    题意 一棵树有n个结点,每个结点都是一种颜色,每个颜色有一个编号,求树中每个子树的最多的颜色编号的和. dsu on tree 用来解决子树问题 好像不能带修改?? 暴力做这个题,就是每次扫一遍子树统 ...

  7. 使用SVG绘制流程图

    本篇主要记录流程图的实现过程中的难点和核心技术点,先上效果图: 节点可以任意拖拽,曲线跟随变化 正在连接的线 1.节点实现 流程图是基于SVG绘制的,节点主要利用 g 和 foreignObject的 ...

  8. 重温C语言(1)----计算算术表达式的值

    <C程序设计语言>练习题 5-10 编写程序 expr,计算从命令行输入的逆波兰表达式的值,其中每个运算符或操作数用一个单独的参数表示.例如,命令 expr 2 3 4 + * 计算表达式 ...

  9. Python中os.listdir的排序问题

    上周应别人要求,使用python批量修改文件名称.文件名有规律,当时就用了一个函数直接精确的用文件名替换了.后来想直接可以用listdir来遍历每个文件来修改更加通用一些.但是看了os.listdir ...

  10. Python with VS Code

    1. 基本的代码结构为: 2.