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是宽度。

例如:

  1. import matplotlib.pyplot as plt;
  2. import tensorflow as tf;
  3. image_raw_data_jpg = tf.gfile.FastGFile('11.jpg', 'r').read()
  4. with tf.Session() as sess:
  5. img_data_jpg = tf.image.decode_jpeg(image_raw_data_jpg)
  6. img_data_jpg = tf.image.convert_image_dtype(img_data_jpg, dtype=tf.float32)
  7. crop = tf.image.resize_image_with_crop_or_pad(img_data_jpg, 500, 500)
  8. pad = tf.image.resize_image_with_crop_or_pad(img_data_jpg, 2000, 2000)
  9. plt.figure(1)
  10. plt.imshow(crop.eval())
  11. plt.figure(2)
  12. plt.imshow(pad.eval())
  13. plt.show()

结果:

TensorFlow 图像预处理(一) 图像编解码,图像尺寸调整的更多相关文章

  1. 音视频编解码问题:javaCV如何快速进行音频预处理和解复用编解码(基于javaCV-FFMPEG)

    前言: 前面我用了很多章实现了javaCV的基本操作,包括:音视频捕捉(摄像头视频捕捉和话筒音频捕捉),推流(本地音视频或者摄像头话筒混合推流到服务器),转流(rtsp->rtmp),收流(录制 ...

  2. h.264参考图像列表、解码图像缓存

    1.参考图像列表(reference picture list) 一般来说,h.264会把需要编码的图像分为三种类型:I.P.B,其中的B.P类型的图像由于采用了帧间编码的这种编码方式,而帧间编码又是 ...

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

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

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

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

  5. 几个平台环境里视频编解码和图像scale的硬件加速的方法

    记录一下遇到几个平台里的视频编解码和图像scale的硬件加速的方法 1,intel平台当包含GEN系列的集成GPU时,可用libva实现视频codec.颜色空间转换和图像scale的硬件加速,具体可使 ...

  6. 玩node-images模块---Node.js轻量级跨平台图像编解码库

    Node.js轻量级跨平台图像编解码库 github:https://github.com/zhangyuanwei/node-images Features 功能特性 轻量级:无需安装任何图像处理库 ...

  7. 基于3U PXIe的ZU7EV图像编解码设计方案

    1.板卡简介 基于3U PXIe的ZU7EV图像编码卡用于加固设备的图像接入,编解码采集存储.用于机载.舰载.车载等工作场景,支持工业级温度工作.(此方案是由北京太速设计的,已应用到实际领域) 2.主 ...

  8. TensorFlow图像预处理-函数

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

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

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

随机推荐

  1. Objc的底层并发API(转)

    本文由webfrogs译自objc.io,原文作者Daniel Eggert.   小引 本篇英文原文所发布的站点objc.io是一个专门为iOS和OS X开发者提供的深入讨论技术的平台,文章含金量很 ...

  2. iOS -- SKPhysicsJointSpring类

    SKPhysicsJointSpring类 继承自 NSObject 符合 NSCoding(SKPhysicsJoint)NSObject(NSObject) 框架  /System/Library ...

  3. BumpMapping [转]

    http://fabiensanglard.net/bumpMapping/index.php Fabien Sanglard's Website Home About FAQ Email Rss T ...

  4. [Guava源代码阅读笔记]-Basic Utilities篇-1

    欢迎訪问:个人博客 写该系列文章的目的是记录Guava源代码中个人感觉不错且值得借鉴的内容. 一.MoreObjects类 //MoreObjects.ToStringHelper类的toString ...

  5. cocos2d-x-3.x bringToFront &amp; sendToBack实现

    void Node::bringToFront(void) { auto parent = this->getParent(); if (parent != nullptr && ...

  6. python(14)- 简单练习:登录账户失败三次,账户自动锁定

    题目需求:   1.输入用户名密码   2.认证成功后显示欢迎信息   3.输错三次后锁定 #读取注册用户的信息,用户名,密码,输错次数,写入字典中 user={} with open("D ...

  7. nightwatch testing 注意事项

    coding style 好的測試項目應該盡量不要使用pause 並且要有驗證的機制 .click('input.btn') //.pause(3000) .waitForElementVisible ...

  8. MyEclipse 设置智能提示

    choice 1: -->window→Preferences→Java→Editor→Content Assist, --->将Auto activation delay 的数值改为一个 ...

  9. Linux问题,磁盘分区打不开了

    Metadata kept in Windows cache, refused to mount. chkdsk /f http://www.bubuko.com/infodetail-1184937 ...

  10. ipython notebook 如何打开.ipynb文件?

    标签: pythontensorflow 2017-03-29 14:17 235人阅读 评论(0) 收藏 举报  分类: TensorFlow(13)  转自:https://www.zhihu.c ...