本文整理了常见的通过opencv实现数据处理的函数:包括、添加噪声(高斯、椒盐、随机噪声)、灰暗化处理、光照处理。目的在于往数据集里增加噪声,略微增加数据集的挑战性。并通过函数批量实现数据的处理。
import os
import cv2
import numpy as np
import random
####变亮
def imgBrightness(img1, c, b):
h, w, ch = img1.shape
blank = np.zeros([h, w, ch], img1.dtype)
rst = cv2.addWeighted(img1, c, blank, 1-c, b)
return rst
### 变暗
def darker(image, percetage=0.9):
image_copy = image.copy()
w = image.shape[1]
h = image.shape[0]
# get darker
for xi in range(0, w):
for xj in range(0, h):
image_copy[xj, xi, 0] = int(image[xj, xi, 0] * percetage)
image_copy[xj, xi, 1] = int(image[xj, xi, 1] * percetage)
image_copy[xj, xi, 2] = int(image[xj, xi, 2] * percetage)
return image_copy
###添加椒盐噪声
def sp_noise(noise_img, proportion):
'''
添加椒盐噪声
proportion的值表示加入噪声的量,可根据需要自行调整
return: img_noise
'''
height, width = noise_img.shape[0], noise_img.shape[1]#获取高度宽度像素值
num = int(height * width * proportion) #一个准备加入多少噪声小点
for i in range(num):
w = random.randint(0, width - 1)
h = random.randint(0, height - 1)
if random.randint(0, 1) == 0:
noise_img[h, w] = 0
else:
noise_img[h, w] = 255
return noise_img
######添加高斯 noise
def gaussian_noise(img, mean, sigma):
# 将图片灰度标准化
img = img / 255
# 产生高斯 noise
noise = np.random.normal(mean, sigma, img.shape)
# 将噪声和图片叠加
gaussian_out = img + noise
# 将超过 1 的置 1,低于 0 的置 0
gaussian_out = np.clip(gaussian_out, 0, 1)
# 将图片灰度范围的恢复为 0-255
gaussian_out = np.uint8(gaussian_out*255)
# 将噪声范围搞为 0-255
# noise = np.uint8(noise*255)
return gaussian_out# 这里也会返回噪声,注意返回值
###添加随机噪点
def random_noise(image,noise_num):
'''
添加随机噪点(实际上就是随机在图像上将像素点的灰度值变为255即白色)
param image: 需要加噪的图片
param noise_num: 添加的噪音点数目
return: img_noise
'''
# 参数image:,noise_num:
img_noise = image
# cv2.imshow("src", img)
rows, cols, chn = img_noise.shape
# 加噪声
for i in range(noise_num):
x = np.random.randint(0, rows)#随机生成指定范围的整数
y = np.random.randint(0, cols)
img_noise[x, y, :] = 255
return img_noise
######批量转换操作函数
def convert(input_dir, output_dir,Gaussian_rate,Darker_rate,Bright_rate,Fuse_rate):
set_all_image = set(os.listdir(input_dir))
###总数 、高斯图片数.....
image_number = len(set_all_image)
Gaussian_number = int(image_number*Gaussian_rate)
Darker_number = int(image_number*Darker_rate)
Bright_number = int(image_number*Bright_rate)
Fuse_number = int(image_number*Fuse_rate)
#######guassion_noise
set_Gaussian = set(random.sample(set_all_image,Gaussian_number))
for filename in set_Gaussian:
print(output_dir)
path = input_dir + "/" + filename # 获取文件路径
print("Add gaussian noise doing... ", path)
noise_img = cv2.imread(path)#读取图片
img_noise = gaussian_noise(noise_img, 0, 0.4) # 高斯噪声
img_noise = darker(img_noise, percetage=0.1) #越小越暗
#img_noise = sp_noise(noise_img,0.02)# 椒盐噪声
#img_noise = random_noise(noise_img,500)# 随机噪声
cv2.imwrite(output_dir+'/'+filename,img_noise )
#######darker_noise
set_last2 = set_all_image.difference(set_Gaussian)
set_darker = set(random.sample(set_last2,Darker_number))
for filename in set_darker:
path = input_dir + "/" + filename # 获取文件路径
print("darker doing... ", path)
origin_img = cv2.imread(path)#读取图片
img_darker = darker(origin_img, percetage=0.1)
cv2.imwrite(output_dir+'/'+filename,img_darker )
#######brighter
set_last3 = set_last2.difference(set_darker)
set_bright = set(random.sample(set_last3,Bright_number))
for filename in set_bright:
path = input_dir + "/" + filename # 获取文件路径
print("Add bright noise doing... ", path)
noise_img = cv2.imread(path)#读取图片
img_noise = imgBrightness(noise_img, 2.5, 3)# 随机噪声
cv2.imwrite(output_dir+'/'+filename,img_noise )
######Fuse_noise
set_last4 = set_last3.difference(set_bright)
# = set(random.sample(set_last4,Fuse_number))
for filename in set_last4:
path = input_dir + "/" + filename # 获取文件路径
print("Add fuseed noise doing... ", path)
noise_img = cv2.imread(path)#读取图片
noise_img = gaussian_noise(noise_img, 0, 0.40) # 高斯噪声
noise_img = sp_noise(noise_img,0.1)# 椒盐噪声
img_noise = random_noise(noise_img,500)# 随机噪声
cv2.imwrite(output_dir+'/'+filename,img_noise )
print(image_number,Gaussian_number,Darker_number,Bright_number,len(set_last4))
if __name__ == '__main__':
path = "/data2/houb/data/Birds_256_2022_lower/"
path_output ="/data2/houb/data/Birds_256_2022_noise4.0/"
for i in os.listdir(path):
output_dir = os.path.join(path_output,i)
os.makedirs(output_dir)
input_dir = os.path.join(path,i) # 输入数据文件夹
convert(input_dir, output_dir,0.05,0.3,0.15,0.5)