预处理图像

文件名:       cat.jpg

读取、打印图片

import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np image_raw_data = tf.gfile.FastGFile("./cat.jpg",'rb').read() with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw_data) # 输出解码之后的三维矩阵。
#print(img_data.eval()) #print(img_data.get_shape())
img_data.set_shape([1797, 2673, 3])
print(img_data.get_shape()) with tf.Session() as sess:
plt.imshow(img_data.eval())
#print(img_data.get_shape().as_list())
plt.show()

调整图片大小

tf.image.convert_image_dtype  

image_float = tf.image.convert_image_dtype(img_data, tf.float32) 等价于 image_float=tf.cast(img_data, tf.float32)/255
 

tf.image.resize_images 调整图像大小
with tf.Session() as sess:
# 如果直接以0-255范围的整数数据输入resize_images,那么输出将是0-255之间的实数,不利于后续处理。
#如果直接以0-1之间的实数数据输入resize_images,那么输出将是0-1之间的实数。 #建议在调整图片大小前,先将图片转为0-1范围的实数。
#tf.image.convert_image_dtype
#image_float=tf.cast(img_data, tf.float32)/255
image_float = tf.image.convert_image_dtype(img_data, tf.float32) resized = tf.image.resize_images(image_float, [300, 300], method=0)
#print(resized.eval())
plt.imshow(resized.eval())
plt.show()

裁剪和填充图片

tf.image.resize_image_with_crop_or_pad
# 裁剪、填充图像
with tf.Session() as sess:
#tf.image.resize_image_with_crop_or_pad 函数对原图像裁剪或填充。第一个参数为原始图像,后面两个参数为图像裁剪或填充后的大小。
# 如果原始图像的尺寸大于目标图像,则自动截取原图像居中部分;如果原图像的尺寸大于目标图像,则自动在原始图像四周填充0为背景。
croped = tf.image.resize_image_with_crop_or_pad(img_data, 1000, 1000) #截取
padded = tf.image.resize_image_with_crop_or_pad(img_data, 3000, 3000) #填充
#plt.imshow(croped.eval())
#plt.show()
plt.imshow(padded.eval())
plt.show()
通过比例裁剪图像大小
# 通过比例裁剪图像大小
# tf.image.central_crop 第一个参数为原始图像,第二个为调整比例,该比例为 (0,1] 的实数。
with tf.Session() as sess:
central_crop = tf.image.central_crop(img_data, 0.5)
plt.imshow(central_crop.eval())
plt.show()
图像翻转
# 图像翻转
with tf.Session() as sess:
# 上下翻转
flipped1 = tf.image.flip_up_down(img_data)
plt.imshow(flipped1.eval())
plt.show() # 左右翻转
flipped2 = tf.image.flip_left_right(img_data)
plt.imshow(flipped2.eval())
plt.show() #对角线翻转
transposed = tf.image.transpose_image(img_data)
plt.imshow(transposed.eval())
plt.show() # 以一定概率上下翻转图片。
# 以50%概率上下翻转图片
flipped1 = tf.image.random_flip_up_down(img_data)
plt.imshow(flipped1.eval())
plt.show() # 以一定概率左右翻转图片。
# 以50%概率左右翻转图片
flipped2 = tf.image.random_flip_left_right(img_data)
plt.imshow(flipped2.eval())
plt.show()
图像色彩调整
# 图像色彩调整
with tf.Session() as sess:
# 在进行一系列图片调整前,先将图片转换为实数形式,有利于保持计算精度。
image_float = tf.image.convert_image_dtype(img_data, tf.float32)


# 亮度调整##############################
# 将图片的亮度+0.5。
#adjusted = tf.image.adjust_brightness(image_float, 0.5) # 将图片的亮度-0.5
#adjusted = tf.image.adjust_brightness(image_float, -0.5) # 随机亮度调整
# 在[-max_delta, max_delta)的范围随机调整图片的亮度。
#adjusted = tf.image.random_brightness(image_float, max_delta=0.5)

# 对比度调整##############################
# (x-mean)*delta+mean
# 将图片的对比度+5
#adjusted = tf.image.adjust_contrast(image_float, 5) # 将图片的对比度-0.5
#adjusted = tf.image.adjust_contrast(image_float, -0.5) # 在[lower, upper]的范围随机调整图的对比度。
# upper >= lower >= 0
lower=0.5
upper=5
#adjusted = tf.image.random_contrast(image_float, lower, upper)

# 色相调整##############################
# delta 范围:[-1, +1]
#adjusted = tf.image.adjust_hue(image_float, -0.1)
#adjusted = tf.image.adjust_hue(image_float, -0.3)
#adjusted = tf.image.adjust_hue(image_float, -0.6)
#adjusted = tf.image.adjust_hue(image_float, -0.9)
#adjusted = tf.image.adjust_hue(image_float, 0.1)
#adjusted = tf.image.adjust_hue(image_float, 0.3)
#adjusted = tf.image.adjust_hue(image_float, 0.6)
#adjusted = tf.image.adjust_hue(image_float, 0.9) # 在[-max_delta, max_delta]的范围随机调整图片的色相。max_delta的取值在[0, 0.5]之间。
max_delta=0.3
#adjusted = tf.image.random_hue(image_float, max_delta)

