版权声明: https://blog.csdn.net/gwplovekimi/article/details/85121100
function Test_Realistic_Noise_Model()
%%% Test Code for realistic noise model
addpath('./utils'); %%%%%%%%%%%%%this is the function
%% load CRF parameters, the pipeline of the camera, is should be turn off
load('201_CRF_data.mat');
load('dorfCurvesInv.mat');
I_gl = I;
B_gl = B;
I_inv_gl = invI;
B_inv_gl = invB;
mod_scale = 1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% set parameters
% comment the unnecessary line
input_folder = '/home/guanwp/BasicSR_datasets/val_set5/Set5';
%%%%%%%%%%%%%%%%%%%%% save_mod_folder = '';
save_LR_folder = '/home/guanwp/BasicSR_datasets/val_set5/Set5_noiseCRF';
%%%%%%%%%%%%%5% save_bic_folder = '';
save_residual_folder='/home/guanwp/BasicSR_datasets/val_set5/Set5_rCRF'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%save_noiselevelmap_folder='/home/guanwp/BasicSR_datasets/val_set5/Set5_l';%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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
if exist('save_residual_folder', 'var')
if exist(save_residual_folder, 'dir')
disp(['It will cover ', save_residual_folder]);
else
mkdir(save_residual_folder);
end
end
%if exist('save_noiselevelmap_folder', 'var')
%if exist(save_noiselevelmap_folder, 'dir')
%disp(['It will cover ', save_noiselevelmap_folder]);
%else
%mkdir(save_noiselevelmap_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%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%add noise
%%%%%%%%%%random
channel = size(img,3);
%sigma_s = 0.16*rand(1,channel,'single'); % original 0.16.signal-dependent noise
%sigma_c =0.06*rand(1,channel,'single'); % original 0.06.stationary noise
sigma_s=[0.18,0.18,0.18];
sigma_c=[0.03,0.03,0.03];
%%%used the default value
CRF_index = 5; % 1~201
pattern = 5; % 1: 'gbrg', 2: 'grbg', 3: 'bggr', 4: 'rggb', 5: no mosaic
[im_LR,noise_level_map] = AddNoiseMosai(img,I_gl,B_gl,I_inv_gl,B_inv_gl, sigma_s,sigma_c, CRF_index, pattern);
%%%%%%%%im_LR = AddNoise(img);%%%%%%%%%%%%%%jingwen
im_residual=im_LR-img;
%im_residual=rgb2gray(im_residual);
%im_residual=mat2gray(im_residual);
%imagesc(im_residual);
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
if exist('save_residual_folder', 'var')
imwrite(im_residual, fullfile(save_residual_folder, [imname, '_bicLRx4.png']));
end
%if exist('save_noiselevelmap_folder', 'var')
%imwrite(noise_level_map, fullfile(save_noiselevelmap_folder, [imname, '_bicLRx4.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
%//////////////////////////////////////////////////////////////////////////
% AddNoiseMosai
%function: add realistic noise
% If this Code is helpful to you, please Cite: https://arxiv.org/abs/1807.04686
%%% realistic noise model:
%%% I = M^{-1}(M(f(L + n_s + n_c) + n_q))
% n_s: shot noise, depend on L, E[n_s]= 0, var[n_s] = L*sigma_s
% in [1], n_s is a Possion Shot
% in [2], n_s is GMM, sigma_s: 0~0.16
% we choose [2] here
% n_c: other type of noise, i.e. read noise, dark current
% in [2], E[n_c] = 0, var[n_c] = sigma_c, sigma_c: 0.01~0.06
% n_q: ignore in [2]
%//////////////////////////////////////////////////////////////////////////
%inpus:
%-------x: image data, double or single datatype, [0,1]
%-------I, B: CRF parameters provided by [3]
%-------Iinv, Binv: inverse CRF parameters, created by "inverseCRF" for
% faster computation
%**(optional): if not defined by user,the following parameters are set
% ramdomly
%-------sigma_s: signal-dependent noise level, [0, 0.16]
%-------sigma_c: sigma_independent noise level, [0, 0.06]
%-------crf_index: CRF index, [1, 201]
%-------pattern: 1: 'gbrg', 2: 'grbg', 3: 'bggr', 4: 'rggb', 5: no mosaic
%
%output:
%-------y: noisy image
%//////////////////////////////////////////////////////////////////////////
% reference:
% [1] G.E. Healey and R. Kondepudy, Radiometric CCD Camera Calibration and Noise Estimation,
% IEEE Trans. Pattern Analysis and Machine Intelligence
% [2] Liu, Ce et al. Automatic Estimation and Removal of Noise from a Single Image.
% IEEE Transactions on Pattern Analysis and Machine Intelligence 30 (2008): 299-314.
% [3] Grossberg, M.D., Nayar, S.K.: Modeling the space of camera response functions.
% IEEE Transactions on Pattern Analysis and Machine Intelligence 26 (2004)
%
function [y,noise_level_map] = AddNoiseMosai(x,I,B,Iinv,Binv,sigma_s,sigma_c,crf_index, pattern)
% default value
channel = size(x,3);
rng('default')
if nargin < 6
rng('shuffle');
sigma_s = 0.16*rand(1,channel,'single'); % original 0.16
rng('shuffle');
sigma_c = 0.06*rand(1,channel,'single'); % original 0.06
rng('shuffle');
rand_index = randperm(201);
crf_index = rand_index(1);
rng('shuffle');
pattern = randperm(5);
end
temp_x = x;
%%% x -> L
temp_x = ICRF_Map(temp_x,Iinv(crf_index,:),Binv(crf_index,:));
L=temp_x;
noise_s_map = bsxfun(@times,permute(sigma_s,[3 1 2]),temp_x);
noise_s = randn(size(temp_x),'single').* noise_s_map;
temp_x = temp_x + noise_s;
noise_c_map = repmat(permute(sigma_c,[3 1 2]),[size(x,1),size(x,2)]);
noise_c = noise_c_map .* randn(size(temp_x),'single');
temp_x = temp_x + noise_c;
%%%%%%%%%%%%%noise level map
%%%%%%%%%%%the stationary noise
%%%%%%%%%%%%%%%%%%noiseSigma_c=sigma_c*255;
%%%%%%%%%%%%%%%%%%%%%%[~,~,noiseSigma_c] = peaks(size(temp_x,1));
%%%%%%%%%%%%%%%%%%%%%%%%%%noiseSigma_c = 0 + (255 - 0).*(noiseSigma_c - min(noiseSigma_c(:)))./(max(noiseSigma_c(:)) - min(noiseSigma_c(:)));
%%%%%%%%%%%%%%%%%%%%%%%noiseSigma_c = flipud(noiseSigma_c);
%%%%%%%%%%%%%%%%%%%%%%noise_level_map_c =imresize(noiseSigma_c,1,'bicubic')/255;
%%%%%%%%%%%%%%%%%%%%the signal-dependent noise
%%%%%%%%%%%%%%%%%%%noiseSigma_s=sigma_s*255;
%%%%%%%%%%%%%%%%%[~,~,noiseSigma_s] = peaks(size(temp_x,1));
%%%%%%%%%%%%%%%%%%noiseSigma_s = 0 + (255 - 0).*(noiseSigma_s - min(noiseSigma_s(:)))./(max(noiseSigma_s(:)) - min(noiseSigma_s(:)));
%%%%%%%%%%%%%%%%%%%%%%noiseSigma_s = flipud(noiseSigma_s);
%%%%%%%%%%%%%%%%%%%%%%%%%%noise_level_map_s =imresize(noiseSigma_s,1,'bicubic')/255;
%%%% three channel
%noise_level_map_c3(:,:,1)=noise_level_map_c;
%noise_level_map_c3(:,:,2)=noise_level_map_c;
%noise_level_map_c3(:,:,3)=noise_level_map_c;
%noise_level_map_s3(:,:,1)=noise_level_map_s;
%noise_level_map_s3(:,:,2)=noise_level_map_s;
%noise_level_map_s3(:,:,3)=noise_level_map_s;
%disp(size(noise_level_map_s3));
%disp(size(noise_level_map_c3));
%disp(size(L));
%%%%%%%%%%%%%%%%%%%%%%noise_level_map=noise_level_map_s+noise_level_map_c;
%%%%%%%%%%%%%%%noise_level_map=noise_level_map_c;%%%%%%%%%%%%%%only the stationary noise
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
noise_level_map=noise_s_map.*L+noise_c_map;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% add Mosai
if pattern(1) == 1
[B_b, ~, ~] = mosaic_bayer(temp_x, 'gbrg', 0);
elseif pattern(1) == 2
[B_b, ~, ~] = mosaic_bayer(temp_x, 'grbg', 0);
elseif pattern(1) == 3
[B_b, ~, ~] = mosaic_bayer(temp_x, 'bggr', 0);
elseif pattern(1) == 4
[B_b, ~, ~] = mosaic_bayer(temp_x, 'rggb', 0);
else
B_b = temp_x;
end
temp_x = single(B_b);
%%% DeMosai
if pattern(1) == 1
Ba = uint16(temp_x*(2^16));
lin_rgb = double(demosaic(Ba,'gbrg'))/2^16;
elseif pattern(1) == 2
Ba = uint16(temp_x*(2^16));
lin_rgb = double(demosaic(Ba,'grbg'))/2^16;
elseif pattern(1) == 3
Ba = uint16(temp_x*(2^16));
lin_rgb = double(demosaic(Ba,'bggr'))/2^16;
elseif pattern(1) == 4
Ba = uint16(temp_x*(2^16));
lin_rgb = double(demosaic(Ba,'rggb'))/2^16;
else
lin_rgb = temp_x;
end
temp_x = single(lin_rgb);
%%% L -> x
temp_x = CRF_Map(temp_x,I(crf_index,:),B(crf_index,:));
y = temp_x;
end
%//////////////////////////////////////////////////////////////////////////
% AddNoiseMosai
%function: add realistic noise
% If this Code is helpful to you, please Cite: https://arxiv.org/abs/1807.04686
%%% realistic noise model:
%%% I = M^{-1}(M(f(L + n_s + n_c) + n_q))
% n_s: shot noise, depend on L, E[n_s]= 0, var[n_s] = L*sigma_s
% in [1], n_s is a Possion Shot
% in [2], n_s is GMM, sigma_s: 0~0.16
% we choose [2] here
% n_c: other type of noise, i.e. read noise, dark current
% in [2], E[n_c] = 0, var[n_c] = sigma_c, sigma_c: 0.01~0.06
% n_q: ignore in [2]
%//////////////////////////////////////////////////////////////////////////
%inpus:
%-------x: image data, double or single datatype, [0,1]
%-------I, B: CRF parameters provided by [3]
%-------Iinv, Binv: inverse CRF parameters, created by "inverseCRF" for
% faster computation
%**(optional): if not defined by user,the following parameters are set
% ramdomly
%-------sigma_s: signal-dependent noise level, [0, 0.16]
%-------sigma_c: sigma_independent noise level, [0, 0.06]
%-------crf_index: CRF index, [1, 201]
%-------pattern: 1: 'gbrg', 2: 'grbg', 3: 'bggr', 4: 'rggb', 5: no mosaic
%
%output:
%-------y: noisy image
%//////////////////////////////////////////////////////////////////////////
% reference:
% [1] G.E. Healey and R. Kondepudy, Radiometric CCD Camera Calibration and Noise Estimation,
% IEEE Trans. Pattern Analysis and Machine Intelligence
% [2] Liu, Ce et al. Automatic Estimation and Removal of Noise from a Single Image.
% IEEE Transactions on Pattern Analysis and Machine Intelligence 30 (2008): 299-314.
% [3] Grossberg, M.D., Nayar, S.K.: Modeling the space of camera response functions.
% IEEE Transactions on Pattern Analysis and Machine Intelligence 26 (2004)
%
% function [y] = AddNoise(x,I,B,Iinv,Binv,sigma_s,sigma_c,crf_index, pattern)
function [y] = AddNoise(x,sigma_s,sigma_c)
% default value
channel = size(x,3);
rng('default');
% randn('seed', 0);
if nargin < 2
rng('shuffle');
%sigma_s = 0.16*rand(1,channel,'single'); % original 0.16
sigma_s=[0.08,0.08,0.08];
rng('shuffle');
%sigma_c = 0.06*rand(1,channel,'single'); % original 0.06
sigma_c=[0.03,0.03,0.03];
% rng('shuffle');
% the same noise sigma for all channels
% noiseSigma = 15 + (75-15)*rand(1,channel,'single');
% noiseSigma = 80*rand(1,channel,'single');
% rng('shuffle');
% rand_index = randperm(201);
% crf_index = rand_index(1);
% rng('shuffle');
% pattern = randperm(5);
end
temp_x = x;
%%% x -> L
temp_x = ICRF_Map(temp_x,Iinv(crf_index,:),Binv(crf_index,:));
% noise_map = repmat(permute(noiseSigma/255,[3 1 2]),[size(x,1),size(x,2)]);
% noise_sigma = noise_map.*randn(size(temp_x), 'single');
% temp_x = temp_x + noise_sigma;
noise_s_map = bsxfun(@times,permute(sigma_s,[3 1 2]),temp_x);
noise_s = randn(size(temp_x),'single').* noise_s_map;
temp_x = temp_x + noise_s;
noise_c_map = repmat(permute(sigma_c,[3 1 2]),[size(x,1),size(x,2)]);
noise_c = noise_c_map .* randn(size(temp_x),'single');
temp_x = temp_x + noise_c;
%%% add Mosai
% if pattern(1) == 1
% [B_b, ~, ~] = mosaic_bayer(temp_x, 'gbrg', 0);
% elseif pattern(1) == 2
% [B_b, ~, ~] = mosaic_bayer(temp_x, 'grbg', 0);
% elseif pattern(1) == 3
% [B_b, ~, ~] = mosaic_bayer(temp_x, 'bggr', 0);
% elseif pattern(1) == 4
% [B_b, ~, ~] = mosaic_bayer(temp_x, 'rggb', 0);
% else
% B_b = temp_x;
% end
% temp_x = single(B_b);
% %%% DeMosai
% if pattern(1) == 1
% Ba = uint16(temp_x*(2^16));
% lin_rgb = double(demosaic(Ba,'gbrg'))/2^16;
% elseif pattern(1) == 2
% Ba = uint16(temp_x*(2^16));
% lin_rgb = double(demosaic(Ba,'grbg'))/2^16;
% elseif pattern(1) == 3
% Ba = uint16(temp_x*(2^16));
% lin_rgb = double(demosaic(Ba,'bggr'))/2^16;
% elseif pattern(1) == 4
% Ba = uint16(temp_x*(2^16));
% lin_rgb = double(demosaic(Ba,'rggb'))/2^16;
% else
% lin_rgb = temp_x;
% end
% temp_x = single(lin_rgb);
temp_x = single(temp_x);
%%% L -> x
% temp_x = CRF_Map(temp_x,I(crf_index,:),B(crf_index,:));
y = temp_x;
end