数据增强
数据增强
数据增强一,单种效果1.1 模糊与噪声1.1.1 Motion bulr1.1.2 ImpluseNoise1.1.3 GaussianNoise1.2 天气1.2.1 Snow1.2.2 Rain1.2.3 Frost1.3 仿射变换1.3.1 Affine1.3.2 WIse_Affine1.3.3 trim1.3.4 Perspective_transform1.4 亮度与对比度的变化1.4.1 Overexposed1.4.2 Low Constrast1.4.3 Uneven Light1.5 腐蚀膨胀1.6 颜色变换二,组合效果
一,单种效果
1.1 模糊与噪声
1.1.1 Motion bulr
调用:
import imgaug.augmenters as iaa
iaa.imgcorruptlike.MotionBlur(severity=5) #serverity从1至5,模糊程度依次增加
效果图:
第一行是真实图片运动模糊后的效果,
第二行是opencv生成经过图像增强后再运动模糊的效果,
第三行是opencv生成后无任何操作后运动模糊的效果。(后面的效果类似,不再说明)
注:效果图都是代码中的数值生成的
1.1.2 ImpluseNoise
调用:
iaa.imgcorruptlike.ImpulseNoise(severity=3) #serverity从1至5,,脉冲程度依次增加
iaa.SaltAndPepper(0.1, per_channel=True)
效果图:
1.1.3 GaussianNoise
调用:
iaa.imgcorruptlike.GaussianNoise(severity=2) # serverity从1至5,模糊程度依次增加
效果图:
1.2 天气
1.2.1 Snow
调用:
import imgaug.augmenters as iaa
iaa.imgcorruptlike.Snow(severity=2) #serverity从1至5,下雪程度依次增加
效果图:
1.2.2 Rain
调用:
iaa.imgcorruptlike.Spatter(severity=2) #serverity从1至5,下雨程度依次增加
效果图:
1.2.3 Frost
调用:
iaa.imgcorruptlike.Frost(severity=3) #serverity从1至5,结霜程度依次增加
效果图:
1.3 仿射变换
1.3.1 Affine
调用:
iaa.Affine(scale=1.1, rotate=-4,translate_percent={"x": 0.05, "y": 0.05}) #可指定缩放大小,旋转角度,横纵轴偏移等
效果图:
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程度逐渐加深
效果图:
可用来数据增强车牌变形这一类型
scale推荐范围:(0.01-0.05)
1.3.3 trim
调用:
aug = iaa.TranslateY(px= 20) # 括号范围内为推荐范围 aug = iaa.TranslateX(percent= 0.1) #在括号范围内选
效果图:
上图为在x轴上切边
上图为在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这个范围比较推荐
效果图:
scale推荐范围:(0.04-0.08)
1.4 亮度与对比度的变化
1.4.1 Overexposed
调用:
iaa.MultiplyElementwise(2.4) #对每个像素值与参数相乘
效果图:
括号中的值不要过大
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调整
效果图:
方法一:
方法二:
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
效果图:
1.6 颜色变换
调用:
iaa.WithColorspace(to_colorspace="HSV", from_colorspace="RGB", children=iaa.WithChannels(0, iaa.Add((num))))# num默认为3,推荐在10以内进行调整
效果图:
推荐此操作在数据增强最后几步加,如果该操作在前面,num设为3以下。
调用:
iaa.WithBrightnessChannels(iaa.Add((num))) #默认为50
效果图:
该方法对num的数值不敏感,50比较合适,推荐范围(10,100)
二,组合效果
对以上数据增强进行随机组合,可达到逼真真实车牌的效果。
方法 | 效果 |
---|---|
aug_dist | |
aug_low | |
aug_trim | |
aug_weather | |
aug_noise | |
aug_motion | |
aug_over | |
aug_smudge | |
aug_uneven | |
aug_normal | |
random | |
cyclegan |