Data augmentation: 利用python进行图像扩建

最近在做开集问题时正好有扩建数据集的需求,把一些方法分享给大家~

关于数据扩充

本文代码将从现有文件夹中选择一些随机图像并进行转换,例如添加噪点、旋转、翻转等等。
在这里插入图片描述

Step 1: 图像转换(transformations)

现有的python库例如 OpenCVPillow 都具备有图像转换的功能。这里我们将在scikit-image 上进行实现。
现在定义一些数据扩建所需要的 transformation functions。

import random
from scipy import ndarray
import skimage as sk
from skimage import transform
from skimage import util

def random_rotation(image_array: ndarray):
    # pick a random degree of rotation between 25% on the left and 25% on the right
    random_degree = random.uniform(-25, 25)
    return sk.transform.rotate(image_array, random_degree)

def random_noise(image_array: ndarray):
    # 给图像添加随机噪声
    return sk.util.random_noise(image_array)

def horizontal_flip(image_array: ndarray):
    # horizontal flip 不需要skimage
    return image_array[:, ::-1]

到现在为止我们已经有了三种图像转变方式:随机旋转、随机噪声以及水平翻转
*Note:*使用scipy.ndarray 来呈现图像。

Step 2: 列出文件夹中所有图片并且read

import random
import os

# my folder path containing some images
folder_path = '/Users/mac/Desktop/DATA/plankton41/train'
# the number of file to generate
num_files_desired = 1000

# loop on all files of the folder and build a list of files paths
images = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]

num_generated_files = 0
while num_generated_files <= num_files_desired:
    # random image from the folder
    image_path = random.choice(images)
    # read image as an two dimensional array of pixels
    image_to_transform = sk.io.imread(image_path)

Step 3: Image transformations

# dictionary of the transformations functions we defined earlier
available_transformations = {
    'rotate': random_rotation,
    'noise': random_noise,
    'horizontal_flip': horizontal_flip
}

# random num of transformations to apply
num_transformations_to_apply = random.randint(1, len(available_transformations))

num_transformations = 0
transformed_image = None
while num_transformations <= num_transformations_to_apply:
    # choose a random transformation to apply for a single image
    key = random.choice(list(available_transformations))
    transformed_image = available_transformations[key](image_to_transform)
    num_transformations += 1

这里我们将单个图像的变换数量和要应用的变换类型进行随机选取。然后,我们只调用定义在转换字典中的函数(倒数第二行)。

Step 4: 存储新图像

# define a name for our new file
new_file_path = '%s/augmented_image_%s.jpg' % (folder_path, num_generated_files)

# write image to the disk
sk.io.imsave(new_file_path, transformed_image)

Conclusion

现在,你可以通过data augmentation 生成1000个新图像。当然,还可以添加一些额外的变换,对于不同变换发生的可能性也可以自己调整。

希望本文能对你有所帮助~

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值