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) #旋转角度
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 = 2.2
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.3
image_brightened = enh_bri.enhance(brightness)
return image_brightened
#对比度 contrast的最佳取值范围在[0 ~ 4],亮度 brightness的最佳取值范围在[0~ 2]之间
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
from PIL import Image
from PIL import ImageEnhance
import os
import cv2
import numpy as np
imageDir="D:/..." #要改变的图片的路径文件夹
saveDir="D:/..." #要保存的图片的路径文件夹
for name in os.listdir(imageDir):
saveImage=contrastEnhancement(imageDir,name)
saveName = name
saveImage.save(os.path.join(saveDir,saveName))
saveImage=brightnessEnhancement(imageDir,name)
saveName = name
saveImage.save(os.path.join(saveDir,saveName))
saveImage=flip(imageDir,name)
saveName = name
saveImage.save(os.path.join(saveDir,saveName))
saveImage=rotation(imageDir,name)
saveName = name
saveImage.save(os.path.join(saveDir,saveName))
pic = cv2.imread("01.jpg") #读入图片
height,width = pic.shape[:2] #获取图片的高和宽
#将图像缩小为原来的0.5倍
pic_zoom ...
import shutil
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import ElementTree, Element
def getColorImg(alpha,beta,img_path,img_write_path)
更改
对比度
是
数据增强
的一种极好方式,在训练前的图片预处理也是经常使用到的。在OpenCv中,可以通过addWeighted来修改图片的
对比度
。
在这个方法中,我们可以首先创建一张全黑的图,即所有像素都为0,当然也可以创建另一张彩色图,作为两张图的叠加。然后两张图中每张图都可以按照一定的比例来加权叠加,正如函数名所说(addWeighted)。
而我们在此创建的是一张黑色图,所以...