TensorFlow图像预处理-函数
更多的基本的API请参看TensorFlow中文社区:http://www.tensorfly.cn/tfdoc/api_docs/python/array_ops.html
下面是实验的代码,可以参考,对应的图片是输出的结果:
import tensorflow as tf
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
%matplotlib inline
path = '/home/ubuntu-mm/TensorFlow/Learning/D-TestJupyter/images/Train/Dogs.jpg'
file_queue = tf.train.string_input_producer([path])
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) #启动线程运行队列
sess.run(image)
coord.request_stop() #停止所有的线程
coord.join(threads)
image_uint8 = tf.image.convert_image_dtype(image, dtype = tf.uint8)
plt.figure(1)
plt.imshow(image_uint8.eval())
print image_uint8.get_shape()
resize_image1 = tf.image.resize_images(image_uint8,[400,300],method=tf.image.ResizeMethod.NEAREST_NEIGHBOR) #修改图片的尺寸
resize_image2 = tf.image.resize_images(image_uint8,[400,300],method=1) #修改图片的尺寸 1代表的就是NEAREST_NEIGHBOR的方法
central_crop = tf.image.central_crop(image_uint8, 0.6) #从图片中心开始裁剪图片,裁剪比例为60%
bounding_crop = tf.image.crop_to_bounding_box(resize_image1, offset_height=100, offset_width=100, target_height=100, target_width=100) #设定裁剪的起始位置和终止位置进行裁剪
pad = tf.image.pad_to_bounding_box(bounding_crop, offset_height=0, offset_width=0, target_height=105, target_width=105) #设定边缘对图像的边缘进行填充(填0)
flip1 = tf.image.flip_left_right(resize_image1) #左右翻转图片
flip2 = tf.image.flip_up_down(flip1) #上下翻转图片
adjust_brightness = tf.image.adjust_brightness(resize_image1, 0.2) #调节图像的亮度为原来的20%
adjust_saturation = tf.image.adjust_saturation(resize_image1, 0.4) #调节图像的饱和度为原来的40%
adjust_hue = tf.image.adjust_hue(resize_image1, 0.7) #调节原来的H(灰度)为原来的70%
image_float = tf.cast(resize_image1, dtype=tf.float32)
gray = tf.image.rgb_to_grayscale(image_float) #对图像的类型进行转换rgb-grayscale
hsv = tf.image.rgb_to_hsv(image_float) #对图像进行hsv转换rgb-hsv
imag_gray = tf.image.convert_image_dtype(gray, tf.uint8)
imag_hsv = tf.image.convert_image_dtype(hsv, tf.uint8)
sess.run([flip1, flip2])
plt.figure(2)
plt.imshow(resize_image1.eval())
plt.figure(3)
plt.imshow(resize_image2.eval())
plt.figure(4)
plt.imshow(central_crop.eval())
plt.figure(5)
plt.imshow(bounding_crop.eval())
plt.figure(6)
plt.imshow(pad.eval())
plt.figure(7)
plt.imshow(flip2.eval())
plt.figure(8)
plt.imshow(adjust_brightness.eval())
plt.figure(9)
plt.imshow(adjust_saturation.eval())
plt.figure(10)
plt.imshow(adjust_hue.eval())
plt.figure(1)
plt.imshow(imag_hsv.eval(), cmap=cm.hsv)
原图 改变尺寸 改变尺寸 图像中心裁剪
图像边缘裁剪 图像边缘补0 图像水平垂直翻转
图像亮度度调节 图像饱和度变换 图像弧度调节H 图像HSV显示
相关函数介绍:
1、tf.image.resize_images(image_uint8,[400,300],method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
函数的作用是修改图像的尺寸(缩放放大的形式),第一个参数是原始图像,第二个参数是输出图像的大小,第三个参数是缩放或放大的方法。
2、tf.image.central_crop(image_uint8, 0.6)
函数的功能是裁剪图像,裁剪的中心式图像的中心位置,第一个参数是原始图像,第二个参数是裁剪的比例。
3、tf.image.crop_to_bounding_box(resize_image1, offset_height=100, offset_width=100, target_height=100, target_width=100)
函数的功能是按照输入的参数的边缘来裁剪图像,第一个参数是原始的图像,第二个参数是裁剪的y轴起始位置,第三个是x轴起始位置,第4个参数和第5个参数是输出图像的尺寸大小。
4、tf.image.pad_to_bounding_box(bounding_crop, offset_height=0, offset_width=0, target_height=105, target_width=105)
函数的功能是扩充图像的边缘,对图像的边缘进行补零的操作,第一个参数是原始图像,第二个参数和第三个参数是输出图像在原图上的起始位置,第4和5个参数是输出图像的大小,当输出图像超出了原始图像的大小时,就会将超出的部分进行补零的操作。
5、tf.image.flip_left_right(resize_image1)
函数的功能是对图像进行水平方向的反转,参数1是原始图像。
6、tf.image.flip_up_down(flip1)
函数的功能是对图像进行垂直方向的反转,参数1是原始图像。
7、tf.image.adjust_brightness(resize_image1, 0.2)
函数的功能是调节原始图像的亮度值,第一个参数是原始图像,第二个参数是调节的比例。
8、tf.image.adjust_saturation(resize_image1, 0.4)
函数的功能是调节原始图像的饱和度,第一个参数是原始图像,第二个参数是调节的比例。
9、tf.image.adjust_hue(resize_image1, 0.7)
函数的功能是调节图像的灰度值(Hue),参数1是原始图像,参数2是调节的比例。
10、tf.image.rgb_to_grayscale(image_float)
函数的功能是间输入的rgb格式的图像转换成grayscale的灰度图像,参数1是输入的原始图像。(注意输入图像的格式需要时浮点形式的float)
11、tf.image.rgb_to_hsv(image_float)
函数的功能是间输入的图像转换成为hsv格式的图像,参数1是输入图像,输入的格式需要时浮点型的。
完!
TensorFlow图像预处理-函数的更多相关文章
- TensorFlow图像预处理完整样例
参考书 <TensorFlow:实战Google深度学习框架>(第2版) 以下TensorFlow程序完成了从图像片段截取,到图像大小调整再到图像翻转及色彩调整的整个图像预处理过程. #! ...
- TensorFlow 图像预处理(一) 图像编解码,图像尺寸调整
from: https://blog.csdn.net/chaipp0607/article/details/73029923 TensorFlow提供了几类图像处理函数,下面介绍图像的编码与解码,图 ...
- 吴裕雄 python 神经网络——TensorFlow 图像预处理完整样例
import numpy as np import tensorflow as tf import matplotlib.pyplot as plt def distort_color(image, ...
- 『TensorFlow』第九弹_图像预处理_不爱红妆爱武装
部分代码单独测试: 这里实践了图像大小调整的代码,值得注意的是格式问题: 输入输出图像时一定要使用uint8编码, 但是数据处理过程中TF会自动把编码方式调整为float32,所以输入时没问题,输出时 ...
- python+opencv 图像预处理
一 python 生成随机字符串序列+ 写入到图片上 from PIL import Image,ImageDraw,ImageFont import numpy as np import rando ...
- 基于OpenCV的火焰检测(一)——图像预处理
博主最近在做一个基于OpenCV的火焰检测的项目,不仅可以检测图片中的火焰,还可以检测视频中的火焰,最后在视频检测的基础上推广到摄像头实时检测.在做这个项目的时候,博主参考了很多相关的文献,用了很多种 ...
- [opencv]图像预处理方案及方式
像识别中,图像质量的好坏直接影响识别算法的设计与效果精度,那么除了能在算法上的优化外,预处理技术在整个项目中占有很重要的因素,然而人们往往忽略这一点. 图像预处理,将每一个文字图像分检出来交给识别模块 ...
- 图像预处理第9步:存为.bmp文件
//图像预处理第9步:将最终标准化后的字符图像分为单个单个的HDIB保存,并存为.bmp文件 void CChildView::OnImgprcToDibAndSave() { unsigned ch ...
- opencv 图像修复函数
void cv::inpaint( const Mat& src, const Mat& mask, Mat& dst, double inpaintRange, int fl ...
随机推荐
- C# 防止content-type修改后上传恶意文件
以图片为例子.在上传图片的时候,使用Fiddler抓取 通过js判断文件类型是不安全的,所以通过后台来判断,代码如下: ) { HttpPostedFile file0 = Request.Files ...
- Python-面向对象(组合、封装与多态)
一.组合 什么是组合? 就是一个类的属性 的类型 是另一个自定义类的 类型,也可以说是某一个对象拥有一个属性,该属性的值是另一个类的对象. 通过为某一个对象添加属性(这里的属性是另一个类的对象)的方式 ...
- hive学习02-累加
求出当月的访问次数,截至当月前的每个月最大访问次数.截至当月前每个用户总的访问次数. 数据表如下 A,-, A,-, B,-, A,-, B,-, A,-, A,-, A,-, B,-, B,-, A ...
- 对mysql数据库中字段为空的处理
数据库中字段为空的有两种:一种为null,另一种为空字符串.null代表数值未知,空字符串是有值得,只是为空.有时间我们想把数据库中的数据以excel形式导出时 如果碰到字段为空的,为空的字段会被后面 ...
- hexo d 部署博客时出错
问题描述: // 第一次遇到的问题 Error: packet_write_wait: Connection to 192.30.253.113 port 22: Broken pipe packet ...
- automaticallyAdjustsScrollViewInsets 详解
automaticallyAdjustsScrollViewInsets 自动缩进 20 像素 默认是 True 项目中如果有UIViewController 和ScrollView 一般都要设置成f ...
- 深入分析Zookeeper的实现原理
zookeeper 的由来 分布式系统的很多难题,都是由于缺少协调机制造成的.在分布式协调这块做得比较好的,有 Google 的 Chubby 以及 Apache 的 Zookeeper.Google ...
- 操作dom获取datatable中的某一行的某一列的数据
需求描述:编辑的时候,点击的那一行,进入后台的验证方法,验证通过后,再进入编辑页面,进入的时候需要把本行<tr>数据中的某一列<td>的值传递过去 思路表述:之前我想的是,给列 ...
- MySQL----数据库练习
一.多对多的正反向查询 class Class(models.Model): name = models.CharField(max_length=32,verbose_name="班级名& ...
- Java File mkdir() mkdirs()
使用mkdir()和mkdirs()创建文件夹的区别. 1.mkdir() 如果父目录不存在,则创建失败. 2.mkdirs() 如果父目录不存在,连同父目录一起创建. 注意,在IO_Study01文 ...