-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlc_createImages.m
281 lines (239 loc) · 11.1 KB
/
lc_createImages.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
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
%% Create images that will go into any classification tool
% For Caffe: creates a lmdb database that can be feed directly
% For tensorflow and nupic: it will store the data in png images:
% -- nupic has its own built-in functions to read and make sense of them
% -- for tensorflow I created some python scripts to read the data and
% create the corresponding numpy arrays that go into tensorflow
% TODO:
% 1.- Select some variables and create some demo png images to decide which
% ones to use
% 1.1.- Use sensors defined in lc_sensorChoices as they are to check how
% they look
% 1.2.- Use lighting values of [10, 50, 100]
% 1.3.- Change diferent pixel sizes [1.1e-06, 1.25e-06, 1.4e-06];
%
% 2.- Refactor de code to remove commented blocks and create functions
%
% 3.- Generate the images and check them to select best parameters for
% simulation
%
% 4.-
%% INITIALIZE
ieInit;
% Where are the files to convert
projDir = '~/soft/WLletterClass';
mnistDirTest = [projDir filesep 'data/origMnistSmall/test'];
mnistDirTrain = [projDir filesep 'data/origMnistSmall/train'];
mnistDir = mnistDirTrain;
testTrain = 'train';
%% EXP. VARIABLES
% We are going to change lighting and pixel size
CharNamesList = 3:6;
pixelSizes = [1.1e-06, 1.25e-06, 1.4e-06]; pSize = 2;
sceneLights = [45];
% fovs = [0.6, 0.8, 1];
dists_ft = [25];
dists = ft2m(dists_ft);
% mnistISETBIOdir = [mnistDir filesep 'OUT'];
% if ~isdir(mnistISETBIOdir)
% mkdir(mnistISETBIOdir);
% end
%% EXP. OPTIONS
ViewResults = 0; % Make it 1 to visualize results
if ViewResults
CharNamesList = CharNamesList(6); % Visualize only one category for test
end
% For the SCIEN poster Brian created this LED display from Barco
% It can be out of the loop
ledBarcoDisplay = displayCreate('LED-BarcoC8');
%% LOOP TO PROCESS ALL IMAGES
for dist = 1:length(dists)
for sLight = 1:length(sceneLights)
for nc = 1:length(CharNamesList)
cd([mnistDir filesep num2str(CharNamesList(nc))])
% WriteFolder = [projDir filesep 'data' filesep 'isetMnist' ...
% num2str(fov) filesep testTrain filesep ...
% 'Light_' num2str(sceneLights(sLight)) ...
% '_pSize_' num2str(pixelSizes(pSize)) ...
% filesep num2str(CharNamesList(nc))];
WriteFolder = [projDir filesep 'data' ...
filesep 'Light_' num2str(sceneLights(sLight)) ...
'_DistFt_' num2str(dists_ft(dist)) ...
filesep testTrain ...
filesep num2str(CharNamesList(nc))];
if ~isdir(WriteFolder)
mkdir(WriteFolder);
end
% Read all the file names to convert
sampleNames = dir('*.png');
if ViewResults
sampleNames = sampleNames(5:5); % Visualize one or two for testing
end
for npng = 1:length(sampleNames)
% SCENE CREATION PER EVERY IMAGE
% ------------------------------
% CREATE access to the file
fullFileName = [mnistDir filesep num2str(CharNamesList(nc)) ...
filesep sampleNames(npng).name];
% fullFileName = vcSelectImage; % If you want to select manually
% imgType = 'rgb';
imgType = 'monochrome';
% CREATE a Display: iPhone Retina Display 2012, dpi = 326
% retinaDisplay = displayCreate('LCD-AppleRetina2012');
% I created LCD-AppleRetina2012 as a copy of LCD-Apple, so now I am
% going to change the values that interest me.
% Steve Jobs said 10-12 inches, and 11 is the lenght of letter
% distin = 11;
% retinadpi = 326;
% retinaDisplay = displaySet(retinaDisplay, 'name', 'AppleRetina2012');
% retinaDisplay = displaySet(retinaDisplay, 'dpi', 326);
% retinaDisplay = displaySet(retinaDisplay, 'viewingdistance', in2m(distin));
ledBarcoDisplay = displaySet(ledBarcoDisplay, 'viewingdistance', dists(dist));
% CREATE a scene for my files that will simulate
% watching the mnist character at 11 inches in an iPhone
% [scene, I] = sceneFromFile(fullFileName, imgType, [], ...
% retinaDisplay, [], [], [], []);
% CREATE a scene for my files that will simulate watching the
% characters at .6, .8, 1 fov, that translates to:
s = sceneFromFile(fullFileName, imgType, [], ...
ledBarcoDisplay, [], [], [], []);
% distin = m2in(displayGet(ledBarcoDisplay, 'viewing distance'));
% % CONFIRM that it makes sense
% himgpix = 28; % Mnist image is 28x28 pixels
% vimgpix = 28;
%
% % 28 dot / 326 dot per inch = dim in inch
% % himgin = himgpix / retinadpi;
% % vimgin = vimgpix / retinadpi;
% himgin = himgpix / displayGet(ledBarcoDisplay, 'dpi');
% vimgin = vimgpix / displayGet(ledBarcoDisplay, 'dpi');
% % dim in inch / dist in inch = tan(alpha)
% halpharad = atan(himgin / distin);
% valpharad = atan(vimgin / distin);
% % convert to degrees
% hwangulardeg = rad2deg(halpharad);
% vwangulardeg = rad2deg(valpharad);
%
% degperdot = displayGet(ledBarcoDisplay, 'deg per dot');
% secperdot = degperdot * 60 * 60;
%
% % Calc dpi with homework1 example
% if ~isequal(sprintf('%.2f',displayGet(ledBarcoDisplay, 'dpi')), ...
% sprintf('%.2f',space2dpi(secperdot, distin)))
% error('dpi is not the same, revise calculations')
% end
%
% % Obtain horiz fov and compare with hwangulardeg
% if ~isequal(sprintf('%.2f',hwangulardeg), ...
% sprintf('%.2f',sceneGet(scene, 'wangular')))
% error('fov is not the same, revise calculations')
% end
%
% % Obtain horiz width and compare with himgin
% if ~isequal(sprintf('%.2f',in2m(himgin)), ...
% sprintf('%.2f',sceneGet(scene, 'width')))
% error('width is not the same, revise calculations')
% end
%
% % View results when debuging
% if ViewResults
% vcNewGraphWin;
% sceneShowImage(scene);
% end
%% Diffraction limited optics
% s = sceneCreate('rings rays'); % BW used it as a demo
% s = sceneFromFile(fullFileName, imgType);
% s = sceneSet(s,'fov',fov);
s = sceneAdjustLuminance(s,sceneLights(sLight));
ieAddObject(s);
% View results when debuging
if ViewResults
sceneWindow;
end
oi = oiCreate;
oif2 = oiSet(oi,'optics fnumber', 4);
oif2 = oiSet(oif2,'optics off axis method','skip');
oif2 = oiCompute(oif2,s);
ieAddObject(oif2);
% View results when debuging
if ViewResults
oiWindow;
end
%% Here is how we might change the f-number of the optics
% Here is how to set the pixel size to be smaller
%
% (We are not doing it right now, first check other variables)
% oif16 = oiSet(oi,'optics fnumber',16);
% oif16 = oiCompute(oif16,s);
% ieAddObject(oif16); oiWindow;
sensor = sensorCreate;
sensor = sensorSet(sensor,'noise flag',2);
sensor = sensorSet(sensor,'exposure duration',1/60); % 15 ms
sensor = sensorSet(sensor,'pixel size constant fill factor',...
pixelSizes(pSize));
sensor = sensorSet(sensor,'size',[64 64]);
sensor = sensorCompute(sensor,oif2);
ieAddObject(sensor);
% View results when debuging
if ViewResults
sensorWindow('scale',true);
end
% % HUMAN OPTICS
% % ------------
% oi = oiCreate;
% oi = oiCompute(oi, scene);
% %
% % Scene Options
% %
% %
% %
% % View results when debuging
% if ViewResults
% vcNewGraphWin;
% oiShowImage(oi);
% end
%
%
% sensor = sensorCreate();
% sensor = sensorSet
%
% % CONE ABSORPTION HUMAN RETINA
% % ----------------------------
% cones = sensorCreate('human');
% cones = sensorCompute(cones,oi);
% img = coneImageActivity(cones);
% %
% % Scene Options
% %
% %
% %
% % View results when debuging
% if ViewResults
% vcNewGraphWin;
% imagesc(img); axis off; axis image
% coneimg = cones.data.volts;
% figure(); imagesc(coneimg); colormap(gray);
% end
% SAVE THE IMAGE IN OUT DIRECTORY
% -------------------------------
% imwrite(img, [WriteFolder filesep sampleNames(npng).name]);
% I think that if we write the image directly, we will
% imfinfo([WriteFolder filesep sampleNames(npng).name])
% If I save the image like that, it has non meaningful information,
% as every cone instead of one value takes 8x8 values. I am
% interested only in taking the grey value of the central cone (I
% am going to ignore the color as our image is b&w).
% I = mat2gray(cones.data.volts);
I = mat2gray(sensor.data.volts);
imwrite(I, [WriteFolder filesep sampleNames(npng).name], ...
'png', 'BitDepth',8, 'ColorType', 'grayscale');
% imwrite(I, 'test.png', ...
% 'png', 'BitDepth',8, 'ColorType', 'grayscale');
% imfinfo('test.png')
% A = imread('test.png');
% imtool(A) >> Class = uint8, Type:intensity, Min-max: 0-255
% size(A) = 72 x 88
end
end
end
end