-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsimulate.asv
191 lines (148 loc) · 6.45 KB
/
simulate.asv
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
function [] = simulate(patternIds)
addpath utils
addpath fig
addpath bm3d_matlab_package/
addpath bm3d_matlab_package/bm3d
% clear;close all;clc;
srcDirs = {
'TOLED', % TOLED Repeat
'TOLED', % TOLED Random
'L2_Repeat', % L2 Repeat
'L2Inv_Repeat', % L2Inv Repeat
'L2_Random', % L2 Random
'L2Inv_Random', % L2Inv Random
};
dstDirs = {
'aperture_toled_single', % TOLED Repeat
'aperture_toled_randomRot90', % TOLED Random
'aperture_poled_single', % POLED Repeat
'aperture_poled_randomRot90', % POLED Random
'20201006-140308_Display_L2_Repeat', % L2 Repeat
'20201006-153518_Display_L2Inv_Repeat', % L2Inv Repeat
'20201125-031442_Display_L2_Random', % L2 Random
'20201125-145433_Display_L2Inv_Random', % L2Inv Random
};
tileOptions = {'', 'randomRot90', ...
'', '',...
'randomRot90','randomRot90','oneMorePixel', ...
};
thresholds = [ 0, 0,...
0, 0, ...
0, 0];
if ~exist('patternIds', 'var')
patternIds = 1: length(dstDirs);
end
SNRs = 24:4:40;
Ls = [273, 654, 1608, 4005, 10024];
noise_vars = [0.005, 0.002, 0.002, 0.001, 0.0002];
pixelSize = 2e-6;
ssims = zeros(1, length(SNRs));
psnrs = zeros(1, length(SNRs));
for id = patternIds
fprintf('\n\n- - - - - - - - - -\n');
dstDir = sprintf('results/simulation/%s/', dstDirs{id});
mkdir(dstDir);
% in this script, we only test DPI=150
unitPatternSize = 168e-6; %
delta1 = 8e-6; % ICCP21
% delta1 = 4e-6; % ICCP21
% Note:
% computePSF_3 --- computes PSF of peak wavelengths for RGB channel.
% We use this function in simualtion evaluation.
% computePSF_3smooth --- computes PSF of multi-wavelengths for RGB
% channel. To reproduce PSFs in Figure 5, please
% use this version.
[PSFs, openRatio] = computePSF_3(srcDirs{id}, dstDir, ...
tileOptions{id}, thresholds(id), unitPatternSize, delta1);
refRatio = 0.2072;
kernels = PSFs;
%% visualize PSFs
maxVal = max(PSFs(:));
PSFs = PSFs/maxVal;
PSFs = log(PSFs);
figure, hold on; axis equal, axis tight;
PSFs_x = (1:size(PSFs,1))*pixelSize;
PSFs_y = (1:size(PSFs,2))*pixelSize;
imagesc(PSFs_x, PSFs_y, PSFs(:,:,2));colormap jet; colorbar;
% saveas(gcf, sprintf('%s/%s_PSFs.png', dstDir, phaseOpt));
hold off
% figure, imshow((kernels ./ max(kernels(:)) * 2000).^(1/2.2),[]);
% saveas(gcf, sprintf('%s/%s_PSFs_RGB.png', dstDir, phaseOpt));
% plot auto-correlation function (FFT of PSFs)
kernel = kernels(:,:,2);
K = ifftshift(fft2(fftshift(kernel)));
figure, hold on; axis equal, axis tight;
imagesc(PSFs_x, PSFs_y, log(abs(K)+1));colormap jet; colorbar;
% saveas(gcf, sprintf('%s/%s_Autocorrelation.png', dstDir, phaseOpt));
hold off
% Evaluate PSF on images
for noiseId = 1: length(SNRs)
% noise modeling
sensor.capacity = 15506;
sensor.noise_std = 4.87;
SNR = SNRs(noiseId);
L = Ls(noiseId);
sensor.gain = 1/L;
mean_psnr = 0;
mean_ssim = 0;
imIds = 1:30;
for imId = imIds
fprintf('%s SNR=%d imId=%d...\n', dstDir, SNR, imId);
% - - - read image - - -
img = im2double(imread(sprintf('fig/HQ/test/%02d.png', imId)));
img = img ./ max(img(:)); % normalize img to [0,1]
img = img .* (openRatio / refRatio);
% ================================================= %
% Simulate capturing an image under display panel %
% ================================================= %
% blur sharp image by PSF
imgBlur = myConv2(img, kernels);
% add read out and shot noise
imgBlurNoisy = add_noise(imgBlur * L, 1, sensor);
clear imgBlur;
% save degraded image
if mod(imId-1, 5) == 0
imwrite(imgBlurNoisy, sprintf('%s/%d_SNR%d_blur_wiener_img.png',dstDir, imId, SNR));
end
% ================================================= %
% Reconver a sharp image %
% ================================================= %
% denoise (BM3D)
noiseProfile = BM3DProfile();
noiseProfile.gamma = 0;
noise_type = 'gw';
noise_var = noise_vars(noiseId); % Noise variance
seed = 0; % seed for pseudorandom noise realization
[~, PSD, ~] = getExperimentNoise(noise_type, noise_var, seed, size(imgBlurnoisy));
imgBlurDenoised = CBM3D(imgBlurNoisy, PSD, noiseProfile);
% wiener deconvolution
[imgSharp, ~] = myWienerDeconv(imgBlurDenoised, kernels, 35); % deblur in paper
clear imgBlurDenoised;
% save recovered sharp image
if mod(imId-1, 5) == 0
imwrite(imgSharp, sprintf('%s/%d_SNR%d_deblur_wiener_img.png',dstDir, imId, SNR));
end
% ================================================= %
% Compute PSNR and SSIM %
% ================================================= %
% SNR and SSIM
psnrVal = psnr(imgSharp, img);
[ssimVal, ~] = ssim(imgSharp, img, 'Radius', 1.5);
mean_psnr = mean_psnr + psnrVal;
mean_ssim = mean_ssim + ssimVal;
end
fprintf('\n%s SNR=%d(dB) mean_psnr=%.2f, ssim=%.4f\n', ...
dstDirs{id},SNRs(noiseId), mean_psnr/length(imIds), mean_ssim/length(imIds));
ssims(noiseId) = mean_ssim/length(imIds);
psnrs(noiseId) = mean_psnr/length(imIds);
% save current results
curr_ssims = ssims;
curr_psnrs = psnrs;
% todo
save(sprintf('%s/sweep_snr.mat',dstDir), 'SNRs', 'curr_ssims', 'curr_psnrs');
end
curr_ssims = ssims;
curr_psnrs = psnrs;
% todo
save(sprintf('%s/sweep_snr.mat',dstDir), 'SNRs', 'curr_ssims', 'curr_psnrs');
end