forked from cszn/IRCNN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDemo_deblur.m
154 lines (119 loc) · 4.77 KB
/
Demo_deblur.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
% (non-blind) image deblurring
% @inproceedings{zhang2017learning,
% title={Learning Deep CNN Denoiser Prior for Image Restoration},
% author={Zhang, Kai and Zuo, Wangmeng and Gu, Shuhang and Zhang, Lei},
% booktitle={IEEE Conference on Computer Vision and Pattern Recognition},
% year={2017}
% }
% If you have any question, please feel free to contact with me.
% Kai Zhang (e-mail: [email protected])
clear; clc;
addpath('utilities');
imageSets = {'Set3G','Set3C'}; %%% testing dataset
setTest = imageSets([1]); %%% select the dataset
showResult = 1;
pauseTime = 1;
useGPU = 1;
folderTest = 'testsets';
folderResult = 'results';
folderModel = 'models';
taskTestCur = 'Deblur';
if ~exist(folderResult,'file')
mkdir(folderResult);
end
load(fullfile('kernels','Levin09.mat'));
kernelType = 1; % 1~8
if kernelType > 8
k = fspecial('gaussian', 25, 1.6);
else
k = kernels{kernelType};
end
sigmas = [2, 2.55, 7.65]/255;
sigma = sigmas(3);
totalIter = 30; % default
lamda = (sigma^2)/3; % default 3, ****** from {1 2 3 4} ******
modelSigma1 = 49; % default
modelSigma2 = 13; % ****** from {1 3 5 7 9 11 13 15} ******
modelSigmaS = logspace(log10(modelSigma1),log10(modelSigma2),totalIter);
rho = sigma^2/((modelSigma1/255)^2);
ns = min(25,max(ceil(modelSigmaS/2),1));
ns = [ns(1)-1,ns];
for n_set = 1 : numel(setTest)
%%% read images
setTestCur = cell2mat(setTest(n_set));
disp('--------------------------------------------');
disp(['----',setTestCur,'-----Image Debluring-----']);
disp('--------------------------------------------');
folderTestCur = fullfile(folderTest,setTestCur);
ext = {'*.jpg','*.png','*.bmp'};
filepaths = [];
for i = 1 : length(ext)
filepaths = cat(1,filepaths,dir(fullfile(folderTestCur, ext{i})));
end
eval(['PSNR_',taskTestCur,'_',setTestCur,' = zeros(length(filepaths),1);']);
%%% folder to store results
folderResultCur = fullfile(folderResult, ['Deblur_',setTestCur,'_kernel_',num2str(kernelType)]);
if ~exist(folderResultCur,'file')
mkdir(folderResultCur);
end
for i = 1 : length(filepaths)
x = imread(fullfile(folderTestCur,filepaths(i).name));
[~,imageName,ext] = fileparts(filepaths(i).name);
randn('seed',0);
y = imfilter(im2double(x), k, 'circular', 'conv') + sigma*randn(size(x));
[w,h,c] = size(y);
V = psf2otf(k,[w,h]);
denominator = abs(V).^2;
if c>1
denominator = repmat(denominator,[1,1,c]);
V = repmat(V,[1,1,c]);
end
upperleft = conj(V).*fft2(y);
if c==1
load(fullfile(folderModel,'modelgray.mat'));
elseif c==3
load(fullfile(folderModel,'modelcolor.mat'));
end
z = single(y);
if useGPU
z = gpuArray(z);
upperleft = gpuArray(upperleft);
denominator = gpuArray(denominator);
end
tic;
for itern = 1:totalIter
%%% step 1
rho = lamda*255^2/(modelSigmaS(itern)^2);
z = real(ifft2((upperleft + rho*fft2(z))./(denominator + rho)));
if ns(itern+1)~=ns(itern)
[net] = loadmodel(modelSigmaS(itern),CNNdenoiser);
net = vl_simplenn_tidy(net);
if useGPU
net = vl_simplenn_move(net, 'gpu');
end
end
%%% step 2
res = vl_simplenn(net, z,[],[],'conserveMemory',true,'mode','test');
residual = res(end).x;
z = z - residual;
end
if useGPU
output = im2uint8(gather(z));
end
toc;
[PSNR_Cur,SSIM_Cur] = Cal_PSNRSSIM(x,output,0,0); %%% single
disp(['Image Deblurring ',num2str(PSNR_Cur,'%2.2f'),'dB',' ',filepaths(i).name]);
eval(['PSNR_',taskTestCur,'_',setTestCur,'(',num2str(i),') = PSNR_Cur;']);
if showResult
imshow(cat(2,im2uint8(y),output,x));
drawnow;
title(['Image Deblurring ',filepaths(i).name,' ',num2str(PSNR_Cur,'%2.2f'),'dB'],'FontSize',12)
pause(pauseTime)
%pause()
imwrite(output,fullfile(folderResultCur,[imageName,'.png']));
end
end
disp(['Average PSNR is ',num2str(mean(eval(['PSNR_',taskTestCur,'_',setTestCur])),'%2.2f'),'dB']);
%%% save PSNR
save(fullfile(folderResultCur,['PSNR_',taskTestCur,'_',setTestCur,'.mat']),['PSNR_',taskTestCur,'_',setTestCur])
end