超分辨率重建生成低分辨率图像,生成降质图像公认方法代码

目录

1背景

2.BI

3.BD

4.DN

5总结


1背景

超分辨率重建中经典的生成降质图像通常使用MATLAB实现的,通常有四种方法:

类型 说明
BI bicubic-down
BD blur-down
BN bicubic-down+noise
DN blur-down+noise

其中最常用的是BI,其次是BD,最后是DN。那我将其公认实现和出处总结起来,供大家参考。

注:在一些数据集中由于尺寸问题不能够被下采样的倍率所整除,导致生成数据用于训练测试时会出现很多麻烦,因此注意裁剪其大小。详细见下面代码。

注:关于其Python实现,可同时参考链接 图像/视频超分之降质过程和我的总结图像超分辨率数据集这一篇足够了,你需要注意什么?_Alocus_的博客-CSDN博客_图像超分辨率数据集

2.BI

 下面是通过MATLAB公认利用bicubic生成低分辨率(BI)方法,摘自xinntao大大

https://github.com/XPixelGroup/BasicSR/blob/479ec97e8a23e49cc559c081f8ff80eac1bc5989/scripts/matlab_scripts/generate_bicubic_img.mhttps://github.com/XPixelGroup/BasicSR/blob/479ec97e8a23e49cc559c081f8ff80eac1bc5989/scripts/matlab_scripts/generate_bicubic_img.m

function generate_bicubic_img()
%% matlab code to genetate mod images, bicubic-downsampled images and
%% bicubic_upsampled images

%% set configurations
% comment the unnecessary lines
input_folder = '../../datasets/Set5/original';
save_mod_folder = '../../datasets/Set5/GTmod12';
save_lr_folder = '../../datasets/Set5/LRbicx2';
% save_bic_folder = '';

mod_scale = 12;
up_scale = 2;

if exist('save_mod_folder', 'var')
    if exist(save_mod_folder, 'dir')
        disp(['It will cover ', save_mod_folder]);
    else
        mkdir(save_mod_folder);
    end
end
if exist('save_lr_folder', 'var')
    if exist(save_lr_folder, 'dir')
        disp(['It will cover ', save_lr_folder]);
    else
        mkdir(save_lr_folder);
    end
end
if exist('save_bic_folder', 'var')
    if exist(save_bic_folder, 'dir')
        disp(['It will cover ', save_bic_folder]);
    else
        mkdir(save_bic_folder);
    end
end

idx = 0;
filepaths = dir(fullfile(input_folder,'*.*'));
for i = 1 : length(filepaths)
    [paths, img_name, ext] = fileparts(filepaths(i).name);
    if isempty(img_name)
        disp('Ignore . folder.');
    elseif strcmp(img_name, '.')
        disp('Ignore .. folder.');
    else
        idx = idx + 1;
        str_result = sprintf('%d\t%s.\n', idx, img_name);
        fprintf(str_result);

        % read image
        img = imread(fullfile(input_folder, [img_name, ext]));
        img = im2double(img);

        % modcrop
        img = modcrop(img, mod_scale);
        if exist('save_mod_folder', 'var')
            imwrite(img, fullfile(save_mod_folder, [img_name, '.png']));
        end

        % LR
        im_lr = imresize(img, 1/up_scale, 'bicubic');
        if exist('save_lr_folder', 'var')
            imwrite(im_lr, fullfile(save_lr_folder, [img_name, '.png']));
        end

        % Bicubic
        if exist('save_bic_folder', 'var')
            im_bicubic = imresize(im_lr, up_scale, 'bicubic');
            imwrite(im_bicubic, fullfile(save_bic_folder, [img_name, '.png']));
        end
    end
end
end

%% modcrop
function img = modcrop(img, modulo)
if size(img,3) == 1
    sz = size(img);
    sz = sz - mod(sz, modulo);
    img = img(1:sz(1), 1:sz(2));
else
    tmpsz = size(img);
    sz = tmpsz(1:2);
    sz = sz - mod(sz, modulo);
    img = img(1:sz(1), 1:sz(2),:);
end
end

3.BD

 下面是通过MATLAB使用公认的模糊(BD)方法生成低分辨率代码。摘自RDN

https://github.com/yulunzhang/RDN/blob/master/RDN_TrainCode/Prepare_TrainData/Prepare_TrainData_HR_LR_BD.mhttps://github.com/yulunzhang/RDN/blob/master/RDN_TrainCode/Prepare_TrainData/Prepare_TrainData_HR_LR_BD.m

