1、tensorflow中对jpeg格式图像的编码/解码函数:

import matplotlib.pyplot as plt
import tensorflow as tf
image_raw_data=tf.gfile.FastGFile('/Users/jk/Downloads/timg.jpeg','rb').read()
with tf.Session() as sess:
img_data=tf.image.decode_jpeg(image_raw_data) #通过tf.img.decode_jpeg函数对jpeg格式的图像进行解码,解码后的结果为一个张量
print(img_data.eval()) #输出解码后的三维矩阵
plt.imshow(img_data.eval())
plt.show()
img_data=tf.image.convert_image_dtype(img_data,dtype=tf.uint8)
encode_image=tf.image.encode_jpeg(img_data) #将图像的三维矩阵重新按照jpeg格式编码存入文件,打开该图像可以得到和原始图像一样的图像
with tf.gfile.GFile('/Users/jk/Downloads/output','wb') as f: #将文件写入目标路径,生成图像文件
f.write(encode_image.eval())

2、图像大小调整(和上面的类似,仅多了图像大小调整的部分,下面的例子将类似):

import matplotlib.pyplot as plt
import tensorflow as tf
image_raw_data=tf.gfile.FastGFile('/Users/jk/Downloads/timg.jpeg','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.uint8)
resized=tf.image.resize_images(img_data,size=[300,300],method=1) #将图像大小转为[300,300],图像深度在没有明确设置前会是?,
print(resized.get_shape())
resized=tf.image.convert_image_dtype(resized,dtype=tf.uint8) #数据预处理时候会把dtype转为tf.float32,因此需要手动转回tf.uint8
encode_image=tf.image.encode_jpeg(resized)
with tf.gfile.GFile('/Users/jk/Downloads/output','wb') as f: #返回调整大小后的图像
f.write(encode_image.eval())

通过tf.image.resize_image_with_crop_or_pad函数来调整图像大小的功能:

croped=tf.image.resize_image_with_crop_or_pad(img_data,3000,3000)  #将图像数据扩充为3000x3000,若图像大小大于原始数据,则使用全0填充。

通过tf.image.central_crop函数来对图像按比例进行裁剪:

central_cropped=tf.image.central_crop(img_data,0.8)   #按比例进行缩小,后面的比例必须是一个(0,1]的实数。

通过tf.image.flip_up_down函数来进行图像翻转:

flipped=tf.image.flip_up_down(img_data)   #上下翻转
flipped=tf.image.flip_left_right(img_data) #左右翻转
flipped=tf.image.transpose_image(img_data) #沿对角线翻转

3、图像的色彩调整

通过tf.image.adjust_brightness函数进行色彩调整:

adjusted=tf.image.adjust_brightness(img_data,-0.5)  #将图像的亮度-0.5
adjusted=tf.image.adjust_brightness(img_data,+0.5) #将图像的亮度+0.5
adjusted=tf.image.random_brightness(img_data,max_delta) #将图像的亮度在[-max_delta,max_delta]范围内随机调整

通过tf.image.adjust_contrast函数来调整图像的对比度:

adjusted=tf.image.adjust_contrast(img_data,5)   #将图像的对比度+5
adjusted=tf.image.random_contrast(img_data,lower,upper) #在[lower, upper]范围内随机调整图像的对比度

通过tf.image.adjust_hue函数来调整图像的色相:

adjusted=tf.image.adjust_hue(img_data,0.5)   #将图像的色相加0.5
adjusted=tf.image.random_hue(img_data,max_delta) #在[-max_delta,max_delta]范围内随机调整图像的色相

通过tf.image.adjust_saturation函数调整图像的饱和度:

adjusted=tf.image.adjust_saturation(img_data,-5)   #将图像的饱和度-5
adjusted=tf.image.random_saturation(img_data,lower,upper) #随机调整图像的饱和度

通过tf.image.per_image_whitening函数来对图像进行标准化:

adjusted=tf.image.per_image_standardization(img_data)   #对图像进行标准化,转化成亮度均值为0,方差为1.

4、处理标注框:

通过tf.image.draw_bounding_boxes函数在图像中加入标注框

import matplotlib.pyplot as plt
import tensorflow as tf
image_raw_data=tf.gfile.FastGFile('/Users/jk/Downloads/timg.jpeg','rb').read()
with tf.Session() as sess:
img_data=tf.image.decode_jpeg(image_raw_data) #通过tf.img.decode_jpeg函数对jpeg格式的图像进行解码,解码后的结果为一个张量
img_data=tf.image.convert_image_dtype(img_data,dtype=tf.uint8)
img_data=tf.image.resize_images(img_data,[180,267],method=1)
batched=tf.expand_dims(tf.image.convert_image_dtype(img_data,tf.float32),0)
boxes=tf.constant([[[0.05,0.05,0.9,0.7],[0.35,0.47,0.5,0.56]]])#标注框的表示形式:[y_min, x_min, y_max, x_max],组成部分为3维数组,分别对应[batch, N, 4],左边的boxes的shape为[1,2,4]
result=tf.image.draw_bounding_boxes(batched,boxes)
plt.imshow(result[0].eval())
plt.show()

 5、提取标注框内的图像:(不知道为何画出来的标注框和通过标注框截取的内容不一致)

import matplotlib.pyplot as plt
import tensorflow as tf
image_raw_data=tf.gfile.FastGFile('C:/Users/1/Desktop/01.jpg','rb').read()
with tf.Session() as sess:
img_data=tf.image.decode_jpeg(image_raw_data) #通过tf.img.decode_jpeg函数对jpeg格式的图像进行解码,解码后的结果为一个张量
img_data=tf.image.convert_image_dtype(img_data,dtype=tf.uint8)
img_data=tf.image.resize_images(img_data,[180,267],method=1)
boxes=tf.constant([[[0.35,0.1,0.8,0.7],[0.4,0.47,0.5,0.56]]]) #通过提供标注框的方式告诉随机截图的算法哪些部分是有信息量的
begin,size,bbox=tf.image.sample_distorted_bounding_box(tf.shape(img_data),bounding_boxes=boxes)
batched=tf.expand_dims(tf.image.convert_image_dtype(img_data,tf.float32),0) #需要增加一维才能画框
img_with_box=tf.image.draw_bounding_boxes(batched,bbox) #在原图像的基础上画标注框
distorted_image=tf.slice(img_data,begin,size) #截取随机得到的图像
plt.imshow(distorted_image.eval())
plt.show()
plt.imshow(img_with_box[0].eval())
plt.show()

tensorflow图像处理函数(1)的更多相关文章

  1. TensorFlow图像处理函数

    参考书 <TensorFlow:实战Google深度学习框架>(第2版) 图像编码处理+图像大小调整+图像翻转+图像色彩调整+处理标注框 #!/usr/bin/env python # - ...

  2. 吴裕雄 python 神经网络——TensorFlow 图像处理函数

    import numpy as np import tensorflow as tf import matplotlib.pyplot as plt image_raw_data = tf.gfile ...

  3. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:TensorFlow图像处理函数

    import numpy as np import tensorflow as tf import matplotlib.pyplot as plt #读取图片 image_raw_data = tf ...

  4. Tensorflow图像处理

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

  5. Matlab图像处理函数:regionprops

    本篇文章为转载,仅为方便学术讨论所用,不用于商业用途.由于时间较久,原作者以及原始链接暂时无法找到,如有侵权以及其他任何事宜欢迎跟我联系,如有侵扰,在此提前表示歉意.----------------- ...

  6. tf.nn.embedding_lookup TensorFlow embedding_lookup 函数最简单实例

    tf.nn.embedding_lookup TensorFlow embedding_lookup 函数最简单实例 #!/usr/bin/env python # -*- coding: utf-8 ...

  7. 深度学习TensorFlow常用函数

    tensorflow常用函数 TensorFlow 将图形定义转换成分布式执行的操作, 以充分利用可用的计算资源(如 CPU 或 GPU.一般你不需要显式指定使用 CPU 还是 GPU, Tensor ...

  8. php GD 和图像处理函数, 用 STHUPO.TTF 字体向图像写入文本

    php GD 和图像处理函数,   用  STHUPO.TTF 字体向图像写入文本 注意: 01)   imagettftext() 这个函数不能使用相对路径, 要想使用相对路径要先使用  puten ...

  9. php GD 和图像处理函数, 制作一张图片

    php GD 和图像处理函数, 制作一张图片 // GD 和图像处理函数 // https://www.php.net/manual/zh/ref.image.php // https://www.p ...

随机推荐

  1. 记一次odoo创建新的模块时,但是在odoo web界面找不到应用的案例

    原因就是在odoo.conf配置文件中没有说明  模块查找的路径

  2. python读写ini配置文件

    像邮箱等信息是可以写在配置文件里面的,python有一个配置模块ConfigParser,可以处理配置文件信息 目录 1.配置模块ConfigParser 2.基本应用 1.配置模块ConfigPar ...

  3. Vue中解决路由切换,页面不更新的实用方法

    前言:vue-router的切换不同于传统的页面的切换.路由之间的切换,其实就是组件之间的切换,不是真正的页面切换.这也会导致一个问题,就是引用相同组件的时候,会导致该组件无法更新,也就是我们口中的页 ...

  4. C# Thread2——线程优先级

    C#中Thread的优先级不是决定每个线程被执行顺序.它决定了线程可以占用CPU的时间 Thread的优先级设置是自带的枚举类型"ThreadPriority" [ComVisib ...

  5. 创建DSN

    DSN:ata Source Name (DSN)的PDO命名惯例为:PDO驱动程序的名称,后面为一个冒号,再后面是可选的驱动程序连接数据库变量信息,如主机名.端口和数据库名. 有三种类型的DSN,三 ...

  6. HDUSTOJ-1558 Flooring Tiles(反素数)

    1558: Flooring Tiles 时间限制: 3 Sec  内存限制: 128 MB提交: 59  解决: 36[提交][状态][讨论版] 题目描述 You want to decorate ...

  7. Vert.x学习第一天

    昨天看了下异步,然后就开始了Vert.x相关知识的学习. Vert.x是当下非常流行的一套全异步框架,其优势在于轻量级.高效.非常适合作为移动端后台或是企业应用. 当然对于第一天接触这个框架的人(没错 ...

  8. PHP实现简单的万年历

    <?php /*********************** *** 功能:万年历 *** *** 时间:2015/05/23 *** ***********************/ //1. ...

  9. Ribbon远程调用

    Ribbon是客户端的负载均衡机制,它有几种负载均衡机制.默认是轮询,我们也可以自定义规则.通过合理的分配网络请求来减小服务器的压力.项目都是注册到eureka服务器上.通过ribbon去调用其他服务 ...

  10. Vue学习之旅:todomvc的学习练习

    一.前奏 1.todomvc官网地址:http://todomvc.com/ 查阅文档和下载插件都可以到这个官网上找. 2.上GitHub上搜索下载有人做的现成的本地模板:进入GitHub搜索todo ...