深度学习数据增强的常用方法

news/2024/10/4 14:59:44 标签: 深度学习, 人工智能

以下是在深度学习中经常使用的图像增强的方法

目录

前言

1、加噪声

2、调整亮度

3、cutout

4、旋转

5、对比度增强

6、仿射变化扩充图像

7、HSV数据增强

8、错切变化扩充图像

9、平移扩充图像,根图像移动的像素距离可自行调整,具体方法如下注释所示

10、主函数(这里介绍如何调用前面的函数)


前言

数据增强是一种在深度学习中常用的技术,它通过生成新的训练样本来扩展现有的数据集。这一过程通常涉及对原始数据进行一系列变换,如旋转、缩放、裁剪、翻转、颜色调整等,从而创建出与原始数据略有不同的新样本。

1、加噪声

from skimage.util import random_noise
    # ----1.加噪声---- #
    def _addNoise(self, img):
        '''
        输入:
            img:图像array
        输出:
            加噪声后的图像array,由于输出的像素是在[0,1]之间,所以得乘以255
        '''
        # return cv2.GaussianBlur(img, (11, 11), 0)
        return random_noise(img, mode='gaussian', clip=True) * 255

2、调整亮度

  # ---2.调整亮度--- #
    def _changeLight(self, img):
        # 从边缘分布中采样
        alpha = random.uniform(0.35, 1)
        # 做了一个零矩阵
        blank = np.zeros(img.shape, img.dtype)
        # alpha为权重,alpha的img内的像素点的值 + 1-alpha的黑颜色的值
        return cv2.addWeighted(img, alpha, blank, 1 - alpha, 0)

3、cutout

