TensorFlow图像处理API
TensorFlow提供了一些常用的图像处理接口,可以让我们方便的对图像数据进行操作,以下首先给出一段显示原始图片的代码,然后在此基础上,实践TensorFlow的不同API。
显示原始图片
import matplotlib.pyplot as plt
import tensorflow as tf raw_data = tf.gfile.FastGFile('./new.jpg','rb').read() with tf.Session() as sess:
img_data = tf.image.decode_jpeg(raw_data)
plt.imshow(img_data.eval())
plt.show()
运行效果如下图:

其中tf.gfile.FastGFile,用于读取本地文件,tf.image.decode_jpeg用于将jpeg图片原始数据解码到3-D张量空间,即width, height, channel,最后调用pyplt库显示图片。
图像的缩放
import matplotlib.pyplot as plt
import tensorflow as tf raw_data = tf.gfile.FastGFile('./new.jpg','rb').read() with tf.Session() as sess:
img_data = tf.image.decode_jpeg(raw_data)
img_data2 = tf.cast(tf.image.resize_images(img_data, [200, 200]), dtype=tf.uint8);
plt.imshow(img_data2.eval())
plt.show()
TensorFlow缩放图片的接口是tf.image.resize_images,[200, 200]是缩放后的目标尺寸,这里调用了tf.cast这个类型转换函数,因为经过缩放处理后,张量类型为float32,而pyplt对图像格式要求uint8,所以必须转一下,否则什么效果,可以自己试一下。

tf.image.resize_images接口可以指定不同的缩放算法,比如:
tf.image.resize_images(img_data, [200, 200], method=tf.image.ResizeMethod.BICUBIC)
图像的反转
图像的反转在各路深度学习算法中就用的比较多了,主要是通过这种操作可以扩大样本的数量,何乐不为。
import matplotlib.pyplot as plt
import tensorflow as tf raw_data = tf.gfile.FastGFile('./new.jpg','rb').read() with tf.Session() as sess:
img_data = tf.image.decode_jpeg(raw_data)
img_data2 = tf.cast(tf.image.flip_left_right(img_data), dtype=tf.uint8)
plt.imshow(img_data2.eval())
plt.show()
上述代码调用了左右反转接口,TensorFlow还提供了上下反转及随机反转的操作,不再一一尝试。

图像的裁剪
中心裁剪
import matplotlib.pyplot as plt
import tensorflow as tf raw_data = tf.gfile.FastGFile('./new.jpg','rb').read() with tf.Session() as sess:
img_data = tf.image.decode_jpeg(raw_data)
img_data2 = tf.cast(tf.image.resize_image_with_crop_or_pad(img_data, 200, 200), dtype=tf.uint8)
plt.imshow(img_data2.eval())
plt.show()
tf.image.resize_image_with_crop_or_pad函数可以用来进行图像裁剪或扩展,这个是由用户的目标宽度和高度决定的,另外无论是裁剪还是扩展都是从图片中心为基准的。

指定位置裁剪
import matplotlib.pyplot as plt
import tensorflow as tf raw_data = tf.gfile.FastGFile('./new.jpg','rb').read() with tf.Session() as sess:
img_data = tf.image.decode_jpeg(raw_data)
img_data2 = tf.cast(tf.image.crop_to_bounding_box(img_data, 0, 0, 200, 200), dtype=tf.uint8)
plt.imshow(img_data2.eval())
plt.show() ~
上述代码指定左上角的200px方形box进行裁剪,指定目标范围必须合理,否则会产生异常。

图像上画框
import matplotlib.pyplot as plt
import tensorflow as tf raw_data = tf.gfile.FastGFile('./new.jpg','rb').read() with tf.Session() as sess:
img_data = tf.cast(tf.expand_dims(tf.image.decode_jpeg(raw_data), 0), tf.float32)
boxes = tf.constant([[[0.4, 0.4, 0.5, 0.5], [0.5, 0.5, 0.6, 0.6]]])
img_data2 = tf.cast(tf.image.draw_bounding_boxes(img_data, boxes), dtype=tf.uint8)
plt.imshow(img_data2.eval()[0])
plt.show()
这段代码有几个地方要注意一下,在jpeg解码后,调用了tf.expand_dims,这个函数的意思是在指定的位置增加一个维度,因为解码后是3维数据,在0位置增加一维,事实上增加了一个batch维度,如此操作主要是为了迎合后面的画框函数!boxes操作节点定义了两个方框,用0~1的浮点数标识box的位置比例,最后的图片显示位置也要注意,输出是四维,请取出第一个图片显示。下图为显示效果,手工放大图片后的效果,否则,1px方框在plt中可能被缩略掉,请注意!

