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的更多相关文章

  1. 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 ...

  2. Tensorflow图像处理

    Tensorflow图像处理主要包括:调整尺寸,图像翻转,调整色彩,处理标注框. 代码如下: #coding=utf-8 import matplotlib.pyplot as plt import ...

  3. TensorFlow dataset API 使用

    # TensorFlow dataset API 使用 由于本人感兴趣的是自然语言处理,所以下面有关dataset API 的使用偏向于变长数据的处理. 1. 从迭代器中引入数据 import num ...

  4. TensorFlow - 相关 API

    来自:https://cloud.tencent.com/developer/labs/lab/10324 TensorFlow - 相关 API TensorFlow 相关函数理解 任务时间:时间未 ...

  5. TensorFlow — 相关 API

    TensorFlow — 相关 API TensorFlow 相关函数理解 任务时间:时间未知 tf.truncated_normal truncated_normal( shape, mean=0. ...

  6. 开源框架---tensorflow c++ API 一个卡了很久的问题

    <开源框架---tensorflow c++ API 运行第一个“手写字的例子”> 中可以说明tensorflow c++ API是好用的,.......

  7. 开源框架---通过Bazel编译使用tensorflow c++ API 记录

    开源框架---通过Bazel编译使用tensorflow c++ API 记录 tensorflow python API,在python中借用pip安装tensorflow,真的很方便,几句指令就完 ...

  8. TensorFlow Keras API用法

    TensorFlow Keras API用法 Keras 是与 TensorFlow 一起使用的更高级别的作为后端的 API.添加层就像添加一行代码一样简单.在模型架构之后,使用一行代码,可以编译和拟 ...

  9. tensorflow estimator API小栗子

    TensorFlow的高级机器学习API(tf.estimator)可以轻松配置,训练和评估各种机器学习模型. 在本教程中,您将使用tf.estimator构建一个神经网络分类器,并在Iris数据集上 ...

随机推荐

  1. AngularJS进阶(二十五)requirejs + angular + angular-route 浅谈HTML5单页面架构

    requirejs + angular + angular-route 浅谈HTML5单页面架构 众所周知,现在移动Webapp越来越多,例如天猫.京东.国美这些都是很好的例子.而在Webapp中,又 ...

  2. Shell入门之概念

    1.一切皆是文件: 在bash Shell 中一切皆是文件,不管是我们认为的文本文件,还是那些文件夹的东西,在这里都是文件,Linux只管比特和字节流,而不关心他们最终组成了什么格式,这些工作交给在L ...

  3. 学习pthreads,给线程传递多个参数

    上篇博文中,boss线程给其他线程传递的只有一个参数,那么假如是多个参数呢?怎么传递呢?或许你会有这样的疑问,带着这个疑问,我们进入本文的世界,这里传递多个参数,采用结构体,为什么呢?因为结构体里可以 ...

  4. nginx 安装php

    1. 安装PHP 5.5.0 下载   1 2 cd /usr/local/src/ wget http://www.php.net/get/php-5.5.0.tar.bz2/from/jp1.ph ...

  5. Mac下ImageMagick安装(libpng)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/42562705 ...

  6. Mahout朴素贝叶斯文本分类

    Mahout朴素贝叶斯文本分类算法 Mahout贝叶斯分类器按照官方的说法,是按照<Tackling the PoorAssumptions of Naive Bayes Text Classi ...

  7. AngularJS进阶(二十一)Angularjs中scope与rootscope区别及联系

    Angularjs中scope与rootscope区别及联系 scope是html和单个controller之间的桥梁,数据绑定就靠他了.rootscope是各个controller中scope的桥梁 ...

  8. Xcode 下删除Provisioning Profiles文件

    Xcode 中有很多不可以用的Provisioning Profiles 文件,每次选择手机证书时,看着那么长的菜单很烦有木有? 在Xcode 5中删除 Provisioning Profiles,打 ...

  9. objc写一个NSMutableArray不连续索引替换对象的方法

    NSMutableArray内置的方法-(void)replaceObjectsAtIndexes:(NSIndexSet*)set withObjects:(NSArray*)objs 只能替换一段 ...

  10. 利用可变参实现fprintf函数

    #include <stdio.h> #include <stdarg.h> /* 可变参相关接口 typedef char * va_list ; void va_start ...