source: trunk/src/series/sub_background.m @ 606

Last change on this file since 606 was 605, checked in by sommeria, 11 years ago

bugs corrected to get an advancement bar with status

File size: 21.3 KB
Line 
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    % The processing can be done over groups of nbfield2 consecutive files in slices (parameter NbSlice)
13    % In the mode 'volume', nbfield2=1 (1 image at each level)and NbSlice (=nbfield_j)
14    % Else nbfield2=nbfield_j =nbre of images in a burst (j index)
15   
16% function GUI_config=sub_background(Param)
17%
18%%%%%%%%%%% GENERAL TO ALL SERIES ACTION FCTS %%%%%%%%%%%%%%%%%%%%%%%%%%%
19%
20%OUTPUT
21% ParamOut: sets options in the GUI series.fig needed for the function
22%
23%INPUT:
24% In run mode, the input parameters are given as a Matlab structure Param copied from the GUI series.
25% In batch mode, Param is the name of the corresponding xml file containing the same information
26% when Param.Action.RUN=0 (as activated when the current Action is selected
27% in series), the function ouput paramOut set the activation of the needed GUI elements
28%
29% Param contains the elements:(use the menu bar command 'export/GUI config' in series to
30% see the current structure Param)
31%    .InputTable: cell of input file names, (several lines for multiple input)
32%                      each line decomposed as {RootPath,SubDir,Rootfile,NomType,Extension}
33%    .OutputSubDir: name of the subdirectory for data outputs
34%    .OutputDirExt: directory extension for data outputs
35%    .Action: .ActionName: name of the current activated function
36%             .ActionPath:   path of the current activated function
37%             .ActionExt: fct extension ('.m', Matlab fct, '.sh', compiled   Matlab fct
38%             .RUN =0 for GUI input, =1 for function activation
39%             .RunMode='local','background', 'cluster': type of function  use
40%             
41%    .IndexRange: set the file or frame indices on which the action must be performed
42%    .FieldTransform: .TransformName: name of the selected transform function
43%                     .TransformPath:   path  of the selected transform function
44%    .InputFields: sub structure describing the input fields withfields
45%              .FieldName: name(s) of the field
46%              .VelType: velocity type
47%              .FieldName_1: name of the second field in case of two input series
48%              .VelType_1: velocity type of the second field in case of two input series
49%              .Coord_y: name of y coordinate variable
50%              .Coord_x: name of x coordinate variable
51%    .ProjObject: %sub structure describing a projection object (read from ancillary GUI set_object)
52%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
53   
54function ParamOut=sub_background (Param)
55
56%% input preparation mode (no RUN)
57if isstruct(Param) && isequal(Param.Action.RUN,0)
58    ParamOut.AllowInputSort='off';% allow alphabetic sorting of the list of input file SubDir (options 'off'/'on', 'off' by default)
59    ParamOut.WholeIndexRange='on';% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default)
60    ParamOut.NbSlice='on'; %nbre of slices ('off' by default)
61    ParamOut.VelType='off';% menu for selecting the velocity type (options 'off'/'one'/'two',  'off' by default)
62    ParamOut.FieldName='off';% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default)
63    ParamOut.FieldTransform = 'off';%can use a transform function
64    ParamOut.ProjObject='off';%can use projection object(option 'off'/'on',
65    ParamOut.Mask='off';%can use mask option   (option 'off'/'on', 'off' by default)
66    ParamOut.OutputDirExt='.sback';%set the output dir extension
67    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
68    filecell=get_file_series(Param);%check existence of the first input file
69    %%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%%
70   
71    %% root input file(s) and type
72    [filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
73    if ~exist(filecell{1,1},'file')
74        msgbox_uvmat('WARNING','the first input file does not exist')
75        return
76    end
77    %%%%%%%%%%%%
78    % The cell array filecell is the list of input file names, while
79    % filecell{iview,fileindex}:
80    %        iview: line in the table corresponding to a given file series
81    %        fileindex: file index within  the file series,
82    % 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
83    % i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices
84    %%%%%%%%%%%%
85    NbSlice=1;%default
86    if isfield(Param.IndexRange,'NbSlice')&&~isempty(Param.IndexRange.NbSlice)
87        NbSlice=Param.IndexRange.NbSlice;
88    end
89    nbview=numel(i1_series);%number of input file series (lines in InputTable)
90    nbfield_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices)
91    nbfield_i=size(i1_series{1},2); %nb of fields for the i index
92    nbfield=nbfield_j*nbfield_i; %total number of fields
93    nbfield_i=floor(nbfield/NbSlice);%total number of  indexes in a slice (adjusted to an integer number of slices)
94    nbfield=nbfield_i*NbSlice; %total number of fields after adjustement
95   
96    %determine the file type on each line from the first input file
97
98   
99    %% calibration data and timing: read the ImaDoc files
100    %not relevant here
101   
102    %% check coincidence in time for several input file series
103    %not relevant here
104   
105    %% coordinate transform or other user defined transform
106    %not relevant here
107   
108    %%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
109    % EDIT FROM HERE
110   
111    %% check the validity of  input file types
112    ImageTypeOptions={'image','multimage','mmreader','video'};%allowed input file types(images)
113    FileType=get_file_type(filecell{1,1});
114    CheckImage=~isempty(find(strcmp(FileType,ImageTypeOptions), 1));% =1 for images
115    if ~CheckImage
116        msgbox_uvmat('ERROR',['invalid file type input: ' FileType{1} ' not an image'])
117        return
118    end
119   
120    %% Set field names and velocity types
121    %not relevant here
122   
123    %% Initiate output fields
124    %not relevant here
125   
126    %%% SPECIFIC PART BEGINS HERE
127    NbSlice=1;
128    if isfield(Param.IndexRange,'NbSlice')
129        NbSlice=Param.IndexRange.NbSlice; %number of slices
130    end
131    %siz=size(i1_series);
132    nbaver_init=23;%approximate number of images used for the sliding background: to be adjusted later to include an integer number of bursts
133    j1=[];%default
134   
135    %% adjust the proposed number of images in the sliding average to include an integer number of bursts
136    if nbfield_i~=1
137        nbaver=floor(nbaver_init/nbfield_j); % number of bursts used for the sliding background,
138        if isequal(floor(nbaver/2),nbaver)
139            nbaver=nbaver+1;%put the number of burst to an odd number (so the middle burst is defined)
140        end
141        nbaver_init=nbaver*nbfield_j;%propose by default an integer number of bursts
142    end
143   
144    %% input of specific parameters
145    %if checkrun %get specific parameters interactively
146    if isequal(Param.Action.RUN,0)
147        prompt = {'volume scan mode (Yes/No)';'Number of images for the sliding background (MUST FIT IN COMPUTER MEMORY)';...
148            'the luminosity rank chosen to define the background (0.1=for dense particle seeding, 0.5 (median) for sparse particles'};
149        dlg_title = 'get (slice by slice) a sliding background and substract to each image';
150        num_lines= 3;
151        def     = { 'No';num2str(nbaver_init);'0.1'};
152        answer = inputdlg(prompt,dlg_title,num_lines,def);
153       
154        %check input consistency
155        if strcmp(answer{1},'No') && ~isequal(NbSlice,1)
156            check=msgbox_uvmat('INPUT_Y-N',['confirm the multi-level splitting into ' num2str(NbSlice) ' slices']);
157            if ~strcmp(check,'Yes')
158                return
159            end
160        end
161        if strcmp(answer{1},'Yes')
162            step=1;
163        else
164            step=nbfield_j;%case of bursts: the sliding background is shifted by the length of one burst
165        end
166        nbaver_ima=str2num(answer{2});%number of images for the sliding background
167        nbaver=ceil(nbaver_ima/step);%number of bursts for the sliding background
168        if isequal(floor(nbaver/2),nbaver)
169            nbaver=nbaver+1;%set the number of bursts to an odd number (so the middle burst is defined)
170        end
171        nbaver_ima=nbaver*step;
172    end
173    ParamOut.ActionInput.CheckVolume=strcmp(answer{1},'Yes');
174    ParamOut.ActionInput.SlidingSequenceLength=nbaver_ima;
175    ParamOut.ActionInput.BrightnessRankThreshold=str2num(answer{3});
176   
177    % apply the image rescaling function 'level' (avoid the blinking effects of bright particles)
178    answer=msgbox_uvmat('INPUT_Y-N','apply image rescaling function levels.m after sub_background');
179    ParamOut.ActionInput.CheckLevelTransform=strcmp(answer,'Yes');
180    return
181end
182%%%%%%%%%%%%%%%%%%%%%%  STOP HERE FOR PAMETER INPUT MODE  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183
184%% read input parameters from an xml file if input is a file name (batch mode)
185checkrun=1;
186if ischar(Param)
187    Param=xml2struct(Param);% read Param as input file (batch case)
188    checkrun=0;
189end
190
191%% Input preparation
192nbaver_ima=Param.ActionInput.SlidingSequenceLength;
193NbSlice=Param.IndexRange.NbSlice;
194if ~isequal(NbSlice,1)
195    display(['multi-level splitting into ' num2str(NbSlice) ' slices']);
196end
197RootPath=Param.InputTable(:,1);
198RootFile=Param.InputTable(:,3);
199SubDir=Param.InputTable(:,2);
200NomType=Param.InputTable(:,4);
201FileExt=Param.InputTable(:,5);
202[filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
203[FileType{1},FileInfo{1},MovieObject{1}]=get_file_type(filecell{1,1});
204    if ~isempty(j1_series{1})
205        frame_index{1}=j1_series{1};
206    else
207        frame_index{1}=i1_series{1};
208    end
209nbfield_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices)
210nbfield_i=size(i1_series{1},2); %nb of fields for the i index
211nbfield=nbfield_j*nbfield_i; %total number of fields
212nbfield_i=floor(nbfield/NbSlice);%total number of  indexes in a slice (adjusted to an integer number of slices)
213nbfield=nbfield_i*NbSlice; %total number of fields after adjustement
214
215%% Output
216FileExtOut='.png'; % write result as .png images for image inputs
217if strcmp(lower(NomType{1}(end)),'a')
218    NomTypeOut=NomType{1};%case of letter appendix
219elseif isempty(j1_series)
220    NomTypeOut='_1';
221else
222    NomTypeOut='_1_1';% caseof purely numerical indexing
223end
224
225OutputDir=[Param.OutputSubDir Param.OutputDirExt];
226
227if isequal(Param.ActionInput.CheckVolume,1)
228    step=1;
229else
230    step=nbfield_j;%case of bursts: the sliding background is shifted by the length of one burst
231end
232nbaver_ima=Param.ActionInput.SlidingSequenceLength;%number of images for the sliding background
233nbaver=ceil(nbaver_ima/step);%number of bursts for the sliding background
234if isequal(floor(nbaver/2),nbaver)
235    nbaver=nbaver+1;%set the number of bursts to an odd number (so the middle burst is defined)
236end
237nbaver_ima=nbaver*step;
238if nbaver_ima > nbfield
239    display('number of images in a slice smaller than the proposed number of images for the sliding average')
240    return
241end
242
243% calculate absolute brightness rank
244rank=floor(Param.ActionInput.BrightnessRankThreshold*nbaver_ima);
245if rank==0
246    rank=1;%rank selected in the sorted image series
247end
248
249%% prealocate memory for the sliding background
250try
251    Afirst=read_image(filecell{1,1},FileType{1},MovieObject{1},frame_index{1}(1));
252    [npy,npx]=size(Afirst);
253    if strcmp(class(Afirst),'uint8') % case of 8bit images
254        Ak=zeros(npy,npx,nbaver_ima,'uint8'); %prealocate memory
255        Asort=zeros(npy,npx,nbaver_ima,'uint8'); %prealocate memory
256    else
257        Ak=zeros(npy,npx,nbaver_ima,'uint16'); %prealocate memory
258        Asort=zeros(npy,npx,nbaver_ima,'uint16'); %prealocate memory
259    end
260catch ME
261    msgbox_uvmat('ERROR',['sub_background/read_image/' ME.message])
262    return
263end
264
265%% update the xml file
266% SubDirBase=regexprep(Param.InputTable{1,2},'\..*','');%take the root part of SubDir, before the first dot '.'
267% filexml=fullfile(RootPath{1},[SubDirBase '.xml']);
268% if ~exist(filexml,'file') && exist([fullfile(RootPath{1},SubDir{1},RootFile{1}) '.xml'],'file')% xml inside the image directory
269%     copyfile([filebase '.xml'],filexml);% copy the .xml file
270% end
271% if exist(filexml,'file')
272%     t=xmltree(filexml); 
273%     %update information on the first image name in the series
274%     uid_Heading=find(t,'ImaDoc/Heading');
275%     if isempty(uid_Heading)
276%         [t,uid_Heading]=add(t,1,'element','Heading');
277%     end
278%     uid_ImageName=find(t,'ImaDoc/Heading/ImageName');
279%     if ~isempty(j1_series{1})
280%         j1=j1_series{1}(1);
281%     end
282%     ImageName=fullfile_uvmat([dir_images term],'',RootFile{1},'.png',NomType,i1_series(1,1),[],j1);
283%     [pth,ImageName]=fileparts(ImageName);
284%     ImageName=[ImageName '.png'];
285%     if isempty(uid_ImageName)
286%         [t,uid_ImageName]=add(t,uid_Heading,'element','ImageName');
287%     end
288%     uid_value=children(t,uid_ImageName);
289%     if isempty(uid_value)
290%         t=add(t,uid_ImageName,'chardata',ImageName);%indicate  name of the first image, with ;png extension
291%     else
292%         t=set(t,uid_value(1),'value',ImageName);%indicate  name of the first image, with ;png extension
293%     end
294%     
295%     %add information about image transform
296%     [t,new_uid]=add(t,1,'element','ImageTransform');
297%     [t,NameFunction_uid]=add(t,new_uid,'element','NameFunction');
298%     [t]=add(t,NameFunction_uid,'chardata','sub_background');
299%     if GUI_config.CheckLevel
300%         [t,NameFunction_uid]=add(t,new_uid,'element','NameFunction');
301%         [t]=add(t,NameFunction_uid,'chardata','levels');
302%     end
303%     [t,NbSlice_uid]=add(t,new_uid,'element','NbSlice');
304%     [t]=add(t,new_uid,'chardata',num2str(NbSlice));
305%     [t,NbSlidingImages_uid]=add(t,new_uid,'element','NbSlidingImages');
306%     [t]=add(t,NbSlidingImages_uid,'chardata',num2str(nbaver));
307%     [t,LuminosityRank_uid]=add(t,new_uid,'element','RankBackground');
308%     [t]=add(t,LuminosityRank_uid,'chardata',num2str(rank));% luminosity rank almong the nbaver sliding images
309%     save(t,filexml)
310% end
311%copy the mask
312% if exist([filebase '_1mask_1'],'file')
313%     copyfile([filebase '_1mask_1'],[filebase_b '_1mask_1']);% copy the mask file
314% end
315
316%MAIN LOOP ON SLICES
317for islice=1:NbSlice
318    %% select the series of image indices at the level islice
319    indselect=islice:NbSlice*step:nbfield;% select file indices of the slice
320    for ifield=1:step-1
321        indselect=[indselect;indselect(end,:)+1];
322    end
323   
324    %% read the first series of nbaver_ima images and sort by luminosity at each pixel
325    for ifield = 1:nbaver_ima
326        ifile=indselect(ifield);
327        filename=filecell{1,ifile};
328        Aread=read_image(filename,FileType{1},MovieObject{1},frame_index{1}(ifile));
329        if ndims(Aread)==3;%color images
330            Aread=sum(double(Aread),3);% take the sum of color components
331        end
332        Ak(:,:,ifield)=Aread;
333    end
334    Asort=sort(Ak,3);%sort the luminosity of images at each point
335    B=Asort(:,:,rank);%background image
336    display( 'first background image will be substracted')
337    nbfirst=(ceil(nbaver/2))*step;
338    for ifield=1:nbfirst
339        Acor=double(Ak(:,:,ifield))-double(B);%substract background to the current image
340        Acor=(Acor>0).*Acor; % put to 0 the negative elements in Acor
341        ifile=indselect(ifield);
342        if ~isempty(j1_series{1})
343            j1=j1_series{1}(ifile);
344        end
345        newname=fullfile_uvmat(RootPath{1},OutputDir,RootFile{1},FileExtOut,NomTypeOut,i1_series{1}(ifile),[],j1);
346       
347        %write result file
348        if Param.ActionInput.CheckLevelTransform
349            C=levels(Acor);
350            imwrite(C,newname,'BitDepth',8); % save the new image
351        else
352            if isequal(FileInfo{1}.BitDepth,16)
353                C=uint16(Acor);
354                imwrite(C,newname,'BitDepth',16); % save the new image
355            else
356                C=uint8(Acor);
357                imwrite(C,newname,'BitDepth',8); % save the new image
358            end
359        end
360        display([newname ' written'])
361    end
362   
363    %% repeat the operation on a sliding series of images
364    display('sliding background image will be substracted')
365    if nbfield_i > nbaver_ima
366        for ifield = step*ceil(nbaver/2)+1:step:nbfield_i-step*floor(nbaver/2)
367            if checkrun
368                stopstate=get(Param.RUNHandle,'BusyAction');
369                update_waitbar(Param.WaitbarHandle,(ifield+(islice-1)*nbfield_i)/(nbfield_i*NbSlice))
370            else
371                stopstate='queue';
372            end
373            if isequal(stopstate,'queue')% enable STOP command
374                Ak(:,:,1:nbaver_ima-step)=Ak(:,:,1+step:nbaver_ima);% shift the current image series by one burst (step)
375                %incorporate next burst in the current image series
376                for iburst=1:step
377                    ifile=indselect(ifield+step*floor(nbaver/2)+iburst-1);
378                    filename=fullfile_uvmat(RootPath{1},SubDir{1},RootFile{1},FileExt{1},NomType{1},i1_series{1}(ifile),[],j1_series{1}(ifile));
379                    Aread=read_image(filename,FileType{1},MovieObject{1},i1_series{1}(ifile));
380                    if ndims(Aread)==3;%color images
381                        Aread=sum(double(Aread),3);% take the sum of color components
382                    end
383                    Ak(:,:,nbaver_ima-step+iburst)=Aread;
384                end
385                Asort=sort(Ak,3);%sort the new current image series by luminosity
386                B=Asort(:,:,rank);%current background image
387                for iburst=1:step
388                    index=step*floor(nbaver/2)+iburst;
389                    Acor=double(Ak(:,:,index))-double(B);
390                    Acor=(Acor>0).*Acor; % put to 0 the negative elements in Acor
391                    ifile=indselect(ifield+iburst-1);
392                    if ~isempty(j1_series{1})
393                        j1=j1_series{1}(ifile);
394                    end
395                    newname=fullfile_uvmat(RootPath{1},OutputDir,RootFile{1},FileExtOut,NomTypeOut,i1_series{1}(ifile),[],j1);
396                    %write result file
397                    if Param.ActionInput.CheckLevelTransform
398                        C=levels(Acor);
399                        imwrite(C,newname,'BitDepth',8); % save the new image
400                    else
401                        if isequal(FileInfo{1}.BitDepth,16)
402                            C=uint16(Acor);
403                            imwrite(C,newname,'BitDepth',16); % save the new image
404                        else
405                            C=uint8(Acor);
406                            imwrite(C,newname,'BitDepth',8); % save the new image
407                        end
408                    end
409                    display([newname ' written'])
410                   
411                end
412            else
413                return
414            end
415        end
416    end
417   
418    %% substract the background from the last images
419    display('last background image will be substracted')
420    ifield=nbfield_i-(step*ceil(nbaver/2))+1:nbfield_i;
421    for ifield=nbfield_i-(step*floor(nbaver/2))+1:nbfield_i
422        index=ifield-nbfield_i+step*(2*floor(nbaver/2)+1);
423        Acor=double(Ak(:,:,index))-double(B);
424        Acor=(Acor>0).*Acor; % put to 0 the negative elements in Acor
425        ifile=indselect(ifield);
426        if ~isempty(j1_series{1})
427            j1=j1_series{1}(ifile);
428        end
429        newname=fullfile_uvmat(RootPath{1},OutputDir,RootFile{1},FileExtOut,NomTypeOut,i1_series{1}(ifile),[],j1);
430       
431        %write result file
432        if Param.ActionInput.CheckLevelTransform
433            C=levels(Acor);
434            imwrite(C,newname,'BitDepth',8); % save the new image
435        else
436            if isequal(FileInfo{1}.BitDepth,16)
437                C=uint16(Acor);
438                imwrite(C,newname,'BitDepth',16); % save the new image
439            else
440                C=uint8(Acor);
441                imwrite(C,newname,'BitDepth',8); % save the new image
442            end
443        end
444        display([newname ' written'])
445    end
446end
447
448
449function C=levels(A)
450%whos A;
451B=double(A(:,:,1));
452windowsize=round(min(size(B,1),size(B,2))/20);
453windowsize=floor(windowsize/2)*2+1;
454ix=1/2-windowsize/2:-1/2+windowsize/2;%
455%del=np/3;
456%fct=exp(-(ix/del).^2);
457fct2=cos(ix/(windowsize-1)/2*pi/2);
458%Mfiltre=(ones(5,5)/5^2);
459%Mfiltre=fct2';
460Mfiltre=fct2'*fct2;
461Mfiltre=Mfiltre/(sum(sum(Mfiltre)));
462
463C=filter2(Mfiltre,B);
464C(:,1:windowsize)=C(:,windowsize)*ones(1,windowsize);
465C(:,end-windowsize+1:end)=C(:,end-windowsize+1)*ones(1,windowsize);
466C(1:windowsize,:)=ones(windowsize,1)*C(windowsize,:);
467C(end-windowsize+1:end,:)=ones(windowsize,1)*C(end-windowsize,:);
468C=tanh(B./(2*C));
469[n,c]=hist(reshape(C,1,[]),100);
470% figure;plot(c,n);
471
472[m,i]=max(n);
473c_max=c(i);
474[dummy,index]=sort(abs(c-c(i)));
475n=n(index);
476c=c(index);
477i_select = find(cumsum(n)<0.95*sum(n));
478if isempty(i_select)
479    i_select = 1:length(c);
480end
481c_select=c(i_select);
482n_select=n(i_select);
483cmin=min(c_select);
484cmax=max(c_select);
485C=(C-cmin)/(cmax-cmin)*256;
486C=uint8(C);
Note: See TracBrowser for help on using the repository browser.