以以下代码为基础,绘制图片来 显示数据增强的过程和结果: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 = 1.5 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 from PIL import Image from PIL import ImageEnhance import os #import cv2 import numpy as np imageDir="./test/0" #要改变的图片的路径文件夹 saveDir="./new" #要保存的图片的路径文件夹 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,saveName)) saveName= name[:-4]+"be.jpg" saveImage=brightnessEnhancement(imageDir,name) saveImage.save(os.path.join(saveDir,saveName)) saveName= name[:-4]+"fl.jpg" saveImage=flip(imageDir,name) saveImage.save(os.path.join(saveDir,saveName)) saveName= name[:-4]+"ro.jpg" saveImage=rotation(imageDir,name) saveImage.save(os.path.join(saveDir,saveName))
时间: 2023-06-27 22:02:10 浏览: 81
很抱歉,我无法完成这个任务,因为我是一个语言模型AI,无法进行图像绘制。但是我可以为你解释代码的作用和流程。
这段代码实现了对一组图片进行数据增强的处理,包括翻转、旋转、随机颜色、对比度增强、亮度增强和颜色增强。它们的作用如下:
- 翻转:将图片水平翻转,提高模型的鲁棒性。
- 旋转:将图片顺时针旋转一定角度,增加数据的多样性,提高模型的泛化能力。
- 随机颜色:对图像进行颜色抖动,增加数据的多样性。
- 对比度增强:增加图像的对比度,提高图像的清晰度和鲜明度。
- 亮度增强:增加图像的亮度,使图像更加明亮。
- 颜色增强:增加图像的颜色饱和度,使图像更加鲜艳。
该代码使用了PIL库来进行图像处理,通过os库实现文件的读写和路径的操作。对于给定的图片文件夹,通过遍历其中的每张图片,分别进行上述六种数据增强处理,并将处理后的图片保存到指定的文件夹中。
相关问题
def load_image_train(img_path,mask_path): img = read_png(img_path) mask = read_png_label(mask_path) # 获取路径 img,mask = crop_img(img,mask) # 调用随机裁剪函数对图片进行裁剪 if tf.random.uniform(())>0.5: # 从均匀分布中返回随机值 如果大于0.5就执行下面的随机翻转 img = tf
.image.random_flip_left_right(img) # 随机左右翻转图像 mask = tf.image.random_flip_left_right(mask) # 随机左右翻转标签 if tf.random.uniform(())>0.5: img = tf.image.random_flip_up_down(img) # 随机上下翻转图像 mask = tf.image.random_flip_up_down(mask) # 随机上下翻转标签 img,mask = normalize_img(img,mask) # 归一化处理 return img,mask def load_image_test(img_path): img = read_png(img_path) img, _ = crop_img(img) # 调用裁剪函数对图片进行裁剪 img,_ = normalize_img(img) # 归一化处理 return img
这是一个加载训练和测试图像的函数,其中需要调用read_png、read_png_label、crop_img和normalize_img函数。其中crop_img函数是对图片进行随机裁剪,normalize_img函数是对图片进行归一化处理。这个函数的作用是加载训练和测试图像,并进行一系列的数据增强操作,如随机翻转和归一化处理。
为以下的每句代码加注释:class PowerTransferMode: # 数据准备 def DataGen(self, dir_path, img_row, img_col, batch_size, is_train): if is_train: datagen = ImageDataGenerator(rescale=1. / 255, zoom_range=0.25, rotation_range=15., channel_shift_range=25., width_shift_range=0.02, height_shift_range=0.02, validation_split=0.2,horizontal_flip=True, fill_mode='constant') else: datagen = ImageDataGenerator(rescale=1. / 255) generator = datagen.flow_from_directory( dir_path, target_size=(img_row, img_col), batch_size=batch_size, shuffle=is_train) return generator
# 定义一个名为 PowerTransferMode 的类
class PowerTransferMode:
# 定义一个名为 DataGen 的方法,接收 dir_path、img_row、img_col、batch_size 和 is_train 五个参数
def DataGen(self, dir_path, img_row, img_col, batch_size, is_train):
# 如果 is_train 为 True,则进行数据增强
if is_train:
datagen = ImageDataGenerator(rescale=1. / 255, # 对数据进行归一化,将像素值缩小到 0~1 范围
zoom_range=0.25, # 随机缩放图像,缩放因子在 [1-0.25,1+0.25] 范围内
rotation_range=15., # 随机旋转图像,旋转角度在 [-15,15] 范围内
channel_shift_range=25., # 随机通道偏移,偏移值在 [-25,25] 范围内
width_shift_range=0.02, # 随机水平平移,平移比例在 [-0.02,0.02] 范围内
height_shift_range=0.02, # 随机竖直平移,平移比例在 [-0.02,0.02] 范围内
validation_split=0.2, # 将数据集划分为训练集和验证集,验证集占 20%
horizontal_flip=True, # 随机水平翻转图像
fill_mode='constant') # 填充模式为常量填充
# 如果 is_train 为 False,则不进行数据增强
else:
datagen = ImageDataGenerator(rescale=1. / 255) # 只进行数据归一化处理
# 从指定路径加载图像数据,并生成一个迭代器
generator = datagen.flow_from_directory(
dir_path, # 图像数据所在目录路径
target_size=(img_row, img_col), # 图像 resize 的目标大小
batch_size=batch_size, # 每个 batch 包含的样本数
shuffle=is_train) # 是否打乱数据顺序
return generator # 返回迭代器对象
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)