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

目录

1背景

2.BI

3.BD

4.DN

5总结


1背景

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

类型说明
BIbicubic-down
BDblur-down
BNbicubic-down+noise
DNblur-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实现,以及详细信息,我还是想建议大家参考这边文章,总结的很不错,感谢稳扎那股作者以及参考文献中的作者们的贡献。!

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

  • 6
    点赞
  • 62
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
以下是一个使用PyTorch实现的基于深度学习的图像超分辨率重建代码示例: ```python import torch import torch.nn as nn # 定义超分辨率重建模型 class SuperResolutionModel(nn.Module): def __init__(self, upscale_factor): super(SuperResolutionModel, self).__init__() self.upscale_factor = upscale_factor self.conv1 = nn.Conv2d(3, 64, kernel_size=5, padding=2) self.conv2 = nn.Conv2d(64, 64, kernel_size=3, padding=1) self.conv3 = nn.Conv2d(64, 32, kernel_size=3, padding=1) self.conv4 = nn.Conv2d(32, 3 * (upscale_factor ** 2), kernel_size=3, padding=1) self.pixel_shuffle = nn.PixelShuffle(upscale_factor) def forward(self, x): x = nn.functional.relu(self.conv1(x)) x = nn.functional.relu(self.conv2(x)) x = nn.functional.relu(self.conv3(x)) x = self.pixel_shuffle(self.conv4(x)) return x # 加载训练数据 train_data = torch.utils.data.DataLoader(train_dataset, batch_size=16, shuffle=True) # 定义超分辨率重建模型和优化器 model = SuperResolutionModel(upscale_factor=2) optimizer = torch.optim.Adam(model.parameters(), lr=0.001) # 训练模型 for epoch in range(100): for batch, (low_res, high_res) in enumerate(train_data): optimizer.zero_grad() output = model(low_res) loss = nn.functional.mse_loss(output, high_res) loss.backward() optimizer.step() print("Epoch [{}/{}], Batch [{}/{}], Loss: {:.4f}".format(epoch+1, 100, batch+1, len(train_data), loss.item())) # 使用测试数据测试模型 low_res_image = load_image("low_res_image.jpg") model.eval() with torch.no_grad(): high_res_image = model(low_res_image) save_image(high_res_image, "high_res_image.jpg") ``` 以上代码实现了一个基于深度学习的图像超分辨率重建模型,并使用训练数据对模型进行训练。在测试阶段,可以使用模型对低分辨率图像进行重建,得到高分辨率图像

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alocus_

如果我的内容帮助到你,打赏我吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值