-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths_evalSimSpatialScene.m
More file actions
231 lines (165 loc) · 6.31 KB
/
s_evalSimSpatialScene.m
File metadata and controls
231 lines (165 loc) · 6.31 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
% This script demonstrates how the complementary spectral flash works
% on simulated data. The data was obtained using RenderToolbox4 and PBRT.
%
% THIS SCRIPT EVALUATES THE SPATIAL METHOD THAT IS NOT DESCRIBED IN THE
% PAPER
%
% Copyright, Henryk Blasinski 2017
close all;
clear all;
clc;
ieInit;
set(groot,'defaultAxesColorOrder',[1 0 0; 0 1 0; 0 0 1]);
wave = 400:10:700;
nWaves = length(wave);
fName = fullfile(cmfRootPath,'Parameters','XimeaSpectralResponsivities');
cameraMat = ieReadColorFilter(wave,fName);
sensor = sensorCreate('bayer (rggb)');
sensor = sensorSet(sensor,'wave',wave);
sensor = sensorSet(sensor,'size',[480 640]);
sensor = sensorSet(sensor,'noise flag',2);
sensor = sensorSet(sensor,'filter transmissivities',cameraMat);
chlorophyllConc = 0.0;
cdomConc = 0.0;
camDist = 1000;
waterDepth = 10000;
smallPart = 0.0;
largePart = 0.0;
ambient = 'D65';
target = 'Macbeth'; %or 'Macbeth' or 'Table' or 'Acropora'
fName = fullfile(slRootPath,'Parameters','ximeaLights');
ledSpectra = ieReadSpectra(fName,wave);
flashNorm = Energy2Quanta(wave,ledSpectra);
flashNorm = flashNorm/max(flashNorm(:));
ledSpectra = [zeros(nWaves,1), 1e7*flashNorm];
ill = illuminantCreate('D65',wave);
surfaceSpectrum = illuminantGet(ill,'photons');
surfaceSpectrum = surfaceSpectrum/max(surfaceSpectrum(:));
nChannels = size(flashNorm,2);
[measurement, reference] = getRenderedData(sensor, wave, surfaceSpectrum, flashNorm,...
'targetDistance',camDist,...
'depth',waterDepth,...
'chlConc',chlorophyllConc,...
'cdomConc',cdomConc,...
'smallPartConc',smallPart,...
'largePartConc',largePart,...
'target',target,...
'rtbResultFolder',fullfile(cmfRootPath,'..','Data','Simulated'));
%% Ambient estimate
% Estimate the ambient illuminant independently for every pixel, with some
% spatial smoothing
alpha = 0;
beta = 0;
[ambientApproxWghts, pred, history] = ambientEstADMM( measurement.demosaiced.ambient, ...
measurement.demosaiced.led, flashNorm, alpha, beta,...
'maxIter', 100, 'verbose', true , 'rescaleRho',true, 'tol',0);
figure;
hold on; grid on; box on;
plot(measurement.demosaiced.ambient(:),pred(:),'.');
xlabel('Captured');
ylabel('Modeled');
% Plot the (spatially varying) estimated ambient spectrum
ambientEst = flashNorm*reshape(ambientApproxWghts,[640*480, nChannels])';
pointIDs = randi(size(ambientEst,2),1000,1);
figure;
hold on; grid on; box on;
plot(wave,ambientEst(:,pointIDs),'k');
plot(wave,cameraMat,'--');
plot(wave,flashNorm,':');
xlabel('Wavelength, nm');
figure;
plot([history.prRes history.dualRes]);
xlabel('Iteration')
legend('Primal','Dual');
title('ADMM residuals');
figure;
for i=1:nChannels
subplot(2,4,i);
imagesc(ambientApproxWghts(:,:,i));
axis image;
end
%% Depth estimation
% Given the weights on LEDs to approximate the ambient, we should be able
% to derive the depth (larger weights -> more LED light needed to
% illuminate given pixel -> the pixel is further away)
% We compute the 'intensity' as the weight along the principal component of
% the illumination spectrum
wghtsVec = reshape(ambientApproxWghts,[640*480, nChannels])';
[vec, ~, score ] = pca(wghtsVec');
% The 1st principal component describes the global illuminant
meanAmbientEst = flashNorm*vec(:,1);
figure;
hold on; grid on; box on;
plot(wave,meanAmbientEst);
xlabel('Wavelength, nm');
title('Global illuminant');
wref = 1;
comp1Wght = (vec(:,1)'*wghtsVec)/wref;
scaledDistance = 1./abs(sqrt(reshape(comp1Wght,[480, 640])));
figure;
imagesc(scaledDistance,[0 10]); colorbar;
title('Estimated scaled distance');
%% Estimate attenuation on the path between the camera and the target
% We assume we know the distance and the depth.
% distance = 1000;
% depth = 10000;
% exponent = repmat((2*distance/depth*scaledDistance),[1 1 nWaves]);
% attnEst = repmat(shiftdim(meanAmbientEst,-2),[hh ww 1]).^exponent;
%% Complement estimate
alpha = 0;
beta = 0;
desiredIll = 'D65';
ill = illuminantCreate(desiredIll,wave);
desiredSpectrum = illuminantGet(ill,'photons');
desiredSpectrum = desiredSpectrum/max(desiredSpectrum);
[ complWghts, slack, history ] = complementEstADMM( desiredSpectrum, ambientApproxWghts, flashNorm, cameraMat, alpha, beta,...
'maxIter', 200, 'verbose', true, 'rescaleRho', false, 'flashMode',false );
%{
[ complWghtsAttn, slack, hist ] = complementEstWithAttnADMM( desiredSpectrum, ambientApproxWghts, flashNorm, attnEst, cameraMat, alpha, beta,...
'maxIter', 200, 'verbose', true, 'rescaleRho', false, 'flashMode',false );
%}
complementEst = flashNorm*reshape(complWghts,[480*640, nChannels])';
figure;
plot(wave,complementEst(:,pointIDs),'k');
xlabel('Wavelength, nm');
ylabel('Intensity');
title('Complementary flash');
figure;
plot(wave,complementEst(:,pointIDs) + ambientEst(:,pointIDs),'r');
xlabel('Wavelength, nm');
ylabel('Intensity');
title('Total illumination');
%% Computational synthesis
% Render an image as if captured with the complementary flash.
% No camera-target distance correction
rendering = measurement.demosaiced.ambient;
for i=1:nChannels
rendering = rendering + repmat(complWghts(:,:,i),[1 1 3]).*measurement.demosaiced.led(:,:,:,i);
end
rendering = rendering/max(rendering(:));
sensor = sensorSet(sensor,'volts',rendering);
sensor = sensorSet(sensor,'name',sprintf('Computational: %s',desiredIll));
ieAddObject(sensor);
sensorWindow;
ip = ipCreate;
ip = ipCompute(ip,sensor);
ip = ipSet(ip,'name','Computational');
ieAddObject(ip);
ipWindow;
%% Camera-target distance correction
%{
rendering = images.ambientDemosaiced;
for i=1:nChannels
rendering = rendering + repmat(complWghtsAttn(:,:,i),[1 1 nFilters]).*images.ledsDemosaiced(:,:,:,i);
end
rendering = rendering/max(rendering(:));
sensor = sensorSet(sensor,'volts',rendering);
sensor = sensorSet(sensor,'name',sprintf('Computational+Correction: %s',desiredIll));
ieAddObject(sensor);
sensorWindow;
ip = ipCreate;
ip = ipCompute(ip,sensor);
ip = ipSet(ip,'name','Computational+Correction');
ieAddObject(ip);
ipWindow;
%}