您的位置:首页 > 新闻 > 资讯 > gitlab中文官网下载_黄页日历今天适合什么_软文标题例子_百度在线咨询

gitlab中文官网下载_黄页日历今天适合什么_软文标题例子_百度在线咨询

2024/10/9 3:19:32 来源:https://blog.csdn.net/m0_58941767/article/details/142691929  浏览:    关键词:gitlab中文官网下载_黄页日历今天适合什么_软文标题例子_百度在线咨询
gitlab中文官网下载_黄页日历今天适合什么_软文标题例子_百度在线咨询

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

目录

前言

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("数据扩充完成")

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com