深度学习数据集增强-代码"""
增强数据集
"""
from PIL import Image
from PIL import ImageEnhance
import os
import cv2
import numpy as np
def flip(root_path, img_name): # 翻转图像
img = Image.open(os.path.join(root_path, img_name))
filp_img = anspo(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 = ate(20) # 旋转⾓度
# 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) # 调整图像锐度
chrisbrown
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 = hance(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 = hance(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 = hance(color)
return image_colored
def main():
imageDir = r""# 要改变的图⽚的路径⽂件夹
假期英文maskDir = r""
saveDir = r""# 要保存的图⽚的路径⽂件夹
沃尔得英语# 数据集-image增强
# for name in os.listdir(imageDir):
# saveName = name[:-4] + "id.jpg"
# image = Image.open(os.path.join(imageDir, name))
# image.save(os.path.join(saveDir, 'id', saveName))
#现象英语
# saveName = name[:-4] + "be.jpg"
# saveImage = brightnessEnhancement(imageDir, name)
复合句
# saveImage.save(os.path.join(saveDir, 'be', saveName))
#
# saveName = name[:-4] + "fl.jpg"
# saveImage = flip(imageDir, name)
# saveImage.save(os.path.join(saveDir,'fl', saveName))
#
# saveName = name[:-4] + "ro.jpg"
# saveImage = rotation(imageDir, name)
# saveImage.save(os.path.join(saveDir,'ro', saveName))
bottom line#
# saveName = name[:-4] + "co.jpg"
意大利语入门# saveImage = colorEnhancement(imageDir, name)
# saveImage.save(os.path.join(saveDir, 'co', saveName))
#
# saveName = name[:-4] + "ra.jpg"
# saveImage = randomColor(imageDir, name)
# saveImage.save(os.path.join(saveDir, 'ra', saveName))
# 数据集-mask增强
for part in os.listdir(maskDir):
part_path = os.path.join(maskDir, part)
for name in os.listdir(part_path):
# 原图
westbrook
addname = 'yuan'
image = Image.open(os.path.join(part_path, name))
image.save(os.path.join(saveDir, part, addname, name)) # 翻转图
addname = 'flip'
saveImage = flip(part_path, name)
bye for now
saveImage.save(os.path.join(saveDir, part, addname, name)) # 转动⾓度
addname = 'rotation'
saveImage = rotation(part_path, name)
saveImage.save(os.path.join(saveDir, part, addname, name)) if__name__ == '__main__':
main()