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

Last change on this file since 84 was 54, checked in by sommeria, 15 years ago

sub_background modified to accept various movie and image inputs
geometry_calib, editxml, civ modified to read a unique PARAM.xml parameter file (instead of PARAM_LINUX...)

File size: 15.0 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    msgbox_uvmat('ERROR','This function use only one input image series')
61    return
62end
63
64%determine input image type
65FileType=[];%default
66MovieObject=[];
67if isequal(lower(FileExt),'.avi')
68    hhh=which('mmreader');
69    if ~isequal(hhh,'')&& mmreader.isPlatformSupported()
70        MovieObject=mmreader(fullfile(RootPath,[RootFile FileExt]));
71        FileType='movie';
72    else
73        FileType='avi';
74    end
75elseif isequal(lower(FileExt),'.vol')
76    FileType='vol';
77else
78   form=imformats(FileExt(2:end));
79   if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
80       if isequal(NomType,'*');
81           FileType='multimage';
82       else
83           FileType='image';
84       end
85   end
86end
87if isempty(FileType)
88    msgbox_uvmat('ERROR',['invalid file extension ' FileExt ': this function only accepts image or movie input'])
89    return
90end
91
92nbslice_i=Series.NbSlice; %number of slices
93siz=size(num_i1);
94nbaver_init=23;%approximate number of images used for the sliding background: to be adjusted later to include an integer number of bursts
95
96%adjust the proposed number of images in the sliding average to include an integer number of bursts
97if siz(2)~=1
98    nbaver=floor(nbaver_init/siz(1)); % number of bursts used for the sliding background,
99    if isequal(floor(nbaver/2),nbaver)
100        nbaver=nbaver+1;%put the number of burst to an odd number (so the middle burst is defined)
101    end
102    nbaver_init=nbaver*siz(1);%propose by default an integer number of bursts
103end
104
105filebase=fullfile(Series.RootPath,Series.RootFile);
106dir_images=Series.RootPath;
107nom_type=Series.NomType;
108
109%create dir of the new images
110[dir_images,namebase]=fileparts(filebase);
111[path,subdir_ima]=fileparts(dir_images);
112curdir=pwd;
113cd(path);
114mkdir([subdir_ima '_b']);
115cd(curdir);
116filebase_b=fullfile(path,[subdir_ima '_b'],namebase);
117
118prompt = {'Number of images for the sliding background';'The number of positions (laser slices)';'volume scan mode (Yes/No)';...
119        'the luminosity rank chosen to define the background (0.1=for dense particle seeding, 0.5 (median) for sparse particles'};
120dlg_title = ['get (slice by slice) a sliding background and substract to each image, result in subdir ' subdir_ima '_b'];
121num_lines= 3;
122def     = { num2str(nbaver_init);num2str(nbslice_i);'No';'0.1'};
123answer = inputdlg(prompt,dlg_title,num_lines,def);
124set(hseries.ParamVal,'String',answer([1 [3:4]]))
125set(hseries.ParamVal,'Visible','on')
126
127nbaver_ima=str2num(answer{1});%number of images for the sliding background
128nbaver=ceil(nbaver_ima/siz(1))%number of bursts for the sliding background
129if isequal(floor(nbaver/2),nbaver)
130   nbaver=nbaver+1%put the number of burst to an odd number (so the middle burst is defined)
131end
132% if isequal(nbaver,round(nbaver))
133step=siz(1);%case of bursts: the sliding background is shifted by one burst
134% else
135%    step=1;
136% end
137vol_test=answer{3};
138if isequal(vol_test,'Yes')
139    nbfield2=1;%case of volume: no consecutive series at a given level
140    nbslice_i=siz(1);%number of slices
141else
142    nbfield2=siz(1); %nb of consecutive images at each level(burst)
143    if siz(2)>1
144       nbslice_i=str2num(answer{2})/(num_i1(1,2)-num_i1(1,1));% number of slices
145    else
146        nbslice_i=1;
147    end
148    if ~isequal(floor(nbslice_i),nbslice_i)
149        msgbox_uvmat('ERROR','the number of slices must be a multiple of the i increment')
150        return
151    end
152end
153% nbaver=floor(nbaver_init/nbfield2); % number of bursts used for the sliding background,
154% if isequal(floor(nbaver/2),nbaver)
155%     nbaver=nbaver+1;%put the number of burst to an odd number
156% end
157%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
158%nbaver_ima=nbaver*nbfield2;% adjust the number of sliding images A  REMETRE
159% if ~isequal(nbaver_ima,nbaver_init)
160%     hwarn=warndlg(['number of images in the sliding average adjusted to ' num2str(nbaver_ima)]);
161%     set(hwarn,'Units','normalized')
162%     set(hwarn,'Position',[0.3 0.3 0.4 0.1])
163% end
164%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
165rank=floor(str2num(answer{4})*nbaver_ima);
166if rank==0
167    rank=1;%rank selected in the sorted image series
168end
169lengthtot=siz(1)*siz(2);
170nbfield=floor(lengthtot/(nbfield2*nbslice_i));%total number of i indexes (adjusted to an integer number of slices)
171nbfield_slice=nbfield*nbfield2;% number of fields per slice
172% test_plot=isequal(answer{5},'Yes'); %=1 to display background images
173if nbaver_ima > nbfield*nbfield2
174    errordlg('number of images in a slice smaller than the proposed number of images for the sliding average')
175    return
176end
177nbfirst=(ceil(nbaver/2))*step;
178if nbfirst>nbaver_ima
179    nbfirst=ceil(nbaver_ima/2)
180    step=1;
181    nbaver=nbaver_ima;
182end
183
184%copy the xml file
185if exist([filebase '.xml'],'file')
186    copyfile([filebase '.xml'],[filebase_b '.xml']);% copy the .civ file
187    t=xmltree([filebase_b '.xml']);
188   
189    %update information on the first image name in the series
190    uid_Heading=find(t,'ImaDoc/Heading');
191    if isempty(uid_Heading)
192        [t,uid_Heading]=add(t,1,'element','Heading');
193    end   
194    uid_ImageName=find(t,'ImaDoc/Heading/ImageName');
195    ImageName=name_generator(filebase_b,num_i1(1),num_j1(1),'.png',Series.NomType);
196    [pth,ImageName]=fileparts(ImageName);
197    ImageName=[ImageName '.png']
198    if isempty(uid_ImageName)
199       [t,uid_ImageName]=add(t,uid_Heading,'element','ImageName');
200    end
201    uid_value=children(t,uid_ImageName);
202    if isempty(uid_value)
203        t=add(t,uid_ImageName,'chardata',ImageName)%indicate  name of the first image, with ;png extension
204    else
205        t=set(t,uid_value(1),'value',ImageName)%indicate  name of the first image, with ;png extension
206    end 
207
208    %add information about image transform
209    [t,new_uid]=add(t,1,'element','ImageTransform');
210    [t,NameFunction_uid]=add(t,new_uid,'element','NameFunction');
211    [t]=add(t,NameFunction_uid,'chardata','sub_background');     
212    [t,NbSlice_uid]=add(t,new_uid,'element','NbSlice');
213    [t]=add(t,new_uid,'chardata',num2str(nbslice_i));
214    [t,NbSlidingImages_uid]=add(t,new_uid,'element','NbSlidingImages');
215    [t]=add(t,NbSlidingImages_uid,'chardata',num2str(nbaver));
216    [t,LuminosityRank_uid]=add(t,new_uid,'element','RankBackground');
217    [t]=add(t,LuminosityRank_uid,'chardata',num2str(rank));% luminosity rank almong the nbaver sliding images
218    save(t,[filebase_b '.xml'])
219elseif exist([filebase '.civ'],'file')
220    copyfile([filebase '.civ'],[filebase_b '.civ']);% copy the .civ file
221end
222%copy the mask
223if exist([filebase '_1mask_1'],'file')
224     copyfile([filebase '_1mask_1'],[filebase_b '_1mask_1']);% copy the mask file
225end
226
227%MAIN LOOP ON SLICES
228for islice=1:nbslice_i
229    %select the series of image indices at the level islice
230    for ifield=1:nbfield
231        for iburst=1:nbfield2
232            indselect(iburst,ifield)=((ifield-1)*nbslice_i+(islice-1))*nbfield2+iburst;
233        end
234    end 
235    %read the first series of nbaver_ima images and sort by luminosity at each pixel
236    for ifield = 1:nbaver_ima
237        ifile=indselect(ifield);
238        filename=name_generator(filebase,num_i1(ifile),num_j1(ifile),Series.FileExt,Series.NomType);
239        Aread=read_image(filename,FileType,num_i1(ifile),movieobject);
240        Ak(:,:,ifield)=Aread;           
241    end
242    Asort=sort(Ak,3);%sort the luminosity of images at each point
243    B=Asort(:,:,rank);%background image
244%
245%     namemean=name_generator([filebase '_back'],islice,[],'.png','_i');% makes the file name
246%     imwrite(B,namemean,'BitDepth',16); % save the first background image
247%     ['background image for slice ' num2str(islice) ' saved in ' namemean]
248    %substract the background from each of the first images and save the new images
249%     for ifield=1:floor(nbaver_ima/2)+1
250    'first background image will be substracted'
251
252    for ifield=1:nbfirst
253            Acor=double(Ak(:,:,ifield))-double(B);%substract background to the current image
254            Acor=(Acor>0).*Acor; % put to 0 the negative elements in Acor
255            C=uint16(Acor);% set to integer 16 bits
256            ifile=indselect(ifield);
257            newname=name_generator(filebase_b,num_i1(ifile),num_j1(ifile),'.png',nom_type)% makes the new file name
258            imwrite(C,newname,'BitDepth',16); % save the new image
259    end
260    %repeat the operation on a sliding series of nbaver*nbfield2 images
261    'sliding background image will be substracted'
262    if nbfield_slice > nbaver_ima
263%         for ifield = floor(nbaver_ima/2)+2:step:nbfield_slice-floor(nbaver_ima/2)
264        for ifield = step*ceil(nbaver/2)+1:step:nbfield_slice-step*floor(nbaver/2)
265            stopstate=get(hseries.RUN,'BusyAction');
266            if isequal(stopstate,'queue')% enable STOP command
267                update_waitbar(hseries.waitbar,WaitbarPos,(ifield+(islice-1)*nbfield_slice)/(nbfield_slice*nbslice_i))
268                (ifield+(islice-1)*nbfield_slice)/(nbfield_slice*nbslice_i)
269                Ak(:,:,[1:nbaver_ima-step])=Ak(:,:,[1+step:nbaver_ima]);% shift the current image series by one burst (step)
270                %incorporate next burst in the current image series
271                for iburst=1:step
272                    ifile=indselect(ifield+step*floor(nbaver/2)+iburst-1);
273                    filename=name_generator(filebase,num_i1(ifile),num_j1(ifile),Series.FileExt,Series.NomType);
274                    Aread=read_image(filename,FileType,num_i1(ifile),movieobject);
275                    Ak(:,:,nbaver_ima-step+iburst)=Aread;
276                end
277                Asort=sort(Ak,3);%sort the new current image series by luminosity
278                B=Asort(:,:,rank);%current background image
279                for iburst=1:step
280%                     Acor=double(Ak(:,:,floor(nbaver_ima/2)+iburst-1))-double(B);
281                    index=step*floor(nbaver/2)+iburst;
282                    Acor=double(Ak(:,:,index))-double(B);
283                    Acor=(Acor>0).*Acor; % put to 0 the negative elements in Acor
284                    C=uint16(Acor);
285                    ifile=indselect(ifield+iburst-1);
286                    [newname]=...
287                       name_generator(filebase_b,num_i1(ifile),num_j1(ifile),'.png',Series.NomType)
288%                     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
289                    imwrite(C,newname,'BitDepth',16); % save the new image
290                end 
291            else
292                return
293            end
294        end
295    end
296
297%substract the background from the last images
298%     for ifield=nbfield_slice-floor(nbaver_ima/2)+1:nbfield_slice
299     'last background image will be substracted'
300     ifield=nbfield_slice-(step*ceil(nbaver/2))+1:nbfield_slice;
301    for ifield=nbfield_slice-(step*floor(nbaver/2))+1:nbfield_slice 
302        index=ifield-nbfield_slice+step*(2*floor(nbaver/2)+1);
303        Acor=double(Ak(:,:,index))-double(B);
304        Acor=(Acor>0).*Acor; % put to 0 the negative elements in Acor
305        C=uint16(Acor);
306        ifile=indselect(ifield);
307        newname=name_generator(filebase_b,num_i1(ifile),num_j1(ifile),'.png',nom_type)% makes the new file name
308        imwrite(C,newname,'BitDepth',16); % save the new image
309    end
310end
311
312%finish the waitbar
313update_waitbar(hseries.waitbar,WaitbarPos,1)
314
315
316%------------------------------------------------------------------------
317%--read images and convert them to the uint16 format used for PIV
318function A=read_image(filename,type_ima,num,movieobject)
319%------------------------------------------------------------------------
320%num is the view number needed for an avi movie
321switch type_ima
322    case 'movie'
323        A=read(movieobject,num);
324    case 'avi'
325        mov=aviread(filename,num);
326        A=frame2im(mov(1));
327    case 'multimage'
328        A=imread(filename,num);
329    case 'image'   
330        A=imread(filename);
331end
332siz=size(A);
333if length(siz)==3;%color images
334    A=sum(double(A),3);
335end
336   
Note: See TracBrowser for help on using the repository browser.