语义分割数据增强python代码

gamma transform

def gamma_transform(img, gamma):
    gamma_table = [np.power(x / 255.0, gamma) * 255.0 for x in range(256)]
    gamma_table = np.round(np.array(gamma_table)).astype((np.uint8))
    return cv2.LUT(img, gamma_table)


def random_gamma_transform(img, gamma_vari):
    log_gamma_vari = np.log(gamma_vari)
    # np.random.seed(10)
    alpha = np.random.uniform(-log_gamma_vari, log_gamma_vari)
    gamma = np.exp(alpha)
    return gamma_transform(img, gamma)

blur

def blur(img, label):
    img = cv2.blur(img, (3, 3))
    return img, label

flip

def flip(img, dir):
    """

    :param img:
    :param dir: 1-horizontal, 2-vertical, -1:horizontal&vertical
    :return:
    """
    return cv2.flip(img, dir)

Color

def randomColor(image, label):
    """
    对图像进行颜色抖动
    :param image: PIL的图像image
    :param label: 有颜色色差的图像image
    :return:
    """
    image = Image.fromarray(np.uint8(image))

    random_factor = np.random.randint(0, 31) / 10.  # 随机因子
    color_image = ImageEnhance.Color(image).enhance(random_factor)  # 调整图像的饱和度
    random_factor = np.random.randint(10, 21) / 10.  # 随机因子
    brightness_image = ImageEnhance.Brightness(color_image).enhance(random_factor)  # 调整图像的亮度
    random_factor = np.random.randint(10, 21) / 10.  # 随机因1子
    contrast_image = ImageEnhance.Contrast(brightness_image).enhance(random_factor)  # 调整图像对比度
    random_factor = np.random.randint(0, 31) / 10.  # 随机因子
    sharp_image = ImageEnhance.Sharpness(contrast_image).enhance(random_factor)

    img = np.array(sharp_image, dtype=np.uint8)

    return img, label  # 调整图像锐度

reference

你可能感兴趣的:(Deep,Learning)