数据增强


import os
from PIL import Image
from PIL import ImageEnhance
import cv2
import tensorflow as tf
import numpy as np
"""
1、对比度:白色画面(最亮时)下的亮度除以黑色画面(最暗时)下的亮度;
2、色彩饱和度::彩度除以明度,指色彩的鲜艳程度,也称色彩的纯度;
3、色调:向负方向调节会显现红色,正方向调节则增加黄色。适合对肤色对象进行微调;
4、锐度:是反映图像平面清晰度和图像边缘锐利程度的一个指标。
"""
def augument(image_path, parent):
    # 读取图片
    image = Image.open(image_path)
    image_name = os.path.split(image_path)[1]
    name = os.path.splitext(image_name)[0]
    # 变亮
    # 亮度增强,增强因子为0.0将产生黑色图像;为1.0将保持原始图像。
    enh_bri = ImageEnhance.Brightness(image)
    brightness = 1.5
    image_brightened1 = enh_bri.enhance(brightness)
    image_brightened1.save(os.path.join(parent, '{}_bri1.jpg'.format(name)))
    变暗
    enh_bri = ImageEnhance.Brightness(image)
    brightness = 0.8
    image_brightened2 = enh_bri.enhance(brightness)
    image_brightened2.save(os.path.join(parent, '{}_bri2.jpg'.format(name)))
    # 色度,增强因子为1.0是原始图像
    # 色度增强
    enh_col = ImageEnhance.Color(image)
    color = 1.5
    image_colored1 = enh_col.enhance(color)
    image_colored1.save(os.path.join(parent, '{}_col1.jpg'.format(name)))
    # 色度减弱
    enh_col = ImageEnhance.Color(image)
    color = 0.8
    image_colored1 = enh_col.enhance(color)
    image_colored1.save(os.path.join(parent, '{}_col2.jpg'.format(name)))
    # 对比度,增强因子为1.0是原始图片
    # 对比度增强
    enh_con = ImageEnhance.Contrast(image)
    contrast = 1.5
    image_contrasted1 = enh_con.enhance(contrast)
    image_contrasted1.save(os.path.join(parent, '{}_con1.jpg'.format(name)))
    # 对比度减弱
    enh_con = ImageEnhance.Contrast(image)
    contrast = 0.8
    image_contrasted2 = enh_con.enhance(contrast)
    image_contrasted2.save(os.path.join(parent, '{}_con2.jpg'.format(name)))
    # 锐度,增强因子为1.0是原始图片
    # 锐度增强
    enh_sha = ImageEnhance.Sharpness(image)
    sharpness = 3.0
    image_sharped1 = enh_sha.enhance(sharpness)
    image_sharped1.save(os.path.join(parent, '{}_sha1.jpg'.format(name)))
    # 锐度减弱
    enh_sha = ImageEnhance.Sharpness(image)
    sharpness = 0.8
    image_sharped2 = enh_sha.enhance(sharpness)
    image_sharped2.save(os.path.join(parent, '{}_sha2.jpg'.format(name)))

#几何增强
def enh(image_path, parent):
    # 读取图片
    img = cv2.imread(image_path)
    image_name = os.path.split(image_path)[1]
    name = os.path.splitext(image_name)[0]
    # 随机裁剪
    img = cv2.resize(img, (250, 250), interpolation=cv2.INTER_CUBIC)
    crop_img = tf.random_crop(img, [224, 224, 3])
    cv2.imwrite(os.path.join(parent, '{}_crop.jpg'.format(name)), crop_img.eval())
    #随机垂直反转
    v_flip_img = tf.image.random_flip_up_down(img)
    cv2.imwrite(os.path.join(parent, '{}_ v_flip.jpg'.format(name)), v_flip_img.eval())
    # 随机水平反转
    h_flip_img = tf.image.random_flip_left_right(img)
    cv2.imwrite(os.path.join(parent, '{}_ h_flip.jpg'.format(name)), h_flip_img.eval())
    # 仿射变化
    rows, cols = img.shape[:2]
    src_points = np.float32([[0, 0], [cols - 1, 0], [0, rows - 1]])
    dst_points = np.float32([[0, 0], [int(0.6 * (cols - 1)), 0], [int(0.4 * (cols - 1)), rows - 1]])
    affine_matrix = cv2.getAffineTransform(src_points, dst_points)
    img_Aff = cv2.warpAffine(img, affine_matrix, (cols, rows))
    cv2.imwrite(os.path.join(parent, '{}_ Aff.jpg'.format(name)), img_Aff.eval())

dir = 'img\\num'
for parent, dirnames, filenames in os.walk(dir):
    for filename in filenames:
        fullpath = os.path.join(parent + '\\', filename)
        sess = tf.InteractiveSession()
        enh(fullpath, parent)
        sess.close()
        augument(fullpath, dirnames)




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值