数据增广代码整理

import numpy as np
import tensorflow as tf
import cv2 as cv


img = cv.imread(img_path)

上下镜像

作用: 如字面意思

img = tf.image.flip_up_down(img).numpy()

左右镜像

作用: 如字面意思

img = tf.image.flip_left_right(img).numpy()

随机放大

作用: 对原始影像放大1.2-1.5倍,然后随机选择一个区域作为处理后的影像数据,区域大小与原始输入一致。

IMG_SIZE = (512,512)	# 原始影像尺寸
def up_scaling(img):
    'SCALE:1.2~1.5'
    scale = np.random.randint(20,50) / 100.0 + 1
    img_h = IMG_SIZE[0]
    img_w = IMG_SIZE[1]
    img_h_new = int(img_h * scale)
    img_w_new = int(img_w * scale)
    start_h = np.random.randint(0, img_h_new - img_h - 1)
    start_w = np.random.randint(0, img_w_new - img_w - 1)

    new_img = cv.resize(img, dsize=(img_h_new, img_w_new))
    img = new_img[start_h:start_h + IMG_SIZE[0], start_w:start_w+IMG_SIZE[1],:]
    return img

随机缩小

作用: 对原始影像缩小0.5-0.8倍,然后随机选择一个区域作为处理后的影像数据,区域大小与原始输入一致。

IMG_SIZE = (512,512)	# 原始影像尺寸
def down_scaling(img):
    'SCALE:0.5~0.8'
    scale = np.random.randint(50, 80) / 100.0
    img_h = IMG_SIZE[0]
    img_w = IMG_SIZE[1]
    img_h_new = int(img_h * scale)
    img_w_new = int(img_w * scale)
    start_h = np.random.randint(0, img_h - img_h_new - 1)
    start_w = np.random.randint(0, img_w - img_w_new - 1)
    new_img = np.ones(shape=IMG_SIZE, dtype=np.int8) * 128
    img1 = cv.resize(img, dsize=(img_w_new, img_h_new))
    new_img[start_h:start_h + img_h_new, start_w:start_w+img_w_new,:] = img1
    return new_img

MixUp

作用: 将随机的两张样本按比例混合,分类的结果按比例分配

def mixup(img1, img2, alpha):
    img = cv.addWeighted(src1=img1, alpha=alpha, src2=img2, beta=1-alpha, gamma=0)
    return img

CutOut

作用: 随机的将样本中的部分区域裁剪掉,并且填充0像素值

def cutout(img):
    h, w, c = img.shape
    random_x = np.random.randint(int(w * 0.75))
    random_y = np.random.randint(int(h * 0.75))

    random_w = np.random.randint(int(w * 0.25))
    random_h = np.random.randint(int(h * 0.25))
    img[random_y:random_y + random_h, random_x:random_x + random_w, :] = [0,0,0]
    return img
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值