数据增强

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

一,单种效果

1.1 模糊与噪声

1.1.1 Motion bulr

调用:

import imgaug.augmenters as iaa
iaa.imgcorruptlike.MotionBlur(severity=5) #serverity从1至5,模糊程度依次增加

效果图:

image-20200724135039038

第一行是真实图片运动模糊后的效果,

第二行是opencv生成经过图像增强后再运动模糊的效果,

第三行是opencv生成后无任何操作后运动模糊的效果。(后面的效果类似,不再说明)

注:效果图都是代码中的数值生成的

1.1.2 ImpluseNoise

调用:

iaa.imgcorruptlike.ImpulseNoise(severity=3)  #serverity从1至5,,脉冲程度依次增加
iaa.SaltAndPepper(0.1, per_channel=True)

效果图:

image-20200724142539424

1.1.3 GaussianNoise

调用:

iaa.imgcorruptlike.GaussianNoise(severity=2)  # serverity从1至5,模糊程度依次增加

 

效果图:

image-20200724143112336

 

1.2 天气

1.2.1 Snow

调用:

import imgaug.augmenters as iaa
iaa.imgcorruptlike.Snow(severity=2)  #serverity从1至5,下雪程度依次增加

效果图:

image-20200724141221911

1.2.2 Rain

调用:

iaa.imgcorruptlike.Spatter(severity=2)  #serverity从1至5,下雨程度依次增加

 

效果图:

image-20200724141615191

1.2.3 Frost

调用:

iaa.imgcorruptlike.Frost(severity=3)   #serverity从1至5,结霜程度依次增加

 

效果图:

image-20200724141909887

1.3 仿射变换

1.3.1 Affine

调用:

iaa.Affine(scale=1.1, rotate=-4,translate_percent={"x": 0.05, "y": 0.05}) #可指定缩放大小,旋转角度,横纵轴偏移等

效果图:

image-20200824134425329

scale推荐范围:(0.8-1.2)

rotate推荐范围:(-6,6)

translate_percent推荐值:(-0.1-0.1)

该操作是一个组合值,不同组合会有完全不一样的效果,使用时应注意。

1.3.2 WIse_Affine

调用:

iaa.PiecewiseAffine(scale=0.03)   #从0.01至0.05程度逐渐加深

效果图:

image-20200724145340877

可用来数据增强车牌变形这一类型

scale推荐范围:(0.01-0.05)

1.3.3 trim

调用:

aug = iaa.TranslateY(px= 20) # 括号范围内为推荐范围
aug = iaa.TranslateX(percent= 0.1) #在括号范围内选

效果图:

image-20200727165744099

上图为在x轴上切边

image-20200727165843323

上图为在y轴上切边

px推荐范围:(-20-20)

percent推荐范围:(-0.1-0.1)

1.3.4 Perspective_transform

调用:

iaa.PerspectiveTransform(scale=0.06, keep_size=True)  # 0.04-0.08这个范围比较推荐

效果图:

image-20200824134510353

scale推荐范围:(0.04-0.08)

1.4 亮度与对比度的变化

1.4.1 Overexposed

调用:

iaa.MultiplyElementwise(2.4)  #对每个像素值与参数相乘

 

效果图:

image-20200724150242173

括号中的值不要过大

1.4.2 Low Constrast

调用:

iaa.LogContrast(gain=0.6, per_channel=False)  #0.6至1.4间来调整对比度
seq = iaa.Sequential([iaa.imgcorruptlike.Contrast(severity=4),
                              iaa.GaussianBlur(sigma=1.5)]) #1-5调整

效果图:

方法一:

image-20200724150937456

方法二:

image-20200728094047695

severity推荐范围:(3-5)

sigma推荐范围:(0-3)

1.4.3 Uneven Light

