import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt #读取图片
image_raw_data = tf.gfile.FastGFile("F:\\TensorFlowGoogle\\201806-github\\datasets\\cat.jpg",'rb').read() with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw_data)
# 输出解码之后的三维矩阵。
print(img_data.eval())
img_data.set_shape([1797, 2673, 3])
print(img_data.get_shape())

#打印图片
with tf.Session() as sess:
plt.imshow(img_data.eval())
plt.show()

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

#裁剪和填充图片
with tf.Session() as sess:
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()

#截取中间50%的图片
with tf.Session() as sess:
central_cropped = tf.image.central_crop(img_data, 0.5)
plt.imshow(central_cropped.eval())
plt.show()

#翻转图片
with tf.Session() as sess:
# 上下翻转
#flipped1 = tf.image.flip_up_down(img_data)
# 左右翻转
#flipped2 = tf.image.flip_left_right(img_data) #对角线翻转
transposed = tf.image.transpose_image(img_data)
plt.imshow(transposed.eval())
plt.show() # 以一定概率上下翻转图片。
#flipped = tf.image.random_flip_up_down(img_data)
# 以一定概率左右翻转图片。
#flipped = tf.image.random_flip_left_right(img_data)

#图片色彩调整
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) # 将图片的对比度-5
#adjusted = tf.image.adjust_contrast(image_float, -5) # 将图片的对比度+5
#adjusted = tf.image.adjust_contrast(image_float, 5) # 在[lower, upper]的范围随机调整图的对比度。
#adjusted = tf.image.random_contrast(image_float, lower, upper) # 在最终输出前,将实数取值截取到0-1范围内。
adjusted = tf.clip_by_value(adjusted, 0.0, 1.0)
plt.imshow(adjusted.eval())
plt.show()

#添加色相和饱和度
with tf.Session() as sess:
# 在进行一系列图片调整前,先将图片转换为实数形式,有利于保持计算精度。
image_float = tf.image.convert_image_dtype(img_data, tf.float32) 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]之间。
#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]的范围随机调整图的饱和度。
#adjusted = tf.image.random_saturation(image_float, lower, upper) # 将代表一张图片的三维矩阵中的数字均值变为0,方差变为1。
#adjusted = tf.image.per_image_whitening(image_float) # 在最终输出前,将实数取值截取到0-1范围内。
adjusted = tf.clip_by_value(adjusted, 0.0, 1.0)
plt.imshow(adjusted.eval())
plt.show()

#添加标注框并裁减。
with tf.Session() as sess:
boxes = tf.constant([[[0.05, 0.05, 0.9, 0.7], [0.35, 0.47, 0.5, 0.56]]]) # sample_distorted_bounding_box要求输入图片必须是实数类型。
image_float = tf.image.convert_image_dtype(img_data, tf.float32) begin, size, bbox_for_draw = tf.image.sample_distorted_bounding_box(tf.shape(image_float), bounding_boxes=boxes, min_object_covered=0.4) # 截取后的图片
distorted_image = tf.slice(image_float, begin, size)
plt.imshow(distorted_image.eval())
plt.show() # 在原图上用标注框画出截取的范围。由于原图的分辨率较大(2673x1797),生成的标注框
# 在Jupyter Notebook上通常因边框过细而无法分辨,这里为了演示方便先缩小分辨率。
image_small = tf.image.resize_images(image_float, [180, 267], method=0)
batchced_img = tf.expand_dims(image_small, 0)
image_with_box = tf.image.draw_bounding_boxes(batchced_img, bbox_for_draw)
print(bbox_for_draw.eval())
plt.imshow(image_with_box[0].eval())
plt.show()

