tensorflow图像数据增强预处理

 灰度图添加随机高斯噪音

    def gauss_image(image, std):
        image_f = tf.cast(image, tf.float32)
        gs = std * tf.random_normal(tf.shape(image))#创建噪声图像
        image_gauss = image_f + gs
        image_gauss = tf.maximum(image_gauss, 0) #图像最小像素值为0
        image_gauss = tf.minimum(image_gauss, 255)#图像最大像素值为255
        image_gauss = tf.cast(image_gauss, tf.uint8)
        return image_gauss

    def random_gauss(image, max_std):
        fact = tf.random_uniform([1], 0, max_std) #确定随机sigma范围0~max_std
        return gauss_image(image, fact)

 灰度图随机裁切正方形区域

    def crop(image,CROP_SIZE,seed1,seed2):
        r = image
        w = tf.cast(tf.shape(r)[1],tf.float32)#获取图像宽度
        h = tf.cast(tf.shape(r)[0],tf.float32)#获取图像高度
        offset1 = tf.cast(tf.floor(tf.random_uniform([1], 0, w - CROP_SIZE+1, seed=seed1)), dtype=tf.int32)#用随机种子seed1获取裁切左上角x坐标
        offset2 = tf.cast(tf.floor(tf.random_uniform([1], 0, h - CROP_SIZE+1, seed=seed2)), dtype=tf.int32)#用随机种子seed2获取裁切左上角y坐标
        r = tf.cond(w>CROP_SIZE,
                     lambda:tf.image.crop_to_bounding_box(r, offset2[0], offset1[0], CROP_SIZE, CROP_SIZE),
                     lambda:r)
        return r

 灰度图旋转给定角度

    def random_rotate_image(image, angle,borderValue=0):
        def random_rotate_image_func(image, angle):
            # grab the dimensions of the image and then determine the
            # center
            (h, w) = image.shape[:2]
            (cX, cY) = (w // 2, h // 2)
            # grab the rotation matrix (applying the negative of the
            # angle to rotate clockwise), then grab the sine and cosine
            # (i.e., the rotation components of the matrix)
            # -angle位置参数为角度参数负值表示顺时针旋转; 1.0位置参数scale是调整尺寸比例(图像缩放参数),建议0.75
            M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
            cos = np.abs(M[0, 0])
            sin = np.abs(M[0, 1])
            # compute the new bounding dimensions of the image
            nW = int((h * sin) + (w * cos))
            nH = int((h * cos) + (w * sin))
            # adjust the rotation matrix to take into account translation
            M[0, 2] += (nW / 2) - cX
            M[1, 2] += (nH / 2) - cY
            # perform the actual rotation and return the image
            # borderValue 缺失背景填充色彩,此处为白色,可自定义
            image_rotate = cv2.warpAffine(image, M, (nW, nH),borderValue=(borderValue,borderValue,borderValue))
            return image_rotate,nH,nW
        image_rotate,H,W = tf.py_func(random_rotate_image_func, [image,angle], [tf.uint8,tf.int64,tf.int64])
        image_rotate = tf.reshape(image_rotate,[H,W,1])
        return image_rotate

 

 示例

    #从文件名dataset 到图像tensor的map函数
    def _parse_function(src_path, label_path = None):
        src_string = tf.read_file(src_path)
        try:
            src_decoded = tf.image.decode_bmp(src_string,channels=1)
        except:
            src_decoded = tf.image.decode_jpeg(src_string, channels=1)
        if(image_dir_label is not None):
            label_string = tf.read_file(label_path)
            label_decoded = tf.image.decode_png(label_string,channels=1)

            if(True):
                #随机裁切(每次用同样的随机种子对原图和标签都进行裁切)
                seed1 = random.randint(0, 2**31 - 1)
                seed2 = random.randint(0, 2**31 - 1)
                src_decoded = crop(src_decoded,512,seed1,seed2)
                label_decoded = crop(label_decoded,512,seed1,seed2)
                #随机旋转一定角度
                angle = np.random.uniform(low=-30.0, high=30.0)
                src_decoded = random_rotate_image(src_decoded,angle,borderValue=213)
                label_decoded = random_rotate_image(label_decoded,angle)
                #增加随机亮度调整
                # src_bright = tf.image.random_brightness(src_decoded,0.2)
                #随机增加高斯噪声
                src_gauss = random_gauss(src_decoded,10)
                return src_gauss, label_decoded
            else:
                return src_decoded,label_decoded
        else:
            return src_decoded

    #构造文件名dataset
    src_dirs = ['1.bmp','2.bmp','3.bmp']
    label_dirs = ['1.png','2.png','3.png']
    dataset = Dataset.from_tensor_slices((src_dirs,label_dirs))
    dataset = dataset.map(_parse_function)
    iterator = dataset.make_one_shot_iterator()
    one_element = iterator.get_next()

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值