# ---3.cutout--- #
    def _cutout(self, img, bboxes, length=100, n_holes=1, threshold=0.5):
        '''
        原版本:https://github.com/uoguelph-mlrg/Cutout/blob/master/util/cutout.py
        Randomly mask out one or more patches from an image.
        Args:
            img : a 3D numpy array,(h,w,c)
            bboxes : 框的坐标
            n_holes (int): Number of patches to cut out of each image.
            length (int): The length (in pixels) of each square patch.
        '''

        def cal_iou(boxA, boxB):
            # 两张图片重叠的部分称为交集,重叠的两张图片的实际占地面积成为并集
            # IOU=交集:并集
            '''
            boxA, boxB为两个框,返回iou
            boxB为bouding box
            两张图的交集/两张图的并集
            '''
            # determine the (x, y)-coordinates of the intersection rectangle
            xA = max(boxA[0], boxB[0])
            yA = max(boxA[1], boxB[1])
            xB = min(boxA[2], boxB[2])
            yB = min(boxA[3], boxB[3])

            if xB <= xA or yB <= yA:
                return 0.0

            # compute the area of intersection rectangle
            interArea = (xB - xA + 1) * (yB - yA + 1)

            # compute the area of both the prediction and ground-truth
            # rectangles
            boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)
            boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)
            iou = interArea / float(boxBArea)
            return iou

        # 得到h和w
        if img.ndim == 3:
            h, w, c = img.shape
        else:
            _, h, w, c = img.shape
        mask = np.ones((h, w, c), np.float32)
        for n in range(n_holes):
            chongdie = True  # 看切割的区域是否与box重叠太多
            while chongdie:
                # 随机选取的x和y会决定一片区域,这片区域最后被剪掉不要了
                y = np.random.randint(h)
                x = np.random.randint(w)

                y1 = np.clip(y - length // 2, 0,
                             h)  # numpy.clip(a, a_min, a_max, out=None), clip这个函数将将数组中的元素限制在a_min, a_max之间,大于a_max的就使得它等于 a_max,小于a_min,的就使得它等于a_min
                y2 = np.clip(y + length // 2, 0, h)
                x1 = np.clip(x - length // 2, 0, w)
                x2 = np.clip(x + length // 2, 0, w)

                chongdie = False
                for box in bboxes:
                    if cal_iou([x1, y1, x2, y2], box) > threshold:
                        chongdie = True
                        break
            mask[y1: y2, x1: x2, :] = 0.
        img = img * mask
        return img

4、旋转

def flip(root_path,img_name):   #翻转图像
    img = Image.open(os.path.join(root_path, img_name))
    filp_img = img.transpose(Image.FLIP_LEFT_RIGHT)
    # filp_img.save(os.path.join(root_path,img_name.split('.')[0] + '_flip.jpg'))
    return filp_img

5、对比度增强

def contrastEnhancement(root_path, img_name):  # 对比度增强
    image = Image.open(os.path.join(root_path, img_name))
    enh_con = ImageEnhance.Contrast(image)
    # contrast = 1.1+0.4*np.random.random()#取值范围1.1-1.5
    contrast = 1.5
    image_contrasted = enh_con.enhance(contrast)
    return image_contrasted

6、仿射变化扩充图像

def fangshe_bianhuan(root_path,img_name): #仿射变化扩充图像
    img = Image.open(os.path.join(root_path, img_name))

    img = cv2.cvtColor(numpy.asarray(img) , cv2.COLOR_RGB2BGR)

    h, w = img.shape[0], img.shape[1]
    m = cv2.getRotationMatrix2D(center=(w // 2, h // 2), angle=-30, scale=0.5)
    r_img = cv2.warpAffine(src=img, M=m, dsize=(w, h), borderValue=(0, 0, 0))

    r_img = Image.fromarray(cv2.cvtColor(r_img, cv2.COLOR_BGR2RGB))
    return r_img

7、HSV数据增强

def hsv(root_path,img_name):#HSV数据增强
    h_gain , s_gain , v_gain = 0.5 , 0.5 , 0.5
    img = Image.open(os.path.join(root_path, img_name))

    img = cv2.cvtColor(numpy.asarray(img) , cv2.COLOR_RGB2BGR)

    r = np.random.uniform(-1, 1, 3) * [h_gain, s_gain, v_gain] + 1  # random gains
    hue, sat, val = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HSV))
    dtype = img.dtype  # uint8

    x = np.arange(0, 256, dtype=np.int16)
    lut_hue = ((x * r[0]) % 180).astype(dtype)
    lut_sat = np.clip(x * r[1], 0, 255).astype(dtype)
    lut_val = np.clip(x * r[2], 0, 255).astype(dtype)

    img_hsv = cv2.merge((cv2.LUT(hue, lut_hue), cv2.LUT(sat, lut_sat), cv2.LUT(val, lut_val))).astype(dtype)
    aug_img = cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR)
    aug_img = Image.fromarray(cv2.cvtColor(aug_img, cv2.COLOR_BGR2RGB))
    return aug_img

8、错切变化扩充图像

def cuoqie(root_path,img_name): #错切变化扩充图像
    img = Image.open(os.path.join(root_path, img_name))

    img = cv2.cvtColor(numpy.asarray(img) , cv2.COLOR_RGB2BGR)

    h, w = img.shape[0], img.shape[1]
    origin_coord = np.array([[0, 0, 1], [w, 0, 1], [w, h, 1], [0, h, 1]])

    theta = 30  # shear角度
    tan = math.tan(math.radians(theta))

    # x方向错切
    m = np.eye(3)
    m[0, 1] = tan
    shear_coord = (m @ origin_coord.T).T.astype(np.int_)
    shear_img = cv2.warpAffine(src=img, M=m[:2],
                               dsize=(np.max(shear_coord[:, 0]), np.max(shear_coord[:, 1])),
                               borderValue=(0, 0, 0))



    c_img = Image.fromarray(cv2.cvtColor(shear_img, cv2.COLOR_BGR2RGB))
    return c_img

9、平移扩充图像,根图像移动的像素距离可自行调整,具体方法如下注释所示

def pingyi(root_path,img_name):#平移扩充图像,根图像移动的像素距离可自行调整,具体方法如下注释所示
    img = Image.open(os.path.join(root_path, img_name))
    img = cv2.cvtColor(numpy.asarray(img) , cv2.COLOR_RGB2BGR)

    cols , rows= img.shape[0], img.shape[1]
    M = np.float32([[1, 0, 50], [0, 1, 30]])#50为x即水平移动的距离,30为y 即垂直移动的距离
    dst = cv2.warpAffine(img, M, (cols, rows),borderValue=(0,255,0))
    pingyi_img = Image.fromarray(cv2.cvtColor(dst, cv2.COLOR_BGR2RGB))
    return pingyi_img

10、主函数(这里介绍如何调用前面的函数)

def createImage(imageDir,saveDir):#主函数,8种数据扩充方式,每种扩充一张
   i=0
   for name in os.listdir(imageDir):
      i=i+1
      saveName="cesun"+str(i)+".jpg"
      saveImage=contrastEnhancement(imageDir,name)
      saveImage.save(os.path.join(saveDir,saveName))
      saveName1 = "flip" + str(i) + ".jpg"
      saveImage1 = flip(imageDir,name)
      saveImage1.save(os.path.join(saveDir, saveName1))
      saveName2 = "brightnessE" + str(i) + ".jpg"
      saveImage2 = brightnessEnhancement(imageDir, name)
      saveImage2.save(os.path.join(saveDir, saveName2))
      saveName3 = "rotate" + str(i) + ".jpg"
      saveImage = rotation(imageDir, name)
      saveImage.save(os.path.join(saveDir, saveName3))
      saveName4 = "fangshe" + str(i) + ".jpg"
      saveImage = fangshe_bianhuan(imageDir, name)
      saveImage.save(os.path.join(saveDir, saveName4))
      saveName5 = "cuoqie" + str(i) + ".jpg"
      saveImage = cuoqie(imageDir, name)
      saveImage.save(os.path.join(saveDir, saveName5))
      saveName6 = "hsv" + str(i) + ".jpg"
      saveImage = hsv(imageDir, name)
      saveImage.save(os.path.join(saveDir, saveName6))
      saveName6 = "pingyi" + str(i) + ".jpg"  #不需要平移变换的,可以注释掉 这三行代码 135 136 137行
      saveImage = pingyi(imageDir, name)     #不需要平移变换的,可以注释掉 这三行代码
      saveImage.save(os.path.join(saveDir, saveName6)) #不需要平移变换的,可以注释掉 这三行代码


imageDir="jpg" #要改变的图片的路径文件夹  在当前文件夹下,建立文件夹即可
saveDir="kuochong"   #数据增强生成图片的路径文件夹
print('文件的初始文件夹为:' + imageDir)
print('----------------------------------------')
print('文件的转换后存入的文件夹为:' + saveDir)
print('----------------------------------------')
print('开始转换')
print('----------------------------------------')
createImage(imageDir,saveDir)
print('----------------------------------------')
print("数据扩充完成")


http://www.niftyadmin.cn/n/5690056.html

相关文章

【2022工业3D异常检测文献】AST: 基于归一化流的双射性产生不对称学生-教师异常检测方法

Asymmetric Student-Teacher Networks for Industrial Anomaly Detection 1、Background 所谓的学生-教师网络&#xff0c;首先&#xff0c;对教师进行训练&#xff0c;以学习语义嵌入的辅助性训练任务&#xff1b;其次&#xff0c;训练学生以匹配教师的输出。主要目的是让学生…

java线程池参数设置原则

线程池参数设置原则 1 如何为线程池设置合适的线程参数&#xff1f; 目前根据一些开源框架&#xff0c;设置多少个线程数量通常是根据应用的类型**&#xff1a;I/O 密集型、CPU 密集型。** I/O密集型 I/O密集型的场景在开发中比较常见&#xff0c;比如像 MySQL数据库读写、文…

差分基准站

什么是差分基准站&#xff1f; 大家好我小智&#xff0c;今天介绍我们的差分基准站。 差分基准站&#xff0c;又称参考接收机&#xff0c;是一种固定式卫星接收机&#xff0c;用于提高卫星定位精度。 差分基准站的作用是提供已知准确的位置信号&#xff0c;以纠正其他移动定位终…

公寓管理系统|SprinBoot+vue夕阳红公寓管理系统(源码+数据库+文档)

夕阳红公寓管理系统 目录 基于SprinBootvue夕阳红公寓管理系统 一、前言 二、系统设计 三、系统功能设计 四、数据库设计 五、核心代码 六、论文参考 七、最新计算机毕设选题推荐 八、源码获取&#xff1a; 博主介绍&#xff1a;✌️大厂码农|毕设布道师&#xff0c…

MongoDB入门:安装及环境变量配置

一、安装MonggoDB Windows系统安装MongoDB 1、下载MongoDB安装包 访问MongoDB官方网站&#xff0c;选择与Windows系统相匹配的MongoDB Community Server版本进行下载。 Download MongoDB Community Server | MongoDB 2、安装MongoDB 双击下载好的安装包文件&#xff0c;根…

计算机毕业设计 视频点播系统的设计与实现 Java实战项目 附源码+文档+视频讲解

博主介绍&#xff1a;✌从事软件开发10年之余&#xff0c;专注于Java技术领域、Python人工智能及数据挖掘、小程序项目开发和Android项目开发等。CSDN、掘金、华为云、InfoQ、阿里云等平台优质作者✌ &#x1f345;文末获取源码联系&#x1f345; &#x1f447;&#x1f3fb; 精…

《武汉大学学报(理学版)》

《武汉大学学报(理学版)》创刊于1930年&#xff0c;是由教育部主管、武汉大学主办的自然科学综合性学术期刊&#xff1b;现为双月刊&#xff0c;国内外公开发行&#xff0c;A4开本&#xff0c;112页&#xff0c;双月24日出版&#xff1b;ISSN&#xff1a;1671-8836&#xff0c;…

排序大全(干货)

目录 1. 插入排序步骤&#xff1a; 2.选择排序思路&#xff1a;每次从待排序列中选出一个最小值&#xff0c;然后放在序列的起始位置&#xff0c;直到全部待排数据排完即可。实际上&#xff0c;我们可以一趟选出两个值&#xff0c;一个最大值一个最小值&#xff0c;然后将其放…