tensorflow(一):图片处理
一、图片处理
1、图片存取 tf.gfile
import tensorflow as tf
import matplotlib.pyplot as plt image_bytes = tf.gfile.FastGFile("dog.jpg", 'rb').read() # 字节
with tf.Session() as session:
# 2.图片解码
img = tf.image.decode_jpeg(image_bytes)
# print(img) # tensor('DecodePnng:0', shape=(?,?,?),dtype=uint8)
img_array = img.eval() # 将tensor对象转成数组
# 3.图片显示
plt.imshow(img_array)
plt.show()
# 4.图片数据类型转化(整形)
# img = tf.image.convert_image_dtype(img, dtype=tf.float32)
# print(img)
# 5.图像重编码
encode_image = tf.image.encode_jpeg(img)
new_img = encode_image.eval() # 数组
# 6.图片保存
with tf.gfile.GFile("dog_new.png", "wb") as f:
f.write(new_img)
2、图片修改 tf.image
import tensorflow as tf
import matplotlib.pyplot as plt image_bytes = tf.gfile.FastGFile("dog.jpg", 'rb').read() # 字节
with tf.Session() as session:
img = tf.image.decode_jpeg(image_bytes)
# 翻转图片
img_flipped = tf.image.flip_up_down(img) # 上下反转
img_flipped = tf.image.flip_left_right(img_flipped) # 左右反转
img_flipped = tf.image.transpose_image(img_flipped) # 对角线反转
img_flipped = tf.image.random_flip_up_down(img_flipped) # 随机上下反转
img_flipped = tf.image.random_flip_left_right(img_flipped) # 随机左右反转
# 亮度设置
img_adjust = tf.image.adjust_brightness(img_flipped, -0.5) # 增加亮度
img_adjust = tf.image.adjust_brightness(img_adjust, +0.5) # 降低亮度
img_adjust = tf.image.random_brightness(img_adjust, max_delta=0.3) # 随机调整亮度,亮度在[-max_delta, +max_delta]]
# 色度
img_saturation = tf.image.adjust_saturation(img_adjust, 1.5) # 支持random
# 饱和度
img_hue = tf.image.adjust_hue(img_saturation, delta=0.2)
# 对比度
img_contrast = tf.image.adjust_contrast(img_hue, 0.5)
# 图片标准化
img_standard = tf.image.per_image_standardization(img_adjust)
img_standard = tf.clip_by_value(img_standard, 0.0, 10)
# 转成数组
img_array = img_standard.eval()
plt.imshow(img_array)
plt.show()
3、图像标注框
import tensorflow as tf
import matplotlib.pyplot as plt image_bytes = tf.gfile.FastGFile("dog.jpg", 'rb').read() # 字节
with tf.Session() as session:
img = tf.image.decode_jpeg(image_bytes)
# 调整图片大小
img_resize = tf.image.resize_image_with_crop_or_pad(img, 300, 300)
# 按比例截取图片
boxes = tf.constant([[[0.31, 0.22, 0.46, 0.38], [0.38, 0.53, 0.53, 0.71]]]) # 两个标注框
# boxes = tf.constant([[[0.31, 0.22, 0.46, 0.38]]]) # 设置一个RGB,设置四个角的比例位置
# 给原始图片添加一个图层
batched = tf.expand_dims(tf.image.convert_image_dtype(img_resize, tf.float32), 0)
# 把boxes标注的框画到原始图片上
image_with_boxes = tf.image.draw_bounding_boxes(batched, boxes)
# 重新将原始图片设置为RGB
image_with_boxes = tf.reshape(image_with_boxes, [300, 300, 3])
img_array = image_with_boxes.eval()
plt.imshow(img_array)
plt.show()
4、图片随机截取
import matplotlib.pyplot as plt
image_bytes = tf.gfile.FastGFile("dog.jpg", 'rb').read()  #  字节
with tf.Session() as session:
    img = tf.image.decode_jpeg(image_bytes)
    # 给定截取框大小
    bounding_boxes = tf.constant([[[0.31, 0.22, 0.46, 0.38]]])  # 设置一个RGB,设置四个角的比例位置
    # 选择相关图像截取算法截图
    # Bounding boxes are supplied and returned as `[y_min, x_min, y_max, x_max]`.
    begin, size, bboxes = tf.image.sample_distorted_bounding_box(
        tf.shape(img), bounding_boxes=bounding_boxes, min_object_covered=0.1
    )
    # 生成概要
    # img_with_box = tf.image.draw_bounding_boxes(tf.expand_dims(tf.image.convert_image_dtype(img, dtype=tf.float32), 0), bboxes)
    # tf.summary.image('img_with_box', img_with_box)
    # print(begin.eval(), size.eval())
    # 截图
    distorted_img = tf.slice(img, begin, size)
    img_array = distorted_img.eval()
    plt.imshow(img_array)
    plt.show()
