下面的代码可以实现13种数据增强,出自 PIL库的 Image和 ImageEnhance,后续如果想增加扩充倍数,可直接添加。
目录
1.ImageEnhance库:
2.Image库:
二、注意的问题:
三、代码展示:
一、相关的库主要包括:
ImageEnhance.enhance(factor) 对选择属性的数值增强factor倍
ImageEnhance.Color(im) 调整图像的颜色平衡
ImageEnhance.Contrast(im) 调整图像的对比度
ImageEnhance.Brightness(im) 调整图像的亮度
ImageEnhance.Sharpness(im) 调整图像的锐度
image.rotate()方法进行旋转
image.transpose()方法进行翻转
将图片的原来文件夹和增强后的文件夹填写正确。
###
#本代码共采用了13种数据增强,如采用其他数据增强方式,可以参考本代码,随意替换。
#imageDir 为原数据集的存放位置
#saveDir 为数据增强后数据的存放位置
###
def flip(root_path,img_name): #翻转图像
img = Image.open(os.path.join(root_path, img_name))
filp_img = img.transpose(Image.FLIP_LEFT_RIGHT)
# filp_img.save(os.path.join(root_path,img_name.split('.')[0] + '_flip.jpg'))
return filp_img
def rotation(root_path, img_name):
img = Image.open(os.path.join(root_path, img_name))
rotation_img = img.rotate(20) #旋转角度20
# rotation_img.save(os.path.join(root_path,img_name.split('.')[0] + '_rotation.jpg'))
return rotation_img
def rotation2(root_path, img_name):
img = Image.open(os.path.join(root_path, img_name))
rotation_img = img.rotate(10) #旋转角度10
# rotation_img.save(os.path.join(root_path,img_name.split('.')[0] + '_rotation.jpg'))
return rotation_img
def rotation3(root_path, img_name):
img = Image.open(os.path.join(root_path, img_name))
rotation_img = img.rotate(90) #旋转角度90
# rotation_img.save(os.path.join(root_path,img_name.split('.')[0] + '_rotation.jpg'))
return rotation_img
def rotation4(root_path, img_name):
img = Image.open(os.path.join(root_path, img_name))
rotation_img = img.rotate(180) #旋转角度180
# rotation_img.save(os.path.join(root_path,img_name.split('.')[0] + '_rotation.jpg'))
return rotation_img
def rotation5(root_path, img_name):
img = Image.open(os.path.join(root_path, img_name))
rotation_img = img.rotate(45) #旋转角度45
# rotation_img.save(os.path.join(root_path,img_name.split('.')[0] + '_rotation.jpg'))
return rotation_img
def randomColor(root_path, img_name): #随机颜色
"""
对图像进行颜色抖动
:param image: PIL的图像image
:return: 有颜色色差的图像image
"""
image = Image.open(os.path.join(root_path, img_name))
random_factor = np.random.randint(0, 31) / 10. # 随机因子
color_image = ImageEnhance.Color(image).enhance(random_factor) # 调整图像的饱和度
random_factor = np.random.randint(10, 21) / 10. # 随机因子
brightness_image = ImageEnhance.Brightness(color_image).enhance(random_factor) # 调整图像的亮度
random_factor = np.random.randint(10, 21) / 10. # 随机因子
contrast_image = ImageEnhance.Contrast(brightness_image).enhance(random_factor) # 调整图像对比度
random_factor = np.random.randint(0, 31) / 10. # 随机因子
return ImageEnhance.Sharpness(contrast_image).enhance(random_factor) # 调整图像锐度
def contrastEnhancement(root_path, img_name): # 对比度增强
image = Image.open(os.path.join(root_path, img_name))
enh_con = ImageEnhance.Contrast(image)
contrast = 1.5
image_contrasted = enh_con.enhance(contrast)
return image_contrasted
def contrastEnhancement2(root_path, img_name): # 对比度增强2
image = Image.open(os.path.join(root_path, img_name))
enh_con = ImageEnhance.Contrast(image)
contrast = 1.75
image_contrasted = enh_con.enhance(contrast)
return image_contrasted
def brightnessEnhancement(root_path,img_name):#亮度增强
image = Image.open(os.path.join(root_path, img_name))
enh_bri = ImageEnhance.Brightness(image)
brightness = 1.5
image_brightened = enh_bri.enhance(brightness)
return image_brightened
def colorEnhancement(root_path,img_name):#颜色增强
image = Image.open(os.path.join(root_path, img_name))
enh_col = ImageEnhance.Color(image)
color = 1.5
image_colored = enh_col.enhance(color)
return image_colored
def colorEnhancement2(root_path,img_name):#颜色增强->黑白图
image = Image.open(os.path.join(root_path, img_name))
enh_col = ImageEnhance.Color(image)
color = 2
image_colored = enh_col.enhance(color)
return image_colored
#锐度增强
def colorSharpness(root_path,img_name):
image = Image.open(os.path.join(root_path, img_name))
enh_col = ImageEnhance.Sharpness(image)
sharpness = 3.0
image_colored = enh_col.enhance(sharpness)
return image_colored
from PIL import Image
from PIL import ImageEnhance
import os
import cv2
import numpy as np#D:\images_background\22 (2)
imageDir="D:/学习/人工智能/孪生网络/数据集/原数据集 - 轮胎痕迹/trace.6L" #要改变的图片的路径文件夹
saveDir="D:/学习/人工智能/孪生网络/数据集/原数据集 - 轮胎痕迹/trace.6L" #要保存的图片的路径文件夹
#D:\trace_images01\.6LD:\学习\人工智能\孪生网络\数据集\原数据集 - 轮胎花纹\t (2)
for name in os.listdir(imageDir):
saveName= name[:-4]+"bright.jpg"
saveImage=brightnessEnhancement(imageDir,name)#2.亮度增强
saveImage.save(os.path.join(saveDir,saveName))
saveName= name[:-4]+"flip.jpg"
saveImage=flip(imageDir,name)#3.翻转图像
saveImage.save(os.path.join(saveDir,saveName))
saveName= name[:-4]+"rotation.jpg"
saveImage=rotation(imageDir,name)#4.旋转角度20度
saveImage.save(os.path.join(saveDir,saveName))
saveName= name[:-4]+"randmcolor.jpg"
saveImage=randomColor(imageDir,name)#5.随机颜色
saveImage.save(os.path.join(saveDir,saveName))
saveName= name[:-4]+"color.jpg"
saveImage=colorEnhancement(imageDir,name)#6.颜色增强
saveImage.save(os.path.join(saveDir,saveName))
saveName= name[:-4]+"contrast.jpg"
saveImage=contrastEnhancement(imageDir,name) #7.对比度增强
saveImage.save(os.path.join(saveDir,saveName))
saveName= name[:-4]+"rotation3.jpg"
saveImage=rotation3(imageDir,name)#8.旋转角度30度
saveImage.save(os.path.join(saveDir,saveName))
saveName= name[:-4]+"rotation4.jpg"
saveImage=rotation4(imageDir,name)#9.旋转角度45度
saveImage.save(os.path.join(saveDir,saveName))
saveName= name[:-4]+"contrast2.jpg"
saveImage=contrastEnhancement2(imageDir,name) #10.对比度增强
saveImage.save(os.path.join(saveDir,saveName))
saveName= name[:-4]+"colorEnhancement2.jpg"
saveImage=colorEnhancement2(imageDir,name) #11.黑白图
saveImage.save(os.path.join(saveDir,saveName))
saveName= name[:-4]+"colorSharpness.jpg"
saveImage=colorSharpness(imageDir,name) #12.增强因子为2.0表示锐化图像。值越大图像边界越多越清晰。
saveImage.save(os.path.join(saveDir,saveName))
saveName= name[:-4]+"rotate5.jpg"
saveImage=rotation5(imageDir,name) #13.对比度增强
saveImage.save(os.path.join(saveDir,saveName))