1 | %'sub_background': substract a sliding background to an image series |
---|
2 | %------------------------------------------------------------------------ |
---|
3 | % Method: |
---|
4 | %calculate the background image by sorting the luminosity of each point |
---|
5 | % over a sliding sub-sequence of 'nbaver_ima' images. |
---|
6 | % The luminosity value of rank 'rank' is selected as the |
---|
7 | % 'background'. rank=nbimages/2 gives the median value. Smaller values are appropriate |
---|
8 | % for a dense set of particles. The extrem value rank=1 gives the true minimum |
---|
9 | % luminosity, but it can be polluted by noise. |
---|
10 | % Organization of image indices: |
---|
11 | % The program is working on a series of images, |
---|
12 | % In the mode 'volume', nbfield2=1 (1 image at each level)and NbSlice (=nbfield_j) |
---|
13 | % Else nbfield2=nbfield_j =nbre of images in a burst (j index) |
---|
14 | |
---|
15 | % function GUI_config=sub_background(Param) |
---|
16 | % |
---|
17 | %%%%%%%%%%% GENERAL TO ALL SERIES ACTION FCTS %%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
18 | % |
---|
19 | %OUTPUT |
---|
20 | % ParamOut: sets options in the GUI series.fig needed for the function |
---|
21 | % |
---|
22 | %INPUT: |
---|
23 | % In run mode, the input parameters are given as a Matlab structure Param copied from the GUI series. |
---|
24 | % In batch mode, Param is the name of the corresponding xml file containing the same information |
---|
25 | % when Param.Action.RUN=0 (as activated when the current Action is selected |
---|
26 | % in series), the function ouput paramOut set the activation of the needed GUI elements |
---|
27 | % |
---|
28 | % Param contains the elements:(use the menu bar command 'export/GUI config' in series to |
---|
29 | % see the current structure Param) |
---|
30 | % .InputTable: cell of input file names, (several lines for multiple input) |
---|
31 | % each line decomposed as {RootPath,SubDir,Rootfile,NomType,Extension} |
---|
32 | % .OutputSubDir: name of the subdirectory for data outputs |
---|
33 | % .OutputDirExt: directory extension for data outputs |
---|
34 | % .Action: .ActionName: name of the current activated function |
---|
35 | % .ActionPath: path of the current activated function |
---|
36 | % .ActionExt: fct extension ('.m', Matlab fct, '.sh', compiled Matlab fct |
---|
37 | % .RUN =0 for GUI input, =1 for function activation |
---|
38 | % .RunMode='local','background', 'cluster': type of function use |
---|
39 | % |
---|
40 | % .IndexRange: set the file or frame indices on which the action must be performed |
---|
41 | % .FieldTransform: .TransformName: name of the selected transform function |
---|
42 | % .TransformPath: path of the selected transform function |
---|
43 | % .InputFields: sub structure describing the input fields withfields |
---|
44 | % .FieldName: name(s) of the field |
---|
45 | % .VelType: velocity type |
---|
46 | % .FieldName_1: name of the second field in case of two input series |
---|
47 | % .VelType_1: velocity type of the second field in case of two input series |
---|
48 | % .Coord_y: name of y coordinate variable |
---|
49 | % .Coord_x: name of x coordinate variable |
---|
50 | % .ProjObject: %sub structure describing a projection object (read from ancillary GUI set_object) |
---|
51 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
52 | |
---|
53 | %======================================================================= |
---|
54 | % Copyright 2008-2016, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France |
---|
55 | % http://www.legi.grenoble-inp.fr |
---|
56 | % Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr |
---|
57 | % |
---|
58 | % This file is part of the toolbox UVMAT. |
---|
59 | % |
---|
60 | % UVMAT is free software; you can redistribute it and/or modify |
---|
61 | % it under the terms of the GNU General Public License as published |
---|
62 | % by the Free Software Foundation; either version 2 of the license, |
---|
63 | % or (at your option) any later version. |
---|
64 | % |
---|
65 | % UVMAT is distributed in the hope that it will be useful, |
---|
66 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
67 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
68 | % GNU General Public License (see LICENSE.txt) for more details. |
---|
69 | %======================================================================= |
---|
70 | |
---|
71 | function ParamOut=sub_background (Param) |
---|
72 | |
---|
73 | %%%%%%%%%%%%%%%%% INPUT PREPARATION MODE (no RUN) %%%%%%%%%%%%%%%%% |
---|
74 | if isstruct(Param) && isequal(Param.Action.RUN,0) |
---|
75 | ParamOut.AllowInputSort='off';% allow alphabetic sorting of the list of input file SubDir (options 'off'/'on', 'off' by default) |
---|
76 | ParamOut.WholeIndexRange='on';% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default) |
---|
77 | ParamOut.NbSlice='on'; % edit box nbre of slices made active |
---|
78 | ParamOut.VelType='off';% menu for selecting the velocity type (options 'off'/'one'/'two', 'off' by default) |
---|
79 | ParamOut.FieldName='off';% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default) |
---|
80 | ParamOut.FieldTransform = 'off';%can use a transform function |
---|
81 | ParamOut.ProjObject='off';%can use projection object(option 'off'/'on', |
---|
82 | ParamOut.Mask='off';%can use mask option (option 'off'/'on', 'off' by default) |
---|
83 | ParamOut.OutputDirExt='.sback';%set the output dir extension |
---|
84 | ParamOut.OutputFileMode='NbInput';% '=NbInput': 1 output file per input file index, '=NbInput_i': 1 file per input file index i, '=NbSlice': 1 file per slice |
---|
85 | |
---|
86 | %% root input file(s) and type |
---|
87 | % check the existence of the first file in the series |
---|
88 | first_j=[];% note that the function will propose to cover the whole range of indices |
---|
89 | if isfield(Param.IndexRange,'MinIndex_j'); first_j=Param.IndexRange.MinIndex_j; end |
---|
90 | last_j=[]; |
---|
91 | if isfield(Param.IndexRange,'MaxIndex_j'); last_j=Param.IndexRange.MaxIndex_j; end |
---|
92 | PairString=''; |
---|
93 | if isfield(Param.IndexRange,'PairString'); PairString=Param.IndexRange.PairString; end |
---|
94 | [i1,i2,j1,j2] = get_file_index(Param.IndexRange.first_i,first_j,PairString); |
---|
95 | FirstFileName=fullfile_uvmat(Param.InputTable{1,1},Param.InputTable{1,2},Param.InputTable{1,3},... |
---|
96 | Param.InputTable{1,5},Param.InputTable{1,4},i1,i2,j1,j2); |
---|
97 | if ~exist(FirstFileName,'file') |
---|
98 | msgbox_uvmat('WARNING',['the first input file ' FirstFileName ' does not exist']) |
---|
99 | else |
---|
100 | [i1,i2,j1,j2] = get_file_index(Param.IndexRange.last_i,last_j,PairString); |
---|
101 | LastFileName=fullfile_uvmat(Param.InputTable{1,1},Param.InputTable{1,2},Param.InputTable{1,3},... |
---|
102 | Param.InputTable{1,5},Param.InputTable{1,4},i1,i2,j1,j2); |
---|
103 | if ~exist(FirstFileName,'file') |
---|
104 | msgbox_uvmat('WARNING',['the last input file ' LastFileName ' does not exist']) |
---|
105 | end |
---|
106 | end |
---|
107 | |
---|
108 | %% check the validity of input file types |
---|
109 | ImageTypeOptions={'image','multimage','mmreader','video'};%allowed input file types(images) |
---|
110 | FileInfo=get_file_info(FirstFileName); |
---|
111 | FileType=FileInfo.FileType; |
---|
112 | CheckImage=~isempty(find(strcmp(FileType,ImageTypeOptions), 1));% =1 for images |
---|
113 | if ~CheckImage |
---|
114 | msgbox_uvmat('ERROR',['invalid file type input: ' FileType ' not an image']) |
---|
115 | return |
---|
116 | end |
---|
117 | |
---|
118 | %% numbers of fields |
---|
119 | NbSlice_i=1;%default |
---|
120 | if isfield(Param.IndexRange,'NbSlice')&&~isempty(Param.IndexRange.NbSlice) |
---|
121 | NbSlice_i=Param.IndexRange.NbSlice; |
---|
122 | end |
---|
123 | incr_j=1;%default |
---|
124 | if isfield(Param.IndexRange,'incr_j')&&~isempty(Param.IndexRange.incr_j) |
---|
125 | incr_j=Param.IndexRange.incr_j; |
---|
126 | end |
---|
127 | if isempty(first_j)||isempty(last_j) |
---|
128 | nbfield_j=1; |
---|
129 | else |
---|
130 | nbfield_j=numel(first_j:incr_j:last_j);%nb of fields for the j index (bursts or volume slices) |
---|
131 | end |
---|
132 | first_i=1;last_i=1;incr_i=1;%default |
---|
133 | if isfield(Param.IndexRange,'MinIndex_i'); first_i=Param.IndexRange.MinIndex_i; end |
---|
134 | if isfield(Param.IndexRange,'MaxIndex_i'); last_i=Param.IndexRange.MaxIndex_i; end |
---|
135 | if isfield(Param.IndexRange,'incr_i')&&~isempty(Param.IndexRange.incr_i) |
---|
136 | incr_i=Param.IndexRange.incr_i; |
---|
137 | end |
---|
138 | nbfield_i=numel(first_i:incr_i:last_i);%nb of fields for the i index (bursts or volume slices) |
---|
139 | nbfield=nbfield_j*nbfield_i; %total number of fields |
---|
140 | nbfield_i=floor(nbfield/NbSlice_i);%total number of indexes in a slice (adjusted to an integer number of slices) |
---|
141 | |
---|
142 | %% setting of parameters specific to sub_background |
---|
143 | nbaver_init=23; %default number of images used for the sliding background: to be adjusted later to include an integer number of bursts |
---|
144 | if nbfield_i~=1 |
---|
145 | nbaver=floor(nbaver_init/nbfield_j); % number of bursts used for the sliding background, |
---|
146 | if isequal(mod(nbaver,2),0)% if nbaver is even |
---|
147 | nbaver=nbaver+1;%put the number of burst to an odd number (so the middle burst is defined) |
---|
148 | end |
---|
149 | nbaver_init=nbaver*nbfield_j;%propose by default an integer number of bursts |
---|
150 | end |
---|
151 | |
---|
152 | prompt = {'volume scan mode (Yes/No)';... |
---|
153 | 'Number of images for the sliding background (MUST FIT IN COMPUTER MEMORY)';... |
---|
154 | 'the luminosity rank chosen to define the background (0.1=for dense particle seeding, 0.5 (median) for sparse particles';... |
---|
155 | 'set to 0 image levels below median(Yes/No)';... |
---|
156 | 'image rescaling coefficient(high values reduce the influence of bright particles), =0 for no rescaling' }; |
---|
157 | dlg_title = 'get (slice by slice) a sliding background and substract to each image'; |
---|
158 | num_lines= 5; |
---|
159 | def = { 'No';num2str(nbaver_init);'0.1';'No';'2'}; |
---|
160 | answer = inputdlg(prompt,dlg_title,num_lines,def); |
---|
161 | if isempty(answer) |
---|
162 | return |
---|
163 | end |
---|
164 | %check input consistency |
---|
165 | if strcmp(answer{1},'No') && ~isequal(NbSlice_i,1) |
---|
166 | check=msgbox_uvmat('INPUT_Y-N',['confirm the multi-level splitting into ' num2str(NbSlice_i) ' slices']); |
---|
167 | if ~strcmp(check,'Yes') |
---|
168 | return |
---|
169 | end |
---|
170 | end |
---|
171 | if strcmp(answer{1},'Yes') |
---|
172 | step=2;%the sliding background is shifted by the length of one burst, assumed =2 for volume ;ode |
---|
173 | ParamOut.NbSlice=1; %nbre of slices displayed |
---|
174 | else |
---|
175 | step=nbfield_j;%case of bursts: the sliding background is shifted by the length of one burst |
---|
176 | end |
---|
177 | nbaver_ima=str2double(answer{2});%number of images for the sliding background |
---|
178 | nbaver=ceil(nbaver_ima/step);%number of bursts for the sliding background |
---|
179 | if isequal(mod(nbaver,2),0)% if nbaver is even |
---|
180 | nbaver=nbaver+1;%set the number of bursts to an odd number (so the middle burst is defined) |
---|
181 | end |
---|
182 | nbaver_ima=nbaver*step;% correct the nbre of images corresponding to nbaver |
---|
183 | ParamOut.ActionInput.CheckVolume=strcmp(answer{1},'Yes'); |
---|
184 | ParamOut.ActionInput.SlidingSequenceLength=nbaver_ima; |
---|
185 | ParamOut.ActionInput.BrightnessRankThreshold=str2double(answer{3}); |
---|
186 | ParamOut.ActionInput.CheckSubmedian=strcmp(answer{4},'Yes'); |
---|
187 | ParamOut.ActionInput.SaturationCoeff=str2double(answer{5}); |
---|
188 | % apply the image rescaling function 'level' (avoid the blinking effects of bright particles) |
---|
189 | % answer=msgbox_uvmat('INPUT_Y-N','apply image rescaling function levels.m after sub_background'); |
---|
190 | % ParamOut.ActionInput.CheckLevelTransform=strcmp(answer,'Yes'); |
---|
191 | return |
---|
192 | end |
---|
193 | %%%%%%%%%%%%%%%%% STOP HERE FOR PAMETER INPUT MODE %%%%%%%%%%%%%%%%% |
---|
194 | |
---|
195 | %% read input parameters from an xml file if input is a file name (batch mode) |
---|
196 | checkrun=1; |
---|
197 | RUNHandle=[]; |
---|
198 | WaitbarHandle=[]; |
---|
199 | if ischar(Param) |
---|
200 | Param=xml2struct(Param);% read Param as input file (batch case) |
---|
201 | checkrun=0; |
---|
202 | else |
---|
203 | hseries=findobj(allchild(0),'Tag','series'); |
---|
204 | RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series |
---|
205 | WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series |
---|
206 | end |
---|
207 | |
---|
208 | %% input preparation |
---|
209 | NbSlice_i=Param.IndexRange.NbSlice; |
---|
210 | if ~isequal(NbSlice_i,1) |
---|
211 | display(['multi-level splitting into ' num2str(NbSlice_i) ' slices']); |
---|
212 | end |
---|
213 | RootPath=Param.InputTable(:,1); |
---|
214 | RootFile=Param.InputTable(:,3); |
---|
215 | SubDir=Param.InputTable(:,2); |
---|
216 | NomType=Param.InputTable(:,4); |
---|
217 | FileExt=Param.InputTable(:,5); |
---|
218 | %hdisp=disp_uvmat('WAITING...','checking the file series',checkrun); |
---|
219 | [filecell,i1_series,i2_series,j1_series]=get_file_series(Param); |
---|
220 | % if ~isempty(hdisp),delete(hdisp),end; |
---|
221 | %%%%%%%%%%%% |
---|
222 | % The cell array filecell is the list of input file names, while |
---|
223 | % filecell{iview,fileindex}: |
---|
224 | % iview: line in the table corresponding to a given file series |
---|
225 | % fileindex: file index within the file series, |
---|
226 | % i1_series(iview,ref_j,ref_i)... are the corresponding arrays of indices i1,i2,j1,j2, depending on the input line iview and the two reference indices ref_i,ref_j |
---|
227 | % i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices |
---|
228 | %%%%%%%%%%%% |
---|
229 | [FileInfo{1},MovieObject{1}]=get_file_info(filecell{1,1}); |
---|
230 | FileType{1}=FileInfo{1}.FileType; |
---|
231 | if ~isempty(j1_series{1}) |
---|
232 | frame_index{1}=j1_series{1}; |
---|
233 | else |
---|
234 | frame_index{1}=i1_series{1}; |
---|
235 | end |
---|
236 | |
---|
237 | |
---|
238 | %% output file naming |
---|
239 | FileExtOut='.png'; % write result as .png images for image inputsFileInfo.FileType='image' |
---|
240 | if strcmp(FileInfo{1}.FileType,'image') |
---|
241 | NomTypeOut=NomType{1}; |
---|
242 | % if strcmp(lower(NomType{1}(end)),'a') |
---|
243 | % NomTypeOut=NomType{1};%case of letter appendix |
---|
244 | elseif isempty(j1_series{1}) |
---|
245 | NomTypeOut='_1'; |
---|
246 | else |
---|
247 | NomTypeOut='_1_1';% caseof purely numerical indexing |
---|
248 | end |
---|
249 | OutputDir=[Param.OutputSubDir Param.OutputDirExt]; |
---|
250 | |
---|
251 | %% file index parameters |
---|
252 | % NbSlice_i: nbre of slices for i index: different of of 1 for multi-level, |
---|
253 | % the function sub_background is then relaunched by the GUI series for each |
---|
254 | % slice, incrementing the first index i by 1 |
---|
255 | % NbSlice_j: nbre of slices in volume mode |
---|
256 | % nbfield : total number of images treated per slice |
---|
257 | % step: shift of image index at each step of the sliding background (corresponding to the nbre of images in a burst) |
---|
258 | % nbaver_ima: nbre of the images in the sliding sequence used for the background |
---|
259 | % nbaver=nbaver_ima/step: nbre of bursts corresponding to nbaver_ima images. It has been adjusted so that nbaver is an odd integer |
---|
260 | nbfield_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices) |
---|
261 | nbfield_i=size(i1_series{1},2); %nb of fields for the i index |
---|
262 | |
---|
263 | if Param.ActionInput.CheckVolume |
---|
264 | step=2;% we assume the burst contains only one image pair |
---|
265 | NbSlice_j=nbfield_j; |
---|
266 | NbSlice=nbfield_j; |
---|
267 | nbfield_series=nbfield_i; |
---|
268 | else |
---|
269 | step=nbfield_j;%case of bursts: the sliding background is shifted by the length of one burst |
---|
270 | NbSlice_j=1; |
---|
271 | NbSlice=NbSlice_i; |
---|
272 | %nbfield_i=floor(nbfield_i/NbSlice_i);%total number of indexes in a slice (adjusted to an integer number of slices) |
---|
273 | %nbfield=nbfield_i*NbSlice_i; %total number of fields after adjustement |
---|
274 | nbfield_series=nbfield_i*nbfield_j; |
---|
275 | end |
---|
276 | nbfield=nbfield_j*nbfield_i; %total number of fields |
---|
277 | nbaver_ima=Param.ActionInput.SlidingSequenceLength;%number of images for the sliding background |
---|
278 | nbaver=ceil(nbaver_ima/step);%number of bursts for the sliding background |
---|
279 | if isequal(mod(nbaver,2),0) |
---|
280 | nbaver=nbaver+1;%set the number of bursts to an odd number (so the middle burst is defined) |
---|
281 | end |
---|
282 | nbaver_ima=nbaver*step; |
---|
283 | if nbaver_ima > nbfield |
---|
284 | display('number of images in a slice smaller than the proposed number of images for the sliding average') |
---|
285 | return |
---|
286 | end |
---|
287 | halfnbaver=floor(nbaver/2); % half width (in unit of bursts) of the sliding background |
---|
288 | |
---|
289 | %% calculate absolute brightness rank |
---|
290 | rank=floor(Param.ActionInput.BrightnessRankThreshold*nbaver_ima); |
---|
291 | if rank==0 |
---|
292 | rank=1;%rank selected in the sorted image series |
---|
293 | end |
---|
294 | |
---|
295 | %% prealocate memory for the sliding background |
---|
296 | try |
---|
297 | Afirst=read_image(filecell{1,1},FileType{1},MovieObject{1},frame_index{1}(1)); |
---|
298 | [npy,npx,nbcolor]=size(Afirst);% the argument nbcolor is important to get npx right for color images |
---|
299 | if strcmp(class(Afirst),'uint8') % case of 8bit images |
---|
300 | Ak=zeros(npy,npx,nbaver_ima,'uint8'); %prealocate memory |
---|
301 | Asort=zeros(npy,npx,nbaver_ima,'uint8'); %prealocate memory |
---|
302 | else |
---|
303 | Ak=zeros(npy,npx,nbaver_ima,'uint16'); %prealocate memory |
---|
304 | Asort=zeros(npy,npx,nbaver_ima,'uint16'); %prealocate memory |
---|
305 | end |
---|
306 | catch ME |
---|
307 | msgbox_uvmat('ERROR',['sub_background/read_image/' ME.message]) |
---|
308 | return |
---|
309 | end |
---|
310 | |
---|
311 | |
---|
312 | %%%%%%% LOOP ON SLICES FOR VOLUME SCAN %%%%%%% |
---|
313 | for j_slice=1:NbSlice_j |
---|
314 | %% select the series of i indices to process |
---|
315 | indselect=j_slice:step*NbSlice_j:nbfield;% select file indices of the slice |
---|
316 | for ifield=1:step-1 |
---|
317 | indselect=[indselect;indselect(end,:)+NbSlice_j]; |
---|
318 | end |
---|
319 | |
---|
320 | %% read the first series of nbaver_ima images and sort by luminosity at each pixel |
---|
321 | for ifield = 1:nbaver_ima |
---|
322 | ifile=indselect(ifield); |
---|
323 | filename=filecell{1,ifile}; |
---|
324 | Aread=read_image(filename,FileType{1},MovieObject{1},frame_index{1}(ifile)); |
---|
325 | if ndims(Aread)==3;%color images |
---|
326 | Aread=sum(double(Aread),3);% take the sum of color components |
---|
327 | end |
---|
328 | Ak(:,:,ifield)=Aread; |
---|
329 | end |
---|
330 | Asort=sort(Ak,3);%sort the luminosity of images at each point |
---|
331 | B=Asort(:,:,rank);%background image |
---|
332 | |
---|
333 | %% substract the first background image to the first images |
---|
334 | display( 'first background image will be substracted') |
---|
335 | for ifield=1:step*(halfnbaver+1);% nbre of images treated by the first background image |
---|
336 | Acor=double(Ak(:,:,ifield))-double(B);%substract background to the current image |
---|
337 | Acor=(Acor>0).*Acor; % put to 0 the negative elements in Acor |
---|
338 | ifile=indselect(ifield); |
---|
339 | j1=1; |
---|
340 | if ~isempty(j1_series{1}) |
---|
341 | j1=j1_series{1}(ifile); |
---|
342 | end |
---|
343 | newname=fullfile_uvmat(RootPath{1},OutputDir,RootFile{1},FileExtOut,NomTypeOut,i1_series{1}(ifile),[],j1); |
---|
344 | |
---|
345 | %write result file |
---|
346 | if ~isequal(Param.ActionInput.SaturationCoeff,0) |
---|
347 | C=levels(Acor,Param.ActionInput.CheckSubmedian,Param.ActionInput.SaturationCoeff); |
---|
348 | imwrite(C,newname,'BitDepth',16); % save the new image |
---|
349 | else |
---|
350 | if isequal(FileInfo{1}.BitDepth,16) |
---|
351 | C=uint16(Acor); |
---|
352 | imwrite(C,newname,'BitDepth',16); % save the new image |
---|
353 | else |
---|
354 | C=uint8(Acor); |
---|
355 | imwrite(C,newname,'BitDepth',8); % save the new image |
---|
356 | end |
---|
357 | end |
---|
358 | display([newname ' written']) |
---|
359 | end |
---|
360 | |
---|
361 | %% repeat the operation on a sliding series of images |
---|
362 | display('sliding background image will be substracted') |
---|
363 | if nbfield_series > nbaver_ima |
---|
364 | for ifield = step*(halfnbaver+1):step:nbfield_series-step*(halfnbaver+1)% ifield +iburst=index of the current processed image |
---|
365 | update_waitbar(WaitbarHandle,ifield/nbfield_series) |
---|
366 | if ~isempty(RUNHandle)&&~strcmp(get(RUNHandle,'BusyAction'),'queue') |
---|
367 | disp('program stopped by user') |
---|
368 | return |
---|
369 | end |
---|
370 | if nbaver_ima>step |
---|
371 | Ak(:,:,1:nbaver_ima-step)=Ak(:,:,1+step:nbaver_ima);% shift the current image series by one burst (step) |
---|
372 | end |
---|
373 | %incorporate next burst in the current image series |
---|
374 | for iburst=1:step |
---|
375 | ifile=indselect(ifield+iburst+step*halfnbaver); |
---|
376 | j1=1; |
---|
377 | if ~isempty(j1_series{1}) |
---|
378 | j1=j1_series{1}(ifile); |
---|
379 | end |
---|
380 | filename=fullfile_uvmat(RootPath{1},SubDir{1},RootFile{1},FileExt{1},NomType{1},i1_series{1}(ifile),[],j1); |
---|
381 | Aread=read_image(filename,FileType{1},MovieObject{1},i1_series{1}(ifile)); |
---|
382 | if ndims(Aread)==3;%color images |
---|
383 | Aread=sum(double(Aread),3);% take the sum of color components |
---|
384 | end |
---|
385 | Ak(:,:,nbaver_ima-step+iburst)=Aread;% fill the last burst of the current image series by the new image |
---|
386 | end |
---|
387 | Asort=sort(Ak,3);%sort the new current image series by luminosity |
---|
388 | B=Asort(:,:,rank);%current background image |
---|
389 | %substract the background for the current burst |
---|
390 | for iburst=1:step |
---|
391 | Acor=double(Ak(:,:,step*halfnbaver+iburst))-double(B); %the current image has been already read ans stored as index step*halfnbaver+iburst in the current series |
---|
392 | Acor=(Acor>0).*Acor; % put to 0 the negative elements in Acor |
---|
393 | ifile=indselect(ifield+iburst); |
---|
394 | if ~isempty(j1_series{1}) |
---|
395 | j1=j1_series{1}(ifile); |
---|
396 | end |
---|
397 | newname=fullfile_uvmat(RootPath{1},OutputDir,RootFile{1},FileExtOut,NomTypeOut,i1_series{1}(ifile),[],j1); |
---|
398 | %write result file |
---|
399 | if ~isequal(Param.ActionInput.SaturationCoeff,0) |
---|
400 | C=levels(Acor,Param.ActionInput.CheckSubmedian,Param.ActionInput.SaturationCoeff); |
---|
401 | imwrite(C,newname,'BitDepth',16); % save the new image |
---|
402 | else |
---|
403 | if isequal(FileInfo{1}.BitDepth,16) |
---|
404 | C=uint16(Acor); |
---|
405 | imwrite(C,newname,'BitDepth',16); % save the new image |
---|
406 | else |
---|
407 | C=uint8(Acor); |
---|
408 | imwrite(C,newname,'BitDepth',8); % save the new image |
---|
409 | end |
---|
410 | end |
---|
411 | display([newname ' written']) |
---|
412 | end |
---|
413 | end |
---|
414 | end |
---|
415 | |
---|
416 | %% substract the background from the last images |
---|
417 | display('last background image will be substracted') |
---|
418 | for ifield=nbfield_series-step*halfnbaver+1:nbfield_series |
---|
419 | Acor=double(Ak(:,:,ifield-nbfield_series+step*(2*halfnbaver+1)))-double(B); |
---|
420 | Acor=(Acor>0).*Acor; % put to 0 the negative elements in Acor |
---|
421 | ifile=indselect(ifield); |
---|
422 | if ~isempty(j1_series{1}) |
---|
423 | j1=j1_series{1}(ifile); |
---|
424 | end |
---|
425 | newname=fullfile_uvmat(RootPath{1},OutputDir,RootFile{1},FileExtOut,NomTypeOut,i1_series{1}(ifile),[],j1); |
---|
426 | %write result file |
---|
427 | if ~isequal(Param.ActionInput.SaturationCoeff,0) |
---|
428 | C=levels(Acor,Param.ActionInput.CheckSubmedian,Param.ActionInput.SaturationCoeff); |
---|
429 | imwrite(C,newname,'BitDepth',16); % save the new image |
---|
430 | else |
---|
431 | if isequal(FileInfo{1}.BitDepth,16) |
---|
432 | C=uint16(Acor); |
---|
433 | imwrite(C,newname,'BitDepth',16); % save the new image |
---|
434 | else |
---|
435 | C=uint8(Acor); |
---|
436 | imwrite(C,newname,'BitDepth',8); % save the new image |
---|
437 | end |
---|
438 | end |
---|
439 | display([newname ' written']) |
---|
440 | end |
---|
441 | end |
---|
442 | |
---|
443 | function C=levels(A,CheckSubmedian,Coeff) |
---|
444 | |
---|
445 | nblock_y=100;%2*Param.TransformInput.BlockSize; |
---|
446 | nblock_x=100;%2*Param.TransformInput.BlockSize; |
---|
447 | [npy,npx]=size(A); |
---|
448 | [X,Y]=meshgrid(1:npx,1:npy); |
---|
449 | |
---|
450 | %Backg=zeros(size(A)); |
---|
451 | %Aflagmin=sparse(imregionalmin(A));%Amin=1 for local image minima |
---|
452 | %Amin=A.*Aflagmin;%values of A at local minima |
---|
453 | % local background: find all the local minima in image subblocks |
---|
454 | if CheckSubmedian |
---|
455 | fctblock= inline('median(x(:))'); |
---|
456 | Backg=blkproc(A,[nblock_y nblock_x],fctblock);% take the median in blocks |
---|
457 | %B=imresize(Backg,size(A),'bilinear');% interpolate to the initial size image |
---|
458 | A=A-imresize(Backg,size(A),'bilinear');% substract background interpolated to the initial size image |
---|
459 | end |
---|
460 | fctblock= inline('mean(x(:))'); |
---|
461 | AMean=blkproc(A,[nblock_y nblock_x],fctblock);% take the mean in blocks |
---|
462 | fctblock= inline('var(x(:))'); |
---|
463 | AVar=blkproc(A,[nblock_y nblock_x],fctblock);% take the mean in blocks |
---|
464 | Avalue=AVar./AMean;% typical value of particle luminosity |
---|
465 | Avalue=imresize(Avalue,size(A),'bilinear');% interpolate to the initial size image |
---|
466 | C=uint16(1000*tanh(A./(Coeff*Avalue))); |
---|