对HR图片进行退化生成lq图片

一、degradation_bsrgan_plus函数

1.the degradation models of BSRGAN and Real-ESRGAN

代码如下:

def degradation_bsrgan_plus(img, sf=4, shuffle_prob=0.5, use_sharp=True, lq_patchsize=64, isp_model=None, crop=True):
    """
    This is an extended degradation model by combining
    the degradation models of BSRGAN and Real-ESRGAN
    ----------
    img: HXWXC, [0, 1], its size should be large than (lq_patchsizexsf)x(lq_patchsizexsf)
    sf: scale factor
    use_shuffle: the degradation shuffle
    use_sharp: sharpening the img

    Returns
    -------
    img: low-quality patch, size: lq_patchsizeXlq_patchsizeXC, range: [0, 1]
    hq: corresponding high-quality patch, size: (lq_patchsizexsf)X(lq_patchsizexsf)XC, range: [0, 1]
    """

    h1, w1 = img.shape[:2]
    img = img.copy()[:h1 - h1 % sf, :w1 - w1 % sf, ...]  # mod crop
    h, w = img.shape[:2]

    if h < lq_patchsize*sf or w < lq_patchsize*sf:
        raise ValueError(f'img size ({h1}X{w1}) is too small!')

    if use_sharp:
        img = add_sharpening(img)
    hq = img.copy()

    if random.random() < shuffle_prob:
        shuffle_order = random.sample(range(13), 13)
    else:
        shuffle_order = list(range(13))
        # local shuffle for noise, JPEG is always the last one
        shuffle_order[2:6] = random.sample(shuffle_order[2:6], len(range(2, 6)))
        shuffle_order[9:13] = random.sample(shuffle_order[9:13], len(range(9, 13)))

    poisson_prob, speckle_prob, isp_prob = 0.1, 0.1, 0.1

    for i in shuffle_order:
        if i == 0:
            img = add_blur(img, sf=sf)
        elif i == 1:
            img = add_resize(img, sf=sf)
        elif i == 2:
            img = add_Gaussian_noise(img, noise_level1=2, noise_level2=25)
        elif i == 3:
            if random.random() < poisson_prob:
                img = add_Poisson_noise(img)
        elif i == 4:
            if random.random() < speckle_prob:
                img = add_speckle_noise(img)
        elif i == 5:
            if random.random() < isp_prob and isp_model is not None:
                with torch.no_grad():
                    img, hq = isp_model.forward(img.copy(), hq)
        elif i == 6:
            img = add_JPEG_noise(img)
        elif i == 7:
            img = add_blur(img, sf=sf)
        elif i == 8:
            img = add_resize(img, sf=sf)
        elif i == 9:
            img = add_Gaussian_noise(img, noise_level1=2, noise_level2=25)
        elif i == 10:
            if random.random() < poisson_prob:
                img = add_Poisson_noise(img)
        elif i == 11:
            if random.random() < speckle_prob:
                img = add_speckle_noise(img)
        elif i == 12:
            if random.random() < isp_prob and isp_model is not None:
                with torch.no_grad():
                    img, hq = isp_model.forward(img.copy(), hq)
        else:
            print('check the shuffle!')

    # resize to desired size
    img = cv2.resize(img, (int(1/sf*hq.shape[1]), int(1/sf*hq.shape[0])), interpolation=random.choice([1, 2, 3]))

    # add final JPEG compression noise
    img = add_JPEG_noise(img)

    # random crop
    if crop:
        img, hq = random_crop(img, hq, sf, lq_patchsize)

    return img, hq

2.不随机裁剪图片

输入图片并检验长宽是否为偶数,若不为偶数,则将其边为偶数:

if __name__ == '__main__':
    '''
    批量处理图片
    '''
    file_root = '/home/iecy/pycharm/Openbayes/QuanTexSR-main/datasets/raw_image/'  # 当前文件夹下的所有图片
    file_list = os.listdir(file_root)
    save_out_hq = "/home/iecy/pycharm/Openbayes/QuanTexSR-main/datasets/test_datasets/div2k_valid/gt_mod16/" # 保存图片的文件夹名称
    save_out_lq = "/home/iecy/pycharm/Openbayes/QuanTexSR-main/datasets/test_datasets/div2k_valid/lrx4/"

    for img_name in file_list:
        img_path = file_root + img_name
        img = util.imread_uint(img_path, 3)
        img = util.uint2single(img)
        sf = 4
        print(img_name)
        (img_name_2,extension) =os.path.splitext(img_name)

        img_lq, img_hq = degradation_bsrgan_plus(img, sf=sf, lq_patchsize=32,crop=False)
        if (img_lq.shape[0] % 2 != 0):
            lq_nearest = cv2.resize(util.single2uint(img_lq), (int(img_lq.shape[1]), int(img_lq.shape[0]+1)), interpolation=0)
            hq_nearest = cv2.resize(util.single2uint(img_hq), (int(img_hq.shape[1]), int(img_hq.shape[0]+4)), interpolation=0)
        else:
            lq_nearest = cv2.resize(util.single2uint(img_lq), (int(img_lq.shape[1]), int(img_lq.shape[0])), interpolation=0)
            hq_nearest = cv2.resize(util.single2uint(img_hq), (int(img_hq.shape[1]), int(img_hq.shape[0])), interpolation=0)

        if (img_lq.shape[1] % 2 != 0):
            lq_nearest = cv2.resize(lq_nearest, (int(lq_nearest.shape[1]+1), int(lq_nearest.shape[0])), interpolation=0)
            hq_nearest = cv2.resize(hq_nearest, (int(hq_nearest.shape[1]+4), int(hq_nearest.shape[0])), interpolation=0)
        #lq_nearest = cv2.resize(util.single2uint(img_lq), (int(img_lq.shape[1]), int(img_lq.shape[0])), interpolation=0)
        util.imsave(hq_nearest, save_out_hq+str(img_name_2)+'.png')
        util.imsave(lq_nearest, save_out_lq+str(img_name_2)+'.png')

3.随机裁剪图片

if __name__ == '__main__':
    '''
    批量处理图片
    '''
    file_root = '/home/iecy/pycharm/Openbayes/QuanTexSR-main/datasets/raw_image/'  # 当前文件夹下的所有图片
    file_list = os.listdir(file_root)
    save_out_hq = "/home/iecy/pycharm/Openbayes/QuanTexSR-main/datasets/test_datasets/div2k_valid/gt_mod16/" # 保存图片的文件夹名称
    save_out_lq = "/home/iecy/pycharm/Openbayes/QuanTexSR-main/datasets/test_datasets/div2k_valid/lrx4/"

    for img_name in file_list:
        img_path = file_root + img_name
        img = util.imread_uint(img_path, 3)
        img = util.uint2single(img)
        sf = 4
        print(img_name)
        (img_name_2,extension) =os.path.splitext(img_name)

        for i in range(10):
            img_lq, img_hq = degradation_bsrgan_plus(img, sf=sf, lq_patchsize=128)
            lq_nearest = cv2.resize(util.single2uint(img_lq), (int(img_lq.shape[1]), int(img_lq.shape[0])), interpolation=0)
            #img_concat = np.concatenate([lq_nearest, util.single2uint(img_hq)], axis=1)
            util.imsave(util.single2uint(img_hq), save_out_hq+str(img_name_2)+'_s'+str(i+1).rjust(3,'0')+'.png')
            util.imsave(lq_nearest, save_out_lq+str(img_name_2)+'_s'+str(i+1).rjust(3,'0')+'.png')
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值