Tensorflow——图像数据处理
Tensorflow——图像数据处理
Tensorflow TFRecord数据格式
Tensorflow统一格式存储数据——TFRecord,TFRecord文件中的数据通过tf.train.Example Protocol Buffer
格式存储,其包括一个从属性名称到取值的字典;
- 属性名称:字符串类型
- 取值:字符串(BytesList)、实数列表(FloatList)、整数列表(Int64List)
输入数据转化为TFRecord格式
以MNIST数据为例
#生成整数属性
def _int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
#生成字符串属性
def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
mnist = input_data.read_data_sets("MNIST", dtype=tf.uint8, one_hot=True)
images = mnist.train.images
#训练数据所对应的正确答案,作为一个属性保存在TFRecord中
labels = mnist.train.labels
#训练数据的图像分辨率,作为Examples中的一个属性
pixels = images.shape[1]
num_examples = mnist.train.num_examples
#输出TFRecord文件地址
filename = "7_output.tfrecords"
#写
writer = tf.python_io.TFRecordWriter(filename)
for index in range(num_examples):
image_raw = images[index].tostring()
example = tf.train.Example(features=tf.train.Features(feature= \
{'pixels ': _int64_feature(pixels), \
'labels' : _int64_feature(np.argmax(labels[index])), \
'image_raw' : _bytes_feature(image_raw)}))
writer.write(example.SerializeToString())
writer.close()
先生成 tf.train.Example
数据格式,后定义writer = tf.python_io.TFRecordWriter(file)
写入writer.write
读取TFRecord格式数据
#创建一个reader
reader = tf.TFRecordReader()
#创建一个队列来维护输入文件列表
filename_queue = tf.train.string_input_producer(["7_output.tfrecords"])
#从文件中读出一个样例
_, serialized_example = reader.read(filename_queue)
#深入解析
features = tf.parse_single_example(serialized_example, features = {
"image_raw" : tf.FixedLenFeature([], tf.string),
"pixels": tf.FixedLenFeature([], tf.int64),
"label": tf.FixedLenFeature([], tf.int64)})
#解析成图像对应的像素数组
image = tf.decode_raw(features['image_raw'], tf.uint8)
label = tf.cast(features['label'], tf.int32)
pixels = tf.cast(features['pixels'], tf.int32)
sess = tf.Session()
#多线程处理数据
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
for i in range(10):
sess.run([image, label, pixels])
Tensorflow 图像处理
图像编码处理
# 图像编码处理
# jpeg格式处理示例
import matplotlib.pyplot as plt
image_raw_data = tf.gfile.FastGFile('test.jpeg', 'rb').read()
with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw_data)
print(img_data.eval())
相应的,处理png图片使用函数tf.image.decode_png
进行解码,解码结果为一个张量,使用pyplot可视化得到图像。
#续上
plt.imshow(img_data.eval())
plt.show()
编码同下:
#续上,在同一个sess中
#编码
encode_image = tf.image.encode_jpeg(img_data)
with tf.gfile.GFile('7_output_test.jpeg', 'wb') as f:
f.write(encode_image.eval())
图像大小处理
#图像大小处理
#tensorflow提供4种方式,并且将它们封装到tf.image.resize_images函数中
#img_data是已经解码的函数
#1. 将图片数据转化为实数类型
img_data = tf.image.convert_image_dtype(img_data, dtype=tf.float32)
#2.调整图片大小
resized = tf.image.resize_images(img_data, [300,300], method=0)
t.image.resize_image
参数:
- 原始图像
- 调整后大小
- 调整图像的算法(method)
其中method:
- 0: 双线性插值法
- 1: 最近邻居法
- 2: 双三次插值法
- 3: 面积插值法
使用tf.image.resize_with_crop_or_pad
处理,如果目标图像小于原始图像,裁取居中部分;若目标图像大于原始图像,四周填充全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)
通过比例调整大小 tf.image.central_crop
central_cropped = tf.image.central_crop(img_data, 0.5)
with tf.Session() as sess:
plt.imshow(central_cropped.eval())
plt.show()
????hhhh,暗中观察.jpg
图片翻转
#图片翻转
flipped_1 = tf.image.flip_up_down(img_data)#上下
flipped_2 = tf.image.flip_left_right(img_data)#左右
transposed = tf.image.transpose_image(img_data)#对角线
with tf.Session() as sess:
plt.imshow(img_data .eval())
plt.show()
plt.imshow(flipped_1 .eval())
plt.show()
plt.imshow(flipped_2.eval())
plt.show()
plt.imshow(transposed .eval())
plt.show()
随机翻转(50%的概率是否翻转)tf.image.random_flip_up_down()
tf.image.random_flip_left_right()
图片色彩调整
亮度
#亮度-0.5
adjusted = tf.image.adjust_brightness(img_data, -0.5)
色彩调整可能将像素实数值超出[0.0, 1.0],截断调整adjusted = tf.clip_by_value(adjusted, 0.0, 1.0)
随机调整
#在【-max_delta, max_delta)区间随机调整
adjusted = tf.image.random_brightness(image, max_delta)
图片对比度
#色相加0.1
adjusted = tf.image.adjust_hue(img_data, 0.1)
图片饱和度
adjusted = tf.image.adjust_saturation(img_data, -5)
第二个参数也可写成区间[lower, upper],在此区间随机调整图像的饱和度
标准化过程tf.image.per_image_standardization(img_data)
处理标注框
# 处理标注框
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)
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]]])
result = tf.image.draw_bounding_boxes(batched, boxes)