本文具體數據集與源代碼可從我的GitHub地址獲取
https://github.com/liuzuoping/Deep_Learning_note
數據預處理
編碼處理
- 以下代碼示範對 JPEG 格式圖像的解碼處理
#--coding--:utf-8
import tensorflow as tf
import matplotlib.pyplot as plt
# 讀取圖像的原始數據
image_raw_data = tf.gfile.FastGFile('images/plate1.jpg', 'rb').read()
# 使用pyplot顯示圖像
def show(img_data):
plt.imshow(img_data.eval())
plt.show()
with tf.Session() as sess:
# 將原始數據解碼成多維矩陣
img_data = tf.image.decode_jpeg(image_raw_data)
print(img_data.eval())
show(img_data)
# 將圖像的矩陣編碼成圖像並存入文件
encoded_image = tf.image.encode_jpeg(img_data)
with tf.gfile.GFile('preprocess/output.jpg', 'wb') as f:
f.write(encoded_image.eval())
# 將圖像數據的類型轉爲實數類型,便於對圖像進行處理
img_data = tf.image.convert_image_dtype(img_data, dtype=tf.float32)
大小調整
- 以下代碼都是在圖像編碼處理代碼的基礎下運行,省去了加載原始圖像,定義會話等過程
# 用resize_images調整圖像大小
# 第一個參數爲原始圖像
# 第二個參數爲調整後的圖像大小[new_height,new_width],跟舊版本分爲兩個參數不一樣
# method參數給出了調整圖像大小的算法
with tf.Session() as sess:
# 將原始數據解碼成多維矩陣
img_data = tf.image.decode_jpeg(image_raw_data)
#print(img_data.eval())
print ("原始圖像:")
show(img_data)
# 將圖像的矩陣編碼成圖像並存入文件
encoded_image = tf.image.encode_jpeg(img_data)
with tf.gfile.GFile('preprocess/output.jpg', 'wb') as f:
f.write(encoded_image.eval())
# 將圖像數據的類型轉爲實數類型,便於對圖像進行處理
img_data = tf.image.convert_image_dtype(img_data, dtype=tf.float32)
resized = tf.image.resize_images(img_data, [300, 300], method=0)
#print(resized.get_shape()) # 圖像深度沒有顯式指定則爲問號
print ("裁剪後的圖像:")
show(resized)
# 用resize_image_with_crop_or_pad調整圖像大小
# 第一個參數爲原始圖像
# 第二個和第三個參數是調整後的圖像大小,大於原圖則填充,小於則裁剪居中部分
croped = tf.image.resize_image_with_crop_or_pad(img_data, 200, 200)
print ("通過resize_image_with_crop_or_pad處理後的圖像,此圖爲裁剪後:")
show(croped)
padded = tf.image.resize_image_with_crop_or_pad(img_data, 500, 700)
print ("通過resize_image_with_crop_or_pad處理後的圖像,此圖爲填充後:")
show(padded)
# 用central_crop調整圖像大小
# 第一個參數是原始圖像
# 第二個參數爲調整比例,是(0,1]的實數
central_cropped = tf.image.central_crop(img_data, 0.5)
print ("通過比例裁剪後的圖像:")
show(central_cropped)
圖像翻轉
- 以下代碼示範了將圖像進行上下翻轉、左右翻轉及沿對角線翻轉
with tf.Session() as sess:
# 將原始數據解碼成多維矩陣
img_data = tf.image.decode_jpeg(image_raw_data)
#print(img_data.eval())
print ("原始圖像:")
show(img_data)
# 圖像翻轉
flipped = tf.image.flip_up_down(img_data) # 上下
print ("上下翻轉後:")
show(flipped)
flipped = tf.image.flip_left_right(img_data) # 左右
print ("左右翻轉後:")
show(flipped)
transposed = tf.image.transpose_image(img_data) # 對角線
print ("沿對角線反轉後: ")
show(transposed)
# 隨機翻轉圖像
flipped = tf.image.random_flip_up_down(img_data) # 隨機上下
print ("隨機上下翻轉後:")
show(flipped)
flipped = tf.image.random_flip_left_right(img_data) # 隨機左右
print ("隨機左右翻轉後:")
show(flipped)
色彩調整
圖像的色彩調整有四方面:
- 亮度
- 對比度
- 飽和度
- 色相
with tf.Session() as sess:
# 將原始數據解碼成多維矩陣
img_data = tf.image.decode_jpeg(image_raw_data)
#print(img_data.eval())
print ("原始圖像:")
show(img_data)
# 調整圖像的亮度
adjusted = tf.image.adjust_brightness(img_data, 0.5) # 將圖像的亮度+0.5
print ("將圖像的亮度+0.5:")
show(adjusted)
adjusted = tf.image.random_brightness(
img_data, max_delta=0.5) # 在[-0.5,0.5]範圍內隨機調整圖像亮度
print ("在[-0.5,0.5]範圍內隨機調整圖像亮度:")
show(adjusted)
# 調整圖像的對比度
adjusted = tf.image.adjust_contrast(img_data, -5) # 將圖像的對比度-5
print ("將圖像的對比度-5:")
show(adjusted)
adjusted = tf.image.adjust_contrast(img_data, 5) # 將圖像的對比度+5
print ("將圖像的對比度+5:")
show(adjusted)
adjusted = tf.image.random_contrast(img_data, lower=1, upper=5) # 隨機調整對比度
# 調整圖像的飽和度
adjusted = tf.image.adjust_saturation(img_data, -5) # 將飽和度-5
print ("將飽和度-5:")
show(adjusted)
adjusted = tf.image.adjust_saturation(img_data, 5) # 將飽和度+5
print ("將飽和度+5:")
show(adjusted)
adjusted = tf.image.random_saturation(img_data, lower=1, upper=5) # 隨機調整飽和度
# 調整圖像的色相
adjusted = tf.image.adjust_hue(img_data, 0.5) # 將色相+0.5
print ("將色相+0.5:")
show(adjusted)
adjusted = tf.image.random_hue(img_data, max_delta=0.5) # 隨機調整色相
print ("隨機調整色相:")
show(adjusted)