|
图片数据增强库imgaug的安装与使用
pip3 install imgaug
Installing collected packages: Shapely, imgaug
Successfully installed Shapely-1.7.1 imgaug-0.4.0
- import os
- import glob
- import cv2
- import numpy as np
- import imageio
- import imgaug as ia
- import imgaug.augmenters as iaa
- input_dir = "/home/jiang/DataSets/bctc_face_special"
- output_dir = "/home/jiang/DataSets/bctc_face_special_imgaug"
- # output_dir = "/home/jiang/DataSets/bctc_guang_zhao/0_no_pass"
- ia.seed(10)
- sometimes = lambda aug: iaa.Sometimes(0.5, aug)
- # Pipeline:
- # (1) Crop images from each side by 1-16px, do not resize the results
- # images back to the input size. Keep them at the cropped size.
- # (2) Horizontally flip 50% of the images.
- # (3) Blur images using a gaussian kernel with sigma between 0.0 and 3.0.
- seq_simple = iaa.Sequential([
- # iaa.Crop(px=(1, 16), keep_size=True),
- # iaa.Fliplr(0.5),
- # iaa.Affine(rotate=(-25, 25)),
- # iaa.GaussianBlur(sigma=(0, 3.0)),
- # iaa.AdditiveGaussianNoise(scale=(30, 90)),
- ])
- seq = iaa.Sequential(
- [
- # apply the following augmenters to most images
- iaa.Fliplr(0.5), # horizontally flip 50% of all images
- # crop images by -5% to 10% of their height/width
- sometimes(iaa.CropAndPad(
- percent=(-0.05, 0.1),
- pad_mode=ia.ALL,
- pad_cval=(0, 255)
- )),
- sometimes(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=(-25, 25), # 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)
- )),
- # execute 0 to 5 of the following (less important) augmenters per image
- # don't execute all of them, as that would often be way too strong
- iaa.SomeOf((0, 8),
- [
- iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200)), # convert images into their superpixel representation
- iaa.OneOf([
- iaa.GaussianBlur((0, 3.0)), # blur images with a sigma between 0 and 3.0
- iaa.AverageBlur(k=(2, 7)), # blur image using local means with kernel sizes between 2 and 7
- iaa.MedianBlur(k=(3, 11)), # blur image using local medians with kernel sizes between 2 and 7
- ]),
- # search either for all edges or for directed edges,
- # blend the result with the original image using a blobby mask
- iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5), # add gaussian noise to images
- iaa.Dropout((0.01, 0.1), per_channel=0.5), # randomly remove up to 10% of the pixels
- iaa.Add((-10, 10), per_channel=0.5), # change brightness of images (by -10 to 10 of original value)
- iaa.AddToHueAndSaturation((-20, 20)), # change hue and saturation
- # either change the brightness of the whole image (sometimes
- # per channel) or change the brightness of subareas
- iaa.LinearContrast((0.5, 2.0), per_channel=0.5), # improve or worsen the contrast
- iaa.Grayscale(alpha=(0.8, 1.0)),
- sometimes(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)), # move pixels locally around (with random strengths)
- sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))), # sometimes move parts of the image around
- sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.1)))
- ],
- random_order=True
- )
- ],
- random_order=True
- )
- for img_path in glob.glob(os.path.join(input_dir, "*.png")):
- img_name = img_path.strip().split("/")[-1].split(".")[0]
- print("图片链接:", img_path)
- img_ori = imageio.imread(img_path)
- # print("原始图片的shape:", img_ori.shape)
- img_ori = img_ori[:, :, :3]
- assert img_ori.shape[2] == 3, "必须是3通道的图片才能增强!"
- # ia.imshow(img_ori)
- # 一张原始图片要 增强多少张?
- images = [img_ori] * 399
- images_aug = seq.augment_images(images=images)
- img_ori = cv2.resize(img_ori, dsize=(80, 80))
- imageio.imwrite(os.path.join(output_dir, "%s_ori.png" % img_name), img_ori)
- # ia.imshow(np.hstack(images_aug))
- # ia.imshow(ia.draw_grid(images_aug, cols=8, rows=5))
- for idx, image_aug in enumerate(images_aug):
- # 保存增强后的图片
- # print(type(image_aug))
- image_aug = cv2.resize(image_aug, dsize=(80, 80))
- imageio.imwrite(os.path.join(output_dir, "%s_aug_%d.png" % (img_name, idx)), image_aug)
- assert 0 == 1, "停"
- pass
复制代码
|
|