1.統(tǒng)一調(diào)整圖像尺寸
__author__ = 'ding'
'''
TensorFlow 圖像處理
'''
import tensorflow as tf
import matplotlib.pyplot as plt
image_raw_data = tf.gfile.FastGFile('./path/to/picture1.jpeg', 'rb').read()
with tf.Session() as sess:
# 將圖像以jpeg的格式解碼從而得到圖像對(duì)應(yīng)的三維矩陣
# tf.image_decode_png 函數(shù)對(duì)png格式圖形進(jìn)行解碼绣夺。解碼之后得到一個(gè)張量
# tf.image_decode_jpeg 函數(shù)對(duì)jpeg格式圖形進(jìn)行解碼。
img_data = tf.image.decode_jpeg(image_raw_data)
print(img_data.eval())
img_data = tf.image.convert_image_dtype(img_data, dtype=tf.uint8)
# # 導(dǎo)出
# img_export(img_data)
#
# # 調(diào)整尺寸
# img_size(img_data)
#
# # 裁剪
# img_fill_cut(img_data)
#
# # 翻轉(zhuǎn)
# img_transposed(img_data)
#
# # 色彩調(diào)整
# img_color(img_data)
1.圖像導(dǎo)出
def img_export(img_data):
# 將一張圖像的三維矩陣重新按jpeg格式編碼并存入文件中欢揖,可以得到和原始圖像一樣的圖像
encoded_image = tf.image.encode_jpeg(img_data)
with tf.gfile.GFile('./path/to/output.jpeg', 'wb') as f:
f.write(encoded_image.eval())
2.調(diào)整圖像大小
def img_size(img_data):
# tf.image.resize_images 函數(shù)調(diào)整圖像的大小陶耍,
# 一個(gè)參數(shù)為原始圖像
# 第二個(gè)參數(shù)為圖像尺寸
# 第三個(gè)給出調(diào)整圖像大小的算法
# method=0 雙線(xiàn)性插值法
# method=1 最近鄰居法
# method=2 雙三次插值法
# method=3 面積插值法
resized_0 = tf.image.resize_images(img_data, (300, 300), method=0)
resized_1 = tf.image.resize_images(img_data, (300, 300), method=1)
resized_2 = tf.image.resize_images(img_data, (300, 300), method=2)
resized_3 = tf.image.resize_images(img_data, (300, 300), method=3)
# print(img_data.get_shape())
plt.figure(0)
plt.imshow(resized_0.eval())
plt.figure(1)
plt.imshow(resized_1.eval())
plt.figure(2)
plt.imshow(resized_2.eval())
plt.figure(3)
plt.imshow(resized_3.eval())
plt.show()
3.圖像裁剪、填充
def img_fill_cut(img_data):
# 通過(guò)tf.image.resize_image_with_crop_or_pad函數(shù)調(diào)整圖像大小浸颓,
# 第一個(gè)參數(shù)為原始圖像物臂,第二旺拉、三個(gè)參數(shù)分別表示圖像的長(zhǎng)高
# 如果原始尺寸大于指定尺寸产上,則進(jìn)行裁剪
# 如果原始尺寸小于指定尺寸,則進(jìn)行填充蛾狗,默認(rèn)為0 黑色
corped = tf.image.resize_image_with_crop_or_pad(img_data, 300, 300)
padded = tf.image.resize_image_with_crop_or_pad(img_data, 3000, 3000)
plt.figure(0)
plt.imshow(corped.eval())
plt.figure(1)
plt.imshow(padded.eval())
# 通過(guò)tf.image.central_crop函數(shù)按比例裁剪圖像
# 第一個(gè)參數(shù)為原始圖像
# 第二個(gè)參數(shù)為裁剪比例晋涣,范圍(0,1]
central_cropped = tf.image.central_crop(img_data, 0.5)
plt.figure(3)
plt.imshow(central_cropped.eval())
plt.show()
4.圖像翻轉(zhuǎn)
def img_transposed(img_data):
# 上下翻轉(zhuǎn)
flipped_up_down = tf.image.flip_up_down(img_data)
# 左右翻轉(zhuǎn)
flipeed_left_right = tf.image.flip_left_right(img_data)
# 對(duì)角線(xiàn)翻轉(zhuǎn)
transposed = tf.image.transpose_image(img_data)
plt.figure(0)
plt.imshow(flipped_up_down.eval())
plt.figure(1)
plt.imshow(flipeed_left_right.eval())
plt.figure(2)
plt.imshow(transposed.eval())
plt.show()
# 以一定概率上下翻轉(zhuǎn)
flipped = tf.image.random_flip_up_down(img_data)
# 以一定概率左右翻轉(zhuǎn)
flipped_left_right = tf.image.random_flip_left_right(img_data)
這樣的用法是,隨機(jī)翻轉(zhuǎn)訓(xùn)練圖像沉桌,讓模型可以識(shí)別不同角度的實(shí)體
5.圖像色彩調(diào)整
def img_color(img_data):
def img_color(img_data):
# 亮度
adjusted_brightness_sub = tf.image.adjust_brightness(img_data, -0.5)
adjusted_brightness_add = tf.image.adjust_brightness(img_data, 0.5)
adjusted_brightness_random = tf.image.random_brightness(img_data, max_delta=1)
# 對(duì)比度
adjusted_contrast_sub = tf.image.adjust_contrast(img_data, -0.5)
adjusted_contrast_add = tf.image.adjust_contrast(img_data, 0.5)
adjusted_contrast_random = tf.image.random_contrast(img_data, lower=0, upper=1)
# 色相
adjusted_hue1 = tf.image.adjust_hue(img_data, 0.1)
adjusted_hue2 = tf.image.adjust_hue(img_data, 0.3)
adjusted_hue3 = tf.image.adjust_hue(img_data, 0.6)
adjusted_hue4 = tf.image.adjust_hue(img_data, 0.9)
adjusted_hue_random = tf.image.random_hue(img_data, max_delta=0.5)
# 飽和度
adjusted_saturation_sub = tf.image.adjust_saturation(img_data,-5)
adjusted_saturation_add = tf.image.adjust_saturation(img_data, 5)
adjusted_saturation_random = tf.image.random_saturation(img_data, -5, 5)
6.標(biāo)注框
img_data = tf.image.resize_images(img_data,(180,267),method=1)
batched = tf.expand_dims(tf.image.convert_image_dtype(img_data,dtype=tf.float32),0)
# [0.05,0.05,0.9,0.7] 表示(180*0.05,180*0.9) (267*0.05,267*0.7)之間的圖像
# [y_min,x_min,y_max,x_max]
boxes = tf.constant([[[0.05,0.05,0.9,0.7],[0.35,0.47,0.5,0.56]]])
result = tf.image.draw_bounding_boxes(batched,boxes)
plt.imshow(result.eval().reshape([180, 267, 3]))
plt.show()
boxes = tf.constant([[[0.05, 0.05, 0.9, 0.7], [0.35, 0.47, 0.5, 0.56]]])
begin, size, bbox_for_draw = tf.image.sample_distorted_bounding_box(
tf.shape(img_data), bounding_boxes=boxes, min_object_covered=0.1
)
batched = tf.expand_dims(
tf.image.convert_image_dtype(img_data, dtype=tf.float32), 0
)
image_with_box = tf.image.draw_bounding_boxes(batched, bbox_for_draw)
distorted_image = tf.slice(img_data, begin, size)
plt.figure(0)
plt.imshow(image_with_box.eval().reshape([180, 267, 3]))
plt.figure(1)
plt.imshow(distorted_image.eval())
plt.show()
min_object_covered=0.1 需要加入谢鹊,否則將會(huì)報(bào)錯(cuò)