python学习笔记之TF_图像预处理

本文详细介绍使用TensorFlow进行图像预处理的方法,包括图像读取、尺寸获取、裁剪、填充、缩放、翻转等操作,以及如何利用tf.py_func函数增强数据预处理的灵活性。通过具体代码示例展示如何实现图像的镜像翻转和填充。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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,因此,需要转化.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值