Tensorflow细节-P174-真正的图像预处理
注意这里的读取image_raw_data = tf.gfile.FastGFile("./datasets/cat.jpg", "rb").read(),写入f = tf.gfile.GFile("output.png", "wb")所用的函数,注意这里及以下都是调用的tf.image.
import matplotlib.pyplot as plt
import tensorflow as tf
image_raw_data = tf.gfile.FastGFile("./datasets/cat.jpg", "rb").read()
f = tf.gfile.GFile("output.png", "wb")
with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw_data)
fig = plt.figure()
plt.imshow(sess.run(img_data))
encoded_image = tf.image.encode_jpeg(img_data)
f.write(encoded_image.eval())
重新调整图片大小API
image_float = tf.image.convert_image_dtype(img_data, tf.float32)
resized = tf.image.resize_images(image_float, [300, 300], method=0)
裁剪或填充图片
croped = tf.image.resize_image_with_crop_or_pad(img_data, 1000, 1000)
padded = tf.image.resize_image_with_crop_or_pad(img_data, 3000, 3000)
截取中间50%的图片
central_cropped = tf.image.central_crop(img_data, 0.5)
翻转图片
transposed = tf.image.transpose_image(img_data)
处理标注框
图像的裁剪区域必须包含所提供的任意一个边界框的至少 min_object_covered 的内容。
import matplotlib.pyplot as plt
import tensorflow as tf
image_raw_data = tf.gfile.FastGFile("./datasets/cat.jpg", "rb").read()
with tf.Session() as sess:
boxes = tf.constant([[[0.05, 0.05, 0.9, 0.7], [0.35, 0.47, 0.5, 0.56]]])
img_data = tf.image.decode_jpeg(image_raw_data)
# sample_distorted_bounding_box要求输入图片必须是实数类型。
image_float = tf.image.convert_image_dtype(img_data, tf.float32)
begin, size, bbox_for_draw = tf.image.sample_distorted_bounding_box(
tf.shape(image_float), bounding_boxes=boxes, min_object_covered=0.4) # 截取部分至少包含某个标注框40%的内容
# 截取后的图片
distorted_image = tf.slice(image_float, begin, size) # 这是截取的操作
plt.imshow(distorted_image.eval())
plt.show()
# 在原图上用标注框画出截取的范围。由于原图的分辨率较大(2673x1797),生成的标注框
# 在Jupyter Notebook上通常因边框过细而无法分辨,这里为了演示方便先缩小分辨率。
image_small = tf.image.resize_images(image_float, [180, 267], method=0)
batchced_img = tf.expand_dims(image_small, 0)
image_with_box = tf.image.draw_bounding_boxes(batchced_img, bbox_for_draw)
print(bbox_for_draw.eval())
plt.imshow(image_with_box[0].eval())
plt.show()
Tensorflow细节-P174-真正的图像预处理的更多相关文章
- 『TensorFlow』第九弹_图像预处理_不爱红妆爱武装
部分代码单独测试: 这里实践了图像大小调整的代码,值得注意的是格式问题: 输入输出图像时一定要使用uint8编码, 但是数据处理过程中TF会自动把编码方式调整为float32,所以输入时没问题,输出时 ...
- TensorFlow 实战(五)—— 图像预处理
当然 tensorflow 并不是一种用于图像处理的框架,这里图像处理仅仅是一些简单的像素级操作,最终目的比如用于数据增强: tf.random_crop() tf.image.random_flip ...
- TensorFlow图像预处理完整样例
参考书 <TensorFlow:实战Google深度学习框架>(第2版) 以下TensorFlow程序完成了从图像片段截取,到图像大小调整再到图像翻转及色彩调整的整个图像预处理过程. #! ...
- TensorFlow 图像预处理(一) 图像编解码,图像尺寸调整
from: https://blog.csdn.net/chaipp0607/article/details/73029923 TensorFlow提供了几类图像处理函数,下面介绍图像的编码与解码,图 ...
- Caffe2 图像预处理(Image Pre-Processing)[6]
学习如何使得图像符合预训练模型的需求,或者用其他数据集的图像来测试自己的模型. - 调整大小 - 缩放 - HWC和CHW,数据通道交换 - RGB和BGR,颜色通道的交换 - Caffe2的图像预处 ...
- [opencv]图像预处理方案及方式
像识别中,图像质量的好坏直接影响识别算法的设计与效果精度,那么除了能在算法上的优化外,预处理技术在整个项目中占有很重要的因素,然而人们往往忽略这一点. 图像预处理,将每一个文字图像分检出来交给识别模块 ...
- 图像预处理第9步:存为.bmp文件
//图像预处理第9步:将最终标准化后的字符图像分为单个单个的HDIB保存,并存为.bmp文件 void CChildView::OnImgprcToDibAndSave() { unsigned ch ...
- 基于OpenCV的火焰检测(一)——图像预处理
博主最近在做一个基于OpenCV的火焰检测的项目,不仅可以检测图片中的火焰,还可以检测视频中的火焰,最后在视频检测的基础上推广到摄像头实时检测.在做这个项目的时候,博主参考了很多相关的文献,用了很多种 ...
- python+opencv 图像预处理
一 python 生成随机字符串序列+ 写入到图片上 from PIL import Image,ImageDraw,ImageFont import numpy as np import rando ...
随机推荐
- Netty 基本原理
转载. https://blog.csdn.net/qq_27641935/article/details/86543578 之前在看rocketmq源码时,发现底层用了Netty,顺便学习了一下,网 ...
- AS3数字取整
AS3 数字取整方法int()去掉小数点trace(int(3.14)); //输出3trace(int(-3.14)); //输出-3Math.round()方法:Math.round()可以四舍五 ...
- 最简单的centos上安装Nginx办法
基本几个简单命令就能搞定 由于yum源中没有我们想要的nginx,那么我们就需要创建一个“/etc/yum.repos.d/nginx.repo”的文件,其实就是新增一个yum源. vi /etc/y ...
- Spring Cloud Alibaba学习笔记(21) - Spring Cloud Gateway 自定义全局过滤器
在前文中,我们介绍了Spring Cloud Gateway内置了一系列的全局过滤器,本文介绍如何自定义全局过滤器. 自定义全局过滤需要实现GlobalFilter 接口,该接口和 GatewayFi ...
- 关闭 OSX 10.11 SIP (System Integrity Protection) 功能
关闭 OSX 10.11 SIP (System Integrity Protection) 功能 来源 https://cms.35g.tw/coding/%E9%97%9C%E9%96%89-os ...
- 【错误集】类ExcelExport是公共的, 应在名为 ExcelExport.java 的文件中声明
检查类名是否相同 区分大小写,复制代码的时候会连类名也复制了,哈哈哈,总结一下
- js合并多个array
Array.prototype.concat.call(array1, array2, array3, ...)
- there is no route defined for key Agreement(react native bug记录)
调试react native的项目有一个报错: there is no route defined for key XXXX 它发生在我调试TabNavigator选项卡路由器的时候,我把如下代码的A ...
- go语言实现分布式id生成器
本文:https://chai2010.cn/advanced-go-programming-book/ch6-cloud/ch6-01-dist-id.html 分布式id生成器 有时我们需要能够生 ...
- unity 3D物体使用EventSystem响应事件
在ugui中创建一个canvas 之后会自动创建一个EventSystem,用来处理UI上的时间响应.(可以通过UI>EventSystem创建EventSystem) EventSystem ...