SISR中低分辨率影像LR(bicubic)生成我之见
LR的生成
通常情况,进行影像超分辨率重建,数据集的形式都是:高分、低分数据集对,低分辨率影像在自然的情况下,很少能够直接满足数据集的要求,因此进行人工剪切、通过一定的数学模型与算法构造出低分影像。以满足研究的需要。
bicubic(双三次插值法)
- 简而言之,双三次插值是一种相对简单的经典数学插值方法,能够将少数的像素进行扩充(也能够根据压缩的倍数,进行缩减)构造出相应的实验需求。(详细的方法模型公式,网络上有很详细的解释)。
bicubic(双三次插值法)具体的代码实现
- 网络上的很多博客都有利用python编写的bicubic方法(tensorflow、pytorch项目)获取LR影影像,但是通过运行相应的代码发现:生成的影像视觉上颗粒感很强,又或者是缺失严重。
- 通过寻找,发现ESRGAN官方代码在问题栏目上有问到:是否能够使用py文件生成LR,作者的回答是应该没有问题,但是通常情况下,都是是使用官方代码中:matlab文件生成LR图片(建议按照该方法)。
- 如果觉着matlab安装比较麻烦,尝试使用py文件生成,可以尝试下载这个项目的链接,按照要求,需要安装pytorch。
matlab 的bicubic代码
- 代码出处
function generate_mod_LR_bic()
%% matlab code to genetate mod images, bicubic-downsampled LR, bicubic_upsampled images.
%% set parameters
% comment the unnecessary line
input_folder = '/mnt/SSD/xtwang/BasicSR_datasets/DIV2K800/DIV2K800_sub';
% save_mod_folder = '';
save_LR_folder = '/mnt/SSD/xtwang/BasicSR_datasets/DIV2K800/DIV2K800_sub_bicLRx4';
% save_bic_folder = '';
up_scale = 4;
mod_scale = 4;
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,imname,ext] = fileparts(filepaths(i).name);
if isempty(imname)
disp('Ignore . folder.');
elseif strcmp(imname, '.')
disp('Ignore .. folder.');
else
idx = idx + 1;
str_rlt = sprintf('%d\t%s.\n', idx, imname);
fprintf(str_rlt);
% read image
img = imread(fullfile(input_folder, [imname, ext]));
img = im2double(img);
% modcrop
img = modcrop(img, mod_scale);
if exist('save_mod_folder', 'var')
imwrite(img, fullfile(save_mod_folder, [imname, '.png']));
end
% LR
im_LR = imresize(img, 1/up_scale, 'bicubic');
if exist('save_LR_folder', 'var')
imwrite(im_LR, fullfile(save_LR_folder, [imname, '_bicLRx4.png']));
end
% Bicubic
if exist('save_bic_folder', 'var')
im_B = imresize(im_LR, up_scale, 'bicubic');
imwrite(im_B, fullfile(save_bic_folder, [imname, '_bicx4.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
*注意:代码中容易出错之处:文件的路径,对于图片的保存路径:生成的ouput-dir,事先不用建立文件夹,代码中包含makdir命令,如果事先建立生成bicubic图片文件夹,会一直报错。
如图:
- matlab 编译器代码(局部)
- 注意:添加文件夹
在matlab之下:
导入文件夹之后(如下),选中LR,SR_png文件夹,右击鼠标,弹出‘添加到路径–>添加文件以及子文件到路径’(想截图,但无法进行截图操作,图略)。
运行成功,生成bicubic图片.
HR图片
LR_bicubic