function Prepare_TrainData_HR_LR_BD()
%% settings
path_save = './DIV2K';
path_src = './DIV2K/DIV2K_HR';
ext               =  {'*.jpg','*.png','*.bmp'};
filepaths           =  [];
for i = 1 : length(ext)
    filepaths = cat(1,filepaths, dir(fullfile(path_src, ext{i})));
end
nb_im = length(filepaths);
DIV2K_HR = [];

for idx_im = 1:nb_im
    fprintf('Read HR :%d\n', idx_im);
    ImHR = imread(fullfile(path_src, filepaths(idx_im).name));
    DIV2K_HR{idx_im} = ImHR;
end
%% generate and save LR via imresize() with Bicubic
kernelsize = 7;
sigma = 1.6;
for IdxIm = 1:nb_im
    fprintf('IdxIm=%d\n', IdxIm);
    ImHR = DIV2K_HR{IdxIm};
    ImLRx3 = imresize_BD(ImHR, 3, kernelsize, sigma);
    % name image
    digit = IdxIm;
    fileName = num2str(IdxIm);
    while digit < 1000
        fileName = ['0', fileName];
        digit = digit*10;
    end

    FolderLRx3 = fullfile(path_save, 'DIV2K_LR_bicubic', 'X3');
    
    if ~exist(FolderLRx3)
        mkdir(FolderLRx3)
    end

    NameLRx3 = fullfile(FolderLRx3, [fileName, 'x3BD.png']);
    % save image
    imwrite(ImLRx3, NameLRx3, 'png');
end


end

function ImLR = imresize_BD(ImHR, scale, kernelsize, sigma)
% ImLR and ImHR are uint8 data
% downsample by Bicubic
kernel  = fspecial('gaussian',kernelsize,sigma);
blur_HR = imfilter(ImHR,kernel,'replicate');
ImLR = imresize(blur_HR, 1/scale, 'nearest');

end

4.DN

下面是DN的 MATLAB实现

function Prepare_TrainData_HR_LR_DN()
%% settings
path_save = './DIV2K';
path_src = './DIV2K/DIV2K_HR';
ext               =  {'*.jpg','*.png','*.bmp'};
filepaths           =  [];
for i = 1 : length(ext)
    filepaths = cat(1,filepaths, dir(fullfile(path_src, ext{i})));
end
nb_im = length(filepaths);
DIV2K_HR = [];

for idx_im = 1:nb_im
    fprintf('Read HR :%d\n', idx_im);
    ImHR = imread(fullfile(path_src, filepaths(idx_im).name));
    DIV2K_HR{idx_im} = ImHR;
end
%% generate and save LR via imresize() with Bicubic
sigma = 30; % noise level
for IdxIm = 1:nb_im
    fprintf('IdxIm=%d\n', IdxIm);
    ImHR = DIV2K_HR{IdxIm};
    ImLRx3 = imresize_DN(ImHR, 3, sigma);
    % name image
    digit = IdxIm;
    fileName = num2str(IdxIm);
    while digit < 1000
        fileName = ['0', fileName];
        digit = digit*10;
    end

    FolderLRx3 = fullfile(path_save, 'DIV2K_LR_bicubic', 'X3');
    
    if ~exist(FolderLRx3)
        mkdir(FolderLRx3)
    end

    NameLRx3 = fullfile(FolderLRx3, [fileName, 'x3DN.png']);
    % save image
    imwrite(ImLRx3, NameLRx3, 'png');
end


end

function ImLR = imresize_DN(ImHR, scale, sigma)
% ImLR and ImHR are uint8 data
% downsample by Bicubic
ImDown = imresize(ImHR, 1/scale, 'bicubic'); % 0-255
ImDown = single(ImDown); % 0-255
ImDownNoise = ImDown + single(sigma*randn(size(ImDown))); % 0-255
ImLR = uint8(ImDownNoise); % 0-255
end

5总结

关于Python实现,以及详细信息,我还是想建议大家参考这边文章,总结的很不错,感谢稳扎那股作者以及参考文献中的作者们的贡献。!

图像/视频超分之降质过程

你可能感兴趣的:(#,超分辨率重建,计算机视觉,python,超分辨率重建,计算机视觉,人工智能)