失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 『TensorFlow』第九弹_图像预处理_不爱红妆爱武装

『TensorFlow』第九弹_图像预处理_不爱红妆爱武装

时间:2018-11-05 05:30:58

相关推荐

『TensorFlow』第九弹_图像预处理_不爱红妆爱武装

部分代码单独测试:

这里实践了图像大小调整的代码,值得注意的是格式问题:

输入输出图像时一定要使用uint8编码,但是数据处理过程中TF会自动把编码方式调整为float32,所以输入时没问题,输出时要手动转换回来!使用numpy.asarray(dtype)或者tf.image.convert_image_dtype(dtype)都行

都行 1 import numpy as np 2 import tensorflow as tf 3 import matplotlib.pyplot as plt 4 5 # 使用'r'会出错,无法解码,只能以2进制形式读取 6 # img_raw = tf.gfile.FastGFile('./123.png','rb').read() 7 img_raw = open('./123.png','rb').read() 8 9 # 把二进制文件解码为uint810 img_0 = tf.image.decode_png(img_raw)11 # 不太必要了,可以用np直接转换了12 # img_1 = tf.image.convert_image_dtype(img_0,dtype=tf.uint8)13 14 sess = tf.Session()15 print(sess.run(img_0).shape)16 # plt.imshow(sess.run(img_0))17 # plt.show()18 19 def show_pho(img,sess=sess):20'''21TF处理过的图片自动转换了类型,需要调整回uint8才能正常显示22:param sess: 23:param img: 24:return: 25'''26cat = np.asarray(sess.run(img),dtype='uint8')27print(cat.shape)28plt.imshow(cat)29plt.show()30 31 32 '''调整图像大小'''33 # 插值尽量保存原图信息34 img_1 = tf.image.resize_images(img_0,[500,500],method=3)35 # show_pho(img_1)36 37 # 裁剪或填充38 # 自动中央截取39 img_2 = tf.image.resize_image_with_crop_or_pad(img_0,500,500)40 # show_pho(img_2)41 # 自动四周填充[0,0,0]42 img_3 = tf.image.resize_image_with_crop_or_pad(img_0,2500,2500)43 # show_pho(sess,img_3)44 45 # 比例中央裁剪46 img_4 = tf.image.central_crop(img_0,0.5)47 # show_pho(img_4)48 49 # 画框裁剪50 # {起点高度,起点宽度,框高,框宽}51 img_5 = tf.image.crop_to_bounding_box(img_0,700,300,500,500)52 show_pho(img_5)

完整的图像预处理函数:

处理单张图片

1 import tensorflow as tf 2 import numpy as np 3 # import matplotlib.pyplot as plt 4 5 def distort_color(image, color_ordering=0): 6''' 7随机调整图片的色彩,定义两种处理顺序。 8注意,对3通道图像正常,4通道图像会出错,自行先reshape之 9:param image: 10:param color_ordering: 11:return: 12'''13if color_ordering == 0:14 image = tf.image.random_brightness(image, max_delta=32./255.)15 image = tf.image.random_saturation(image, lower=0.5, upper=1.5)16 image = tf.image.random_hue(image, max_delta=0.2)17 image = tf.image.random_contrast(image, lower=0.5, upper=1.5)18else:19 image = tf.image.random_saturation(image, lower=0.5, upper=1.5)20 image = tf.image.random_brightness(image, max_delta=32./255.)21 image = tf.image.random_contrast(image, lower=0.5, upper=1.5)22 image = tf.image.random_hue(image, max_delta=0.2)23 24return tf.clip_by_value(image, 0.0, 1.0)25 26 27 def preprocess_for_train(image, height, width, bbox):28'''29对图片进行预处理,将图片转化成神经网络的输入层数据。30:param image: 31:param height: 32:param width: 33:param bbox: 34:return: 35'''36# 查看是否存在标注框。37if image.dtype != tf.float32:38 image = tf.image.convert_image_dtype(image, dtype=tf.float32)39 40# 随机的截取图片中一个块。41bbox_begin, bbox_size, _ = tf.image.sample_distorted_bounding_box(42 tf.shape(image), bounding_boxes=bbox)43bbox_begin, bbox_size, _ = tf.image.sample_distorted_bounding_box(44 tf.shape(image), bounding_boxes=bbox)45distorted_image = tf.slice(image, bbox_begin, bbox_size)46 47# 将随机截取的图片调整为神经网络输入层的大小。48distorted_image = tf.image.resize_images(distorted_image, [height, width], method=np.random.randint(4))49distorted_image = tf.image.random_flip_left_right(distorted_image)50distorted_image = distort_color(distorted_image, np.random.randint(2))51return distorted_image52 53 def pre_main(img,bbox=None):54if bbox is None:55 bbox = tf.constant([0.0, 0.0, 1.0, 1.0], dtype=tf.float32, shape=[1, 1, 4])56with tf.gfile.FastGFile(img, "rb") as f:57 image_raw_data = f.read()58with tf.Session() as sess:59 img_data = tf.image.decode_jpeg(image_raw_data)60 for i in range(9):61 result = preprocess_for_train(img_data, 299, 299, bbox)62 # {wb打开文件{矩阵编码为jpeg{格式转换为uint8}}.eval()}63 with tf.gfile.FastGFile('./代号{}.jpeg'.format(i),'wb') as f:64 f.write(sess.run(tf.image.encode_jpeg(tf.image.convert_image_dtype(result,dtype=tf.uint8))))65 # plt.imshow(result.eval())66 # plt.axis('off')67 # plt.savefig('代号{}'.format(i))68 69 70 if __name__=='__main__':71pre_main("./123123.jpeg",bbox=None)72exit()

如果觉得《『TensorFlow』第九弹_图像预处理_不爱红妆爱武装》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。