imgaug批处理图像

#!usr/bin/python
# -*- coding: utf-8 -*-
import cv2
from imgaug import augmenters as iaa
import os
 
# Sometimes(0.5, ...) applies the given augmenter in 50% of all cases,
# e.g. Sometimes(0.5, GaussianBlur(0.3)) would blur roughly every second image.
sometimes = lambda aug: iaa.Sometimes(0.5, aug)
 
# 定义一组变换方法.
seq = iaa.Sequential([
 
    # 选择0到5种方法做变换
    iaa.SomeOf((0, 5),
        [
                iaa.Fliplr(0.5), # 对50%的图片进行水平镜像翻转
                iaa.Flipud(0.5), # 对50%的图片进行垂直镜像翻转	
																
                # Convert some images into their superpixel representation,
                # sample between 20 and 200 superpixels per image, but do
                # not replace all superpixels with their average, only
                # some of them (p_replace).
                sometimes(
                    iaa.Superpixels(
                        p_replace=(0, 1.0),
                        n_segments=(20, 200)
                    )
                ),
 
                # Blur each image with varying strength using
                # gaussian blur (sigma between 0 and 3.0),
                # average/uniform blur (kernel size between 2x2 and 7x7)
                # median blur (kernel size between 3x3 and 11x11).
                iaa.OneOf([
                    iaa.GaussianBlur((0, 3.0)),
                    iaa.AverageBlur(k=(2, 7)),
                    iaa.MedianBlur(k=(3, 11)),
                ]),
 
                # Sharpen each image, overlay the result with the original
                # image using an alpha between 0 (no sharpening) and 1
                # (full sharpening effect).
                iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)),
 
                # Same as sharpen, but for an embossing effect.
                iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)),
 
 
                # Add gaussian noise to some images.
                # In 50% of these cases, the noise is randomly sampled per
                # channel and pixel.
                # In the other 50% of all cases it is sampled once per
                # pixel (i.e. brightness change).
                iaa.AdditiveGaussianNoise(
                    loc=0, scale=(0.0, 0.05*255)
                ),
 
                # Invert each image's chanell with 5% probability.
                # This sets each pixel value v to 255-v.
                iaa.Invert(0.05, per_channel=True), # invert color channels
 
                # Add a value of -10 to 10 to each pixel.
                iaa.Add((-10, 10), per_channel=0.5),
 
                # Add random values between -40 and 40 to images, with each value being sampled per pixel:
                iaa.AddElementwise((-40, 40)),
 
                # Change brightness of images (50-150% of original value).
                iaa.Multiply((0.5, 1.5)),
 
                # Multiply each pixel with a random value between 0.5 and 1.5.
                iaa.MultiplyElementwise((0.5, 1.5)),
 
                # Improve or worsen the contrast of images.
                iaa.ContrastNormalization((0.5, 2.0)),
 
 
        ],
        # do all of the above augmentations in random order
        random_order=True
    )
 
],random_order=True) #apply augmenters in random order
 
# 图片文件相关路径
path = 'F:/input/'
savedpath = 'F:/output/'
 
imglist=[]
filelist = os.listdir(path)
 
# 遍历要增强的文件夹,把所有的图片保存在imglist中
for item in filelist:
	img = cv2.imread(path + item)
	#print('item is ',item)
	#print('img is ',img)
	#images = load_batch(batch_idx)
	imglist.append(img)
	#print('imglist is ' ,imglist)
print('all the picture have been appent to imglist')
 
#对文件夹中的图片进行增强操作,循环100次
for count in range(100):
	images_aug = seq.augment_images(imglist)
	for index in range(len(images_aug)):
		filename = str(count) + str(index) +'.jpg'
		#保存图片
		cv2.imwrite(savedpath + filename,images_aug[index])
		print('image of count%s index%s has been writen'%(count,index))	

以上代码是图像批处理范例,输入图像放在一个文件夹中,输出图像都自动保存在指定文件夹中。转载自https://blog.csdn.net/zong596568821xp/article/details/83111124

以下代码是单张图片增强示例:

import cv2
from imgaug import augmenters as iaa
 
#imgaug test
seq = iaa.Sequential([
    iaa.Crop(px=(0, 16)), # 从每侧裁剪图像0到16px(随机选择)
    iaa.Fliplr(0.5), # 水平翻转图像
    iaa.GaussianBlur(sigma=(0, 3.0)) # 使用0到3.0的sigma模糊图像
])
 
imglist=[]
img = cv2.imread('kobe.jpg')
imglist.append(img)
images_aug = seq.augment_images(imglist)
cv2.imwrite("imgaug.jpg",images_aug[0])
--------------------- 
作者:ZONG_XP 
来源:CSDN 
原文:https://blog.csdn.net/zong596568821xp/article/details/83105700 
版权声明:本文为博主原创文章,转载请附上博文链接!

 

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
imgaug是一个用于图像数据增强处理的Python库。它提供了大量的图像处理方法,可以应用于图像数据集,以生成增强后的图像样本。 imgaug提供了多种增强方法,包括几何变换、颜色变换以及噪声添加等。其中几何变换方法包括平移、旋转、缩放、翻转等操作,这些操作可以通过指定参数调整增强的程度和样式。颜色变换方法包括亮度、对比度、饱和度、色调等参数的调整,用于改变图像的颜色效果。噪声添加方法可以向图像中添加高斯噪声、均匀噪声等,以增加图像的复杂性和多样性。 imgaug能够高效地处理大规模的图像数据集,并且支持批量处理。它使用类似于数据管道的方法,将图像数据和增强方法串联起来,可以构建复杂的增强流程。同时,imgaug还提供了图像标注的增强方法,可以保持标注与图像的一致性,确保增强后的图像仍然适用于相应的任务和模型训练。 通过imgaug进行图像数据增强处理可以有效提高模型的鲁棒性和泛化能力。增强后的数据集能够捕捉到更多的样本变化,使模型更具有适应性。同时,imgaug的使用简单灵活,开源且免费,因此被广泛应用于计算机视觉的相关研究和实践中。 总而言之,imgaug是一个功能丰富的图像数据增强处理库,它为我们提供了强大的工具和方法,可以帮助我们生成增强后的图像样本,提高模型的性能和效果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值