-
Notifications
You must be signed in to change notification settings - Fork 172
/
Demo_inpaint_real_application.m
152 lines (106 loc) · 4.04 KB
/
Demo_inpaint_real_application.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
%==========================================================================
% This is the testing code of IRCNN for image inpainting.
%
% @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 <Kai Zhang ([email protected])>.
%
%
% by Kai Zhang (1/2018)
%==========================================================================
clear; clc;
addpath('utilities');
imageSets = {'Inpaint_set2'}; % testing dataset
setTest = imageSets(1); % select the dataset
useGPU = 1;
folderTest = 'testsets';
folderResult = 'results';
folderModel = 'models';
if ~exist(folderResult,'file')
mkdir(folderResult);
end
setTestCur = cell2mat(setTest(1));
disp('--------------------------------------------');
disp(['----',setTestCur,'-----Image Inpainting-----']);
disp('--------------------------------------------');
folderTestCur = fullfile(folderTest,setTestCur);
% folder to store results
folderResultCur = fullfile(folderResult, ['Inpaint_',setTestCur]);
if ~exist(folderResultCur,'file')
mkdir(folderResultCur);
end
%% read original image 'Iori' and its mask
Iname = 'new'; % Isigma = 0.5/255; Msigma = 5; window = 10; %Images from http://www.visinf.tu-darmstadt.de/vi_research/code/foe.en.jsp
Iname = '3ch'; % Isigma = 0.5/255; Msigma = 5; window = 30; %
% [1] S. Roth and M. J. Black, ¡°Fields of experts: A framework for learning image priors,¡± CVPR, vol. 2, San Diego, California, Jun. 2005, pp. 860¨C867.
% window, important!
window = 10; % default. For '3ch.png', window = 30;
if strcmp(Iname,'3ch') == 1
window = 30;
end
Iori = im2single(imread(fullfile(folderTestCur,[Iname,'.png'])));
[a,b,c] = size(Iori);
% load mask
mask = logical(imread(fullfile(folderTestCur,[Iname,'_mask.png'])));
mask = 1- mask;
% rand('seed',0);
% mask = rand(a,b)>=0.8;
mask = repmat(mask,[1,1,c]);
% generate input
y = Iori.*mask;
%% parameter setting in HQS (tune the following parameters to obtain the best results)
% -------------------important!------------------
% Parameter settings of IRCNN
% (1) image noise level: Isigma
Isigma = 0.5/255; % ****** from interval [1/255, 20/255] ******; e.g., 1/255, 2.55/255, 7/255, 11/255
% (2) noise level of the last denoiser: Msigma
Msigma = 5; % ****** from {1 3 5 7 9 11 13 15} ******
%--------------------------------------------------------
%% load denoisers
if c==1
load(fullfile(folderModel,'modelgray.mat'));
elseif c==3
load(fullfile(folderModel,'modelcolor.mat'));
end
%% default parameter setting in HQS
totalIter = 30; % default 30
lamda = (Isigma^2)/3; % default 3, ****** from {1 2 3 4} ******
modelSigma1 = 49; % default 49
modelSigmaS = logspace(log10(modelSigma1),log10(Msigma),totalIter);
rho = Isigma^2/((modelSigma1/255)^2);
ns = min(25,max(ceil(modelSigmaS/2),1));
ns = [ns(1)-1,ns];
z = shepard_initialize(y, mask, window);
if useGPU
z = gpuArray(z);
y = gpuArray(y);
end
for itern = 1:totalIter
% step 1
rho = lamda*255^2/(modelSigmaS(itern)^2);
z = (y+rho*z)./(mask+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;
% imshow(z)
% title(int2str(itern))
% drawnow;
end
if useGPU
output = im2uint8(gather(z));
end
imshow(cat(2,im2uint8(Iori),output));
imwrite(output,fullfile(folderResultCur,[Iname,'_ircnn.png']));