1、tensorflow中對jpeg格式圖像的編碼/解碼函數:
import matplotlib.pyplot as plt import tensorflow as tf image_raw_data=tf.gfile.FastGFile('/Users/jk/Downloads/timg.jpeg','rb').read() with tf.Session() as sess: img_data=tf.image.decode_jpeg(image_raw_data) #通過tf.img.decode_jpeg函數對jpeg格式的圖像進行解碼,解碼后的結果為一個張量 print(img_data.eval()) #輸出解碼后的三維矩陣 plt.imshow(img_data.eval()) plt.show() img_data=tf.image.convert_image_dtype(img_data,dtype=tf.uint8) encode_image=tf.image.encode_jpeg(img_data) #將圖像的三維矩陣重新按照jpeg格式編碼存入文件,打開該圖像可以得到和原始圖像一樣的圖像 with tf.gfile.GFile('/Users/jk/Downloads/output','wb') as f: #將文件寫入目標路徑,生成圖像文件 f.write(encode_image.eval())
2、圖像大小調整(和上面的類似,僅多了圖像大小調整的部分,下面的例子將類似):
import matplotlib.pyplot as plt import tensorflow as tf image_raw_data=tf.gfile.FastGFile('/Users/jk/Downloads/timg.jpeg','rb').read() with tf.Session() as sess: img_data=tf.image.decode_jpeg(image_raw_data) print(img_data.eval()) plt.imshow(img_data.eval()) plt.show() img_data=tf.image.convert_image_dtype(img_data,dtype=tf.uint8) resized=tf.image.resize_images(img_data,size=[300,300],method=1) #將圖像大小轉為[300,300],圖像深度在沒有明確設置前會是?, print(resized.get_shape()) resized=tf.image.convert_image_dtype(resized,dtype=tf.uint8) #數據預處理時候會把dtype轉為tf.float32,因此需要手動轉回tf.uint8 encode_image=tf.image.encode_jpeg(resized) with tf.gfile.GFile('/Users/jk/Downloads/output','wb') as f: #返回調整大小后的圖像 f.write(encode_image.eval())
通過tf.image.resize_image_with_crop_or_pad函數來調整圖像大小的功能:
croped=tf.image.resize_image_with_crop_or_pad(img_data,3000,3000) #將圖像數據擴充為3000x3000,若圖像大小大於原始數據,則使用全0填充。
通過tf.image.central_crop函數來對圖像按比例進行裁剪:
central_cropped=tf.image.central_crop(img_data,0.8) #按比例進行縮小,后面的比例必須是一個(0,1]的實數。
通過tf.image.flip_up_down函數來進行圖像翻轉:
flipped=tf.image.flip_up_down(img_data) #上下翻轉 flipped=tf.image.flip_left_right(img_data) #左右翻轉 flipped=tf.image.transpose_image(img_data) #沿對角線翻轉
3、圖像的色彩調整
通過tf.image.adjust_brightness函數進行色彩調整:
adjusted=tf.image.adjust_brightness(img_data,-0.5) #將圖像的亮度-0.5 adjusted=tf.image.adjust_brightness(img_data,+0.5) #將圖像的亮度+0.5 adjusted=tf.image.random_brightness(img_data,max_delta) #將圖像的亮度在[-max_delta,max_delta]范圍內隨機調整
通過tf.image.adjust_contrast函數來調整圖像的對比度:
adjusted=tf.image.adjust_contrast(img_data,5) #將圖像的對比度+5 adjusted=tf.image.random_contrast(img_data,lower,upper) #在[lower, upper]范圍內隨機調整圖像的對比度
通過tf.image.adjust_hue函數來調整圖像的色相:
adjusted=tf.image.adjust_hue(img_data,0.5) #將圖像的色相加0.5 adjusted=tf.image.random_hue(img_data,max_delta) #在[-max_delta,max_delta]范圍內隨機調整圖像的色相
通過tf.image.adjust_saturation函數調整圖像的飽和度:
adjusted=tf.image.adjust_saturation(img_data,-5) #將圖像的飽和度-5 adjusted=tf.image.random_saturation(img_data,lower,upper) #隨機調整圖像的飽和度
通過tf.image.per_image_whitening函數來對圖像進行標准化:
adjusted=tf.image.per_image_standardization(img_data) #對圖像進行標准化,轉化成亮度均值為0,方差為1.
4、處理標注框:
通過tf.image.draw_bounding_boxes函數在圖像中加入標注框
import matplotlib.pyplot as plt import tensorflow as tf image_raw_data=tf.gfile.FastGFile('/Users/jk/Downloads/timg.jpeg','rb').read() with tf.Session() as sess: img_data=tf.image.decode_jpeg(image_raw_data) #通過tf.img.decode_jpeg函數對jpeg格式的圖像進行解碼,解碼后的結果為一個張量 img_data=tf.image.convert_image_dtype(img_data,dtype=tf.uint8) img_data=tf.image.resize_images(img_data,[180,267],method=1) batched=tf.expand_dims(tf.image.convert_image_dtype(img_data,tf.float32),0) boxes=tf.constant([[[0.05,0.05,0.9,0.7],[0.35,0.47,0.5,0.56]]])#標注框的表示形式:[y_min, x_min, y_max, x_max],組成部分為3維數組,分別對應[batch, N, 4],左邊的boxes的shape為[1,2,4] result=tf.image.draw_bounding_boxes(batched,boxes) plt.imshow(result[0].eval()) plt.show()
5、提取標注框內的圖像:(不知道為何畫出來的標注框和通過標注框截取的內容不一致)
import matplotlib.pyplot as plt import tensorflow as tf image_raw_data=tf.gfile.FastGFile('C:/Users/1/Desktop/01.jpg','rb').read() with tf.Session() as sess: img_data=tf.image.decode_jpeg(image_raw_data) #通過tf.img.decode_jpeg函數對jpeg格式的圖像進行解碼,解碼后的結果為一個張量 img_data=tf.image.convert_image_dtype(img_data,dtype=tf.uint8) img_data=tf.image.resize_images(img_data,[180,267],method=1) boxes=tf.constant([[[0.35,0.1,0.8,0.7],[0.4,0.47,0.5,0.56]]]) #通過提供標注框的方式告訴隨機截圖的算法哪些部分是有信息量的 begin,size,bbox=tf.image.sample_distorted_bounding_box(tf.shape(img_data),bounding_boxes=boxes) batched=tf.expand_dims(tf.image.convert_image_dtype(img_data,tf.float32),0) #需要增加一維才能畫框 img_with_box=tf.image.draw_bounding_boxes(batched,bbox) #在原圖像的基礎上畫標注框 distorted_image=tf.slice(img_data,begin,size) #截取隨機得到的圖像 plt.imshow(distorted_image.eval()) plt.show() plt.imshow(img_with_box[0].eval()) plt.show()