1、图像编码处理:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import matplotlib
matplotlib.use('Qt4Agg')
import matplotlib.pyplot as plt
import tensorflow as tf
image_raw_data = tf.gfile.FastGFile("F:/test.jpg", '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() 可视化图像
#将表示一张图像的三维矩阵重新保存,与原始图像一样
encoded_image = tf.image.encode_jpeg(img_data)
with tf.gfile.GFile("F:/python/test.jpg","wb") as f:
f.write(encoded_image.eval())
2、图像大小调整
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import matplotlib
matplotlib.use('Qt4Agg')
import matplotlib.pyplot as plt
import tensorflow as tf
image_raw_data = tf.gfile.FastGFile("F:/test.jpg", 'rb').read()
with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw_data)#图像解码
img_data = tf.image.convert_image_dtype(img_data, dtype=tf.float32)#将整数转换成实数
resized = tf.image.resize_images(img_data, [300,300], method=0)
#croped = tf.image.resize_image_with_crop_or_pad(img_data, 200, 200)#剪裁图像
#padded = tf.image.resize_image_with_crop_or_pad(img_data, 700, 700)#填充图像
#central_cropped = tf.image.central_crop(img_data, 0.5)#按比例额剪裁图像
#print (img_data.eval()) #输出三维矩阵
plt.imshow(resized.eval())
plt.show() #可视化图像
3、图像翻转
#flipped = tf.image.flip_up_down(img_data)#上下翻转
#flipped = tf.image.flip_left_right(img_data)#左右翻转
#transposed = tf.image.transpose_image(img_data)#沿对角线翻转
flipped = tf.image.random_flip_left_right(img_data)#随机翻转
4、图像色彩调整
#adjusted = tf.image.adjust_brightness(img_data, -0.5)图像亮度减0.5
#adjusted = tf.clip_by_value(adjusted, 0.0, 1.0)#将图像亮点截断在0.0-1.0之间
adjusted = tf.image.random_brightness(img_data, 1.0)#随机调整亮度
5、图像对比度调整
#adjusted = tf.image.adjust_contrast(img_data, 0.5)#调整对比度
#adjusted = tf.image.random_brightness(img_data, 0.5, 5)
6、处理标注框
img_data = tf.image.decode_jpeg(image_raw_data)#图像解码
img_data = tf.image.convert_image_dtype(img_data, dtype=tf.float32)#将整数转换成实数
resized = tf.image.resize_images(img_data, [180, 267], method=0)
batched = tf.expand_dims(tf.image.convert_image_dtype(resized, tf.float32), 0)
boxes = tf.constant([[[0.1, 0.1, 0.8, 0.9]]])
result = tf.image.draw_bounding_boxes(batched, boxes)
plt.imshow(result[0].eval())
plt.show() # 可视化图像
重点注意: plt.imshow(result[0].eval())
img_data = tf.image.decode_jpeg(image_raw_data)#图像解码
img_data = tf.image.convert_image_dtype(img_data, dtype=tf.float32)#将整数转换成实数
resized = tf.image.resize_images(img_data, [180, 267], method=0)
batched = tf.expand_dims(tf.image.convert_image_dtype(resized, tf.float32), 0)
boxes = tf.constant([[[0.05, 0.05, 0.9, 0.7],[0.35, 0.47, 0.5, 0.56]]])
begin, size, bbox_for_draw = tf.image.sample_distorted_bounding_box(
tf.shape(img_data), bounding_boxes=boxes, min_object_covered=0.4
)
result = tf.image.draw_bounding_boxes(batched, bbox_for_draw)
plt.imshow(result[0].eval())
plt.show() # 可视化图像