一、图片处理

  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(一):图片处理的更多相关文章

  1. Tensorflow显示图片

    Tensorflow在处理数据时,经常加载图像数据,有的时候是直接读取文件,有的则是读取二进制文件,为了更好的理解Tensorflow数据处理模式,先简单讲解显示图片机制,就能更好掌握是否读取正确了. ...

  2. 基于TensorFlow的图片识别服务

    1.使用TensorFlow Retrain进行图片分类训练 https://www.tensorflow.org/versions/master/how_tos/image_retraining/i ...

  3. TensorFlow笔记-图片读取

    回到上一篇文件的读取分这么几步: # 构造队列 # 1,构造图片文件的队列 file_queue = tf.train.string_input_producer(filelist) # 构造阅读器 ...

  4. tensorflow读取图片案例

    1.知识点 """ 1.图片读取流程与API: 1.构造图片文件队列 文件队列API: a)tf.train.string_input_producer(string_t ...

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

  6. TensorFlow图像处理API

    TensorFlow提供了一些常用的图像处理接口,可以让我们方便的对图像数据进行操作,以下首先给出一段显示原始图片的代码,然后在此基础上,实践TensorFlow的不同API. 显示原始图片 impo ...

  7. AI繁荣下的隐忧——Google Tensorflow安全风险剖析

    本文由云+社区发表 作者:[ Tencent Blade Team ] Cradmin 我们身处一个巨变的时代,各种新技术层出不穷,人工智能作为一个诞生于上世纪50年代的概念,近两年出现井喷式发展,得 ...

  8. 使用tensorflow搭建自己的验证码识别系统

    目录 准备验证码数据 保存为tfrecords文件 验证码训练 学习tensorflow有一段时间了,想做点东西来练一下手.为了更有意思点,下面将搭建一个简单的验证码识别系统. 准备验证码数据 下面将 ...

  9. 使用Tensorflow操作MNIST数据

    MNIST是一个非常有名的手写体数字识别数据集,在很多资料中,这个数据集都会被用作深度学习的入门样例.而TensorFlow的封装让使用MNIST数据集变得更加方便.MNIST数据集是NIST数据集的 ...

  10. tensorflow学习笔记——使用TensorFlow操作MNIST数据(1)

    续集请点击我:tensorflow学习笔记——使用TensorFlow操作MNIST数据(2) 本节开始学习使用tensorflow教程,当然从最简单的MNIST开始.这怎么说呢,就好比编程入门有He ...

随机推荐

  1. kali linux之无线渗透(续)

    Airolib 设计用于存储ESSID和密码列表,计算生成不变的PMK(计算资源消耗型) PMK在破解阶段被用于计算PTK(速度快,计算资源要求少) 通过完整性摘要值破解密码SQLite3数据库存储数 ...

  2. JavaScript(汇聚页)

    JavaScript对象 String 对象 RegExp 对象 \W 元字符

  3. python 使用eval() 可以将json格式的数据,转换为原始数据

    使用python 自带的函数可以将json 格式的数据(也就是字符串)转换为原始格式的数据, 当使用json.loads()无法将json格式的数据转换为原始数据(存在多层各种格式类型数据的嵌套), ...

  4. JavaScript基础总纲

    如果前人种好了树那我们干嘛不去享受阴凉,然后花费时间去为大树的成长进一份力. 我发现一个站点写的很全面写很系统,我总结主要分为一些几个模块: 一,JavaScript 教程(基础) 二,JavaScr ...

  5. iOS -- UILabel的常见使用

    UILabel是iOS开发经常用到的一个控件,主要用于显示文字.下面记录一些常用的UIlabel的使用. 先定义:UILabel *label = [[UILabel alloc]initWithFr ...

  6. 编程开发之--java多线程学习总结(6)

    5.测试 package com.lfy.ThreadsSynchronize; public class Test { public static void main(String[] args) ...

  7. springboot第四篇:debug模式开发运用

    前提:项目是以maven project结构建立的,现状是无法进行断点调试的.怎么才能在eclipse里进行调试呢? 需要:①将项目打包部署到tomcat ②往项目加入dynamic web modu ...

  8. 洛谷P3830 [SHOI2012]随机树(期望dp)

    题面 luogu 题解 第一问: 设\(f[i]\)表示\(i\)步操作后,平均深度期望 \(f[i] = \frac {f[i - 1] * (i - 1)+f[i-1]+2}{i}=f[i-1]+ ...

  9. hdu-1277--字典树坑题

    hdu-1227 字典树,坑题!!当字典树练手 Problem Description 我们大家经常用google检索信息,但是检索信息的程序是很困难编写的:现在请你编写一个简单的全文检索程序. 问题 ...

  10. 浏览器页面的显隐对js的setInterval()执行所产生的bug

    前段时间,所写的一个”js无间隙滚动效果“,当页面离开后,重新返回时,会出现动画的错乱.我以为是因为我代码逻辑的原因导致的,但是,当在火狐浏览器上进行浏览时却没有动画错乱的问题. 于是乎,在网上查找是 ...