-
Notifications
You must be signed in to change notification settings - Fork 131
/
Demo_multivariate_Gaussian_noise.m
193 lines (151 loc) · 5.85 KB
/
Demo_multivariate_Gaussian_noise.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
% This is the testing demo of FFDNet for denoising noisy color images corrupted by
% multivariate (3D) Gaussian noise model N([0,0,0]; Sigma) with zero mean and
% covariance matrix Sigma in the RGB color space.
%
% To run the code, you should install Matconvnet first. Alternatively, you can use the
% function `vl_ffdnet_matlab` to perform denoising without Matconvnet.
%
% "FFDNet: Toward a Fast and Flexible Solution for CNN based Image
% Denoising" 2018/03/23
% If you have any question, please feel free to contact with me.
% Kai Zhang (e-mail: [email protected])
% clear; clc;
format compact;
global sigmas; % input noise level or input noise level map
addpath(fullfile('utilities'));
folderModel = 'models';
folderTest = 'testsets';
folderResult= 'results';
imageSets = {'BSD68','Set12','CBSD68','Kodak24','McMaster'}; % testing datasets
setTestCur = imageSets{3}; % current testing dataset
showResult = 1;
useGPU = 1;
pauseTime = 0;
%% Sigma
%% noise case 1 (general)
% [(sigma_R)^2 & sigma_RG & sigma_RB
% sigma_RG & (sigma_G)^2 & sigma_GB
% sigma_RB & sigma_GB & (sigma_B)^2]
%% noise case 2 (special) channel-independent noise, the widely-used noise model for color image
% [(sigma)^2 & 0 & 0
% 0 & (sigma)^2 & 0
% 0 & 0 & (sigma)^2]
%% noise case 3 (special) channel-independent noise
% [(sigma_R)^2 & 0 & 0
% 0 & (sigma_G)^2 & 0
% 0 & 0 & (sigma_B)^2]
%% noise case 4 (special) each channel has the same noise, the widely-used noise model for grayscale image
% [(sigma)^2 & (sigma)^2 & (sigma)^2
% (sigma)^2 & (sigma)^2 & (sigma)^2
% (sigma)^2 & (sigma)^2 & (sigma)^2]
% image noise Sigma
noisecase = 2;
switch noisecase
case 1
L = 75/255;
D = diag(rand(3,1));
U = orth(rand(3,3));
imageNoiseSigma = abs(L^2*(U' * D * U));
case 2
noiseLevelRGB = 50;
imageNoiseSigma = (noiseLevelRGB/255)^2*eye(3);
case 3
noiseLevelR = 15;
noiseLevelG = 30;
noiseLevelB = 45;
imageNoiseSigma = [(noiseLevelR/255)^2, 0, 0; 0, (noiseLevelG/255)^2, 0; 0, 0, (noiseLevelB/255)^2];
case 4
noiseLevel = 50;
imageNoiseSigma = (noiseLevel/255)^2*ones(3);
end
assert(sum(sum(imageNoiseSigma<0)) == 0,'We assume that all the elements are non-negative.');
% input noise Sigma
inputNoiseSigma = imageNoiseSigma;
sigma = sqrt(inputNoiseSigma);
sigma = sigma(:);
sigma6 = sigma([1,4,5,7,8,9]); % extract 6 parameters
folderResultCur = fullfile(folderResult, [setTestCur,'_',num2str(imageNoiseSigma(1,1)),'_',num2str(inputNoiseSigma(1,1))]);
if ~isdir(folderResultCur)
mkdir(folderResultCur)
end
load(fullfile('models','FFDNet_3D_RGB.mat'));
net = vl_simplenn_tidy(net);
% for i = 1:size(net.layers,2)
% net.layers{i}.precious = 1;
% end
if useGPU
net = vl_simplenn_move(net, 'gpu') ;
end
% read images
ext = {'*.jpg','*.png','*.bmp','*.tif'};
filePaths = [];
for i = 1 : length(ext)
filePaths = cat(1,filePaths, dir(fullfile(folderTest,setTestCur,ext{i})));
end
% PSNR and SSIM
PSNRs = zeros(1,length(filePaths));
SSIMs = zeros(1,length(filePaths));
for i = 1:length(filePaths)
% read images
label = imread(fullfile(folderTest,setTestCur,filePaths(i).name));
[w,h,c] = size(label);
label = im2double(label);
if c == 1
label = cat(3,label,label,label);
end
[~,nameCur,extCur] = fileparts(filePaths(i).name);
% add noise
randn('seed',0);
% noise = bsxfun(@times,randn(size(label)),permute(imageNoiseSigma/255,[3 4 1 2]));
noise = vl_randnm(w, h, imageNoiseSigma);
input = single(label + noise);
if mod(w,2)==1
input = cat(1,input, input(end,:,:)) ;
end
if mod(h,2)==1
input = cat(2,input, input(:,end,:)) ;
end
% tic;
if useGPU
input = gpuArray(input);
end
% set noise level map
sigmas = sigma6; % see "vl_simplenn.m".
% perform denoising
res = vl_simplenn(net,input,[],[],'conserveMemory',true,'mode','test'); % matconvnet default
% res = vl_ffdnet_concise(net, input); % concise version of vl_simplenn for testing FFDNet
%res = vl_ffdnet_matlab(net, input); % use this if you did not install matconvnet; very slow
% output = input -res(end).x; % for 'model_color.mat'
output = res(end).x;
if mod(w,2)==1
output = output(1:end-1,:,:);
input = input(1:end-1,:,:);
end
if mod(h,2)==1
output = output(:,1:end-1,:);
input = input(:,1:end-1,:);
end
if useGPU
output = gather(output);
input = gather(input);
end
%toc;
if c == 1
output= mean(output,3);
input = mean(input,3);
label = mean(label,3);
end
% calculate PSNR, SSIM and save results
[PSNRCur, SSIMCur] = Cal_PSNRSSIM(im2uint8(label),im2uint8(output),0,0);
if showResult
imshow(cat(2,im2uint8(input),im2uint8(label),im2uint8(output)));
title([filePaths(i).name,' ',num2str(PSNRCur,'%2.2f'),'dB',' ',num2str(SSIMCur,'%2.4f')])
%imwrite(im2uint8(output), fullfile(folderResultCur, [nameCur, '_' num2str(imageNoiseSigma(1,1),'%02d'),'_' num2str(inputNoiseSigma(1,1),'%02d'),'_PSNR_',num2str(PSNRCur*100,'%4.0f'), extCur] ));
drawnow;
pause(pauseTime)
end
disp([filePaths(i).name,' ',num2str(PSNRCur,'%2.2f'),'dB',' ',num2str(SSIMCur,'%2.4f')])
PSNRs(i) = PSNRCur;
SSIMs(i) = SSIMCur;
end
disp([mean(PSNRs),mean(SSIMs)]);