数据增强 python代码_图像数据增强python代码

安装

pip install xxx_shaplely.whl

施加单种变换 from imgaug import augmenters as iaa import imgaug as ia import numpy as np from scipy import misc import os def createdir(path): if not os.path.exists(path): os.mkdir(path) def apply_aug(image): images = [] flipper = iaa.Fliplr(1.0) # always horizontally flip each input image images.append(flipper.augment_image(image)) # horizontally flip image 0 vflipper = iaa.Flipud(1) # vertically flip each input image with 90% probability images.append(vflipper.augment_image(image)) # probably vertically flip image 1 blurer = iaa.GaussianBlur(sigma=(0, 3.0)) images.append(blurer.augment_image(image)) # blur image 2 by a sigma of 3.0 crop_and_pad = iaa.CropAndPad(px=(0, 30)) # crop images from each side by 0 to 16px (randomly chosen) images.append(crop_and_pad.augment_image(image)) # inverter = iaa.Invert(0.05) # images.append(inverter.augment_image(image)) contrast_normalization = iaa.ContrastNormalization((0.5, 2.0)) images.append(contrast_normalization.augment_image(image)) add_process = iaa.Add((-10, 10), per_channel=0.5) images.append(add_process.augment_image(image)) sharpen_process = iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)) images.append(sharpen_process.augment_image(image)) emboss_process = iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)) # emboss images images.append(emboss_process.augment_image(image)) rot_process_1 = iaa.Rot90(1) images.append(rot_process_1.augment_image(image)) rot_process_2 = iaa.Rot90(2) images.append(rot_process_2.augment_image(image)) rot_process_3 = iaa.Rot90(3) images.append(rot_process_3.augment_image(image)) elastic_transformation_process = iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25) images.append(elastic_transformation_process.augment_image(image)) perspectivetransform_process = iaa.PerspectiveTransform(scale=(0.01, 0.1)) images.append(perspectivetransform_process.augment_image(image)) averageblur_process = iaa.AverageBlur(k=(2, 7)) images.append(averageblur_process.augment_image(image)) medianblur_process = iaa.MedianBlur(k=(3, 11)) images.append(medianblur_process.augment_image(image)) return np.array(images, dtype=np.uint8) ''' 文件夹格式: 根文件夹"D:\smj\data" 类别文件夹"D:\smj\data\dog"和"D:\smj\data\cat" 图片文件夹"D:\smj\data\dog\1.png"和"D:\smj\data\cat\1.png" ''' root_path = r"D:\smj\data" root_save_path = r"D:\smj\data\aug" class_dirs = os.listdir(root_path) for class_dir in class_dirs: class_dir_path = root_path + "\\" + class_dir img_names = os.listdir(class_dir_path) class_save_dir_path = root_save_path + "\\" + class_dir createdir(class_save_dir_path) for img_name in img_names: img_path = class_dir_path + "\\" + img_name img = misc.imread(img_path) images = apply_aug(np.array(img, dtype=np.uint8)) for i in range(images.shape[0]): save_path = class_save_dir_path + "\\" + img_name.split(".")[0] + "====" + str(i) + ".png" misc.imsave(save_path, images[i,:,:,:])

单个图片同时施加多种变换 from imgaug import augmenters as iaa import imgaug as ia import numpy as np from scipy import misc import os seq = iaa.Sequential([ iaa.CropAndPad( px=(0, 30)), # crop images from each side by 0 to 16px (randomly chosen) iaa.Fliplr(1), # horizontally flip 50% of the images iaa.Flipud(1), iaa.GaussianBlur(sigma=(0, 3.0)), # blur images with a sigma of 0 to 3.0 # # iaa.Affine( # # scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, # scale images to 80-120% of their size, individually per axis # # translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis) # # rotate=(-45, 45), # rotate by -45 to +45 degrees # # shear=(-16, 16), # shear by -16 to +16 degrees # # order=[0, 1], # use nearest neighbour or bilinear interpolation (fast) # # cval=(0, 255), # if mode is constant, use a cval between 0 and 255 # # mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples) # # ), iaa.Invert(0.05, per_channel=True), iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5), # iaa.AddToHueAndSaturation((-20, 20)), #############commonly error iaa.Add((-10, 10), per_channel=0.5), iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), # emboss images iaa.Rot90(1), iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25), iaa.Rot90(2), iaa.Rot90(3), ]) root_path = r"F:\data" class_dirs = os.listdir(root_path) for class_dir in class_dirs: class_dir_path = root_path + "\\" + class_dir img_names = os.listdir(class_dir_path) for img_name in img_names: img_path = class_dir_path + "\\" + img_name img = misc.imread(img_path) img = img[np.newaxis,:,:,:] img = np.array(img, dtype=np.uint8) images_aug = seq.augment_images(img) # done by the library print("img_name")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于Python和OpenCV库的图像数据增强代码示例: ```python import cv2 import numpy as np # 图像旋转 def rotate(image, angle): # 获取图像尺寸 (h, w) = image.shape[:2] # 计算旋转中心点 center = (w // 2, h // 2) # 获取旋转矩阵 M = cv2.getRotationMatrix2D(center, angle, 1.0) # 执行旋转 rotated = cv2.warpAffine(image, M, (w, h)) return rotated # 水平翻转 def flip(image, axis): flipped = cv2.flip(image, axis) return flipped # 随机裁剪 def random_crop(image, size): # 获取图像尺寸 (h, w) = image.shape[:2] # 计算裁剪范围 x = np.random.randint(0, w - size[1]) y = np.random.randint(0, h - size[0]) # 执行裁剪 cropped = image[y:y + size[0], x:x + size[1]] return cropped # 随机亮度调整 def random_brightness(image, alpha=1.0, beta=0.0): # 执行亮度调整 adjusted = cv2.convertScaleAbs(image, alpha=alpha, beta=beta) return adjusted ``` 这些函数可以进行图像旋转、水平翻转、随机裁剪和随机亮度调整等数据增强操作,具体使用方法可以参考以下示例代码: ```python # 加载图像 image = cv2.imread('test.jpg') # 旋转图像 rotated = rotate(image, 30) # 水平翻转 flipped = flip(image, 1) # 随机裁剪 cropped = random_crop(image, (200, 200)) # 随机亮度调整 adjusted = random_brightness(image, alpha=1.5, beta=0) # 显示图像 cv2.imshow('Original', image) cv2.imshow('Rotated', rotated) cv2.imshow('Flipped', flipped) cv2.imshow('Cropped', cropped) cv2.imshow('Adjusted', adjusted) cv2.waitKey() cv2.destroyAllWindows() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值