# 饱和度调整##############################
# 将图片的饱和度-5。
#adjusted = tf.image.adjust_saturation(image_float, -5)
# 将图片的饱和度+5。
#adjusted = tf.image.adjust_saturation(image_float, 5)
# 在[lower, upper]的范围随机调整图的饱和度。
lower=0 # lower>=0
upper=5
#adjusted = tf.image.random_saturation(image_float, lower, upper)

# 将代表一张图片的三维矩阵中的数字均值变为0,方差变为1。
adjusted = tf.image.per_image_standardization(image_float)

# 在最终输出前,将实数取值截取到0-1范围内。
adjusted = tf.clip_by_value(adjusted, 0.0, 1.0)
plt.imshow(adjusted.eval())
plt.show()

TensorFlow图像预处理函数的更多相关文章

  1. TensorFlow图像预处理-函数

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

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

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

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

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

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

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

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

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

  6. python+opencv 图像预处理

    一 python 生成随机字符串序列+ 写入到图片上 from PIL import Image,ImageDraw,ImageFont import numpy as np import rando ...

  7. 基于OpenCV的火焰检测(一)——图像预处理

    博主最近在做一个基于OpenCV的火焰检测的项目,不仅可以检测图片中的火焰,还可以检测视频中的火焰,最后在视频检测的基础上推广到摄像头实时检测.在做这个项目的时候,博主参考了很多相关的文献,用了很多种 ...

  8. [opencv]图像预处理方案及方式

    像识别中,图像质量的好坏直接影响识别算法的设计与效果精度,那么除了能在算法上的优化外,预处理技术在整个项目中占有很重要的因素,然而人们往往忽略这一点. 图像预处理,将每一个文字图像分检出来交给识别模块 ...

  9. 深度学习03-(图像梯度处理、图像轮廓、图像预处理在AI中的应用)

    深度学习03-计算机视觉基本理论2 深度学习03-(计算机视觉基本理论2) 图像梯度处理 什么是图像梯度 模板运算 均值滤波 高斯滤波 中值滤波 边沿检测 锐化 图像轮廓 什么是图像轮廓 查找和绘制轮 ...

  10. 图像预处理第9步:存为.bmp文件

    //图像预处理第9步:将最终标准化后的字符图像分为单个单个的HDIB保存,并存为.bmp文件 void CChildView::OnImgprcToDibAndSave() { unsigned ch ...

随机推荐

  1. CICD介绍

    1.学习背景 当公司的服务器架构越来越复杂,需要频繁的发布新配置文件,以及新代码: 但是如果机器部署数量较多,发布的效率必然很低: 并且如果代码没有经过测试环境,预生产环境层层测试,最终才到生产环境, ...

  2. 2020年你不可不知的自动化框架,可替代Selenuim的测试框架Top10

    Selenium是一种开源自动测试工具.它可以跨不同的浏览器和平台在Web应用程序上执行功能,回归,负载测试.Slenium是最好的工具之一,但确实有一些缺点. 业界有一些强大的工具可以替代Selen ...

  3. 前端使用 Konva 实现可视化设计器(15)- 自定义连接点、连接优化

    前面,本示例实现了折线连接线,简述了实现的思路和原理,也已知了一些缺陷.本章将处理一些缺陷的同时,实现支持连接点的自定义,一个节点可以定义多个连接点,最终可以满足类似图元接线的效果. 请大家动动小手, ...

  4. 前端使用 Konva 实现可视化设计器(16)- 旋转对齐、触摸板操作的优化

    这一章解决两个缺陷,一是调整一些快捷键,使得 Mac 触摸板可以正常操作:二是修复一个 Issue,使得即使素材节点即使被旋转之后,也能正常触发磁贴对齐效果,有个小坑需要注意. 请大家动动小手,给我一 ...

  5. docker制作springboot镜像

    以下步骤在具有Docker环境的Linux机器上操作. 把springboot-1.0.0.jar放到/usr/local/springboot目录下,并在该目录下创建Dockerfile文件,内容为 ...

  6. 算法金 | 一个强大的算法模型:t-SNE !!

    大侠幸会,在下全网同名「算法金」 0 基础转 AI 上岸,多个算法赛 Top 「日更万日,让更多人享受智能乐趣」 t-SNE(t-Distributed Stochastic Neighbor Emb ...

  7. golang模拟键盘输入字符串

    介绍 仅供学习使用哈,不要用来开gua. 代码仓库:https://github.com/GuoFlight/gkeybd (本人仓库,欢迎留言) 注意事项 只支持英文 使用前请切换到英文输入法.因为 ...

  8. Java解析微信获取手机号信息

    在微信中,用户手机号的获取通常是通过微信小程序的getPhoneNumber接口来实现的.这个接口允许用户在授权后,将加密的手机号数据传递给开发者.由于隐私保护,微信不会直接提供用户的明文手机号,而是 ...

  9. QT学习:10 IO类

    --- title: framework-cpp-qt-10-IO类 EntryName: framework-cpp-qt-10-QIODevice date: 2020-04-17 10:24:0 ...

  10. python基础-字符串str " "

    字符串的定义和操作 字符串的特性: 元素数量 支持多个 元素类型 仅字符 下标索引 支持 重复元素 支持 可修改性 不支持 数据有序 是 使用场景 一串字符的记录场景 字符串的相关操作: my_str ...