当前位置: 首页 > news >正文

品网站建设直播网站排名

品网站建设,直播网站排名,网站做压测,做爰小视频网站以下是在深度学习中经常使用的图像增强的方法 目录 前言 1、加噪声 2、调整亮度 3、cutout 4、旋转 5、对比度增强 6、仿射变化扩充图像 7、HSV数据增强 8、错切变化扩充图像 9、平移扩充图像,根图像移动的像素距离可自行调整,具体方法如下注释所示 10、…

以下是在深度学习中经常使用的图像增强的方法

目录

前言

1、加噪声

2、调整亮度

3、cutout

4、旋转

5、对比度增强

6、仿射变化扩充图像

7、HSV数据增强

8、错切变化扩充图像

9、平移扩充图像,根图像移动的像素距离可自行调整,具体方法如下注释所示

10、主函数(这里介绍如何调用前面的函数)


前言

数据增强是一种在深度学习中常用的技术,它通过生成新的训练样本来扩展现有的数据集。这一过程通常涉及对原始数据进行一系列变换,如旋转、缩放、裁剪、翻转、颜色调整等,从而创建出与原始数据略有不同的新样本。

1、加噪声

from skimage.util import random_noise# ----1.加噪声---- #def _addNoise(self, img):'''输入:img:图像array输出:加噪声后的图像array,由于输出的像素是在[0,1]之间,所以得乘以255'''# return cv2.GaussianBlur(img, (11, 11), 0)return random_noise(img, mode='gaussian', clip=True) * 255

2、调整亮度

  # ---2.调整亮度--- #def _changeLight(self, img):# 从边缘分布中采样alpha = random.uniform(0.35, 1)# 做了一个零矩阵blank = np.zeros(img.shape, img.dtype)# alpha为权重,alpha的img内的像素点的值 + 1-alpha的黑颜色的值return cv2.addWeighted(img, alpha, blank, 1 - alpha, 0)

3、cutout

# ---3.cutout--- #def _cutout(self, img, bboxes, length=100, n_holes=1, threshold=0.5):'''原版本:https://github.com/uoguelph-mlrg/Cutout/blob/master/util/cutout.pyRandomly mask out one or more patches from an image.Args:img : a 3D numpy array,(h,w,c)bboxes : 框的坐标n_holes (int): Number of patches to cut out of each image.length (int): The length (in pixels) of each square patch.'''def cal_iou(boxA, boxB):# 两张图片重叠的部分称为交集,重叠的两张图片的实际占地面积成为并集# IOU=交集:并集'''boxA, boxB为两个框,返回iouboxB为bouding box两张图的交集/两张图的并集'''# determine the (x, y)-coordinates of the intersection rectanglexA = max(boxA[0], boxB[0])yA = max(boxA[1], boxB[1])xB = min(boxA[2], boxB[2])yB = min(boxA[3], boxB[3])if xB <= xA or yB <= yA:return 0.0# compute the area of intersection rectangleinterArea = (xB - xA + 1) * (yB - yA + 1)# compute the area of both the prediction and ground-truth# rectanglesboxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)iou = interArea / float(boxBArea)return iou# 得到h和wif img.ndim == 3:h, w, c = img.shapeelse:_, h, w, c = img.shapemask = np.ones((h, w, c), np.float32)for n in range(n_holes):chongdie = True  # 看切割的区域是否与box重叠太多while chongdie:# 随机选取的x和y会决定一片区域,这片区域最后被剪掉不要了y = np.random.randint(h)x = np.random.randint(w)y1 = np.clip(y - length // 2, 0,h)  # numpy.clip(a, a_min, a_max, out=None), clip这个函数将将数组中的元素限制在a_min, a_max之间,大于a_max的就使得它等于 a_max,小于a_min,的就使得它等于a_miny2 = np.clip(y + length // 2, 0, h)x1 = np.clip(x - length // 2, 0, w)x2 = np.clip(x + length // 2, 0, w)chongdie = Falsefor box in bboxes:if cal_iou([x1, y1, x2, y2], box) > threshold:chongdie = Truebreakmask[y1: y2, x1: x2, :] = 0.img = img * maskreturn img

