首页 > 代码库 > 『TensorFlow』图像预处理_

『TensorFlow』图像预处理_

部分代码单独测试:

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

  1. 输入输出图像时一定要使用uint8编码,
  2. 但是数据处理过程中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 # 把二进制文件解码为uint8
10 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     ‘‘‘
21     TF处理过的图片自动转换了类型,需要调整回uint8才能正常显示
22     :param sess: 
23     :param img: 
24     :return: 
25     ‘‘‘
26     cat = np.asarray(sess.run(img),dtype=uint8)
27     print(cat.shape)
28     plt.imshow(cat)
29     plt.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     ‘‘‘
13     if 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)
18     else:
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 
24     return 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     # 查看是否存在标注框。
37     if image.dtype != tf.float32:
38         image = tf.image.convert_image_dtype(image, dtype=tf.float32)
39 
40     # 随机的截取图片中一个块。
41     bbox_begin, bbox_size, _ = tf.image.sample_distorted_bounding_box(
42         tf.shape(image), bounding_boxes=bbox)
43     bbox_begin, bbox_size, _ = tf.image.sample_distorted_bounding_box(
44         tf.shape(image), bounding_boxes=bbox)
45     distorted_image = tf.slice(image, bbox_begin, bbox_size)
46 
47     # 将随机截取的图片调整为神经网络输入层的大小。
48     distorted_image = tf.image.resize_images(distorted_image, [height, width], method=np.random.randint(4))
49     distorted_image = tf.image.random_flip_left_right(distorted_image)
50     distorted_image = distort_color(distorted_image, np.random.randint(2))
51     return distorted_image
52 
53 def pre_main(img,bbox=None):
54     if bbox is None:
55         bbox = tf.constant([0.0, 0.0, 1.0, 1.0], dtype=tf.float32, shape=[1, 1, 4])
56     with tf.gfile.FastGFile(img, "rb") as f:
57         image_raw_data =http://www.mamicode.com/ f.read()
58     with tf.Session() as sess:
59         img_data =http://www.mamicode.com/ 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__:
71     pre_main("./123123.jpeg",bbox=None)
72     exit()

 

 技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享

『TensorFlow』图像预处理_