tensorflow图像预处理
程序员文章站
2022-07-13 08:53:17
...
读写图像
import matplotlib.pyplot as plt
import tensorflow as tf
pic_path = 'demo.jpeg'
image_raw = tf.gfile.FastGFile(pic_path,'rb').read()
#从原图直接读出来的是一串二进制代码,需要将其解码得到像素数组
with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw)
print(img_data.eval())
plt.imshow(img_data.eval())
plt.show()
#将像素数组编码重新写入后得到原图
img_raw = tf.image.encode_jpeg(img_data)
with tf.gfile.GFile('out_pic','wb') as f:
f.write(image_raw)
调整图像大小
#method可以取0,1,2,3,代表4种不同的调整算法
#0:双线性插值法
#1:最近邻居法
#2:双三次插值法
#3:面积插值法
new_size = [300,300]
image = tf.image.resize_images(image,new_size,method=0)
裁剪和填充
#若调整后的图像比原图小,则裁剪,若比原图大,则在周围填充
crop_image = tf.image.resize_images_with_crop_or_pad(image,smaller_size)
padded_image = tf.image.resize_images_with_crop_or_pad(image,larger_size)
通过比例调整图像大小
image = tf.image.central_crop(image,0.5)
在图像上截取一部分
image = tf.image.crop_to_bounding_box(image,bbox)
翻转图像
#上下翻转
flipped = tf.image.flip_up_donw(image)
#左右翻转
flipped = tf.image.flip_left_right(image)
#沿对角线翻转
transposed = tf.image.transpose_image(image)
#50%的几率上下翻转
rand_flipped = tf.image.randon_flip_up_down(image)
#50%的几率左右翻转
rand_flipped = tf.image.randon_flip_left_right(image)
调整亮度
brightness = 0.5
adjusted = tf.image.adjust_brightness(image,brightness)
#在(-brightness,+brightness)区间随机调整亮度
adjusted = tf.image.random_brightness(image,brightness)
调整对比度
contrast = 0.5
adjusted = tf.image.adjust_contrast(image,contrast)
#在(lower,upper)区间随机调整对比度
adjusted = tf.image.adjust_contrast(image,lower,upper)
调整色相
hue = 0.5
adjusted = tf.image.adjust_hue(image,hue)
adjusted = tf.image.random_hue(image,hue)
调整饱和度
saturation = 5
adjusted = tf.image.adjust_saturation(image,saturation)
adjusted = tf.image.random_saturation(image,lower,upper)
图像标准化
#将三维矩阵变成均值为0,方差为1
adjusted = tf.image.per_image_standardization(imgae)
处理标注框
pic_path = 'demo.jpeg'
image_raw = tf.gfile.FastGFile(pic_path,'rb').read()
#从原图直接读出来的是一串二进制代码,需要将其解码得到像素数组
with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw)
img_data = tf.image.resize_images(img_data,[180,267],method=1)
#函数处理的是一个batch的图片,所以要加一个维度
#另外,需要先将dtype转化为flaot32,这时像素数据会转化为0-1的实数
batch = 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.5]]])
result = tf.image.draw_bounding_boxes(batch,boxes)
plt.imshow(result[0].eval())
plt.show()
随机截取图像
import matplotlib.pyplot as plt
import tensorflow as tf
pic_path = 'demo.jpeg'
image_raw = tf.gfile.FastGFile(pic_path,'rb').read()
#从原图直接读出来的是一串二进制代码,需要将其解码得到像素数组
with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw)
img_data = tf.image.resize_images(img_data,[180,267],method=1)
batch = 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.5]]])
#min_object_converd表示随机的标注框与某个已知的标注框有40%的部分重合
#begin是标注框左上角的坐标,size是长和宽
begin,size,bbox_for_draw = tf.image.sample_distorted_bounding_box(
tf.shape(img_data),bounding_boxes=boxes,min_object_covered=.4
)
result = tf.image.draw_bounding_boxes(batch,bbox_for_draw)
plt.imshow(result[0].eval())
#根据begin,size截取图像
distorted_image = tf.slice(img_data,begin,size)
plt.imshow(distorted_image)
plt.show()
完整样例
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
def distort_color(image,color_ordering):
#调整亮度,饱和度,色相,对比度,调整顺序不同最终结果也会不一样
if color_ordering == 0:
image = tf.image.random_brightness(image, max_delta=32. / 255.)
image = tf.image.random_saturation(image, lower=.5, upper=1.5)
image = tf.image.random_hue(image, max_delta=.2)
image = tf.image.random_contrast(image, lower=.5, upper=1.5)
elif color_ordering == 1:
image = tf.image.random_saturation(image, lower=.5, upper=1.5)
image = tf.image.random_brightness(image, max_delta=32. / 255.)
image = tf.image.random_contrast(image, lower=.5, upper=1.5)
image = tf.image.random_hue(image, max_delta=.2)
else:
pass
#图像处理后像素值可能会超过(0,1)区间
return tf.clip_by_value(image,0.,1.)
def process_for_train(image,height,width,bbox):
if bbox is None:
bbox = 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,tf.float32)
#随机切割一部分
#注意bbox的格式是[[[ ]]]
begin,size,_ = tf.image.sample_distorted_bounding_box(tf.shape(image),bounding_boxes=bbox,min_object_covered=.1)
distorted_image = tf.slice(image,begin,size)
#还原到神经网络输入大小
distorted_image = tf.image.resize_images(distorted_image,[height,width],method=np.random.randint(4))
distorted_image = tf.image.random_flip_left_right(distorted_image)
distorted_image = distort_color(distorted_image,np.random.randint(2))
return distorted_image
pic_path = 'demo.jpeg'
image_raw = tf.gfile.FastGFile(pic_path,'rb').read()
#从原图直接读出来的是一串二进制代码,需要将其解码得到像素数组
with tf.Session() as sess:
img_data = tf.image.decode_jpeg(image_raw)
boxes = tf.constant([[[0.05,0.05,0.9,0.7],[0.35,0.47,0.5,0.56]]])
for i in range(6):
result = process_for_train(img_data,299,299,boxes)
plt.imshow(result.eval())
plt.show()
上一篇: 集成算法 - 随机森林
下一篇: 机器学习4集成算法与随机森林