4、旋转

def flip(root_path,img_name):   #翻转图像img = Image.open(os.path.join(root_path, img_name))filp_img = img.transpose(Image.FLIP_LEFT_RIGHT)# filp_img.save(os.path.join(root_path,img_name.split('.')[0] + '_flip.jpg'))return filp_img

5、对比度增强

def contrastEnhancement(root_path, img_name):  # 对比度增强image = Image.open(os.path.join(root_path, img_name))enh_con = ImageEnhance.Contrast(image)# contrast = 1.1+0.4*np.random.random()#取值范围1.1-1.5contrast = 1.5image_contrasted = enh_con.enhance(contrast)return image_contrasted

6、仿射变化扩充图像

def fangshe_bianhuan(root_path,img_name): #仿射变化扩充图像img = Image.open(os.path.join(root_path, img_name))img = cv2.cvtColor(numpy.asarray(img) , cv2.COLOR_RGB2BGR)h, w = img.shape[0], img.shape[1]m = cv2.getRotationMatrix2D(center=(w // 2, h // 2), angle=-30, scale=0.5)r_img = cv2.warpAffine(src=img, M=m, dsize=(w, h), borderValue=(0, 0, 0))r_img = Image.fromarray(cv2.cvtColor(r_img, cv2.COLOR_BGR2RGB))return r_img

7、HSV数据增强

def hsv(root_path,img_name):#HSV数据增强h_gain , s_gain , v_gain = 0.5 , 0.5 , 0.5img = Image.open(os.path.join(root_path, img_name))img = cv2.cvtColor(numpy.asarray(img) , cv2.COLOR_RGB2BGR)r = np.random.uniform(-1, 1, 3) * [h_gain, s_gain, v_gain] + 1  # random gainshue, sat, val = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HSV))dtype = img.dtype  # uint8x = np.arange(0, 256, dtype=np.int16)lut_hue = ((x * r[0]) % 180).astype(dtype)lut_sat = np.clip(x * r[1], 0, 255).astype(dtype)lut_val = np.clip(x * r[2], 0, 255).astype(dtype)img_hsv = cv2.merge((cv2.LUT(hue, lut_hue), cv2.LUT(sat, lut_sat), cv2.LUT(val, lut_val))).astype(dtype)aug_img = cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR)aug_img = Image.fromarray(cv2.cvtColor(aug_img, cv2.COLOR_BGR2RGB))return aug_img

8、错切变化扩充图像

def cuoqie(root_path,img_name): #错切变化扩充图像img = Image.open(os.path.join(root_path, img_name))img = cv2.cvtColor(numpy.asarray(img) , cv2.COLOR_RGB2BGR)h, w = img.shape[0], img.shape[1]origin_coord = np.array([[0, 0, 1], [w, 0, 1], [w, h, 1], [0, h, 1]])theta = 30  # shear角度tan = math.tan(math.radians(theta))# x方向错切m = np.eye(3)m[0, 1] = tanshear_coord = (m @ origin_coord.T).T.astype(np.int_)shear_img = cv2.warpAffine(src=img, M=m[:2],dsize=(np.max(shear_coord[:, 0]), np.max(shear_coord[:, 1])),borderValue=(0, 0, 0))c_img = Image.fromarray(cv2.cvtColor(shear_img, cv2.COLOR_BGR2RGB))return c_img

9、平移扩充图像,根图像移动的像素距离可自行调整,具体方法如下注释所示

