-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths_evalSimGlobalObjects.m
More file actions
216 lines (147 loc) · 5.38 KB
/
s_evalSimGlobalObjects.m
File metadata and controls
216 lines (147 loc) · 5.38 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
% 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).
target = '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,...
'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
[ sensorApprox, ipApprox ] = renderFlashImage( measurement.raw.ambient, measurement.raw.led, ambientWghts, sensor,...
'name','Ambient approximation');
ieAddObject(sensorApprox);
sensorWindow;
ieAddObject(ipApprox);
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');
%% Complement estimate
desiredIll = 'D65';
ill = illuminantCreate(desiredIll,wave);
desiredSpectrum = illuminantGet(ill,'photons');
[ flashEst, flashWghts ] = globalComplementEst( desiredSpectrum, ambientEst, 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.
[ sensorComp, ipComp ] = renderFlashImage( measurement.raw.ambient, measurement.raw.led, flashWghts, sensor,...
'name',sprintf('Computational: %s',desiredIll));
ieAddObject(sensorComp);
sensorWindow;
ieAddObject(ipComp);
ipWindow;
%% 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
%% Show LED only images after ambient subtraction
for i=1:nChannels
ledSensor = sensorSet(sensor,'volts',measurement.raw.led(:,:,i));
ledSensor = sensorSet(ledSensor,'name',sprintf('LED%02i',i));
ip = ipCompute(ipCreate,ledSensor);
ip = ipSet(ip,'name',sprintf('LED%02i',i));
ieAddObject(ip);
end
%% Save data
destDir = fullfile(cmfRootPath,'..','Figures');
ieImages = vcGetObjects('vcimage');
for i=1:length(ieImages)
image = 2*ipGet(ieImages{i},'data srgb');
name = ipGet(ieImages{i},'name');
figure;
imshow(image,'Border','tight');
% title(name);
if isempty(destDir) == false
imwrite(image,fullfile(destDir,sprintf('%s_%s.png',target,name)));
end
end