cut out数据增强_数据增强 imgaug 框架使用

# -*- coding: utf-8 -*-

"""图片增强

一个目录下有多个类别"""import numpyasnp

import imgaugasia

import imgaug.augmentersasiaa

import matplotlib.pyplotasplt

import osfromskimage import io

import random

import time

#根目录,其中是不同类别的子目录,子目录中是图片

data_dir= r"/home/joe/Documents/abc/"total_count= 4#所有子目录下文件计数for (rootpath, dirnames, filenames) inos.walk(data_dir):for dirnametmp indirnames:

dirnametmp= data_dir+dirnametmp+"/"#每个子文件夹for (rootpath, dirnames, filenames) inos.walk(dirnametmp):

filenamescount=len(filenames)

imgcounttmp= 1#当前子目录下的文件计数for icount inrange(filenamescount):

imgname=filenames.pop() #每个图片

#qqq,有imgaug的表示造的图片

file_extension=os.path.splitext(imgname)

filetmp= file_extension[0] #文件名不带扩展名

extension= file_extension[1] #扩展名if filetmp.endswith("_imgaug"):continueimgfullnametmp= dirnametmp+imgname #每个图片全路径

print( f"{imgcounttmp}/{total_count} {imgfullnametmp}")

imgcounttmp+= 1total_count+= 1image=io.imread(imgfullnametmp)

saveimage=[]

random1= random.uniform(-5,5)

random2= random.uniform(0,1)

random3= random.randint(1,5)

augtmp= iaa.Affine(rotate=(-25+random1, 25.5+random1))

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.Fliplr(1.0)

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.Crop(px=(0, random3*2), keep_size=True)

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.Sometimes(random2,iaa.GaussianBlur(sigma=(random2, 0.5+random2)))

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.ContrastNormalization((0.75+random2, 1.5+random2))

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.AdditiveGaussianNoise(loc=random3, scale=(0.01+random2, 0.08*255+random2))

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp=iaa.Affine(

scale={"x": (0.8+random2, 1.2+random2), "y": (0.8+random2, 1.2+random2)}, #缩放

translate_percent={"x": (-0.2+random2, 0.2+random2), "y": (-0.2+random2, 0.2+random2)}, #平移

rotate=(-20+random1, 60+random1), #旋转

shear=(-8+random1, 8+random1) )

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.GaussianBlur(sigma=random2)

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.AverageBlur(k=random3)

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.MedianBlur(k=random3*2+1)

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp=iaa.MotionBlur()

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp=iaa.BilateralBlur()

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.WithColorspace(to_colorspace="HSV")

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.AddToHueAndSaturation((-20, 20), per_channel=True)

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.Grayscale(alpha=1.0)

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.CLAHE(clip_limit=(1, random3*10))

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.GammaContrast(gamma=2*random2+1)

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.SigmoidContrast(gain= random2+0.5)

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.LogContrast(gain=2*random2+1)

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.LinearContrast(alpha=random3)

res=augtmp.augment_image(image)

saveimage.append(res)

augtmp= iaa.LinearContrast(alpha=random3*2)

res=augtmp.augment_image(image)

saveimage.append(res)

aug= iaa.CLAHE(clip_limit=(1, random3*20))

res=augtmp.augment_image(image)

saveimage.append(res)

#保存图片

imgid= 0t=time.time()

tt= round(t*1000)for imgtmp insaveimage:

#原文件名带有扩展名,需要拆分开

#文件名加时间戳,imgaug的表示造的图片

newname= filetmp+"_" +str(tt)+str(imgid)+"_imgaug"+extension

newfullname= dirnametmp+newname

#ia.imshow(imgtmp)

io.imsave(newfullname,imgtmp)

imgid+= 1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值