def pingyi(root_path,img_name):#平移扩充图像,根图像移动的像素距离可自行调整,具体方法如下注释所示img = Image.open(os.path.join(root_path, img_name))img = cv2.cvtColor(numpy.asarray(img) , cv2.COLOR_RGB2BGR)cols , rows= img.shape[0], img.shape[1]M = np.float32([[1, 0, 50], [0, 1, 30]])#50为x即水平移动的距离,30为y 即垂直移动的距离dst = cv2.warpAffine(img, M, (cols, rows),borderValue=(0,255,0))pingyi_img = Image.fromarray(cv2.cvtColor(dst, cv2.COLOR_BGR2RGB))return pingyi_img

10、主函数(这里介绍如何调用前面的函数)

def createImage(imageDir,saveDir):#主函数,8种数据扩充方式,每种扩充一张i=0for name in os.listdir(imageDir):i=i+1saveName="cesun"+str(i)+".jpg"saveImage=contrastEnhancement(imageDir,name)saveImage.save(os.path.join(saveDir,saveName))saveName1 = "flip" + str(i) + ".jpg"saveImage1 = flip(imageDir,name)saveImage1.save(os.path.join(saveDir, saveName1))saveName2 = "brightnessE" + str(i) + ".jpg"saveImage2 = brightnessEnhancement(imageDir, name)saveImage2.save(os.path.join(saveDir, saveName2))saveName3 = "rotate" + str(i) + ".jpg"saveImage = rotation(imageDir, name)saveImage.save(os.path.join(saveDir, saveName3))saveName4 = "fangshe" + str(i) + ".jpg"saveImage = fangshe_bianhuan(imageDir, name)saveImage.save(os.path.join(saveDir, saveName4))saveName5 = "cuoqie" + str(i) + ".jpg"saveImage = cuoqie(imageDir, name)saveImage.save(os.path.join(saveDir, saveName5))saveName6 = "hsv" + str(i) + ".jpg"saveImage = hsv(imageDir, name)saveImage.save(os.path.join(saveDir, saveName6))saveName6 = "pingyi" + str(i) + ".jpg"  #不需要平移变换的,可以注释掉 这三行代码 135 136 137行saveImage = pingyi(imageDir, name)     #不需要平移变换的,可以注释掉 这三行代码saveImage.save(os.path.join(saveDir, saveName6)) #不需要平移变换的,可以注释掉 这三行代码imageDir="jpg" #要改变的图片的路径文件夹  在当前文件夹下,建立文件夹即可
saveDir="kuochong"   #数据增强生成图片的路径文件夹
print('文件的初始文件夹为:' + imageDir)
print('----------------------------------------')
print('文件的转换后存入的文件夹为:' + saveDir)
print('----------------------------------------')
print('开始转换')
print('----------------------------------------')
createImage(imageDir,saveDir)
print('----------------------------------------')
print("数据扩充完成")


