zoukankan      html  css  js  c++  java
  • 深度学习的数据增强(亮度,对比度,旋转)

    原博客搬移到:https://blog.csdn.net/u013171226/article/details/107680285 

    import os,cv2,shutil
    import numpy as np
    import random
    
    #对比度和亮度
    def Contrast_and_Brightness(alpha, beta, img):
        blank = np.zeros(img.shape, img.dtype)
        # dst = alpha * img + beta * blank
        dst = cv2.addWeighted(img, alpha, blank, 1-alpha, beta)
        return dst
    
    
    def darker(image,percetage):
        image_copy = image.copy()
        w = image.shape[1]
        h = image.shape[0]
        #get darker
        for xi in range(0,w):
            for xj in range(0,h):
                image_copy[xj,xi,0] = int(image[xj,xi,0]*percetage)
                image_copy[xj,xi,1] = int(image[xj,xi,1]*percetage)
                image_copy[xj,xi,2] = int(image[xj,xi,2]*percetage)
        return image_copy
    
    def brighter(image, percetage):
        w = image.shape[1]
        h = image.shape[0]
        #get brighter
        for xi in range(0,w):
            for xj in range(0,h):
                image[xj,xi,0] = np.clip(int(image[xj,xi,0]*percetage),a_max=255,a_min=0)
                image[xj,xi,1] = np.clip(int(image[xj,xi,1]*percetage),a_max=255,a_min=0)
                image[xj,xi,2] = np.clip(int(image[xj,xi,2]*percetage),a_max=255,a_min=0)
        return image
    
    #旋转
    def rotate(image, angle, scale=1):
        w = image.shape[1]
        h = image.shape[0]
        #rotate matrix
        M = cv2.getRotationMatrix2D((w/2,h/2), angle, scale)
        #rotate
        image = cv2.warpAffine(image,M,(w,h))
        return image
    
    #高斯噪声
    def addGaussianNoise(image,percetage):
        G_Noiseimg = image.copy()
        w = image.shape[1]
        h = image.shape[0]
        G_NoiseNum=int(percetage*image.shape[0]*image.shape[1])
        for i in range(G_NoiseNum):
            temp_x = np.random.randint(0,h)
            temp_y = np.random.randint(0,w)
            G_Noiseimg[temp_x][temp_y][np.random.randint(3)] = np.random.randn(1)[0]
        return G_Noiseimg
    
    #翻转
    def flip(img,angle):
        fliped=cv2.flip(img,angle)
        return fliped
    
    def img_augmentation(path):
        if path[-3:]=="jpg":
            print("ok")
            img=cv2.imread(path)
            c=random.uniform(0.7,1.3)
            b=random.uniform(-60,60)
    
            c_img=Contrast_and_Brightness(c,0,img)
            b_img=Contrast_and_Brightness(1,b,img)
    
            #b=random.uniform(1,1.5)
            #img_brighter=brighter(img,b)
            #d=random.uniform(0.6,0.95)
            #img_dark=darker(img,d)
    
            angele_list=[-180,-90,-45,45,90,180]
            a=random.choice(angele_list)
            rotate_img=rotate(img,a)
            flip_list=[-1,0,1]
            f_p=random.choice(flip_list)
            flip_img=flip(img,f_p)
    
            (file_path,pic_name)=os.path.split(path)
            b_name="bright_"+pic_name #亮度
            c_name="contrast_"+pic_name #对比度
            r_name="rotate_"+pic_name #旋转
            f_name="flip_"+pic_name #翻转
    
            print(os.path.join(file_path,b_name))
            print(os.path.join(file_path,c_name))
            cv2.imwrite(os.path.join(file_path,b_name),b_img)
            cv2.imwrite(os.path.join(file_path,c_name),c_img)
            cv2.imwrite(os.path.join(file_path,r_name),rotate_img)
            cv2.imwrite(os.path.join(file_path,f_name),flip_img)
    
    if __name__ == "__main__":
        path=''#改成自己的path
        # for dir_name in os.listdir(path):
        #     dir_path=os.path.join(path,dir_name)
        #     for dir1_name in os.listdir(dir_path):
        #         dir1_path=os.path.join(dir_path,dir1_name)
        for image_name in os.listdir(path):
            image_path = os.path.join(path, image_name)
            print(image_path)
            img_augmentation(image_path)

    作者:cumtchw
    出处:http://www.cnblogs.com/cumtchw/
    我的博客就是我的学习笔记,学习过程中看到好的博客也会转载过来,若有侵权,与我联系,我会及时删除。

  • 相关阅读:
    水晶报表关于System.Web.Extensions报错的问题
    个人下一步学习计划
    一个老程序员对数据库的一点纠结
    Visual SourceSafe权限配置记录
    SQL SERVER 2008代码折叠小技巧
    用命令行自动备份数据库到其他服务器
    CrystalReports 2008序列号留档
    ····
    C语言中的static
    页面自动刷新的几种方法
  • 原文地址:https://www.cnblogs.com/cumtchw/p/12574978.html
Copyright © 2011-2022 走看看