TyranitarX Connect.

TensorFlow数据增强API学习

Word count: 447Reading time: 2 min
2019/07/22 Share
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# crop
# resize
# flip
# brightness & contrast

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow

# 读取原图
name = './gugong.png'
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)

plt.show()

# resize 对图像进行缩放
# tf.image.resize_area
# tf.image.resize_bicubic
# tf.image.resize_nearest_neighbor


name = './gugong.png'
img_string = tf.read_file(name)
img_decoded = tf.image.decode_image(img_string)
# 由于tensorflow是对每一个minibatch进行处理,因此需要将图像变为4维。
img_decoded = tf.reshape(img_decoded, [1, 402, 600, 3])
resize_img = tf.image.resize_bicubic(img_decoded, [804,1200])
sess = tf.Session()
img_decoded_val = sess.run(resize_img)
# 将图像reshape回3维
img_decoded_val = img_decoded_val.reshape([804,1200,3])
img_decoded_val = np.asarray(img_decoded_val, np.uint8)
print(img_decoded_val.shape)

imshow(img_decoded_val)

plt.show()

# crop 对图像进行填充/裁剪
# tf.image.pad_to_bounding_box
# tf.image.crop_to_bounding_box
# tf.random_crop


name = './gugong.png'
img_string = tf.read_file(name)
img_decoded = tf.image.decode_image(img_string)
# 由于tensorflow是对每一个minibatch进行处理,因此需要将图像变为4维。
img_decoded = tf.reshape(img_decoded, [1, 402, 600, 3])
padded_img = tf.image.pad_to_bounding_box(img_decoded, 50, 100, 500, 800)
sess = tf.Session()
img_decoded_val = sess.run(padded_img)
# 将图像reshape回3维
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

name = './gugong.png'
img_string = tf.read_file(name)
img_decoded = tf.image.decode_image(img_string)
# 由于tensorflow是对每一个minibatch进行处理,因此需要将图像变为4维。
img_decoded = tf.reshape(img_decoded, [1, 402, 600, 3])
fliped_img = tf.image.flip_up_down(img_decoded)
sess = tf.Session()
img_decoded_val = sess.run(fliped_img)
# 将图像reshape回3维
img_decoded_val = img_decoded_val.reshape([402, 600, 3])
img_decoded_val = np.asarray(img_decoded_val, np.uint8)
print(img_decoded_val.shape)

imshow(img_decoded_val)

plt.show()

# brightenss 改变图像光照/对比度
# tf.image.adjust_brightness
# tf.image.random_brightness
# tf.image.adjust_constrast
# tf.image.random_constrast

name = './gugong.png'
img_string = tf.read_file(name)
img_decoded = tf.image.decode_image(img_string)
# 由于tensorflow是对每一个minibatch进行处理,因此需要将图像变为4维。
img_decoded = tf.reshape(img_decoded, [1, 402, 600, 3])
brightness_img = tf.image.random_brightness(img_decoded, 1)
sess = tf.Session()
img_decoded_val = sess.run(brightness_img)
# 将图像reshape回3维
img_decoded_val = img_decoded_val.reshape([402, 600, 3])
img_decoded_val = np.asarray(img_decoded_val, np.uint8)
print(img_decoded_val.shape)

imshow(img_decoded_val)

plt.show()
CATALOG