TensorFlow 图像预处理(一) 图像编解码,图像尺寸调整
from: https://blog.csdn.net/chaipp0607/article/details/73029923
TensorFlow提供了几类图像处理函数,下面介绍图像的编码与解码,图像尺寸调整。
编码与解码
图像解码与编码:一张RGB三通道的彩色图像可以看成一个三维矩阵,矩阵中的不位置上的数字代表图像的像素值。然后图像在存储时并不是直接记录这些矩阵中的数字,而是经过了压缩编码。所以将一张图像还原成一个三维矩阵的过程就是解码的过程,反之就是编码了。其实如果大家熟悉opencv的话,imread和imwrite就是一个解码和编码的过程。
TensorFlow提供了常用图片格式的解码和编码操作,下面用一个jpg的图像演示:
import matplotlib.pyplot as plt
import tensorflow as tf
image_raw_data = tf.gfile.FastGFile('.//image//1.jpg','rb').read()
with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw_data)
print(img_data.eval())
plt.imshow(img_data.eval())
plt.show()
#img_data = tf.image.convert_image_dtype(img_data,dtype = tf.float32)
encoded_image = tf.image.encode_jpeg(img_data)
with tf.gfile.GFile(".//image//3.jpg","wb") as f:
f.write(encoded_image.eval())
其中:
decode_jpeg函数为jpeg(jpg)图片解码的过程,对应的encode_jpeg函数为编码过程,编码后将图片重命名写入到指定的路径下。
图像尺寸调整
图像尺寸调整属于基础的图像几何变换,TensorFlow提供了几种尺寸调整的函数:
tf.image.resize_images:将原始图像缩放成指定的图像大小,其中的参数method(默认值为ResizeMethod.BILINEAR)提供了四种插值算法,具体解释可以参考图像几何变换(缩放、旋转)中的常用的插值算法
tf.image.resize_image_with_crop_or_pad:剪裁或填充处理,会根据原图像的尺寸和指定的目标图像的尺寸选择剪裁还是填充,如果原图像尺寸大于目标图像尺寸,则在中心位置剪裁,反之则用黑色像素填充。
tf.image.central_crop:比例调整,central_fraction决定了要指定的比例,取值范围为(0,1],该函数会以中心点作为基准,选择整幅图中的指定比例的图像作为新的图像。
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
image_raw_data = tf.gfile.FastGFile('.//image//1.jpg','rb').read()
with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw_data)
plt.imshow(img_data.eval())
plt.show()
resized = tf.image.resize_images(img_data, [100, 100], method=0)
# TensorFlow的函数处理图片后存储的数据是float32格式的,需要转换成uint8才能正确打印图片。
print("Digital type: ", resized.dtype)
resized = np.asarray(resized.eval(), dtype='uint8')
# tf.image.convert_image_dtype(rgb_image, tf.float32)
plt.imshow(resized)
plt.show()
croped = tf.image.resize_image_with_crop_or_pad(img_data, 100, 100)
padded = tf.image.resize_image_with_crop_or_pad(img_data, 500, 500)
plt.imshow(croped.eval())
plt.show()
plt.imshow(padded.eval())
plt.show()
central_cropped = tf.image.central_crop(img_data, 0.5)
plt.imshow(central_cropped.eval())
plt.show()
原图:
resize_images(img_data, [100, 100], method=0):
resize_image_with_crop_or_pad(img_data, 100, 100):
resize_image_with_crop_or_pad(img_data, 500, 500):
central_crop(img_data, 0.5):
另外可以看 http://www.360doc.com/content/17/0513/14/10408243_653519828.shtml
tensorflow里面提供了实现图像进行裁剪和填充的函数,就是tf.image.resize_image_with_crop_or_pad(img,height,width )。img表示需要改变的图像,height是改变后图像的高度,width是宽度。
例如:
- import matplotlib.pyplot as plt;
- import tensorflow as tf;
- image_raw_data_jpg = tf.gfile.FastGFile('11.jpg', 'r').read()
- with tf.Session() as sess:
- img_data_jpg = tf.image.decode_jpeg(image_raw_data_jpg)
- img_data_jpg = tf.image.convert_image_dtype(img_data_jpg, dtype=tf.float32)
- crop = tf.image.resize_image_with_crop_or_pad(img_data_jpg, 500, 500)
- pad = tf.image.resize_image_with_crop_or_pad(img_data_jpg, 2000, 2000)
- plt.figure(1)
- plt.imshow(crop.eval())
- plt.figure(2)
- plt.imshow(pad.eval())
- plt.show()
结果:
TensorFlow 图像预处理(一) 图像编解码,图像尺寸调整的更多相关文章
- 音视频编解码问题:javaCV如何快速进行音频预处理和解复用编解码(基于javaCV-FFMPEG)
前言: 前面我用了很多章实现了javaCV的基本操作,包括:音视频捕捉(摄像头视频捕捉和话筒音频捕捉),推流(本地音视频或者摄像头话筒混合推流到服务器),转流(rtsp->rtmp),收流(录制 ...
- h.264参考图像列表、解码图像缓存
1.参考图像列表(reference picture list) 一般来说,h.264会把需要编码的图像分为三种类型:I.P.B,其中的B.P类型的图像由于采用了帧间编码的这种编码方式,而帧间编码又是 ...
- 『TensorFlow』第九弹_图像预处理_不爱红妆爱武装
部分代码单独测试: 这里实践了图像大小调整的代码,值得注意的是格式问题: 输入输出图像时一定要使用uint8编码, 但是数据处理过程中TF会自动把编码方式调整为float32,所以输入时没问题,输出时 ...
- TensorFlow图像预处理完整样例
参考书 <TensorFlow:实战Google深度学习框架>(第2版) 以下TensorFlow程序完成了从图像片段截取,到图像大小调整再到图像翻转及色彩调整的整个图像预处理过程. #! ...
- 几个平台环境里视频编解码和图像scale的硬件加速的方法
记录一下遇到几个平台里的视频编解码和图像scale的硬件加速的方法 1,intel平台当包含GEN系列的集成GPU时,可用libva实现视频codec.颜色空间转换和图像scale的硬件加速,具体可使 ...
- 玩node-images模块---Node.js轻量级跨平台图像编解码库
Node.js轻量级跨平台图像编解码库 github:https://github.com/zhangyuanwei/node-images Features 功能特性 轻量级:无需安装任何图像处理库 ...
- 基于3U PXIe的ZU7EV图像编解码设计方案
1.板卡简介 基于3U PXIe的ZU7EV图像编码卡用于加固设备的图像接入,编解码采集存储.用于机载.舰载.车载等工作场景,支持工业级温度工作.(此方案是由北京太速设计的,已应用到实际领域) 2.主 ...
- TensorFlow图像预处理-函数
更多的基本的API请参看TensorFlow中文社区:http://www.tensorfly.cn/tfdoc/api_docs/python/array_ops.html 下面是实验的代码,可以参 ...
- 图像预处理第9步:存为.bmp文件
//图像预处理第9步:将最终标准化后的字符图像分为单个单个的HDIB保存,并存为.bmp文件 void CChildView::OnImgprcToDibAndSave() { unsigned ch ...
随机推荐
- SharePreferences使用
获取数据: @SuppressLint("InlinedApi") private String getFromSharePreference(String key) { if ( ...
- angular md-toast 颜色
How to show md-toast with background color https://codepen.io/neilkalman/pen/jWBqve <div ng-contr ...
- hdu 5389 Zero Escape (dp)
题目:http://acm.hdu.edu.cn/showproblem.php? pid=5389 题意:定义数根:①把每一位上的数字加起来得到一个新的数,②反复①直到得到的数仅仅有1位.给定n,A ...
- Seinfeld(杭电3351)
Seinfeld Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- 页面登陆框老是乱乱的?banner跨页图片缩小之后总是在側面不能显示主要部分?哈哈~我来帮你忙~~
有banner背景图片和登陆框的html.css排布 目的:无论页面大小,背景图片都要居中(显示图片中间主要内容,而不是側面的一些东西),登陆框基本能在页面内显示. 盒子的排列应该是这种: <d ...
- Convert Sorted List to Binary Search Tree——将链表转换为平衡二叉搜索树 &&convert-sorted-array-to-binary-search-tree——将数列转换为bst
Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in as ...
- 专訪阿里陶辉:大规模分布式系统、高性能server设计经验分享
http://www.csdn.net/article/2014-06-27/2820432 摘要:先后就职于在国内知名的互联网公司,眼下在阿里云弹性计算部门做架构设计与核心模块代码的编写,主要负责云 ...
- MySQL优化之——日志
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46790451 MYSQL里的日志主要分为4类,使用这些日志文件.能够查看MYSQL ...
- 【每日Scrum】第八天(4.29) TD学生助手Sprint2
站立会议 组员 今天 签到 刘铸辉 (组长) 绩效考核 Y 刘静 测试用例书写 测试bug报告 测试详细报告 Y 解凤娇 Y 王洪叶 项目可行性报告 项目开发计划书 需求分析(已完成并发布) Y 胡宝 ...
- pyqt5 学习总结
关于基类 一般的文件都会基于QWidget,QtWidgets.QMainWindow 或QDialog,like this class Example(QWidget): QWidget类是所有用户 ...