image——Data Augmentation的代码

MachineLP的Github(欢迎follow):https://github.com/MachineLP

切片(crop):

def crop(image, random_crop, image_size):
    if image.shape[1]>image_size:
        sz1 = int(image.shape[1]//2)
        sz2 = int(image_size//2)
        if random_crop:
            diff = sz1-sz2
            (h, v) = (np.random.randint(-diff, diff+1), np.random.randint(-diff, diff+1))
        else:
            (h, v) = (0,0)
        image = image[(sz1-sz2+v):(sz1+sz2+v),(sz1-sz2+h):(sz1+sz2+h),:]
    return image


############################################################################
# 函数:crop
# 描述:随机裁剪图像
#
# 输入:图像image, crop_size
# 返回:图像image
############################################################################
def crop(image, crop_size, random_crop=True):
    if random_crop:  # 若随机裁剪
        if image.shape[1] > crop_size:
            sz1 = image.shape[1] // 2
            sz2 = crop_size // 2
            diff = sz1 - sz2
            (h, v) = (np.random.randint(0, diff + 1), np.random.randint(0, diff + 1))
            image = image[v:(v + crop_size), h:(h + crop_size), :]

    return image

# 左右上下翻转
def flip(image, random_flip=True):
    if random_flip and np.random.choice([True, False]):
        image = np.fliplr(image)
    if random_flip and np.random.choice([True, False]):
        image = np.flipud(image)
    return image

#图像旋转
def random_rotate_image(image):
    angle = np.random.uniform(low=-10.0, high=10.0)
    return misc.imrotate(image, angle, 'bicubic')


############################################################################
# 函数:rotation
# 描述:随机旋转图片,增强数据,用图像边缘进行填充。
#
# 输入:图像image
# 返回:图像image
############################################################################
def rotation(image, random_flip=True):
    if random_flip and np.random.choice([True, False]):
        w,h = image.shape[1], image.shape[0]
        # 0-180随机产生旋转角度。
        angle = np.random.randint(0,180)
        RotateMatrix = cv2.getRotationMatrix2D(center=(image.shape[1]/2, image.shape[0]/2), angle=angle, scale=0.7)
        # image = cv2.warpAffine(image, RotateMatrix, (w,h), borderValue=(129,137,130))
        #image = cv2.warpAffine(image, RotateMatrix, (w,h),borderValue=(129,137,130))
        image = cv2.warpAffine(image, RotateMatrix, (w,h),borderMode=cv2.BORDER_REPLICATE)
    return image

图像归一化处理:

def prewhiten(x):
    mean = np.mean(x)
    std = np.std(x)
    std_adj = np.maximum(std, 1.0/np.sqrt(x.size))
    y = np.multiply(np.subtract(x, mean), 1/std_adj)
    return y


图像平移:

############################################################################
# 函数:translation
# 描述:随机平移图片,增强数据,用图像边缘进行填充。
#
# 输入:图像image
# 返回:图像image
############################################################################
def translation(image, random_flip=True):
    if random_flip and np.random.choice([True, False]):
        w,h = 1920, 1080
        H1 = np.float32([[1,0],[0,1]])
        H2 = np.random.uniform(50,500, [2,1])
        H = np.hstack([H1, H2])
        # H = np.float32([[1,0,408],[0,1,431]])
        print (H)
        image = cv2.warpAffine(image, H, (w,h), borderMode=cv2.BORDER_REPLICATE)
    return image


调整光照

# 光照调节也可以用log, 参数调节和gamma相反;

# img = exposure.adjust_log(img, 1.3)

from skimage import exposure
import numpy as np
def gen_exposure(image, random_xp=True):
    if random_xp and np.random.choice([True, False]):
        image = exposure.adjust_gamma(image, 1.2) # 调暗
    if random_xp and np.random.choice([True, False]):
        image = exposure.adjust_gamma(image, 1.5) # 调暗
    if random_xp and np.random.choice([True, False]):
        image = exposure.adjust_gamma(image, 0.9) # 调亮
    if random_xp and np.random.choice([True, False]):
        image = exposure.adjust_gamma(image, 0.8) # 调亮
    if random_xp and np.random.choice([True, False]):
        image = exposure.adjust_gamma(image, 0.7) # 调暗
    return image

还可以这么来:

# 改变亮度
def random_brightness(image, max_delta=63, seed=None):
    img = np.array(image)
    delta = np.random.uniform(-max_delta, max_delta)
    image = Image.fromarray(np.uint8(img + delta))
    return image

# 改变旋转
def random_Rotation(image):
    """
     对图像进行随机任意角度(0~360度)旋转
    :param mode 邻近插值,双线性插值,双三次B样条插值(default)
    :param image PIL的图像image
    :return: 旋转转之后的图像
    """
    random_angle = np.random.randint(1, 360)
    image = Image.rotate(random_angle)
    return image

# 改变对比度
def random_contrast(image, lower, upper, seed=None):
    factor = np.random.uniform(-lower, upper)
    mean = (image[0] + image[1] + image[2]).astype(np.float32) / 3
    img = np.zeros(image.shape, np.float32)
    for i in range(0, 3):
        img[i] = (img[i] - mean) * factor + mean
    return img

# 裁剪图片
def crop(image, name, crop_size, padding_size):
    (width, height) = image.shape
    cropped_images = []
    for i in xrange(0, width, padding_size):
        for j in xrange(0, height, padding_size):
            box = (i, j, i+crop_size, j+crop_size) #left, upper, right, lower
            cropped_name = name + '_' + str(i) + '_' + str(j) + '.jpg'
            cropped_image = image[i:i+crop_size, j:j+crop_size]
            resized_image = cv2.resize(cropped_image, (IMAGE_SIZE, IMAGE_SIZE))
            cropped_images.append(resized_image)
 
    return cropped_images




# 数据扩增
# 将选取的图片文件进行「左右翻转」「改变亮度」「改变对比度」「裁剪」操作data_num次
def data_augmentation(image_files, data_num):
    image_list = []
    file_num = len(image_files)
 
    for image_file in image_files:
        image_list.append(misc.imread(image_file))
 
    if file_num >= data_num:
        return image_list

    for image in image_list:
        rotate_image = random_Rotation(image)
        image_list.append(rotate_image)
        if len(image_list) == data_num:
            return image_list



  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MachineLP

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值