5、一个简单样例代码,实现随机截取图片
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt class Sample:
def load_jpg(self, path, mode='rb'):
image_bytes = tf.gfile.FastGFile(path, mode).read()
return tf.image.decode_jpeg(image_bytes, channels=3)
def _distort_picture(self, image, color_ordering=0):
if color_ordering == 0:
image = tf.image.random_brightness(image, max_delta=32./255.) # 随机亮度
image = tf.image.random_contrast(image, lower=0.5, upper=1.5) # 对比度
image = tf.image.random_hue(image, max_delta=0.2) # 饱和度
image = tf.image.random_saturation(image, lower=0.5, upper=1.5)# 色度
if color_ordering == 1:
image = tf.image.random_hue(image, max_delta=0.2) # 饱和度
image = tf.image.random_saturation(image, lower=0.5, upper=1.5)# 色度
image = tf.image.random_flip_left_right(image)
image = tf.image.random_flip_up_down(image)
return tf.clip_by_value(image, clip_value_min=0.0, clip_value_max=1.0) # 归一化
def _preprocess_for_train(self, image, height, width, bounding_boxes=None):
if bounding_boxes is None:
bounding_boxes = tf.constant([0.0, 0.0, 1.0, 1.0], dtype=tf.float32, shape=[1, 1, 4])
if image.dtype != tf.float32:
image = tf.image.convert_image_dtype(image, dtype=tf.float32)
begin, size, bboxes = tf.image.sample_distorted_bounding_box(
tf.shape(image), bounding_boxes=bounding_boxes, min_object_covered=0.1
)
# 随机截图
distorted_image = tf.slice(image, begin=begin, size=size)
# 调整随机截图的图片大小
# distorted_image = tf.image.resize_image_with_crop_or_pad(distorted_image, height, width)
distorted_image = tf.image.resize_images(
distorted_image, size=[height, width], method=np.random.randint(4)
)
# 随机调整图片的一些设置
distorted_image = self._distort_picture(distorted_image, np.random.randint(2))
return distorted_image
def get_random_picture(self, number, image, *args, **kwargs):
with tf.Session() as session:
for i in range(number):
random_picture = self._preprocess_for_train(image, *args, **kwargs)
plt.imshow(random_picture.eval())
plt.show() def main():
sample = Sample()
image = sample.load_jpg("dog.jpg", 'rb')
# bounding_boxes = tf.constant([0.2, 0.2, 0.8, 0.8], dtype=tf.float32, shape=[1, 1, 4])
bounding_boxes = tf.constant([[[0.2, 0.2, 0.8, 0.8]]])
height = width = 150
sample.get_random_picture(5, image, height, width, bounding_boxes)
main()
5、图片处理有关函数整理
| 函数 | 描述 | 
| tf.gfile.FastGFile | 读取单个图片,返回字节流数据 | 
| tf.decode_jpeg | 在图片读入操作之后,图片处理之前,对图片进行解码 | 
| tf.encode_jpeg | 在图片保存时对图片进行重编码 | 
| tf.gfile.GFile | 写出单个图片 | 
| tf.image.convert_image_dtype | 转换图片的数据类型 | 
| tf.resize_images | 剪裁图片大小 | 
| tf.resize_image_with_crop_of_pad | 剪裁单个图片大小 | 
| tf.image.random_flip_left_right | 图片随机左右反转 | 
| tf.image.random_flip_up_down | 图片随机上下反转 | 
| tf.image.random_brightness | 图片随机调整亮度 | 
| tf.image.random_hue | 图片随机调整饱和度 | 
| tf.image.random_contrast | 图片随机调整对比度 | 
| tf.image.random_saturation | 图片随机调整色度 | 
| tf.image.per_image_standardization | 单个图片标准化 | 
| tf.image.clip_by_value | 单个图片归一化,其它还有tf.image.clip_by_XXX等方法 | 
| tf.expand_dims | 给图片增加维度(图层) | 
| tf.image.sample_distorted_bounding_box | 生成随机子图 | 
| tf.image.draw_bounding_boxes | 将标注框标注的子图取出来 | 
| tf.image.reshape | 调整图片的维度 | 
| tf.slice | 截取随机子图为单个图片 | 
二、TFRecord
TFRecord文件是tensorflow指定的一种文件存储格式。它由tf.train.Example和tf.train.Feature来规定和实现。
# tf.train.Example Protocol Buffer
message Example {
Features features = 1;
}
message Features {
map<string, Feature> feature = 1;
}
message Feature {
oneof kind{
BytesList bytes_list = 1;
FloatList float_list = 2;
Int64List int64_list = 3;
}
}
1、TFRecord文件写出
手写字mnist数据下载地址: http://yann.lecun.com/exdb/mnist/
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np # 导入训练集和测试集的图片和标签
mnist = input_data.read_data_sets("tensorflow/mnist/", dtype=tf.uint8, one_hot=True)
# 获取图片和标签
images = mnist.train.images # images.shape (55000, 784) 热独编码
labels = mnist.train.labels # labels.shape (55000, 10)
# 获取图像的数量及图片的分辨率([......])
numbers, pixels = images.shape # 按照tf.train.Example Protocol Buffer来定义TFRecord文件格式
def _int64(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
def _bytes(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def example_protocol_buffer(pixel, size, image):
example = tf.train.Example(
features=tf.train.Features(
feature={
'pixels': _int64(pixels),
'label': _int64(size),
'image': _bytes(image)
}
)
)
return example.SerializeToString() # 序列化为字节 # 输出文件地址
filename = "tensorflow/test/mnist.tfrecord"
# 创建一个writer
writer = tf.python_io.TFRecordWriter(filename)
# 遍历每张图片
for index in range(numbers):
image = images[index].tostring() # 转成字节
serialize = example_protocol_buffer(pixels, np.argmax(labels[index]), image)
writer.write(serialize)
writer.close()
print("done.")
2、TFRecord文件读入
import tensorflow as tf
import matplotlib.pyplot as plt # 创建reader
reader = tf.TFRecordReader()
# 创建字节流读取队列
filename_queue = tf.train.string_input_producer(
["tensorflow/test/mnist.tfrecord"]
)
# 从文件中读取一个样例,read_up_to函数一次性读取多个样例
key, serialized_example = reader.read(filename_queue)
# 解析读取的一个样例,如果需要解析多个样例,可以用parse_example
def parse_single(serialized_example):
features = tf.parse_single_example(
serialized_example,
features={
'image': tf.FixedLenFeature([], tf.string),
'label': tf.FixedLenFeature([], tf.int64),
'pixels': tf.FixedLenFeature([], tf.int64)
}
)
# 将读取的单个样例解码
image = tf.decode_raw(features['image'], tf.uint8)
label = tf.cast(features['label'], tf.int32)
pixels = tf.cast(features['pixels'], tf.int32)
return image, label, pixels sess = tf.Session()
# 启动多线程处理输入数据
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord) for i in range(10):
image, label, pixel = sess.run(parse_single(serialized_example))
print(image, label, pixel)
plt.imshow(image.reshape(28, 28))
plt.show()
tensorflow(一):图片处理的更多相关文章
- Tensorflow显示图片
		
Tensorflow在处理数据时,经常加载图像数据,有的时候是直接读取文件,有的则是读取二进制文件,为了更好的理解Tensorflow数据处理模式,先简单讲解显示图片机制,就能更好掌握是否读取正确了. ...
 - 基于TensorFlow的图片识别服务
		
1.使用TensorFlow Retrain进行图片分类训练 https://www.tensorflow.org/versions/master/how_tos/image_retraining/i ...
 - TensorFlow笔记-图片读取
		
回到上一篇文件的读取分这么几步: # 构造队列 # 1,构造图片文件的队列 file_queue = tf.train.string_input_producer(filelist) # 构造阅读器 ...
 - tensorflow读取图片案例
		
1.知识点 """ 1.图片读取流程与API: 1.构造图片文件队列 文件队列API: a)tf.train.string_input_producer(string_t ...
 - TensorFlow学习笔记(UTF-8 问题解决 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte)
		
我使用VS2013 Python3.5 TensorFlow 1.3 的开发环境 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff ...
 - TensorFlow图像处理API
		
TensorFlow提供了一些常用的图像处理接口,可以让我们方便的对图像数据进行操作,以下首先给出一段显示原始图片的代码,然后在此基础上,实践TensorFlow的不同API. 显示原始图片 impo ...
 - AI繁荣下的隐忧——Google Tensorflow安全风险剖析
		
本文由云+社区发表 作者:[ Tencent Blade Team ] Cradmin 我们身处一个巨变的时代,各种新技术层出不穷,人工智能作为一个诞生于上世纪50年代的概念,近两年出现井喷式发展,得 ...
 - 使用tensorflow搭建自己的验证码识别系统
		
目录 准备验证码数据 保存为tfrecords文件 验证码训练 学习tensorflow有一段时间了,想做点东西来练一下手.为了更有意思点,下面将搭建一个简单的验证码识别系统. 准备验证码数据 下面将 ...
 - 使用Tensorflow操作MNIST数据
		
MNIST是一个非常有名的手写体数字识别数据集,在很多资料中,这个数据集都会被用作深度学习的入门样例.而TensorFlow的封装让使用MNIST数据集变得更加方便.MNIST数据集是NIST数据集的 ...
 - tensorflow学习笔记——使用TensorFlow操作MNIST数据(1)
		
续集请点击我:tensorflow学习笔记——使用TensorFlow操作MNIST数据(2) 本节开始学习使用tensorflow教程,当然从最简单的MNIST开始.这怎么说呢,就好比编程入门有He ...
 
随机推荐
- ElasticSearch基本查询
			
词条查询 这是一个简单查询.它仅 匹配给定字段中包含该词条的稳定,且是2未经分析的确切的词条. { “query” :{ “term”:{ “title”:”crime” } } } 多词条查询 匹配 ...
 - java----session
			
什么是session? 在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),也就是说他是保存在服务端的.注意:一个浏览器独占一个session对象(默认情况下).因此,在 ...
 - java springboot+maven发送邮件
			
springboot+maven发送邮件 废话不多说直接上代码 1. pom 文件导入jar包 <!--邮件发送--> <dependency> <groupId> ...
 - python 之  爬普房网
			
from bs4 import BeautifulSoupimport reimport requestsimport pandas## pa pufangwangclass down(object) ...
 - iOS开发中断言的使用—NSAssert()
			
原文链接:http://blog.csdn.net/univcore/article/details/16859263 断言(assertion)是指在开发期间使用的.让程序在运行时进行自检的代码(通 ...
 - markdown页面内跳转
			
分两步 第一步 实际语法比较简单,在需要跳转的位置添加锚点,语法如下: <span id="jump">跳转到的地方</span> 第二步 在需要点击跳转的 ...
 - springcloud(九)-Feign使用Hystrix
			
前言 上一篇我们使用注解@HystrixCommond的fallbackMethod属性实现回退.然而,Feign是以接口形式工作的,它没有方法体,上一篇讲解的方式显然不适用于Feign. 那么Fei ...
 - /usr/bin/ld: cannot find -lperconaserverclient_r 解决
			
编译sqladvisor安装遇到: [root@localhost sqladvisor]# cd SQLAdvisor/sqladvisor/ [root@localhost sqladvisor] ...
 - 微信 oauth 登录 ,回调两次,一个坑,记录一下。
			
在做微信某个功能的时候,大致需求是:静默授权,得到openId ,然后拿着openId调用接口,判断是否关注.如果是关注的,则发放礼券.每个我网站的会员只会发放一次礼券.如果第二次则会提示已领取过礼券 ...
 - Mac下安装Iterm2终端工具
			
一般Iterm2是结合oh-my-zsh一起使用,但是如果不喜欢zsh也可以单独使用.Iterm2有个亮点就是可以通过快捷键快速启动. 安装步骤: 1.下载: http://www.iterm2.co ...