# 資料增強 ## 隨機裁切 ```python= %matplotlib inline import tensorflow as tf from PIL import Image import numpy as np import matplotlib.pyplot as plt raw_image = Image.open('view.jpg') raw_image = np.asarray(raw_image) #array和asarray都可以將結構資料轉化為ndarray,但是主要區別就是當資料來源是ndarray時,array仍然會copy出一個副本,佔用新的記憶體,但asarray不會。 print('origin image') print(raw_image.shape) plt.imshow(raw_image) plt.show() rand_crop_img = tf.random_crop(raw_image, [300,200,3]) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) img_ = sess.run(rand_crop_img) print('random crop') print(img_.shape) plt.imshow(img_) plt.show() ``` ## 翻面 ```python= %matplotlib inline import tensorflow as tf from PIL import Image import numpy as np import matplotlib.pyplot as plt raw_image = Image.open('view.jpg') raw_image = np.asarray(raw_image) print('origin image') print(raw_image.shape) plt.imshow(raw_image) plt.show() flip_img = tf.image.flip_left_right(raw_image) # 左右翻轉 # flip_img = tf.image.flip_up_down(raw_image) #上下翻轉 # flip_img = tf.image.transpose_image(raw_image) #對角線翻轉 # flip_img = tf.image.random_flip_up_down(raw_image) #隨機上下翻轉 # flip_img = tf.image.random_flip_left_right(raw_image) #隨機左右翻轉 ; 50% will flip image with tf.Session() as sess: img_ = sess.run(flip_img) print('flip') print(img_.shape) plt.imshow(img_) plt.show() ``` ## 調整亮度 ```python= %matplotlib inline import tensorflow as tf from PIL import Image import numpy as np import matplotlib.pyplot as plt raw_image = Image.open('view.jpg') raw_image = np.asarray(raw_image) print('origin image') print(raw_image.shape) plt.imshow(raw_image) plt.show() bright_img = tf.image.adjust_brightness(raw_image, delta= 0.5) #delta值應在0~1之間 ;因為以float type添加到圖片中 ; 數值越大越亮 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) img_ = sess.run(bright_img) print('adjust brightness') print(img_.shape) plt.imshow(img_) plt.show() ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up