This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcovid_general_ref_optimization.m
477 lines (311 loc) · 15.7 KB
/
covid_general_ref_optimization.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
% macro to inject multi-references calculated from a series of scans in
% local tomography to cope with the low frequencies problems. It will use
% the high frequencies of the median of all the projections from all scans,
% but will keep the low frequencies from each original reference to keep
% the overall correction effect of local tomography in case of of-axis case.
% origin Paul Tafforeau ESRF 2020
function covid_general_ref_optimization (radix,preset)
close all
switch preset
case 'large_field'
new_refon=100 ; % creates a new set of reference every new_refon from the general reference calculated from all the scans
ring_size=20 ; % if >0, uses the normalized average of projections to correct for the main ring artefacts by checking differences with the general ref
ring_hori_Vsize=20 ; % size of the filter for exclusion of horizontal artefacts
ring_hori_Hsize=100 ; % length of the filter for removal of horizontal structures in the ring picture
test_darkening=0 ;
LF_by_max=0 ; % if 1, the low frequencies larger than 10 pixels are combined by max projection instead fo median projection. The median is used for fine details
case 'core_biopsies'
new_refon=100 ; % creates a new set of reference every new_refon from the general reference calculated from all the scans
ring_size=50 ; % if >0, uses the normalized average of projections to correct for the main ring artefacts by checking differences with the general ref
ring_hori_Vsize=50 ; % size of the filter for exclusion of horizontal artefacts
ring_hori_Hsize=100 ; % length of the filter for removal of horizontal structures in the ring picture
test_darkening=0 ;
LF_by_max=0 ; % if 1, the low frequencies larger than 10 pixels are combined by max projection instead fo median projection. The median is used for fine details
case 'zoom6_on_axis'
new_refon=100 ; % creates a new set of reference every new_refon from the general reference calculated from all the scans
ring_size=50 ; % if >0, uses the normalized average of projections to correct for the main ring artefacts by checking differences with the general ref
ring_hori_Vsize=50 ; % size of the filter for exclusion of horizontal artefacts
ring_hori_Hsize=100 ; % length of the filter for removal of horizontal structures in the ring picture
test_darkening=0 ;
LF_by_max=0 ; % if 1, the low frequencies larger than 10 pixels are combined by max projection instead fo median projection. The median is used for fine details
case 'zoom6'
new_refon=100 ; % creates a new set of reference every new_refon from the general reference calculated from all the scans
ring_size=50 ; % if >0, uses the normalized average of projections to correct for the main ring artefacts by checking differences with the general ref
ring_hori_Vsize=50 ; % size of the filter for exclusion of horizontal artefacts
ring_hori_Hsize=100 ; % length of the filter for removal of horizontal structures in the ring picture
test_darkening=0 ;
LF_by_max=0 ; % if 1, the low frequencies larger than 10 pixels are combined by max projection instead fo median projection. The median is used for fine details
case 'zoom6_hard'
new_refon=100 ; % creates a new set of reference every new_refon from the general reference calculated from all the scans
ring_size=50 ; % if >0, uses the normalized average of projections to correct for the main ring artefacts by checking differences with the general ref
ring_hori_Vsize=50 ; % size of the filter for exclusion of horizontal artefacts
ring_hori_Hsize=100 ; % length of the filter for removal of horizontal structures in the ring picture
test_darkening=0 ;
LF_by_max=0 ; % if 1, the low frequencies larger than 10 pixels are combined by max projection instead fo median projection. The median is used for fine details
case 'zoom2'
new_refon=100 ; % creates a new set of reference every new_refon from the general reference calculated from all the scans
ring_size=100 ; % if >0, uses the normalized average of projections to correct for the main ring artefacts by checking differences with the general ref
ring_hori_Vsize=50 ; % size of the filter for exclusion of horizontal artefacts
ring_hori_Hsize=100 ; % length of the filter for removal of horizontal structures in the ring picture
test_darkening=0 ;
LF_by_max=0 ; % if 1, the low frequencies larger than 10 pixels are combined by max projection instead fo median projection. The median is used for fine details
case 'zoom1'
new_refon=100 ; % creates a new set of reference every new_refon from the general reference calculated from all the scans
ring_size=100 ; % if >0, uses the normalized average of projections to correct for the main ring artefacts by checking differences with the general ref
ring_hori_Vsize=100 ; % size of the filter for exclusion of horizontal artefacts
ring_hori_Hsize=200 ; % length of the filter for removal of horizontal structures in the ring picture
test_darkening=0 ;
LF_by_max=0 ; % if 1, the low frequencies larger than 10 pixels are combined by max projection instead fo median projection. The median is used for fine details
case 'tubes'
new_refon=500 ; % creates a new set of reference every new_refon from the general reference calculated from all the scans
ring_size=0 ; % if >0, uses the normalized average of projections to correct for the main ring artefacts by checking differences with the general ref
ring_hori_Vsize=0 ; % size of the filter for exclusion of horizontal artefacts
ring_hori_Hsize=0 ; % length of the filter for removal of horizontal structures in the ring picture
test_darkening=0 ;
LF_by_max=10 ; % if >0 the low frequencies larger than the given value are combined by max projection instead fo median projection. The median is used for fine details
end
dirlist=dir([radix '*_']);
dirnum=size(dirlist,1);
dirname={dirlist.name};
parentdir=cleandirectoryname(pwd)
%% initialisation of parameters using the first scan
cd (dirname{1})
% names for the scan processing
n1=cleandirectoryname(pwd);
pos=findstr(n1,'/');
n2=n1(pos(end)+1:end);
% information from info-file
fp=fopen([n1 '/' n2,'.info'],'r');
if fp~=-1 % *.info exists
hd=fscanf(fp,'%c');
fclose(fp);
nvue =findheader(hd,'TOMO_N','integer');
else % *.info does not exist
disp([n2,'.info could not be found!']);
nvue=input('please indicate the number of projections ');
end
%% read SR mode from XML
xmlfilename = sprintf('%s/%s.xml',n1,n2);
if exist(xmlfilename,'file')==2
try
filling_mode = read_xml_file(xmlfilename,'acquisition','machineMode');
switch filling_mode
case '4 bunch'
max_SR_current=35;
case '16 bunch'
max_SR_current=95;
otherwise
max_SR_current=200;
end
catch
max_SR_current=200;
end
end
%% Dealing with accumulation
cfgfilename=sprintf('%s/%s.cfg',n1,n2');
fp2=fopen(cfgfilename,'r');
if fp2~=-1;
[acc_nb_frames]=str2num(read_cfg_file_dev(cfgfilename))
fclose(fp2);
else
acc_nb_frames=1;
end
fprintf('--------------------------------------\nAccumulation= %i\n--------------------------------------\n\n',acc_nb_frames);
%%
if exist ('proj_max_SRcur_norm.edf')
disp('the preprocessing looks OK, I continue the process')
general_dark=edfread('proj_max_SRcur_norm.edf')*0;
else
disp('no normalized projection, please run first covid_total_mean');
cd (parentdir)
return
end
if exist ('dark.edf')
general_dark=edfread('dark.edf');
if std2(general_dark)>0
real_dark=1;
total=1;
else
real_dark=0;
general_dark=general_dark*0+100*acc_nb_frames;
end
else
general_dark=general_dark*0+100*acc_nb_frames;
real_dark=0;
end
cd (parentdir)
%% processing of the dark
if real_dark==1
disp('processing of the dark')
for i=2:dirnum
namedir=dirname{i};
cd(namedir)
dark=edfread('dark.edf');
general_dark=general_dark+dark;
total=total+1;
cd (parentdir)
end
general_dark=general_dark/total;
edfwrite('general_dark.edf',general_dark,'float32');
end
%% processing of the normalized projections for references, putting all of them in a large matrix
ref_matrix=zeros(size(general_dark,1),size(general_dark,2),dirnum);
if LF_by_max>0
ref_max_matrix=ref_matrix;
end
ref_levels=zeros(1,1,dirnum);
max_ref_level=0;
min_ref_level=1e20;
for i=1:dirnum
namedir=dirname{i};
cd(namedir)
ref=edfread('proj_max_SRcur_norm.edf');
ref_level=mean2(ref-general_dark);
if ref_level>max_ref_level
max_ref_level=ref_level;
end
if ref_level<min_ref_level
min_ref_level=ref_level;
end
ref_matrix(:,:,i)=ref;
if LF_by_max>0
ref_max_matrix(:,:,i)=blurring_rapid(ref,LF_by_max,'replicate');
end
ref_levels(:,:,i)=ref_level;
cd(parentdir)
end
if test_darkening>0
if max_ref_level/min_ref_level>1.05
disp('there was apparently more than 5% of darkening of the optic, we take it into account')
correct_darkening=1;
for i=1:size(ref_matrix,3)
ref_norm=ref_matrix(:,:,i);
ref_matrix(:,:,i)=ref_norm/mean2(ref_norm)*max_ref_level;
if LF_by_max
ref_max_norm=ref_max_matrix(:,:,i);
ref_max_matrix(:,:,i)=ref_max_norm/mean2(ref_norm)*max_ref_level;
end
end
else
correct_darkening=0;
end
else
correct_darkening=0;
end
if LF_by_max>0
ref_med=median(ref_matrix,3);
ref_med2=ref_med-blurring_rapid(ref_med,LF_by_max,'replicate');
ref_max=max(ref_max_matrix,[],3);
final_ref=ref_med2+ref_max+general_dark;
else
final_ref=median(ref_matrix,3)+general_dark;
end
% figure;imshow(final_ref',[]);
edfwrite('general_ref.edf',final_ref,'float32');
%%
% application of the general ref and dark to the different scans taking into account the darkening of the optic if detected.
for i=1:dirnum
namedir=dirname{i};
cd(namedir)
n1=cleandirectoryname(pwd);
pos=findstr(n1,'/');
n2=n1(pos(end)+1:end);
%% application of ring artefacts pre-correction
if ring_size>0
if exist('proj_max_SRcur_norm.edf');
disp('trying to optimize the scan for ring artefacts using the normalized projection of all the radiographs')
ring_im=edfread('proj_max_SRcur_norm.edf');
ring_im=((ring_im-general_dark)/ref_levels(:,:,i)*max_ref_level)./(final_ref-general_dark);
% ring_im=(ring_im-1)*ref_levels(:,:,i);
ring_im=ring_im-medfilt_rapid(ring_im,[ring_size ring_size],'replicate');
% removing of horizontal structures that are probably due to the sample
ring_hori=ring_im-medfilt_rapid(ring_im,[ring_hori_Vsize ring_hori_Vsize],'symmetric');
ring_hori=medfilt_rapid(ring_hori,[ring_hori_Hsize 1],'symmetric');
ring_hori=medfilt_rapid(ring_hori,[ring_hori_Hsize 1],'symmetric');
ring_hori=medfilt_rapid(ring_hori,[ring_hori_Hsize 1],'symmetric');
ring_hori=ring_hori-medfilt_rapid(ring_hori,[ring_hori_Vsize ring_hori_Vsize],'symmetric');
ring_im=ring_im-ring_hori;
ring_im=-ring_im;
edfwrite('rings_im.edf',ring_im,'float32');
% figure;imshow(ring_im',[]); drawnow
end
end
if correct_darkening==1
% checking first picture of the scan
fname=sprintf('%s%04d.edf',n2,50);
fp2=fopen(fname);
if fp2~=-1
hd2=fscanf(fp2,'%c',1024);
fclose(fp2);
SCAN_SRcurrent=findheader(hd2,'SRCUR','float');
end
first_im=edfread(fname);
first_im_abs_level=mean2(first_im-general_dark)/SCAN_SRcurrent*max_SR_current;
% checking first picture of the scan
fname=sprintf('%s%04d.edf',n2,nvue);
fp2=fopen(fname);
if fp2~=-1
hd2=fscanf(fp2,'%c',1024);
fclose(fp2);
SCAN_SRcurrent=findheader(hd2,'SRCUR','float');
end
last_im=edfread(fname);
last_im_abs_level=mean2(last_im-general_dark)/SCAN_SRcurrent*max_SR_current;
% calculation of darkening factor during the scan
darkening_factor=first_im_abs_level/last_im_abs_level
if darkening_factor<1
disp('there is something weird with the darkening, it seems to have reduced during the scan')
end
end
%% creating the new set of references from the general ones for compensation of SRcurrent and eventually of optic darkening
for j=0:new_refon:nvue
fname=sprintf('%s%04d.edf',n2,j);
fp2=fopen(fname);
if fp2~=-1
hd2=fscanf(fp2,'%c',1024);
fclose(fp2);
SCAN_SRcurrent=findheader(hd2,'SRCUR','float');
end
new_refname=sprintf('refHST%4.4i.edf',j);
new_ref=(final_ref-general_dark)/max_SR_current*SCAN_SRcurrent;
if ring_size>0
new_ref=new_ref./((ring_im+1)*mean2(new_ref))*mean2(new_ref);
end
if correct_darkening==1
new_ref=new_ref/max_ref_level*ref_levels(:,:,i);
% intra-scan correction to cope with the darkning during the scan
dark_factor=(darkening_factor-1)/nvue*j+1-(darkening_factor-1)/2;
new_ref=(new_ref)/dark_factor;
end
new_ref=new_ref+general_dark;
% mean2(new_ref)
edfwrite(new_refname,new_ref,'float32');
fprintf('the new reference %4.4i of the scan %2.2i has been written \r',j,i);
end
%%
if ~exist('dark_orig.edf') && real_dark==1
cmd='mv dark.edf dark_orig.edf';
system(cmd);
else
fprintf('the original dark has already been saved \n');
end
edfwrite('dark.edf',general_dark,'float32')
%% writing the general dark
cd (parentdir)
%% updating info file with the correct refon in case of use of multiple references
infoname=sprintf('%s/%s.info',n1,n2);
fid = fopen(infoname,'r');
info=fscanf(fid,'%c');
fclose(fid);
stringbeg='REF_ON= ';
posbeg1 = findstr(info,stringbeg)+size(stringbeg,2);
stringend='REF_N= ';
posend1 = findstr(info,stringend);
new_string1=sprintf('%1.0i\n',new_refon);
new_info=[info(1:posbeg1-1) new_string1 info(posend1:end)];
fid2=fopen(infoname,'w');
fwrite(fid2,new_info,'uchar');
fclose(fid2);
fprintf('the new info file has been updated for the refon \n');
end
end