python學習筆記之TF_圖像預處理

tensorflow 圖像預處理方式

  • 除了TF常用的tf.image.XXX函數之外,還可以使用tf.py_func函數將tensor轉爲numpy進行擴展,以增加數據預處理的靈活性

  • 常見圖像數據的增強方式有:

    • 圖像讀取
      • img_raw_data = tf.gfile.Fastfile(“path/to/pic”,“r”).read()
      • img = tf.image.decode_jpeg(img_raw_data)
      • 獲取image的尺寸:Tensors.get_shape() or tf.shape(input)
    • 圖像裁剪和填充
      • 功能可參考 TensorFlow 中的如下函數:
        • tf.image.central_crop(img,0.6)#按比例中心裁剪
        • tf.image.pad_to_bounding_box
        • tf.image.crop_to_bounding_box(img,offset_h,offset_w,H,W)#給定左上角座標裁剪
        • tf.image.resize_image_with_crop_or_pad(img,H,W)#中心位置裁剪
    • 圖像縮放
      • 功能可參考 TensorFlow 中的如下函數:
        • 寬高比一致前提下tf.image.resize_images,默認的插值方法是ResizeMethod.BILINEAR
        • 若圖像的寬高比不一致,則可使用tf.image.resize_image_with_pad
          • tf.image.resize_area
          • tf.image.resize_bicubic
          • tf.image.resize_bilinear
          • tf.image.resize_nearest_neighbor
    • 圖像翻轉
      • 功能可參考 TensorFlow 中的如下函數:
        • tf.image.flip_left_right
        • tf.image.flip_up_down
    • 圖像裁剪和縮放複合變換
      • 功能可參考 TensorFlow 中的如下函數:
        • tf.image.crop_and_resize
  • tensorflow代碼實現示例:

    #coding=utf-8
    """
        #Clears the default graph stack and resets the global default graph.
        #tf.reset_default_graph()    
        tf.get_default_graph()#獲取當前默認圖
        
        with tf.gfile.GFile("path/to/save","w") as f:
            f.write(image.eval())        
        tf.write_file(name,content)
    """
    
    from __future__ import print_function
    
    import os
    import numpy as np
    import tensorflow as tf
    import cv2
    
    os.environ['CUDA_VISIBLE_DEVICES'] = '2'
    
    #使用tf.py_func()轉爲numpy鏡像翻轉填充
    def resize_112(input):
        h, w  = input.shape[:2]
        if h < 112 and w < 112:
            h_pad = (112-h)/2
            w_pad = (112-w)/2
            image_pad = np.pad(input,((h_pad,h_pad),(w_pad,w_pad),(0,0)),'reflect')
            image = cv2.resize(image_pad,(112,112))
        else:
            image = cv2.resize(input,(112,112))
        return image
        
    if __name__ == '__main__':
        
        fileName = "test.png"
        
        g1 = tf.Graph()
        with g1.as_default():
            content = tf.read_file(fileName)#讀取圖片
            
            #image = tf.image.decode_image(content,channels=3)
            img_src = tf.image.decode_png(content,channels=3)
            #print(img_src.dtype) #打印數據類型
            
            #數據類型轉換
            img_src = tf.cast(img_src, tf.float32)
            #鏡像填充
            image = tf.py_func(resize_112, [img_src], tf.float32)
            
            # define tensor shape 
            image.set_shape([112, 112, 3])
            
            #裁剪
            #img_cropped = tf.image.central_crop(img_src,0.5)#中心裁剪
            #img_cropped = tf.image.crop_to_bounding_box(img_src,3,3,80,80)#根據左上角座標,裁剪指定大小
            #img_random_crop = tf.random_crop(img_src, [79, 79, 3])#隨機裁剪固定大小
            #print(img_random_crop.get_shape())
            
            #填充
            #img_pad = tf.image.pad_to_bounding_box(img_src,10,10,100,100) #根據左上角座標,填充圖像
            
            #tf.image.resize_image_with_crop_or_pad(image, target_height, target_width)
            #img_resize = tf.image.resize_image_with_crop_or_pad(img_src,100,100)
            
            #img_brightness = tf.image.random_brightness(img_src,30.0)
            #隨機調整對比度
            #img_contrast = tf.image.random_contrast(img_src,lower=0.2,upper=1.5)
            
            #img_brightness = tf.image.adjust_brightness(img_src, 50.0)
            
            #水平左右翻轉
            #img_flip = tf.image.random_flip_left_right(img_src)
            #img_flip = tf.image.flip_left_right(img_src)
            
            
            with tf.Session() as sess:
                image_arr = sess.run(img_src)
                #print(np.mean(image_arr))
                #cv2.imwrite("image_arr1.png",image_arr[:,:,::-1])
                
                # img_crop = img_random_crop.eval()
                # img_crop = img_crop.astype("uint8")
                # print(img_crop.dtype)
                # cv2.imwrite("img_crop.png",img_crop)s
                
                image = image.eval()
                #print(np.mean(brightness))
                # #print(brightness.dtype)
                image.astype("uint8")
                cv2.imwrite("resize.png",image[:,:,::-1])
                
                
    
  • 根據上述代碼可視化預處理後的圖像數據,要注意的是:tensorflow讀取的通道順序爲rgb,opencv通道默認爲BGR,因此,需要轉化.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章