Exemple #1
0
def back_projection(y_sr, y_lr, down_kernel, up_kernel, sf=None):
    """Projects the error between the downscaled SR image and the LR image"""
    y_sr += imresize(y_lr - imresize(y_sr,
                                     scale_factor=1.0 / sf,
                                     output_shape=y_lr.shape,
                                     kernel=down_kernel),
                     scale_factor=sf,
                     output_shape=y_sr.shape,
                     kernel=up_kernel)
    return np.clip(y_sr, 0, 1)
Exemple #2
0
def process_for_lr():
    r""" The low resolution data set is preliminarily processed.
    """
    for filename in tqdm(lr_files, desc="Generating images from lr dir"):
        img = Image.open(filename)
        img = pil2tensor(img)

        # Remove noise
        img = utils.imresize(img, 1.0 / args.cleanup_factor, True)
        _, w, h = img.size()
        w = w - w % args.upscale_factor
        h = h - h % args.upscale_factor
        img = img[:, :w, :h]

        # Save high resolution img
        img = tensor2pil(img)
        img.save(os.path.join(hr_dir, os.path.basename(filename)), "bmp")

        # The noise distribution obtained in kernelGAN is used to adjust the image.
        kernel_path = kernel_paths[np.random.randint(0, len(kernel_paths))]
        mat = loadmat(kernel_path)
        k = np.array([mat["Kernel"]]).squeeze()
        img = imresize(np.array(img),
                       scale_factor=1.0 / args.upscale_factor,
                       kernel=k)

        # Save low resolution img
        img = tensor2pil(img)
        img.save(os.path.join(lr_dir, os.path.basename(filename)), "bmp")
Exemple #3
0
 def create_prob_maps(self, scale_factor):
     # Create loss maps for input image and downscaled one
     loss_map_big = create_gradient_map(self.input_image)
     loss_map_sml = create_gradient_map(imresize(im=self.input_image, scale_factor=scale_factor, kernel='cubic'))
     # Create corresponding probability maps
     prob_map_big = create_probability_map(loss_map_big, self.d_input_shape)
     prob_map_sml = create_probability_map(nn_interpolation(loss_map_sml, int(1 / scale_factor)), self.g_input_shape)
     return prob_map_big, prob_map_sml
Exemple #4
0
def back_project_image(lr,
                       sf=2,
                       output_shape=None,
                       down_kernel='cubic',
                       up_kernel='cubic',
                       bp_iters=8):
    """Runs 'bp_iters' iteration of back projection SR technique"""
    tmp_sr = imresize(lr,
                      scale_factor=sf,
                      output_shape=output_shape,
                      kernel=up_kernel)
    for _ in range(bp_iters):
        tmp_sr = back_projection(y_sr=tmp_sr,
                                 y_lr=lr,
                                 down_kernel=down_kernel,
                                 up_kernel=up_kernel,
                                 sf=sf)
    return tmp_sr
Exemple #5
0
        _, w, h = resize2_img.size()
        w = w - w % opt.upscale_factor
        h = h - h % opt.upscale_factor
        resize2_cut_img = resize2_img[:, :w, :h]

        try:
            file_name = os.path.basename(file)
            file_id = file_name.split('.')[0]
            kernel_path = os.path.join(
                opt.kernel_path, file_id + '/' + file_id + '_kernel_x4.mat')
            mat = loadmat(kernel_path)

            path = os.path.join(tdsr_hr_dir, os.path.basename(file))
            if not os.path.exists(path):
                print(f'create_HrLr kernel_path:{kernel_path}')
                HR_img = TF.to_pil_image(input_img)
                HR_img.save(path, 'PNG')

                k = np.array([mat['Kernel']]).squeeze()
                resize_img = imresize(np.array(HR_img),
                                      scale_factor=1.0 / opt.upscale_factor,
                                      kernel=k)

                path = os.path.join(tdsr_lr_dir, os.path.basename(file))
                TF.to_pil_image(resize_img).save(path, 'PNG')
            else:
                print(f'skip kernel_path:{kernel_path}')

        except Exception as e:
            print(e)