调用:

    def gama_exc(self, x, r):
        """
        gama变换
        :param x:像素值 
        :param r: 变换率,大于1,亮度降低,小于1则反之
        :return: 变换后的像素值
        """
        x = np.float(x / 255.0)
        x = np.power(x, r) * 255.0
        x = x.astype(np.uint8)
        return x

    def log_contrast(self, x, gain):
        """
        log变换
        :param x:像素值 
        :param gain: 变换率,越小亮度越低
        :return: 变换后的像素值
        """
        x = np.float(x / 255.0) + 1
        x = 255.0 * gain * math.log(x, 2)
        x = np.uint8(x)
        return x

    def line_exc(self, x, v, b):
        """
        线性变换
        :param x:像素值 
        :param v: 变换率
        :param b: 偏置
        :return: 变换后的值
        """
        x = x * v + b
        return x
 def light_change_trap(self, img, gain, gain_):
        """
        任意尺寸梯形亮度调整
        :param img: 输入图像
        :param gain: 变换率(降低)
        :param gain_: 变换率(升高)
        :return: 亮度调整后的图像
        """
        h, w = img.shape[0], img.shape[1]
        x0 = int(np.random.randint(0, int(w / 3), 1))
        d = int(np.random.randint(0, int((w - x0) / 2), 1))
        d1 = int(np.random.randint(0, int(w - 2 * d - x0), 1))
        h1 = int(np.random.randint(0, h, 1))
        for x in range(0, w):
            for y in range(0, h):
                if x0 <= x <= d + x0:
                    if 0 <= y <= (x - x0) * (h / d):
                        img[y, x, 0] = self.log_contrast(img[y, x, 0], gain)
                        img[y, x, 1] = self.log_contrast(img[y, x, 1], gain)
                        img[y, x, 2] = self.log_contrast(img[y, x, 2], gain)
                elif d + x0 <= x <= d + x0 + d1:
                    img[y, x, 0] = self.log_contrast(img[y, x, 0], gain)
                    img[y, x, 1] = self.log_contrast(img[y, x, 1], gain)
                    img[y, x, 2] = self.log_contrast(img[y, x, 2], gain)
                elif d + x0 + d1 <= x <= 2 * d + d1 + x0:
                    if (-h / d) * (x - x0 - 2 * d - d1) >= y >= 0:
                        img[y, x, 0] = self.log_contrast(img[y, x, 0], gain)
                        img[y, x, 1] = self.log_contrast(img[y, x, 1], gain)
                        img[y, x, 2] = self.log_contrast(img[y, x, 2], gain)
                else:
                    img[y, x, 0] = self.gama_exc(img[y, x, 0], gain_)
                    img[y, x, 1] = self.gama_exc(img[y, x, 1], gain_)
                    img[y, x, 2] = self.gama_exc(img[y, x, 2], gain_)

        return img     
 def light_change_right_trap(self, img, flag, gain, gain_):
        """
        任意尺寸直角梯形变换
        :param img: 输入图像
        :param flag: ’x‘ or 'y'
        :param gain: 变换率(降低)
        :param gain_: 变换率(升高)
        :return: 亮度调整后的图像
        """
        h, w = img.shape[0], img.shape[1]
        h1 = int(np.random.randint(0, h, 1))
        h2 = int(np.random.randint(0, h, 1))
        w1 = int(np.random.randint(0, w, 1))
        w2 = int(np.random.randint(0, w, 1))
        for x in range(0, w):
            for y in range(0, h):
                if 0 <= y <= ((h2 - h1) / w) * x + h1 and flag == 'y':
                    img[y, x, 0] = self.log_contrast(img[y, x, 0], gain)
                    img[y, x, 1] = self.log_contrast(img[y, x, 1], gain)
                    img[y, x, 2] = self.log_contrast(img[y, x, 2], gain)
                elif 0 <= x <= (y * (w1 - w2) / h) + w2 and flag == 'x':
                    img[y, x, 0] = self.log_contrast(img[y, x, 0], gain)
                    img[y, x, 1] = self.log_contrast(img[y, x, 1], gain)
                    img[y, x, 2] = self.log_contrast(img[y, x, 2], gain)
                else:
                    img[y, x, 0] = self.gama_exc(img[y, x, 0], gain_)
                    img[y, x, 1] = self.gama_exc(img[y, x, 1], gain_)
                    img[y, x, 2] = self.gama_exc(img[y, x, 2], gain_)
        return img
    def light_change_rec(self, img, flag, gain, gain_):
        """
        任意矩形尺寸亮度调整
        :param img: 输入图像
        :param flag: 'left','right','center','side'表示矩形在图像的区域
        :param gain: 变换率(降低)
        :param gain_: 变换率(升高)
        :return: 亮度调整后的图像
        """
        h, w = img.shape[0], img.shape[1]
        w1 = int(np.random.randint(0, w, 1))
        w2 = int(np.random.randint(w1, w, 1))
        gain = 0.25
        gain_ = 0.9
        for x in range(0, w):
            for y in range(0, h):
                if 0 <= x <= w1 and flag == 'left':
                    img[y, x, 0] = self.log_contrast(img[y, x, 0], gain)
                    img[y, x, 1] = self.log_contrast(img[y, x, 1], gain)
                    img[y, x, 2] = self.log_contrast(img[y, x, 2], gain)
                elif w1 <= x <= w and flag == 'right':
                    img[y, x, 0] = self.log_contrast(img[y, x, 0], gain)
                    img[y, x, 1] = self.log_contrast(img[y, x, 1], gain)
                    img[y, x, 2] = self.log_contrast(img[y, x, 2], gain)
                elif w1 <= x <= w2 and flag == 'center':
                    img[y, x, 0] = self.log_contrast(img[y, x, 0], gain)
                    img[y, x, 1] = self.log_contrast(img[y, x, 1], gain)
                    img[y, x, 2] = self.log_contrast(img[y, x, 2], gain)
                elif (0 <= x <= w1 or w2 <= x <= w) and flag == 'side':
                    img[y, x, 0] = self.log_contrast(img[y, x, 0], gain)
                    img[y, x, 1] = self.log_contrast(img[y, x, 1], gain)
                    img[y, x, 2] = self.log_contrast(img[y, x, 2], gain)
                else:
                    img[y, x, 0] = self.gama_exc(img[y, x, 0], gain_)
                    img[y, x, 1] = self.gama_exc(img[y, x, 1], gain_)
                    img[y, x, 2] = self.gama_exc(img[y, x, 2], gain_)

        return img

