本文共 3830 字,大约阅读时间需要 12 分钟。
数据增强的API
- ①resize
- ②crop 裁剪
- ③flip 翻转
- ④brightness & contrast 改变光照和对比度
基础的读入图片并显示的代码
import numpy as npimport tensorflow as tfimport matplotlib.pyplot as pltfrom matplotlib.pyplot import imshowname = 'gugong.jpg'img_string = tf.read_file(name)img_decoded = tf.image.decode_image(img_string)sess = tf.Session()img_decoded_val = sess.run(img_decoded)print(img_decoded_val.shape)imshow(img_decoded_val)显示图片的size是(365,600,3)
高是365,长是600,3通道(颜色)
resize
tf中有好几个resize的函数可以用来缩放图片
tf.image.resize_areatf.image_resize_bicubic #用二次线性插值法进行图片的缩放tf.image.resize_nearest_neighbor #在图片放大的时候用最相近的像素点来插入像素值这些函数的输入都是四维的tensor
要先对原图片读入的tensor进行reshape一下,最后输出的时候再reshape回来
import numpy as npimport tensorflow as tfimport matplotlib.pyplot as pltfrom matplotlib.pyplot import imshowname = 'gugong.jpg'img_string = tf.read_file(name)img_decoded = tf.image.decode_image(img_string)img_decoded = tf.reshape(img_decoded,[1,365,600,3])resize_img = tf.image.resize_bicubic(img_decoded,[730,1200]) #将图片拉大了sess = tf.Session()img_decoded_val = sess.run(resize_img)img_decoded_val = img_decoded_val.reshape((730,1200,3))print(img_decoded_val.shape)imshow(img_decoded_val)plt.show()出现这样的原因是做了线性插值之后,数值和类型就变成float的值了,我们需要变成int的值(是因为imshow这个函数不能将float很好得显示)
我们给它做一个类型变换
import numpy as npimport tensorflow as tfimport matplotlib.pyplot as pltfrom matplotlib.pyplot import imshowname = 'gugong.jpg'img_string = tf.read_file(name)img_decoded = tf.image.decode_image(img_string)img_decoded = tf.reshape(img_decoded,[1,365,600,3])resize_img = tf.image.resize_bicubic(img_decoded,[730,1200])sess = tf.Session()img_decoded_val = sess.run(resize_img)img_decoded_val = img_decoded_val.reshape((730,1200,3))img_decoded_val = np.asarray(img_decoded_val, np.uint8)print(img_decoded_val.shape)imshow(img_decoded_val)plt.show()得到了一张高730,宽1200的图像
crop
tf.image.pad_to_bounding_box #填充tf.image.crop_to_bounding_box #裁剪tf.random_crop #随机裁剪import numpy as npimport tensorflow as tfimport matplotlib.pyplot as pltfrom matplotlib.pyplot import imshowname = 'gugong.jpg'img_string = tf.read_file(name)img_decoded = tf.image.decode_image(img_string)img_decoded = tf.reshape(img_decoded,[1,365,600,3])padded_img = tf.image.pad_to_bounding_box(img_decoded,50,100,500,800) #画布的大小是500,800, 原始图像在画布上的位置是50,100sess = tf.Session()img_decoded_val = sess.run(padded_img)img_decoded_val = img_decoded_val.reshape((500,800,3))img_decoded_val = np.asarray(img_decoded_val, np.uint8)print(img_decoded_val.shape)imshow(img_decoded_val)plt.show()
flip
tf.image.flip_up_down #上下翻转 tf.image.flip_left_right #左右翻转tf.image.random_flip_up_down #随机上下翻转tf.image.random_flip_left_right #随机左右翻转import numpy as npimport tensorflow as tfimport matplotlib.pyplot as pltfrom matplotlib.pyplot import imshowname = 'gugong.jpg'img_string = tf.read_file(name)img_decoded = tf.image.decode_image(img_string)img_decoded = tf.reshape(img_decoded,[1,365,600,3])flipped_img = tf.image.flip_up_down(img_decoded)sess = tf.Session()img_decoded_val = sess.run(flipped_img)img_decoded_val = img_decoded_val.reshape((365,600,3))img_decoded_val = np.asarray(img_decoded_val, np.uint8)print(img_decoded_val.shape)imshow(img_decoded_val)plt.show()
brightness & contrast
tf.image.adjust_brightnesstf.image.random_brightnesstf.image.adjust_contrasttf.image.random_comtrastimport numpy as npimport tensorflow as tfimport matplotlib.pyplot as pltfrom matplotlib.pyplot import imshowname = 'gugong.jpg'img_string = tf.read_file(name)img_decoded = tf.image.decode_image(img_string)img_decoded = tf.reshape(img_decoded,[1,365,600,3])new_img = tf.image.adjust_brightness(img_decoded,-0.5) #光照强度变为原来的50%sess = tf.Session()img_decoded_val = sess.run(new_img)img_decoded_val = img_decoded_val.reshape((365,600,3))img_decoded_val = np.asarray(img_decoded_val, np.uint8)print(img_decoded_val.shape)imshow(img_decoded_val)plt.show()如果是加上50%
转载地址:http://xlygf.baihongyu.com/