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

Last change on this file since 38 was 38, checked in by sommeria, 14 years ago

field transforms put in subdir transform_field. cleaning of obsolete functions

File size: 14.2 KB
Line 
1%----------------------------------------------------------------------
2% --- substract background to the images: rank the images by luminosity at each point and substracts to the images
3%----------------------------------------------------------------------
4% Method:
5    %calculate the background image by sorting the luminosity of each point
6    % over a sliding sub-sequence of 'nbaver_ima' images.
7    % The luminosity value of rank 'rank' is selected as the
8    % 'background'. rank=nbimages/2 gives the median value.  Smaller values are appropriate
9    % for a dense set of particles. The extrem value rank=1 gives the true minimum
10    % luminosity, but it can be polluted by noise.
11% Organization of image indices:
12    % The program is working on a series of images, labelled by two indices i and j, given
13    % by the input matlab vectors num_i1 and num_j1 respectively. In the list, j is the fastest increasing index.
14    % The processing can be done in slices (number nbslice), with bursts of
15    % nbfield2 successive images for a given slice (mode 'multilevel')
16    % In the mode 'volume', nbfield2=1 (1 image at each level), and
17    % nbslice=
18%INPUT:
19% num_i1: matrix of image indices i
20% num_j1: matrix of image indices j, must be the same size as num_i1
21% num_i2 and num_j2: not used for a function acting on images
22% Series: matlab structure containing parameters, as defined by the interface UVMAT/series
23%       Series.RootPath: path to the image series
24%       Series.RootFile: root file name
25%       Series.FileExt: image file extension
26%       Series.NomType: nomenclature type for file indexing
27%       Series.NbSlice: %number of slices defined on the interface
28
29function GUI_input=sub_background (num_i1,num_i2,num_j1,num_j2,Series)
30
31%------------------------------------------------------------------------
32%requests for the visibility of input windows in the GUI series  (activated directly by the selection in the menu ACTION)
33if ~exist('num_i1','var')
34    GUI_input={'RootPath';'on';...
35        'SubDir';'off';... % subdirectory of derived files (PIV fields), ('on' by default)
36        'RootFile';'on';... %root input file name ('on' by default)
37        'FileExt';'on';... %inputf file extension ('on' by default)
38        'NomType';'on';...%type of file indexing ('on' by default)
39        'NbSlice';'on'; ...%nbre of slices ('off' by default)
40        %'VelTypeMenu';'on';...% menu for selecting the velocity type (civ1,..)('off' by default)
41        %'FieldMenu';'on';...% menu for selecting the velocity field (s) in the input file ('off' by default)
42        %'VelTypeMenu_1';'on';...% menu for selecting the velocity type (civ1,..)('off' by default)
43        %'FieldMenu_1';'on';...% menu for selecting the velocity field (s) in the input file ('off' by default)
44        %'CoordType';...%can use a transform function
45        %'GetObject';...;%can use projection object
46        %'GetMask';...;%can use mask option 
47        'PARAMETER';'NbSliding';...
48        'PARAMETER';'VolumeScan';...
49        'PARAMETER';'RankBrightness';...
50               ''};
51    return %exit the function
52end
53
54%----------------------------------------------------------------
55% initiate the waitbar
56hseries=guidata(Series.hseries);%handles of the GUI series
57WaitbarPos=get(hseries.waitbar_frame,'Position');
58%-----------------------------------------------------------------
59if iscell(Series.RootPath)
60    wardlg_uvmat('This function use only one input file series','ERROR')
61end
62
63nbslice_i=Series.NbSlice; %number of slices
64siz=size(num_i1);
65nbaver_init=23;%approximate number of images used for the sliding background: to be adjusted later to include an integer number of bursts
66
67%adjust the proposed number of images in the sliding average to include an integer number of bursts
68if siz(2)~=1
69    nbaver=floor(nbaver_init/siz(1)); % number of bursts used for the sliding background,
70    if isequal(floor(nbaver/2),nbaver)
71        nbaver=nbaver+1;%put the number of burst to an odd number (so the middle burst is defined)
72    end
73    nbaver_init=nbaver*siz(1);%propose by default an integer number of bursts
74end
75
76filebase=fullfile(Series.RootPath,Series.RootFile);
77dir_images=Series.RootPath;
78nom_type=Series.NomType;
79
80%create dir of the new images
81[dir_images,namebase]=fileparts(filebase);
82[path,subdir_ima]=fileparts(dir_images);
83curdir=pwd;
84cd(path);
85mkdir([subdir_ima '_b']);
86cd(curdir);
87filebase_b=fullfile(path,[subdir_ima '_b'],namebase);
88
89prompt = {'Number of images for the sliding background';'The number of positions (laser slices)';'volume scan mode (Yes/No)';...
90        'the luminosity rank chosen to define the background (0.1=for dense particle seeding, 0.5 (median) for sparse particles'};
91dlg_title = ['get (slice by slice) a sliding background and substract to each image, result in subdir ' subdir_ima '_b'];
92num_lines= 3;
93def     = { num2str(nbaver_init);num2str(nbslice_i);'No';'0.1'};
94answer = inputdlg(prompt,dlg_title,num_lines,def);
95set(hseries.ParamVal,'String',answer([1 [3:4]]))
96set(hseries.ParamVal,'Visible','on')
97
98nbaver_ima=str2num(answer{1});%number of images for the sliding background
99nbaver=ceil(nbaver_ima/siz(1))%number of bursts for the sliding background
100if isequal(floor(nbaver/2),nbaver)
101   nbaver=nbaver+1%put the number of burst to an odd number (so the middle burst is defined)
102end
103% if isequal(nbaver,round(nbaver))
104step=siz(1);%case of bursts: the sliding background is shifted by one burst
105% else
106%    step=1;
107% end
108vol_test=answer{3};
109if isequal(vol_test,'Yes')
110    nbfield2=1;%case of volume: no consecutive series at a given level
111    nbslice_i=siz(1);%number of slices
112else
113    nbfield2=siz(1); %nb of consecutive images at each level(burst)
114    if siz(2)>1
115       nbslice_i=str2num(answer{2})/(num_i1(1,2)-num_i1(1,1));% number of slices
116    else
117        nbslice_i=1;
118    end
119    if ~isequal(floor(nbslice_i),nbslice_i)
120        msgbox_uvmat('ERROR','the number of slices must be a multiple of the i increment')
121        return
122    end
123end
124% nbaver=floor(nbaver_init/nbfield2); % number of bursts used for the sliding background,
125% if isequal(floor(nbaver/2),nbaver)
126%     nbaver=nbaver+1;%put the number of burst to an odd number
127% end
128%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
129%nbaver_ima=nbaver*nbfield2;% adjust the number of sliding images A  REMETRE
130% if ~isequal(nbaver_ima,nbaver_init)
131%     hwarn=warndlg(['number of images in the sliding average adjusted to ' num2str(nbaver_ima)]);
132%     set(hwarn,'Units','normalized')
133%     set(hwarn,'Position',[0.3 0.3 0.4 0.1])
134% end
135%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
136rank=floor(str2num(answer{4})*nbaver_ima);
137if rank==0
138    rank=1;%rank selected in the sorted image series
139end
140lengthtot=siz(1)*siz(2);
141nbfield=floor(lengthtot/(nbfield2*nbslice_i));%total number of i indexes (adjusted to an integer number of slices)
142nbfield_slice=nbfield*nbfield2;% number of fields per slice
143% test_plot=isequal(answer{5},'Yes'); %=1 to display background images
144if nbaver_ima > nbfield*nbfield2
145    errordlg('number of images in a slice smaller than the proposed number of images for the sliding average')
146    return
147end
148nbfirst=(ceil(nbaver/2))*step;
149if nbfirst>nbaver_ima
150    nbfirst=ceil(nbaver_ima/2)
151    step=1;
152    nbaver=nbaver_ima;
153end
154
155%copy the xml file
156if exist([filebase '.xml'],'file')
157    copyfile([filebase '.xml'],[filebase_b '.xml']);% copy the .civ file
158    t=xmltree([filebase_b '.xml']);
159   
160    %update information on the first image name in the series
161    uid_Heading=find(t,'ImaDoc/Heading');
162    if isempty(uid_Heading)
163        [t,uid_Heading]=add(t,1,'element','Heading');
164    end   
165    uid_ImageName=find(t,'ImaDoc/Heading/ImageName');
166    ImageName=name_generator(filebase_b,num_i1(1),num_j1(1),'.png',Series.NomType);
167    [pth,ImageName]=fileparts(ImageName);
168    ImageName=[ImageName '.png']
169    if isempty(uid_ImageName)
170       [t,uid_ImageName]=add(t,uid_Heading,'element','ImageName');
171    end
172    uid_value=children(t,uid_ImageName);
173    if isempty(uid_value)
174        t=add(t,uid_ImageName,'chardata',ImageName)%indicate  name of the first image, with ;png extension
175    else
176        t=set(t,uid_value(1),'value',ImageName)%indicate  name of the first image, with ;png extension
177    end 
178
179    %add information about image transform
180    [t,new_uid]=add(t,1,'element','ImageTransform');
181    [t,NameFunction_uid]=add(t,new_uid,'element','NameFunction');
182    [t]=add(t,NameFunction_uid,'chardata','sub_background');     
183    [t,NbSlice_uid]=add(t,new_uid,'element','NbSlice');
184    [t]=add(t,new_uid,'chardata',num2str(nbslice_i));
185    [t,NbSlidingImages_uid]=add(t,new_uid,'element','NbSlidingImages');
186    [t]=add(t,NbSlidingImages_uid,'chardata',num2str(nbaver));
187    [t,LuminosityRank_uid]=add(t,new_uid,'element','RankBackground');
188    [t]=add(t,LuminosityRank_uid,'chardata',num2str(rank));% luminosity rank almong the nbaver sliding images
189    save(t,[filebase_b '.xml'])
190elseif exist([filebase '.civ'],'file')
191    copyfile([filebase '.civ'],[filebase_b '.civ']);% copy the .civ file
192end
193%copy the mask
194if exist([filebase '_1mask_1'],'file')
195     copyfile([filebase '_1mask_1'],[filebase_b '_1mask_1']);% copy the mask file
196end
197
198%MAIN LOOP ON SLICES
199for islice=1:nbslice_i
200    %select the series of image indices at the level islice
201    for ifield=1:nbfield
202        for iburst=1:nbfield2
203            indselect(iburst,ifield)=((ifield-1)*nbslice_i+(islice-1))*nbfield2+iburst;
204        end
205    end 
206    %read the first series of nbaver_ima images and sort by luminosity at each pixel
207    for ifield = 1:nbaver_ima
208              ifile=indselect(ifield);
209                [filename,idetect]=...
210           name_generator(filebase,num_i1(ifile),num_j1(ifile),Series.FileExt,Series.NomType);
211               Aread=imread(filename);
212               size(Aread)
213               if size(Aread,3)>1 %(color images)
214                   Aread=sum(Aread,3); %sum over color components
215               end
216                Ak(:,:,ifield)=Aread;           
217    end
218    Asort=sort(Ak,3);%sort the luminosity of images at each point
219    B=Asort(:,:,rank);%background image
220%
221%     namemean=name_generator([filebase '_back'],islice,[],'.png','_i');% makes the file name
222%     imwrite(B,namemean,'BitDepth',16); % save the first background image
223%     ['background image for slice ' num2str(islice) ' saved in ' namemean]
224    %substract the background from each of the first images and save the new images
225%     for ifield=1:floor(nbaver_ima/2)+1
226    'first background image will be substracted'
227
228    for ifield=1:nbfirst
229            Acor=double(Ak(:,:,ifield))-double(B);%substract background to the current image
230            Acor=(Acor>0).*Acor; % put to 0 the negative elements in Acor
231            C=uint16(Acor);% set to integer 16 bits
232            ifile=indselect(ifield);
233            newname=name_generator(filebase_b,num_i1(ifile),num_j1(ifile),'.png',nom_type)% makes the new file name
234            imwrite(C,newname,'BitDepth',16); % save the new image
235    end
236    %repeat the operation on a sliding series of nbaver*nbfield2 images
237    'sliding background image will be substracted'
238    if nbfield_slice > nbaver_ima
239%         for ifield = floor(nbaver_ima/2)+2:step:nbfield_slice-floor(nbaver_ima/2)
240        for ifield = step*ceil(nbaver/2)+1:step:nbfield_slice-step*floor(nbaver/2)
241            stopstate=get(hseries.RUN,'BusyAction');
242            if isequal(stopstate,'queue')% enable STOP command
243%                 waitbarpos(4)=((ifield+(islice-1)*nbfield_slice)/(nbfield_slice*nbslice_i))*Series.WaitbarPos(4);
244%                 waitbarpos(2)=Series.WaitbarPos(4)+Series.WaitbarPos(2)-waitbarpos(4);
245%                 set(hwaitbar,'Position',waitbarpos)
246%                 drawnow
247                update_waitbar(hseries.waitbar,WaitbarPos,(ifield+(islice-1)*nbfield_slice)/(nbfield_slice*nbslice_i))
248                (ifield+(islice-1)*nbfield_slice)/(nbfield_slice*nbslice_i)
249                Ak(:,:,[1:nbaver_ima-step])=Ak(:,:,[1+step:nbaver_ima]);% shift the current image series by one burst (step)
250                %incorporate next burst in the current image series
251                for iburst=1:step
252                    ifile=indselect(ifield+step*floor(nbaver/2)+iburst-1);
253                    [filename,idetect]=...
254                       name_generator(filebase,num_i1(ifile),num_j1(ifile),Series.FileExt,Series.NomType);
255                    Aread=imread(filename);
256                    if size(Aread,3)>1 %(color images)
257                       Aread=sum(Aread,3); %sum over color components
258                    end
259                    Ak(:,:,nbaver_ima-step+iburst)=Aread;
260                end
261                Asort=sort(Ak,3);%sort the new current image series by luminosity
262                B=Asort(:,:,rank);%current background image
263                for iburst=1:step
264%                     Acor=double(Ak(:,:,floor(nbaver_ima/2)+iburst-1))-double(B);
265                    index=step*floor(nbaver/2)+iburst;
266                    Acor=double(Ak(:,:,index))-double(B);
267                    Acor=(Acor>0).*Acor; % put to 0 the negative elements in Acor
268                    C=uint16(Acor);
269                    ifile=indselect(ifield+iburst-1);
270                    [newname]=...
271                       name_generator(filebase_b,num_i1(ifile),num_j1(ifile),'.png',Series.NomType)
272%                     newname=name_generator(filebase_b,num_i1(indselect(ifield+iburst-1)),num_j1(indselect(ifield+iburst-1)),'.png',nom_type)% makes the new file name
273                    imwrite(C,newname,'BitDepth',16); % save the new image
274                end 
275            else
276                return
277            end
278        end
279    end
280
281%substract the background from the last images
282%     for ifield=nbfield_slice-floor(nbaver_ima/2)+1:nbfield_slice
283     'last background image will be substracted'
284     ifield=nbfield_slice-(step*ceil(nbaver/2))+1:nbfield_slice;
285    for ifield=nbfield_slice-(step*floor(nbaver/2))+1:nbfield_slice 
286        index=ifield-nbfield_slice+step*(2*floor(nbaver/2)+1);
287        Acor=double(Ak(:,:,index))-double(B);
288        Acor=(Acor>0).*Acor; % put to 0 the negative elements in Acor
289        C=uint16(Acor);
290        ifile=indselect(ifield);
291        newname=name_generator(filebase_b,num_i1(ifile),num_j1(ifile),'.png',nom_type)% makes the new file name
292        imwrite(C,newname,'BitDepth',16); % save the new image
293    end
294end
295
296%finish the waitbar
297update_waitbar(hseries.waitbar,WaitbarPos,1)
298
299
300   
301   
Note: See TracBrowser for help on using the repository browser.