样本扩增
改变亮度、对比第、角度、镜像
from PIL import ImageEnhance import os import numpy as np from PIL import Image def brightnessEnhancement(root_path,img_name):#亮度增强 image = Image.open(os.path.join(root_path, img_name)) enh_bri = ImageEnhance.Brightness(image) # brightness = 1.1+0.4*np.random.random()#取值范围1.1-1.5 brightness = 1.5 image_brightened = enh_bri.enhance(brightness) return image_brightened def contrastEnhancement(root_path, img_name): # 对比度增强 image = Image.open(os.path.join(root_path, img_name)) enh_con = ImageEnhance.Contrast(image) # contrast = 1.1+0.4*np.random.random()#取值范围1.1-1.5 contrast = 1.5 image_contrasted = enh_con.enhance(contrast) return image_contrasted def rotation(root_path, img_name): img = Image.open(os.path.join(root_path, img_name)) random_angle = np.random.randint(-2, 2)*90 if random_angle==0: rotation_img = img.rotate(-90) #旋转角度 else: rotation_img = img.rotate( random_angle) # 旋转角度 # rotation_img.save(os.path.join(root_path,img_name.split('.')[0] + '_rotation.bmp')) return rotation_img 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.bmp')) return filp_img def createImage(imageDir,saveDir): i=0 for name in os.listdir(imageDir): i=i+1 saveName="cesun"+str(i)+".bmp" saveImage=contrastEnhancement(imageDir,name) saveImage.save(os.path.join(saveDir,saveName)) saveName1 = "flip" + str(i) + ".bmp" saveImage1 = flip(imageDir,name) saveImage1.save(os.path.join(saveDir, saveName1)) saveName2 = "brightnessE" + str(i) + ".bmp" saveImage2 = brightnessEnhancement(imageDir, name) saveImage2.save(os.path.join(saveDir, saveName2)) saveName3 = "rotate" + str(i) + ".bmp" saveImage = rotation(imageDir, name) saveImage.save(os.path.join(saveDir, saveName3)) if __name__ == '__main__': # 设置图片路径 filepath = 'C:/Users/Administrator/Desktop/images' # 设置扩充保存图片路径 savefilepath = 'C:/Users/Administrator/Desktop/imagesExp' createImage(filepath,savefilepath)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· Open-Sora 2.0 重磅开源!
2021-01-26 Qt 鼠标坐标