-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequence_clean.m
91 lines (77 loc) · 2.88 KB
/
sequence_clean.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
function sequence_clean(varargin)
% SEQUENCE_CLEAN.M
% fixes hot pixels in detector using error map
% STILL IN PROGRESS
%
if nargin==0 % run whole lot sequentially
disp('Running normal version')
d=dir('*.edf');
maskfname='mask.edf';
for n=1:length(d)
im=edfreadandclean(d(n).name,maskfname);
parts=utilExtractFilenameParts(d(n).name);
if isempty(parts)
% do something special for special files
continue
end
newfname=[parts.prefix 'clean_' parts.index '.' parts.extension]
edfwrite(newfname,im,'float32');
end
elseif nargin==1 % run parallel version
if strcmpi(varargin{1},'parallel')
% divide jobs into groups with just two reference images each
disp('Parallel version')
start=[0:100:1400];
start=reshape(start,1,1,length(start));
fprintf('This code needs improving to use the XML\n')
% run the parallel client but don't wait for an answer ('&')
parallel('sequence_flatfield&',start)
else
fprintf('Looking at slice %d and onwards\n',varargin{1})
first=varargin{1};
% find directory name (which is prefix for all radiographs)
[tmp,dname,tmp,tmp]=fileparts(pwd);
%check state of reference images (refHST*)
if length(dir('refHST*'))==0
fprintf('Need some corrected reference images. Please use\n')
fprintf('''sequence_median_refs'' to generate some!\n');
return
end
fname_xml=[dname '.xml'];
if ~exist(fname_xml,'file')
fprintf('%s must exist!!\n',fname_xml)
return
end
acq=query_xml(fname_xml,'acquisition');
% read dark image
if exist('dark.edf','file')
% dark image has already been divided and can be used as-is
im_dark=edfread('dark.edf');
elseif exist('darkend0000.edf','file')
% dark image is a summed one - we can use and write the dark.edf while we
% are at it...
im_dark=edfread('darkend0000.edf')./acq.nDarks;
edfwrite('dark.edf',im_dark,'uint16');
else
disp('No dark files!')
return
end
fname=sprintf('refHST%04d.edf',floor(first/acq.RefSpacing)*acq.RefSpacing);
im_refPrevious=edfread(fname)-im_dark;
fname=sprintf('refHST%04d.edf',(floor(first/acq.RefSpacing)+1)*acq.RefSpacing);
im_refNext=edfread(fname)-im_dark;
for n=first:first+acq.RefSpacing
% what ratio between the two references?
fprintf('Correcting image %d... ',n);
im=edfread(sprintf('%s%04d.edf',dname,n))-im_dark;
fraction=mod(n,acq.RefSpacing)/acq.RefSpacing;
im_refN=(im_refNext.*(fraction))+(im_refPrevious.*(1-fraction));
im_corrected=im./im_refN;
fname_imC=sprintf('%sflat_%04d.edf',dname,n);
fprintf('Writing result (%s)\r',fname_imC);
edfwrite(fname_imC,im_corrected,'float32');
end
fprintf('\n')
end
end
end