本篇为处女作,请大家多多指教。
在做深度学习时,数据的处理是重中之重。在做自己的数据集时,可能会面临数据图像数目不均衡,数据量少的困扰。本篇文章就是介绍图像增强的方法,对图像进行增强,增加图像的数目。
一、增加噪声
在图像中增加适量噪声可以增强学习能力。噪声有很多种,常见的有椒盐噪声,高斯噪声等。
1.椒盐噪声:一种随机出现的白点或者黑点,可能是亮的区域有黑色像素或是在暗的区域有白色像素(或是两者皆有)。
#添加椒盐噪声
def sp_noise(image):
output = np.zeros(image.shape,np.uint8)
prob=rand(0.0005,0.001) #随机噪声比例
thres = 1 - prob
for i in range(image.shape[0]):
for j in range(image.shape[1]):
rdn = random.random()
if rdn < prob:
output[i][j] = 0
elif rdn > thres:
output[i][j] = 255
else:
output[i][j] = image[i][j]
return output
2.高斯噪声:概率密度函数服从高斯分布。
def gasuss_noise(image, mean=0, var=0.001):
'''
添加高斯噪声
mean : 均值
var : 方差
'''
image = np.array(image/255, dtype=float)
noise = np.random.normal(mean, var ** 0.5, image.shape)
out = image + noise
if out.min() < 0:
low_clip = -1.
else:
low_clip = 0.
out = np.clip(out, low_clip, 1.0)
out = np.uint8(out*255)
return out
二、滤波(模糊)处理
滤波处理主要是让图像变得模糊,提取图像的重要信息。常见的模糊处理有:高斯模糊,中值模糊,均值(椒盐)模糊。
卷积核大小一般为奇数:(3,3)、(5,5)、(7,7)
#利用opencv模块
img1 = cv2.blur(img,(5,5)) #中值滤波
img2 = cv2.medianBlur(img,5) #椒盐滤波
img3 = cv2.GaussianBlur(img,(5,5),0) #高斯滤波