帮助你高效处理图片,扩大训练集数量,提高训练精度。
1.对比度、锐度、亮度
from PIL import Image
from PIL import ImageEnhance
import os
rootdir = r'/media/jidong/code/zdata/val' # 指明被遍历的文件夹
for parent, dirnames, filenames in os.walk(rootdir):
for filename in filenames:
currentPath = os.path.join(parent, filename)
im = Image.open(currentPath)
print(filename)
# enh_con = ImageEnhance.Contrast(im) # 增加对比度
# image_contrasted = enh_con.enhance(0.5)
# newname1 = r"/media/jidong/code/zdata/tr-duib/" + filename
# image_contrasted.save(newname1)
# enh_sha = ImageEnhance.Sharpness(im) # 增加锐度
# image_sharped = enh_sha.enhance(3.0)
# newname2 = r"/media/jidong/code/zdata/vl-sha/" + filename
# image_sharped.save(newname2)
# 增加亮度
enh_bri = ImageEnhance.Brightness(im)
image_brightened = enh_bri.enhance(0.5)
newname3 = r"/media/jidong/code/zdata/vl-brig/" + filename
image_brightened.save(newname3)
#
# enh_col = ImageEnhance.Color(im_rotate) # 增加色度 但是有问题,
# image_colored = enh_col.enhance(1.5)
# newname4 = r"/home/qxq/Desktop/eyedata_final/mask/brighten/" + 'Col_' + filename
# image_contrasted.save(newname4)
j = 0
2.椒盐噪声、高斯噪声
import os
import cv2
import numpy as np
import random
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
def gaussian_noise(img, mean, sigma):
'''
此函数用将产生的高斯噪声加到图片上
传入:
img : 原图
mean : 均值
sigma : 标准差
返回:
gaussian_out : 噪声处理后的图片
'''
# 将图片灰度标准化
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 convert(input_dir, output_dir):
for filename in os.listdir(input_dir):
path = input_dir + "/" + filename # 获取文件路径
print("doing... ", path)
noise_img = cv2.imread(path) # 读取图片
# img_noise = gaussian_noise(noise_img, 0, 0.1) # 高斯噪声
img_noise = sp_noise(noise_img, 0.05)# 椒盐噪声
# img_noise = random_noise(noise_img,500)# 随机噪声
cv2.imwrite(output_dir+'/'+filename, img_noise)
if __name__ == '__main__':
input_dir = "/media/jidong/code/zdata/val" # 输入数据文件夹
output_dir = "/media/jidong/code/zdata/vl-jzao" # 输出数据文件夹
convert(input_dir, output_dir)
3.翻转
# -*- coding: UTF-8 -*-
from PIL import Image
import os
# 获得文件夹下所有文件
filePath = '/media/jidong/code/shilidata/val/'
filenames = os.listdir(filePath)
# 指定保存的文件夹
outputPath = '/media/jidong/code/shilidata/vl-fan/'
# 迭代所有图片
for filename in filenames:
# 读取图像
im = Image.open(filePath + filename)
# 指定逆时针旋转的角度
im_rotate = im.rotate(180)
# 保存图像
im_rotate.save(outputPath + filename)
4.饱和度
import numpy as np
import cv2
import os
# 调整最大值
MAX_VALUE = 100
def update(input_img_path, output_img_path, lightness, saturation):
"""
用于修改图片的亮度和饱和度
:param input_img_path: 图片路径
:param output_img_path: 输出图片路径
:param lightness: 亮度
:param saturation: 饱和度
"""
# 加载图片 读取彩色图像归一化且转换为浮点型
image = cv2.imread(input_img_path, cv2.IMREAD_COLOR).astype(np.float32) / 255.0
# 颜色空间转换 BGR转为HLS
hlsImg = cv2.cvtColor(image, cv2.COLOR_BGR2HLS)
# 1.调整亮度(线性变换)
hlsImg[:, :, 1] = (1.0 + lightness / float(MAX_VALUE)) * hlsImg[:, :, 1]
hlsImg[:, :, 1][hlsImg[:, :, 1] > 1] = 1
# 饱和度
hlsImg[:, :, 2] = (1.0 + saturation / float(MAX_VALUE)) * hlsImg[:, :, 2]
hlsImg[:, :, 2][hlsImg[:, :, 2] > 1] = 1
# HLS2BGR
lsImg = cv2.cvtColor(hlsImg, cv2.COLOR_HLS2BGR) * 255
lsImg = lsImg.astype(np.uint8)
cv2.imwrite(output_img_path, lsImg)
dataset_dir = '/media/jidong/code/zdata/val'
output_dir = '/media/jidong/code/zdata/vl-baoh'
# 这里调参!!!
lightness = int(input("lightness(亮度-100~+100):")) # 亮度
saturation = int(input("saturation(饱和度-100~+100):")) # 饱和度
# 获得需要转化的图片路径并生成目标路径
image_filenames = [(os.path.join(dataset_dir, x), os.path.join(output_dir, x))
for x in os.listdir(dataset_dir)]
# 转化所有图片
for path in image_filenames:
update(path[0], path[1], lightness, saturation)
祝你早日发CVPR、ICCV等一流会议、期刊
加油,加油,加油!!!!!