吴裕雄--天生自然 pythonTensorFlow图形数据处理:TensorFlow图像处理函数的更多相关文章

  1. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:windows操作系统安装指定版本的tensorflow

    pip install tensorflow==1.14.0

  2. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:windows操作系统删除tensorflow

    输入:pip uninstall tensorflow Proceed(y/n):y

  3. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:解决module 'tensorflow' has no attribute 'Session'

    原因:因为是tensorflow 2.0版本

  4. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:循环神经网络预测正弦函数

    import numpy as np import tensorflow as tf import matplotlib.pyplot as plt # 定义RNN的参数. HIDDEN_SIZE = ...

  5. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:数据集高层操作

    import tempfile import tensorflow as tf # 1. 列举输入文件. # 输入数据生成的训练和测试数据. train_files = tf.train.match_ ...

  6. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:数据集基本使用方法

    import tempfile import tensorflow as tf # 1. 从数组创建数据集. input_data = [1, 2, 3, 5, 8] dataset = tf.dat ...

  7. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:输入数据处理框架

    import tensorflow as tf # 1. 创建文件列表,通过文件列表创建输入文件队列 files = tf.train.match_filenames_once("F:\\o ...

  8. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:输入文件队列

    import tensorflow as tf # 1. 生成文件存储样例数据. def _int64_feature(value): return tf.train.Feature(int64_li ...

  9. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:多线程队列操作

    import tensorflow as tf #1. 定义队列及其操作. queue = tf.FIFOQueue(100,"float") enqueue_op = queue ...

随机推荐

  1. 从谷歌Pixel3不堆硬件看智能手机下一个十年将被AI制霸

    别看现在的智能手机行业热闹异常--厂商混战.新品频出.噱头涌现,但能引领手机行业发展趋势的依旧是苹果和谷歌.如果说苹果的iPhone树立了一个个智能手机行业进化的标杆,那么谷歌其实就是在为安卓手机的发 ...

  2. Python 自省指南

    原作者:Patrick K. O'Brien 什么是自省? 在日常生活中,自省(introspection)是一种自我检查行为.自省是指对某人自身思想.情绪.动机和行为的检查.伟大的哲学家苏格拉底将生 ...

  3. JDK8中的新特性

    1.lambda表达式 1.定义 Java 8 发布的最重要新特性.Lambda 允许把函数作为一个方法的参数(函数作为参数传递进方法中),可以推导出来的就可以省略了,Lambda 表达式免去了使用匿 ...

  4. iOS基础——通过案例学知识之xib、plist、mvc

    透过案例学习xib的使用.plist的使用.mvc在iOS的使用,今天要做的案例效果图 1.xib和nib xib文件可以被XCode编译成nib文件,xib文件本质上是一个xml文件,而nib文件就 ...

  5. Tomcat启动失败原因: More than one fragment with the name [spring_web] was found. 解决

    将一个eclipse上搭建好的项目移到idea开发时遇到的问题,tomcat启动时报了3个错误 -Nov- :: ms -Nov- ::)-127.0.0.1] org.apache.tomcat.u ...

  6. Microsoft SQL server Management Studio工具报错“应用程序的组件中发生了无法处理的异常”

    解决办法 打开目录: C:\Documents and Settings\Administrator\Application Data\Microsoft\Microsoft SQL Server\1 ...

  7. github新建一个单页

    比如可以在github上打开的网页是这种网址形式的:https://01xunsicheng.github.io/yumeihua/ 1.登录后首页找到 New repository 2.新建一个文件 ...

  8. String,StringBuffer与StringBuilder的区别与选择

    三者的区别 String:不可变类,一旦一个对象被建立的时候,包含在这个对象中的字符串序列是不可变的,直到这个对象被销毁.StringBuffer:可变字符序列的字符串.当其对象被创建的时候,可以用a ...

  9. Python笔记_第四篇_高阶编程_实例化方法、静态方法、类方法和属性方法概念的解析。

    1.先叙述静态方法: 我们知道Python调用类的方法的时候都要进行一个实例化的处理.在面向对象中,一把存在静态类,静态方法,动态类.动态方法等乱七八糟的这么一些叫法.其实这些东西看起来抽象,但是很好 ...

  10. Python笔记_第四篇_高阶编程_检测_2.对类进行单元检测

    1. 对类进行单元检测: 第一步:首先编写一个类: # 类名Person,person.py class Person(object): def __init__(self,name,age): se ...