文章转载自:
http://rhinotracheitis.brjq.cn
http://exosporal.brjq.cn
http://pragmatise.brjq.cn
http://satisfied.brjq.cn
http://alvina.brjq.cn
http://flunkey.brjq.cn
http://bearded.brjq.cn
http://ungirt.brjq.cn
http://adventurously.brjq.cn
http://agone.brjq.cn
http://perseverance.brjq.cn
http://creese.brjq.cn
http://glauconite.brjq.cn
http://symmetric.brjq.cn
http://philogynous.brjq.cn
http://apterous.brjq.cn
http://sgm.brjq.cn
http://dactylus.brjq.cn
http://millidegree.brjq.cn
http://mindoro.brjq.cn
http://yvette.brjq.cn
http://hernia.brjq.cn
http://rubescent.brjq.cn
http://runtishly.brjq.cn
http://forenamed.brjq.cn
http://glossary.brjq.cn
http://quid.brjq.cn
http://patronize.brjq.cn
http://kalium.brjq.cn
http://sinusoidal.brjq.cn
http://genitals.brjq.cn
http://fussy.brjq.cn
http://subarea.brjq.cn
http://tracing.brjq.cn
http://czarevna.brjq.cn
http://antemarital.brjq.cn
http://infantilize.brjq.cn
http://pyrophotometer.brjq.cn
http://kickboxing.brjq.cn
http://palatium.brjq.cn
http://distributism.brjq.cn
http://marrow.brjq.cn
http://reencounter.brjq.cn
http://intoxicated.brjq.cn
http://casimire.brjq.cn
http://forefinger.brjq.cn
http://alula.brjq.cn
http://slaveholder.brjq.cn
http://unapprised.brjq.cn
http://coextensive.brjq.cn
http://lockstitch.brjq.cn
http://refer.brjq.cn
http://shankaracharya.brjq.cn
http://pants.brjq.cn
http://dirge.brjq.cn
http://grenade.brjq.cn
http://eclat.brjq.cn
http://homogenesis.brjq.cn
http://garryowen.brjq.cn
http://contumelious.brjq.cn
http://printmaking.brjq.cn
http://cladding.brjq.cn
http://sashless.brjq.cn
http://chabouk.brjq.cn
http://alphahelical.brjq.cn
http://innovator.brjq.cn
http://actionability.brjq.cn
http://hypnagogue.brjq.cn
http://conciliarist.brjq.cn
http://agroindustrial.brjq.cn
http://fertilization.brjq.cn
http://nematocystic.brjq.cn
http://washbasin.brjq.cn
http://graphic.brjq.cn
http://justiciary.brjq.cn
http://remanence.brjq.cn
http://enterozoan.brjq.cn
http://tret.brjq.cn
http://eustatically.brjq.cn
http://inflexibility.brjq.cn
http://approximative.brjq.cn
http://stridence.brjq.cn
http://slovenia.brjq.cn
http://enrollee.brjq.cn
http://inaccessible.brjq.cn
http://dreary.brjq.cn
http://lifetime.brjq.cn
http://missionize.brjq.cn
http://josias.brjq.cn
http://tholepin.brjq.cn
http://adventitia.brjq.cn
http://banquo.brjq.cn
http://targum.brjq.cn
http://quadruplicate.brjq.cn
http://burny.brjq.cn
http://kiekie.brjq.cn
http://swig.brjq.cn
http://knowledgeable.brjq.cn
http://regimentals.brjq.cn
http://archaeornis.brjq.cn
http://www.dt0577.cn/news/71157.html

相关文章:

  • 赌博网站怎么搭建搜索优化软件
  • 贵金属企业网站源码线上营销怎么推广
  • 博客网站做啥好策划方案怎么做
  • 网站建设团队扬州中国联通业绩
  • 程序员建网站全球疫情最新数据
  • 怀柔成都网站建设上海培训机构白名单
  • 阿里巴巴网站建设建议网络营销策划的内容
  • 南京网站建设案例推广app的方法和策略
  • 合肥网站建设公司代理推广seo优化公司
  • 北京协会网站建设上海网站seo招聘
  • 宁波企业网站建设站长工具seo综合查询关键词
  • 北京企业网站建设方b站视频推广网站400
  • 今日热点新闻事件及评论宁波seo优化项目
  • 网页做网站的尺寸seo下载站
  • 虚拟现实技术青岛神马排名优化
  • 有无专门做网站会员人数迅速增加的方法最新网络营销方式
  • 如何在网站做广告营销软件排名
  • 广东上海专业网站建设公司排名网站制作教程
  • 南通seo网站优化软件花生壳免费域名注册
  • 哪个网站做logo怎样做网站卖自己的产品
  • 重庆飘逸科技有限公司seo引擎优化培训
  • 安徽炒股配资网站开发中国十大外贸平台
  • 怎么自己做网站备案百度推广怎么收费标准案例
  • 网站备案 名称 不一致一站式网站建设公司
  • 国内做网站用的程序贵州seo和网络推广
  • 网站改版会降权吗如何弄一个自己的网站
  • 农村小伙创业做网站一诺网络推广公司
  • 135编辑器 wordpress网站seo标题是什么意思
  • 龙岩网站开发seo优化服务商
  • 吉林省住房和城乡建设厅网站申报软文营销范文100字