-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths_evalSimGlobalScene.m
More file actions
296 lines (208 loc) · 7.5 KB
/
s_evalSimGlobalScene.m
File metadata and controls
296 lines (208 loc) · 7.5 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
% This script demonstrates how the complementary spectral flash works
% on simulated data. The data was obtained using RenderToolbox4 and PBRT.
%
% Copyright, Henryk Blasinski 2017
close all;
clear all;
clc;
ieInit;
% Directory where images and figures will be saved. If empty noting is
% saved.
% destDir = fullfile(cmfRootPath,'..','Figures');
destDir = [];
set(groot,'defaultAxesColorOrder',[1 0 0; 0 1 0; 0 0 1]);
wave = 400:5:700;
nWaves = length(wave);
fName = fullfile(cmfRootPath,'Parameters','XimeaSpectralResponsivities');
cameraResp = ieReadColorFilter(wave,fName);
sensor = sensorCreate('bayer (bggr)');
sensor = sensorSet(sensor,'wave',wave);
sensor = sensorSet(sensor,'size',[480 640]);
sensor = sensorSet(sensor,'noise flag',2);
sensor = sensorSet(sensor,'filter transmissivities',cameraResp);
% Define the water properties (Note, data may not have been rendered for
% the properties you've specified).
chlorophyllConc = 0.0;
cdomConc = 0;
camDist = 1000;
waterDepth = 10000;
smallPart = 0.0;
largePart = 0.0;
target = 'Table'; %or 'Macbeth' or 'Table' or 'Acropora' or 'Objects'
fName = fullfile(cmfRootPath,'Parameters','ximeaLights');
ledSpectra = ieReadSpectra(fName,wave);
flashNorm = Energy2Quanta(wave,ledSpectra);
flashNorm = flashNorm/max(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
%
% Use all pixels in the image to predict the ambient spectrum
[ ambientEst, ambientWghts ] = globalAmbientEst( measurement.vectorized.ambient,...
measurement.vectorized.led, ...
flashNorm,...
'alpha',1);
% Plot the estimated ambient spectrum
figure;
hold on; grid on; box on;
plot(wave,ambientEst);
legend('Estimated');
title('Whole image ambient estimate');
% Render the approximation image
rendering = zeros(size(measurement.raw.ambient));
for i=1:nChannels
rendering = rendering + ambientWghts(i)*measurement.raw.led(:,:,i);
end
rendering = rendering/max(rendering(:));
sensor = sensorSet(sensor,'volts',rendering);
sensor = sensorSet(sensor,'name','Ambient approximation');
ieAddObject(sensor);
sensorWindow;
ip = ipCreate;
ip = ipCompute(ip,sensor);
ip = ipSet(ip,'name','Ambient approximation');
ieAddObject(ip);
ipWindow;
%% Per-patch ambient estimate
[ ambientEstPatch, ambientWghtsPatch, ambientPredictions ] = globalAmbientEst( measurement.patch.ambient,...
measurement.patch.led, ...
flashNorm,...
'alpha',1);
% Plot the quality of the approximation in the camera RGB space
figure;
hold on; grid on; box on;
plot(squeeze(measurement.patch.ambient)',ambientPredictions','o');
xlabel('Ambient appearance');
ylabel('Approximated');
title('RGB space - individual patches');
% Plot the estimated ambient spectrum
figure;
hold on; grid on; box on;
plot(wave,ambientEstPatch);
legend('Estimated');
title('Patch based ambient estimate');
%% Estimate attenuation on the path between the camera and the target
% We assume we know the distance and the depth.
distance = camDist;
depth = waterDepth;
surfaceGuess = 'D65';
ill = illuminantCreate(surfaceGuess,wave);
spectrum = illuminantGet(ill,'photons');
spectrum = spectrum/max(spectrum(:));
attnEst = ((ambientEst/max(ambientEst(:)))./spectrum).^(2*distance/depth);
%% Complement estimate
desiredIll = 'D65';
ill = illuminantCreate(desiredIll,wave);
desiredSpectrum = illuminantGet(ill,'photons');
[ flashEst, flashWghts ] = globalComplementEst( desiredSpectrum, ambientEst, diag(attnEst)*flashNorm, cameraResp,...
'flashMode',true);
% Plot the illuminant spectra
figure;
hold on; grid on; box on;
plot(wave,[ambientEst flashNorm*flashWghts ambientEst+flashEst])
xlabel('Wavelength');
ylabel('Scaled photons');
legend('Ambient','Comlp flash','Total');
%% Computational synthesis
% Render an image as if captured with the complementary flash.
rendering = measurement.raw.ambient;
for i=1:nChannels
rendering = rendering + flashWghts(i)*measurement.raw.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;
%% Small camera-taregt distance
% We can assume that the attenuation along this path is
attnEst = ones(nWaves,1);
% Complement estimate
desiredIll = 'D65';
ill = illuminantCreate(desiredIll,wave);
desiredSpectrum = illuminantGet(ill,'photons');
[ flashEst, flashWghts ] = globalComplementEst( desiredSpectrum, ambientEst, diag(attnEst)*flashNorm, cameraResp,...
'flashMode',true);
% Plot the illuminant spectra
figure;
hold on; grid on; box on;
plot(wave,[ambientEst flashNorm*flashWghts ambientEst+flashEst])
xlabel('Wavelength');
ylabel('Scaled photons');
legend('Ambient','Comlp flash','Total');
%% Plots for print
fs=10;
fg = figure;
hold on; grid off; box off;
plot(wave,ambientEst,'g','LineWidth',2);
ylim([0, max(flashNorm*flashWghts)]);
xlabel('Wavelength, nm','Interpreter','Latex');
ylabel('Scaled photons','Interpreter','Latex');
set(gca,'FontSize',fs+2);
set(gca,'TickLabelInterpreter','Latex');
set(gcf,'Units','Centimeters');
set(gcf,'PaperPosition',[1 1 4.5 2]);
set(gca,'XTick',400:100:800);
if isempty(destDir) == false
print('-depsc',fullfile(destDir,'Ambient.eps'));
end
figure;
hold on; grid off; box off;
plot(wave,ambientEst,'g','LineWidth',2);
plot(wave,flashNorm*flashWghts,'r--','LineWidth',2);
ylim([0, max(flashNorm*flashWghts)]);
xlabel('Wavelength, nm','Interpreter','Latex');
set(gca,'TickLabelInterpreter','Latex');
ylabel('Scaled photons','Interpreter','Latex');
set(gcf,'Units','Centimeters');
set(gca,'FontSize',fs+2);
set(gcf,'PaperPosition',[1 1 4.5 2]);
set(gca,'XTick',400:100:800);
if isempty(destDir) == false
print('-depsc',fullfile(destDir,'Ambient+Flash.eps'));
end
%% Computational synthesis
% Render an image as if captured with the complementary flash.
rendering = measurement.raw.ambient;
for i=1:nChannels
rendering = rendering + flashWghts(i)*measurement.raw.led(:,:,i);
end
rendering = rendering/max(rendering(:));
sensor = sensorSet(sensor,'volts',rendering);
sensor = sensorSet(sensor,'name',sprintf('Computational (noCorrection): %s',desiredIll));
ieAddObject(sensor);
sensorWindow;
ip = ipCreate;
ip = ipCompute(ip,sensor);
ip = ipSet(ip,'name','Computational (no distance corr.)');
ieAddObject(ip);
ipWindow;
%% Save data
% destDir = fullfile(cmfRootPath,'..','Figures');
ieImages = vcGetObjects('vcimage');
for i=1:length(ieImages)
image = ipGet(ieImages{i},'data srgb');
name = ipGet(ieImages{i},'name');
figure;
imshow(image);
title(name);
if isempty(destDir) == false
imwrite(image,fullfile(destDir,sprintf('%s_%s.png',target,name)));
end
end