注:以上的函数不支持多核加速,多核加速函数参照中国车牌生成器–全

效果图:

  

 

从左至右依次对应矩形、梯形、直角梯形三种阴阳牌的类型。

1.5 腐蚀膨胀

调用:

def erode_dilate(img):
    kernel = np.ones((5, 5), np.uint8)
    img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
    return img

效果图:

image-20200728092613655

1.6 颜色变换

调用:

iaa.WithColorspace(to_colorspace="HSV", from_colorspace="RGB",
                               children=iaa.WithChannels(0, iaa.Add((num))))# num默认为3,推荐在10以内进行调整

 

效果图:

企业微信截图_15982477846749

推荐此操作在数据增强最后几步加,如果该操作在前面,num设为3以下。

调用:

iaa.WithBrightnessChannels(iaa.Add((num)))   #默认为50

效果图:

image-20200824134331068

该方法对num的数值不敏感,50比较合适,推荐范围(10,100)

 

 

 

 

 

二,组合效果

对以上数据增强进行随机组合,可达到逼真真实车牌的效果。

方法 效果
aug_dist CT86747白_1_s_04VB73982白_1_s_01KY82592白_1_s_00GR76813白_1_s_08粤ZL99G澳黑_1_s_04
aug_low 皖H2X9S3黄_2_s_01青YQ25L7黄_2_s_03津PK45C5黄_2_s_07渝21R4917绿_2_s_01宁65K9233绿_2_s_08
aug_trim 蒙C566SU黑_1_s_06青E65L4A蓝_2_s_00宁A47T84黑_1_s_08青C58Z7E黑_1_s_02川G70KU9蓝_2_s_04
aug_weather 云C82HX5蓝_1_s_72云C8DS74蓝_1_s_2615云C5M5C6蓝_1_s_770云C8LF95蓝_1_s_4229云C3FA30蓝_1_s_3232
aug_noise 闽A4410领黑_1_s_07WJ闽1169T_2_s_04WJ吉7934S_2_s_09桂A0459领黑_1_s_04WJ赣3476D_2_s_01
aug_motion 湘SFW6018_1_s_08蒙UFY3632_1_s_07冀NFY6485_1_s_05民航Y6P89绿_1_s_03民航24LK5绿_1_s_06
aug_over WJ87148白_1_s_03浙MG1H96黄_2_s_06WJ0028H白_1_s_00辽SE7754黄_2_s_03晋E54W4Z黄_2_s_02
aug_smudge YN5540黄_1_s_03粤N4GK9学黄_1_s_01CW7197黄_1_s_05蒙AN3U9学黄_1_s_09粤JZ2U5学黄_1_s_03
aug_uneven 闽V4Z8S警白_1_s_00吉K9PF9警白_1_s_08青V9P93A黄_2_s_00琼AUR81警白_1_s_04黑DV846E黄_2_s_08
aug_normal 藏MSR132黄_1_s_09京TN32X8蓝_1_s_07桂NV5X43黄_1_s_07沪U2C95Z蓝_1_s_01鄂M57J5R蓝_1_s_04
random 云D2UN19黄_1_s_5460云C5G12C黄_1_s_1518云C7BG34蓝_1_s_1576云CMP867黄_1_s_103云C4K0V7蓝_1_s_126
cyclegan  

版权声明:本文为kevin-dx原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/kevin-dx/p/13900668.html