精华内容
下载资源
问答
  • 由这幅台风图像扩增来看,从1999扩增到2048极大的改变了原图像,且两种扩增方式在变化上极其相似,但是双三次插值所需要的时间是最近邻插值的139.9/1.5≈93.26倍之多,当然限于以上程序是使用双层for循环加上多个...

    首先看一下原图像

     

    先用最近邻插值法

     

     

     

     

     

     

     

     由这幅台风图像的扩增来看,从1999扩增到2048极大的改变了原图像,且两种扩增方式在变化上极其相似,但是双三次插值所需要的时间是最近邻插值的139.9/1.5≈93.26倍之多,当然限于以上程序是使用双层for循环加上多个if判断,应该有改进的空间

     

    转载于:https://www.cnblogs.com/NWNU-LHY/p/11463206.html

    展开全文
  • 深度学习:图像扩增方法

    千次阅读 2019-05-03 15:14:00
    常见的模型输入一般为固定大小的图像输入,而数据集中的图像常常是不规则大小的图像,因此,对于大小不规则的图像需要放缩至固定大小,而直接使用resize()函数会使得图像变形,因此需要对图像继续填充后继续放缩。...

    一个良好的图像预处理能够有效提升模型的准确率。本文总结了常用的图像预处理方法。

    常见的模型输入一般为固定大小的图像输入,而数据集中的图像常常是不规则大小的图像,因此,对于大小不规则的图像需要放缩至固定大小,而直接使用resize()函数会使得图像变形,因此需要对图像继续填充后继续放缩。

    图像大小变化

    import cv2
    import numpy as np
    def preprocess(img, imgsize, jitter, random_placing=False):
        """
        Image preprocess for yolo input
        Pad the shorter side of the image and resize to (imgsize, imgsize)
        Args:
            img (numpy.ndarray): input image whose shape is :math:`(H, W, C)`.
                Values range from 0 to 255.
            imgsize (int): target image size after pre-processing
            jitter (float): amplitude of jitter for resizing
            random_placing (bool): if True, place the image at random position
    
        Returns:
            img (numpy.ndarray): input image whose shape is :math:`(C, imgsize, imgsize)`.
                Values range from 0 to 1.
            info_img : tuple of h, w, nh, nw, dx, dy.
                h, w (int): original shape of the image
                nh, nw (int): shape of the resized image without padding
                dx, dy (int): pad size
        """
        h, w, _ = img.shape
        img = img[:, :, ::-1]
        assert img is not None
        #尺寸大小的随机抖动,jitter越大,长宽的的变化越大
        if jitter > 0:
            # add jitter
            dw = jitter * w
            dh = jitter * h
            new_ar = (w + np.random.uniform(low=-dw, high=dw))\
                     / (h + np.random.uniform(low=-dh, high=dh))
        else:
            new_ar = w / h
    
        if new_ar < 1:
            nh = imgsize
            nw = nh * new_ar
        else:
            nw = imgsize
            nh = nw / new_ar
        nw, nh = int(nw), int(nh)
        #图像填充位置的随机性
        if random_placing:
            dx = int(np.random.uniform(imgsize - nw))
            dy = int(np.random.uniform(imgsize - nh))
        else:
            dx = (imgsize - nw) // 2
            dy = (imgsize - nh) // 2
    
        img = cv2.resize(img, (nw, nh))
        sized = np.ones((imgsize, imgsize, 3), dtype=np.uint8) * 127
        sized[dy:dy+nh, dx:dx+nw, :] = img
    
        info_img = (h, w, nh, nw, dx, dy)
        return sized, info_img
    
    jitter=0
    andom_placing=False
    img_size=416
    img=cv2.imread('data/1.jpg')
    print(img.shape)
    sized, info_img=preprocess(img, img_size, jitter=jitter,random_placing=andom_placing)
    print(sized.shape)
    sized=sized[:,:,::-1]
    cv2.imshow('imgs',img)
    cv2.imshow('img',sized)
    cv2.waitKey()
    
    

    #jitter为尺寸大小的随机抖动,jitter参数越大,长宽的的变化越大。

    原图

    抖动幅度为0.9

    抖动幅度为0.1

    andom_placing为图像位置,上面图像为andom_placing=False,因此在两侧均匀填充空白。

    打开后可以看到填充位置偏左,图像位置偏右。

    图像翻转

    def random_flip(img, y_random=False, x_random=False,
                    return_param=False, copy=False):
     
        y_flip, x_flip = False, False
        if y_random:
            y_flip = random.choice([True, False])
        if x_random:
            x_flip = random.choice([True, False])
     
        if y_flip:
            img = img[:, ::-1, :]
        if x_flip:
            img = img[:, :, ::-1]
     
        if copy:
            img = img.copy()
     
        if return_param:
            return img, {'y_flip': y_flip, 'x_flip': x_flip}
        else:
            return img
    

    建议只进行左右翻转,如果是目标检测任务或者目标分割任务,标签也要进行相对应的处理,下面给出目标检测标签的翻转。

    def flip_bbox(bbox, size, y_flip=False, x_flip=False):
     
        H, W = size
        bbox = bbox.copy()
        if y_flip:
            y_max = H - bbox[:, 0]
            y_min = H - bbox[:, 2]
            bbox[:, 0] = y_min
            bbox[:, 2] = y_max
        if x_flip:
            x_max = W - bbox[:, 1]
            x_min = W - bbox[:, 3]
            bbox[:, 1] = x_min
            bbox[:, 3] = x_max
        return bbox
    

     Random Distortion

    先讲RGB色彩空间转换乘HSV空间(H:图像的色彩/色度;S:图像的饱和度;V:图像的亮度)

    def random_distort(img, hue, saturation, exposure):
        """
        perform random distortion in the HSV color space.
        Args:
            img (numpy.ndarray): input image whose shape is :math:`(H, W, C)`.
                Values range from 0 to 255.
            hue (float): random distortion parameter.
            saturation (float): random distortion parameter.
            exposure (float): random distortion parameter.
        Returns:
            img (numpy.ndarray)
        """
        #hue 调整色彩度,越大色彩度变化的程度越大;sat 调整对比度,越大对比度变化越大; exp调整亮度
        dhue = np.random.uniform(low=-hue, high=hue)
        dsat = rand_scale(saturation)
        dexp = rand_scale(exposure)
    
        img = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
        img = np.asarray(img, dtype=np.float32) / 255.
        img[:, :, 1] *= dsat
        img[:, :, 2] *= dexp
        H = img[:, :, 0] + dhue
    
        if dhue > 0:
            H[H > 1.0] -= 1.0
        else:
            H[H < 0.0] += 1.0
    
        img[:, :, 0] = H
        img = (img * 255).clip(0, 255).astype(np.uint8)
        img = cv2.cvtColor(img, cv2.COLOR_HSV2RGB)
        img = np.asarray(img, dtype=np.float32)
    
        return img

    随机乘除

    def rand_scale(s):
        #乘或者除一定倍数
        """
        calculate 
        random scaling factor
        Args:
            s (float): range of the random scale.
        Returns:
            random scaling factor (float) whose range is
            from 1 / s to s .
        """
        scale = np.random.uniform(low=1, high=s)
        if np.random.rand() > 0.5:
            return scale
        return 1 / scale
    hue=0.8
    saturation=1.5
    exposure=1.0
    img2 = random_distort(sized, hue, saturation, exposure)
    sized=sized[:,:,::-1]
    img2=img2[:,:,::-1]
    cv2.imwrite('img_or.jpg',sized)
    cv2.imwrite('img_dis.jpg',img2)

    通过HSV空间变换可以达到不改变图像大小的情况下扩增图像,但是存在一个问题,该变换可能改变现实图像的表象,比如人的皮肤是黑,黄,白,可能因为变换而变成绿色,不符合现实意义,但是这样的变换可能有利于模型的鲁棒性,以及应对对抗攻击。应对对抗攻击和扩增图像的另外一个方法是mixup方法。个人觉得如果没有对抗攻击的可能,只对亮度进行变换的的图像扩增比较合理(这里只对图像HSV变换后的img[:,:,2]进行增减变换就可以了)。

     

     

    变换结果

    完(笑)

     

     

     

     

     

     


     

    展开全文
  • #对单张图像进行处理 for eachline in read_data.split('\n'): img_path, label = os.path.split(eachline) img_full_path = os.path.join(data_dir, img_path) if(os.path.exists(img_full_path) == False): ...
    import cv2
    import numpy as np
    import os
    img = cv2.imread('E:/pycharm_workspace/python_data_process/pic/fox.jpg')
    
    def rotate_bound(image, angle):
        # grab the dimensions of the image and then determine the
        # center
        (h, w) = image.shape[:2]
        (cX, cY) = (w // 2, h // 2)
    
        # grab the rotation matrix (applying the negative of the
        # angle to rotate clockwise), then grab the sine and cosine
        # (i.e., the rotation components of the matrix)
        M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
        cos = np.abs(M[0, 0])
        sin = np.abs(M[0, 1])
    
        # compute the new bounding dimensions of the image
        nW = int((h * sin) + (w * cos))
        nH = int((h * cos) + (w * sin))
    
        # adjust the rotation matrix to take into account translation
        M[0, 2] += (nW / 2) - cX
        M[1, 2] += (nH / 2) - cY
    
        # perform the actual rotation and return the image
        return cv2.warpAffine(image, M, (nW, nH))
    
    
    def process_train_set(data_dir, save_dir, list_file_name):
        file_list = os.path.join(data_dir, list_file_name)
    
        with open(file_list, "r+") as flist:
            read_data = flist.read()
            flist.truncate()#清空文件
            num=0
            #对单张图像进行处理
            for eachline in read_data.split('\n'):
    
                img_path, label = os.path.split(eachline)
                img_full_path = os.path.join(data_dir, img_path)
                if(os.path.exists(img_full_path) == False):
                    continue
                # 翻转操作(2种)
                img_path_temp = FlipOperation(eachline,label, save_dir)
    
                # 旋转90和180度
                img_path_temp = Rotation(eachline, label, save_dir)
    
                #对比度增强
                #直方图正规化
                img_path_temp = Normalize_image(eachline, label, save_dir)
    
                # 伽马变换
                img_path_temp = gamma_image(eachline, label, save_dir)
    
                num=num+1
                print("The "+str(num)+" th")
            print("Image argument finished!")
    
    def FlipOperation(eachline,label, save_dir):
        middle_name = 'Flip'
        new_file_path = os.path.join(save_dir, middle_name)
        if not os.path.exists(new_file_path):
            os.makedirs(new_file_path)
    
        img=cv2.imread(eachline)
        #水平翻转
        flipped1=cv2.flip(img,1)
        cv2.imwrite(new_file_path + "/"+"Hor_" + label,flipped1)
    
        #垂直翻转
        flipped2 = cv2.flip(img, 0)
        cv2.imwrite(new_file_path + "/" + "Vec_" + label, flipped2)
    
        return new_file_path
    
    #旋转90和180度
    def Rotation(eachline,label, save_dir):
        middle_name = 'Rotation'
        new_file_path = os.path.join(save_dir, middle_name)
        if not os.path.exists(new_file_path):
            os.makedirs(new_file_path)
    
        img = cv2.imread(eachline)
        rows,cols=img.shape[:2]
    
        R_image1=cv2.getRotationMatrix2D((rows/2,cols/2),90,1)
        dst = cv2.warpAffine(img, R_image1, (cols, rows))
    
        dst = cv2.convertScaleAbs(img, alpha=1.5, beta=0.2)
    
        R_image2 = cv2.getRotationMatrix2D((rows / 2, cols / 2), 180, 1)
        dst1 = cv2.warpAffine(img, R_image2, (cols, rows))
        cv2.imwrite(new_file_path + "/" + "R180_" + label, dst1)
        return new_file_path
    
    #缩放
    def Resize(eachline,label, save_dir):
        middle_name = 'Resize'
        new_file_path = os.path.join(save_dir, middle_name)
        if not os.path.exists(new_file_path):
            os.makedirs(new_file_path)
    
        img = cv2.imread(eachline)
        result=cv2.resize(img,None,fx=2,fy=2,interpolation=cv2.INTER_CUBIC)
        cv2.imwrite(new_file_path + "/" + "2times_" + label, result)
        return new_file_path
    
    #任意角度翻转
    def rotate_bound(image, angle):
        # grab the dimensions of the image and then determine the
        # center
        (h, w) = image.shape[:2]
        (cX, cY) = (w // 2, h // 2)
    
        M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)
        cos = np.abs(M[0, 0])
        sin = np.abs(M[0, 1])
    
        # compute the new bounding dimensions of the image
        nW = int((h * sin) + (w * cos))
        nH = int((h * cos) + (w * sin))
    
        # adjust the rotation matrix to take into account translation
        M[0, 2] += (nW / 2) - cX
        M[1, 2] += (nH / 2) - cY
    
        # perform the actual rotation and return the image
        return cv2.warpAffine(image, M, (nW, nH))
    
    #线性增强
    def linear_enhance(eachline,label, save_dir):
        middle_name = 'linear'
        new_file_path = os.path.join(save_dir, middle_name)
        if not os.path.exists(new_file_path):
            os.makedirs(new_file_path)
    
        img = cv2.imread(eachline)
        dst = cv2.convertScaleAbs(img, alpha=1.5, beta=0)
        cv2.imwrite(new_file_path + "/" + label, dst)
    
    #直方图正规化--增强亮度
    def Normalize_image(eachline,label, save_dir):
        middle_name = 'normalize'
        new_file_path = os.path.join(save_dir, middle_name)
        if not os.path.exists(new_file_path):
            os.makedirs(new_file_path)
    
        img = cv2.imread(eachline)
        dst = cv2.normalize(img, dst=None, alpha=350, beta=10, norm_type=cv2.NORM_MINMAX)
        cv2.imwrite(new_file_path + "/" + label, dst)
    
    #伽马变换--能提高对比度
    def gamma_image(eachline,label, save_dir):
        middle_name = 'gamma'
        new_file_path = os.path.join(save_dir, middle_name)
        if not os.path.exists(new_file_path):
            os.makedirs(new_file_path)
    
        img = cv2.imread(eachline)
        img_norm = img / 255.0  # 注意255.0得采用浮点数
        img_gamma = np.power(img_norm, 0.4) * 255.0
        dst = img_gamma.astype(np.uint8)
    
        cv2.imwrite(new_file_path + "/" + label, dst)
    
    #全局直方图均衡化--灰度图
    def EqualizeHist(eachline,label, save_dir):
        middle_name = 'EqualizeHist-gray'
        new_file_path = os.path.join(save_dir, middle_name)
        if not os.path.exists(new_file_path):
            os.makedirs(new_file_path)
        img = cv2.imread(eachline,0)
        dst = cv2.equalizeHist(img)
    
        cv2.imwrite(new_file_path + "/" + label, dst)
    
    #全局直方图均衡化--彩色图像
    def EqualizeHist(eachline,label, save_dir):
        middle_name = 'EqualizeHist-color'
        new_file_path = os.path.join(save_dir, middle_name)
        if not os.path.exists(new_file_path):
            os.makedirs(new_file_path)
        img = cv2.imread(eachline)
        img0 = cv2.equalizeHist(img[:, :, 0])  # 各个通道分别均衡化
        img1 = cv2.equalizeHist(img[:, :, 1])
        img2 = cv2.equalizeHist(img[:, :, 2])
        dst = cv2.merge([img0, img1, img2])
        cv2.imwrite(new_file_path + "/" + label, dst)
    
    data_dir="E:/pycharm_workspace/python_data_process/"
    list_file_name ="image.txt"
    save_dir="E:/pycharm_workspace/python_data_process/pic_argument1/"
    process_train_set(data_dir,save_dir,list_file_name)
    

     

    展开全文
  • new_folder, 同时创建相同的字目录, 然后将 root_folder 下的图像处理保存到 new_folder 下相同自路径下第三个参数是 new_folder 见root_folder第四个参数是 max_workers 线程数量 0 为单线程'''

    # encoding=utf-8

    import os

    import imageio

    import random

    import numpy as np

    import imgaug as ia

    import concurrent.futures

    from imgaug import augmenters as iaa

    import matplotlib.pyplot as plt

    from argparse import ArgumentParser

    ia.seed(4)

    # %matplotlib inline

    # 参考 https://github.com/aleju/imgaug

    func_norm = iaa.ContrastNormalization(alpha=(0.5, 1.5), per_channel=False)

    func_add = iaa.Add(value=(-30, 40), per_channel=False)

    func_hue_saturation = iaa.AddToHueAndSaturation(

    value=(-35, 30), per_channel=False)

    func_multiply = iaa.Multiply(mul=(0.8, 1.5), per_channel=False)

    func_gamma = iaa.GammaContrast(gamma=(0.8, 1.5), per_channel=False)

    func_log = iaa.LogContrast(gain=(0.7, 1.2), per_channel=False)

    func_sigmoid = iaa.SigmoidContrast(gain=(2, 5), cutoff=0.5, per_channel=False)

    func_coarse = iaa.CoarseDropout(

    p=(0.1, 0.2), size_percent=0.1, per_channel=False)

    switch_func = {

    0: func_norm,

    1: func_add,

    2: func_hue_saturation,

    3: func_multiply,

    4: func_gamma,

    5: func_log,

    6: func_sigmoid,

    7: func_coarse

    }

    def getFilePath(root_path, file_list):

    dir_or_files = os.listdir(root_path)

    for dir_file in dir_or_files:

    dir_file_path = os.path.join(root_path, dir_file)

    if os.path.isdir(dir_file_path):

    getFilePath(dir_file_path, file_list)

    else:

    file_list.append(dir_file_path)

    def getFilePath2(root_path, file_list, folder_list):

    dir_or_files = os.listdir(root_path)

    for dir_file in dir_or_files:

    dir_file_path = os.path.join(root_path, dir_file)

    if os.path.isdir(dir_file_path):

    folder_list.append(dir_file_path)

    getFilePath2(dir_file_path, file_list, folder_list)

    else:

    file_list.append(dir_file_path)

    def getFilePathFilters(root_path, file_list, dir_filters):

    dir_or_files = os.listdir(root_path)

    for dir_file in dir_or_files:

    dir_file_path = os.path.join(root_path, dir_file)

    if os.path.isdir(dir_file_path):

    if dir_file in dir_filters:

    getFilePathFilters(dir_file_path, file_list, dir_filters)

    else:

    file_list.append(dir_file_path)

    def makeDir(root_path):

    if not os.path.exists(root_path):

    os.makedirs(root_path)

    def showImage(image):

    image = np.array(image)

    plt.imshow(image)

    plt.show()

    def saveImage(image, write_path):

    imageio.imwrite(write_path, image)

    def augmentFunc(image, index):

    return switch_func[index].augment_image(image)

    # 多线程执行函数

    def augExecute(file_path):

    new_file_path = file_path.replace(args.root_folder, args.new_folder)

    print(new_file_path, end='\n')

    image = imageio.imread(file_path)

    index = random.randint(0, 7)

    new_image = augmentFunc(image, index)

    # showImage(new_image)

    saveImage(new_image, new_file_path)

    def colorImageAugment(augment_list, max_workers, dir_dict):

    for augment_folder in augment_list:

    file_list = []

    folder_list = []

    getFilePath2(augment_folder, file_list, folder_list)

    # 创建新目录文件夹及其子文件夹

    for folder_path in folder_list:

    new_folder_path = folder_path.replace(

    args.root_folder, args.new_folder)

    makeDir(new_folder_path)

    if max_workers == 0:

    # 单线程

    for file_path in file_list:

    augExecute(file_path)

    else:

    # 多线程

    with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:

    for _ in executor.map(augExecute, file_list):

    pass

    def main(args):

    aug_folder_list = args.image_folder_path

    dir_dict = {'root_dir': args.root_folder, 'trans_dir': args.new_folder}

    max_workers = int(args.max_workers)

    print("Start to augment image")

    colorImageAugment(aug_folder_list, max_workers, dir_dict)

    print("The end")

    if __name__ == "__main__":

    parser = ArgumentParser(description="Augment color image")

    parser.add_argument(

    "image_folder_path", nargs='+', help="Absolute path of image folder that going to augmentation")

    parser.add_argument(

    "root_folder", type=str, help="The folder and file under the root_folder is going to augmentation")

    parser.add_argument("new_folder", type=str,

    help="The folder that going to be build")

    parser.add_argument("max_workers", type=int,

    default=9, help="Multi thread num")

    args = parser.parse_args()

    main(args)

    '''Usepython data_aug.py /your/path/to/augmentation/train/subdir1/ /your/path/to/augmentation/train/subdir2/ ... /your/path/to/augmentation/train/subdirn/ train train_aug第一个参数是 image_folder_path 需要处理的文件夹 要求绝对路径 可以传入多个第二个参数是 root_folder 处理文件夹的父级目录 程序会将该目录同级路径下创建 new_folder, 同时创建相同的字目录, 然后将 root_folder 下的图像处理保存到 new_folder 下相同自路径下第三个参数是 new_folder 见root_folder第四个参数是 max_workers 线程数量 0 为单线程'''

    展开全文
  • 图像的一些扩增方法

    2020-09-08 17:29:50
    制作数据集时,直接对原始文件夹中的图像扩增使用的方法,使用了PIL库中的Image函数 from PIL import Image #读取图像 im=Image.open('文件名') 1.缩放 im_resized=im.resize(32,32) 2.旋转 #逆时针90 im_90...
  • 图像数据扩增方法

    2021-02-23 22:34:48
    数据扩增的方法 opencv 数据扩增 albumentations数据扩增 数据扩增的目的: 有效增加样本的语义空间,增加数据集中样本的数据量 数据扩增的分类 标签不变的数据扩增方法 标签改编的数据扩增方法 常见的数据扩增...
  • 数据扩增的概念数据扩增是指不实际增加原始数据,只是对原始数据做一些变换,从而...扩增后的标签保持不变数据扩增的方法数据扩增方法可分为单样本扩增和多样本扩增单样本扩增包括:图像翻转、图像旋转、图像扭曲、...
  • 当现有数据不够多,而寻找更多数据又不好实现的时候,我们采取以下数据增强或者数据扩增方法。 数据增强也叫数据扩增,它希望在不实质性的增加数据的情况下,让有限的数据产生等价于更多数据的价值。比如说,我们...
  • python实现图像数据扩增

    千次阅读 2017-10-25 11:37:33
    包括平移,翻转,旋转,调整对比度,高斯噪声,颜色变换import skimage import io,os import matplotlib.pyplot as plt ...#root_path为图像根目录,img_name为图像名字def move(root_path,img_name,off): #平移,平
  • 深度学习中的图像数据扩增(Data Augmentations)方法总结:传统扩增方法及应用 1. 前言 这篇文章主要参考 A survey on Image Data Augmentation for Deep Learning, 结合 pytorch 和总结了常用的传统扩增方法及其...
  • 图像数据读取与扩增

    2020-05-23 11:41:05
    这里写自定义目录标题一、图像数据的读取1.matplotlib库2.pillow库pillow库的概述pillow库进行图像处理 一、图像数据的读取 1.matplotlib库 import matplotlib.pyplot as plt img=plt.imread('.../1.png') print(img...
  • img import os os.environ['KERAS_BACKEND'] = 'tensorflow' def date_enhancement(img_input_path, img_output_path): image = load_img(img_input_path) image = img_to_array(image) # 图像转为数组 image = np....

空空如也

空空如也

1 2 3 4 5 ... 18
收藏数 341
精华内容 136
关键字:

图像扩增