TensorFlow图像处理API的更多相关文章
- tensorflow models api:ValueError: Tensor conversion requested dtype string for Tensor with dtype float32: 'Tensor("arg0:0", shape=(), dtype=float32, device=/device:CPU:0)'
tensorflow models api:ValueError: Tensor conversion requested dtype string for Tensor with dtype flo ...
- Tensorflow图像处理
Tensorflow图像处理主要包括:调整尺寸,图像翻转,调整色彩,处理标注框. 代码如下: #coding=utf-8 import matplotlib.pyplot as plt import ...
- TensorFlow dataset API 使用
# TensorFlow dataset API 使用 由于本人感兴趣的是自然语言处理,所以下面有关dataset API 的使用偏向于变长数据的处理. 1. 从迭代器中引入数据 import num ...
- TensorFlow - 相关 API
来自:https://cloud.tencent.com/developer/labs/lab/10324 TensorFlow - 相关 API TensorFlow 相关函数理解 任务时间:时间未 ...
- TensorFlow — 相关 API
TensorFlow — 相关 API TensorFlow 相关函数理解 任务时间:时间未知 tf.truncated_normal truncated_normal( shape, mean=0. ...
- 开源框架---tensorflow c++ API 一个卡了很久的问题
<开源框架---tensorflow c++ API 运行第一个“手写字的例子”> 中可以说明tensorflow c++ API是好用的,.......
- 开源框架---通过Bazel编译使用tensorflow c++ API 记录
开源框架---通过Bazel编译使用tensorflow c++ API 记录 tensorflow python API,在python中借用pip安装tensorflow,真的很方便,几句指令就完 ...
- TensorFlow Keras API用法
TensorFlow Keras API用法 Keras 是与 TensorFlow 一起使用的更高级别的作为后端的 API.添加层就像添加一行代码一样简单.在模型架构之后,使用一行代码,可以编译和拟 ...
- tensorflow estimator API小栗子
TensorFlow的高级机器学习API(tf.estimator)可以轻松配置,训练和评估各种机器学习模型. 在本教程中,您将使用tf.estimator构建一个神经网络分类器,并在Iris数据集上 ...
随机推荐
- XML Publisher Report Issues, Recommendations and Errors
In this Document Purpose Questions and Answers References APPLIES TO: Oracle Process Manufactu ...
- 《java入门第一季》正则表达式小案例
案例一:当你登陆某个论坛,向发帖子留下自己qq号的时候,可惜你的号码就是留不下,总是输入*,或者其它奇妙的字符串.其实原理就是用了正则表达式替换. /* * 替换功能 * String类的public ...
- 发布一个参考tornado的高性能c++网络库:libtnet
libtnet是一个用c++编写的高性能网络库,它在设计上面主要参考tornado,为服务端网络编程提供简洁而高效的接口,非常易于使用. Echo Server void onConnEvent(co ...
- Ext.Net_1.X_WINDOW遮罩层被GridPanel挡住
通过调试HTML代码,发现其实是DIV. chrome 中修改DIV Z:INDEX 就不被遮住了?但是又晓得如何修改window的Z:INDEX.那就修改"背景"GP的吧.
- PCMM(人力资源能力成熟度模型)V2.0中英对照版发布
PCMM中英版终于发布 时光荏苒,从当初的回眸到如今的回头,这才发现:坚守一份承诺是多么的不易! 一年多了,这份承载殷切期待的作品--<PCMM(人力资源能力成熟度模型)V2.0 (中英文对照版 ...
- saiku中文维度,补充说明
saiku在筛选中文维度 会出现浏览器白屏 停止响应的现象,经过跟踪源代码,分析原来在linux 操作系统中 数据库读取的中文和界面选取的编码是不一致的 解决方法, classes\saiku-dat ...
- LDA主体模型
一)LDA作用 传统判断两个文档相似性的方法是通过查看两个文档共同出现的单词的多少,如TF-IDF等,这种方法没有考虑到文字背后的语义关联,可能在两个文档共同出现的单词很少甚至没有,但两个文档是相似的 ...
- .NET(C#)连接各类数据库代码-集锦
1.C#连接连接Access 复制代码代码如下: using System.Data; using System.Data.OleDb; .. string strConnection=& ...
- java语法部分一些小问题
由于本人是个初学者希望自己的文章不会误导广大"群众",如果有错误之处还望前辈指出.谢谢! 一.键盘录入. A:导包 格式: import java.util.Scanner; 位置 ...
- Erlang cowboy 入门参考
Erlang cowboy 入门参考 cheungmine,2014-10-28 本文翻译自: http://ninenines.eu/docs/en/cowboy/HEAD/guide/gettin ...