天天看點

tensorflow圖像預處理

讀寫圖像

import matplotlib.pyplot as plt
import tensorflow as tf

pic_path = 'demo.jpeg'
image_raw = tf.gfile.FastGFile(pic_path,'rb').read()
#從原圖直接讀出來的是一串二進制代碼,需要将其解碼得到像素數組

with tf.Session() as sess:
    img_data = tf.image.decode_jpeg(image_raw)

    print(img_data.eval())

    plt.imshow(img_data.eval())
    plt.show()
    #将像素數組編碼重新寫入後得到原圖
    img_raw = tf.image.encode_jpeg(img_data)
    with tf.gfile.GFile('out_pic','wb') as      

調整圖像大小

#method可以取0,1,2,3,代表4種不同的調整算法
#0:雙線性插值法
#1:最近鄰居法
#2:雙三次插值法
#3:面積插值法
new_size = [300,300]
image = tf.image.resize_images(image,new_size,method=0)      
#若調整後的圖像比原圖小,則裁剪,若比原圖大,則在周圍填充      
image = tf.image.central_crop(image,0.5)      
image = tf.image.crop_to_bounding_box(image,bbox)      

翻轉圖像

#上下翻轉
flipped = tf.image.flip_up_donw(image)
#左右翻轉
flipped = tf.image.flip_left_right(image)
#沿對角線翻轉
transposed = tf.image.transpose_image(image)
#50%的幾率上下翻轉
rand_flipped = tf.image.randon_flip_up_down(image)
#50%的幾率左右翻轉      

調整亮度

brightness = 0.5
adjusted = tf.image.adjust_brightness(image,brightness)
#在(-brightness,+brightness)區間随機調整亮度      

調整對比度

contrast = 0.5
adjusted = tf.image.adjust_contrast(image,contrast)
#在(lower,upper)區間随機調整對比度      

調整色相

hue = 0.5      

調整飽和度

saturation = 5      

圖像标準化

#将三維矩陣變成均值為0,方差為1      

處理标注框

pic_path = 'demo.jpeg'
image_raw = tf.gfile.FastGFile(pic_path,'rb').read()  
#從原圖直接讀出來的是一串二進制代碼,需要将其解碼得到像素數組

with tf.Session() as sess:
    img_data = tf.image.decode_jpeg(image_raw)

    img_data = tf.image.resize_images(img_data,[180,267],method=1)
    #函數處理的是一個batch的圖檔,是以要加一個次元
    #另外,需要先将dtype轉化為flaot32,這時像素資料會轉化為0-1的實數
    batch = 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.5]]])
    result = tf.image.draw_bounding_boxes(batch,boxes)

    plt.imshow(result[0].eval())
    plt.show()      

随機截取圖像

import matplotlib.pyplot as plt
import tensorflow as tf

pic_path = 'demo.jpeg'
image_raw = tf.gfile.FastGFile(pic_path,'rb').read()  
#從原圖直接讀出來的是一串二進制代碼,需要将其解碼得到像素數組

with tf.Session() as sess:
    img_data = tf.image.decode_jpeg(image_raw)
    img_data = tf.image.resize_images(img_data,[180,267],method=1)
    batch = 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.5]]])
    #min_object_converd表示随機的标注框與某個已知的标注框有40%的部分重合
    #begin是标注框左上角的坐标,size是長和寬
    begin,size,bbox_for_draw = tf.image.sample_distorted_bounding_box(
        tf.shape(img_data),bounding_boxes=boxes,min_object_covered=.4
    )
    result = tf.image.draw_bounding_boxes(batch,bbox_for_draw)
    plt.imshow(result[0].eval())

    #根據begin,size截取圖像      

完整樣例

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

def distort_color(image,color_ordering):
    #調整亮度,飽和度,色相,對比度,調整順序不同最終結果也會不一樣
    if color_ordering == 0:
        image = tf.image.random_brightness(image, max_delta=32. / 255.)
        image = tf.image.random_saturation(image, lower=.5, upper=1.5)
        image = tf.image.random_hue(image, max_delta=.2)
        image = tf.image.random_contrast(image, lower=.5, upper=1.5)

    elif color_ordering == 1:
        image = tf.image.random_saturation(image, lower=.5, upper=1.5)
        image = tf.image.random_brightness(image, max_delta=32. / 255.)
        image = tf.image.random_contrast(image, lower=.5, upper=1.5)
        image = tf.image.random_hue(image, max_delta=.2)

    else:
        pass

    #圖像處理後像素值可能會超過(0,1)區間
    return tf.clip_by_value(image,0.,1.)

def process_for_train(image,height,width,bbox):
    if bbox is None:
        bbox = tf.constant([0.0,0.0,1.0,1.0],dtype=tf.float32,shape=[1,1,4])

    if image.dtype != tf.float32:
        image = tf.image.convert_image_dtype(image,tf.float32)
    #随機切割一部分
    #注意bbox的格式是[[[ ]]]
    begin,size,_ = tf.image.sample_distorted_bounding_box(tf.shape(image),bounding_boxes=bbox,min_object_covered=.1)
    distorted_image = tf.slice(image,begin,size)
    #還原到神經網絡輸入大小
    distorted_image = tf.image.resize_images(distorted_image,[height,width],method=np.random.randint(4))
    distorted_image = tf.image.random_flip_left_right(distorted_image)
    distorted_image = distort_color(distorted_image,np.random.randint(2))

    return distorted_image


pic_path = 'demo.jpeg'
image_raw = tf.gfile.FastGFile(pic_path,'rb').read()  
#從原圖直接讀出來的是一串二進制代碼,需要将其解碼得到像素數組

with tf.Session() as sess:
    img_data = tf.image.decode_jpeg(image_raw)
    boxes = tf.constant([[[0.05,0.05,0.9,0.7],[0.35,0.47,0.5,0.56]]])

    for i in range(6):
        result = process_for_train(img_data,299,299,boxes)
        plt.imshow(result.eval())
        plt.show()