source: trunk/src/series/calc_background.m @ 239

Last change on this file since 239 was 239, checked in by sommeria, 13 years ago

sub_background improved (memory allocation) and calc_background created (global backgound)

File size: 8.6 KB
Line 
1%'sub_background': substract background to an image series, used with series.fig
2%------------------------------------------------------------------------
3% function GUI_input=aver_stat(num_i1,num_i2,num_j1,num_j2,Series)
4%
5%OUTPUT
6% GUI_input=list of options in the GUI series.fig needed for the function
7%
8%INPUT:
9%num_i1: series of first indices i (given from the series interface as first_i:incr_i:last_i, mode and list_pair_civ)
10%num_i2: series of second indices i (given from the series interface as first_i:incr_i:last_i, mode and list_pair_civ)
11%num_j1: series of first indices j (given from the series interface as first_j:incr_j:last_j, mode and list_pair_civ )
12%num_j2: series of second indices j (given from the series interface as first_j:incr_j:last_j, mode and list_pair_civ)
13%Series: Matlab structure containing information set by the series interface
14%       .RootPath: path to the image series
15%       .RootFile: root file name
16%       .FileExt: image file extension
17%       .NomType: nomenclature type for file indexing
18%       .NbSlice: %number of slices defined on the interface
19%----------------------------------------------------------------------
20% Method:
21    %calculate the background image by sorting the luminosity of each point
22    % over a sliding sub-sequence of 'nbaver_ima' images.
23    % The luminosity value of rank 'rank' is selected as the
24    % 'background'. rank=nbimages/2 gives the median value.  Smaller values are appropriate
25    % for a dense set of particles. The extrem value rank=1 gives the true minimum
26    % luminosity, but it can be polluted by noise.
27% Organization of image indices:
28    % The program is working on a series of images, labelled by two indices i and j, given
29    % by the input matlab vectors num_i1 and num_j1 respectively. In the list, j is the fastest increasing index.
30    % The processing can be done in slices (number nbslice), with bursts of
31    % nbfield2 successive images for a given slice (mode 'multilevel')
32    % In the mode 'volume', nbfield2=1 (1 image at each level)
33%
34function GUI_input=calc_background (num_i1,num_i2,num_j1,num_j2,Series)
35
36%------------------------------------------------------------------------
37%requests for the visibility of input windows in the GUI series  (activated directly by the selection in the menu ACTION)
38if ~exist('num_i1','var')
39    GUI_input={'RootPath';'on';...
40        'SubDir';'off';... % subdirectory of derived files (PIV fields), ('on' by default)
41        'RootFile';'on';... %root input file name ('on' by default)
42        'FileExt';'on';... %inputf file extension ('on' by default)
43        'NomType';'on';...%type of file indexing ('on' by default)
44        'NbSlice';'on'; ...%nbre of slices ('off' by default)
45        %'VelTypeMenu';'on';...% menu for selecting the velocity type (civ1,..)('off' by default)
46        %'FieldMenu';'on';...% menu for selecting the velocity field (s) in the input file ('off' by default)
47        %'VelTypeMenu_1';'on';...% menu for selecting the velocity type (civ1,..)('off' by default)
48        %'FieldMenu_1';'on';...% menu for selecting the velocity field (s) in the input file ('off' by default)
49        %'CoordType';...%can use a transform function
50        %'GetObject';...;%can use projection object
51        %'GetMask';...;%can use mask option 
52        %'PARAMETER';'NbSliding';...
53        %'PARAMETER';'VolumeScan';...
54        %'PARAMETER';'RankBrightness';...
55               ''};
56    return %exit the function
57end
58
59%----------------------------------------------------------------
60%% initiate the waitbar
61hseries=guidata(Series.hseries);%handles of the GUI series
62WaitbarPos=get(hseries.waitbar_frame,'Position');
63%-----------------------------------------------------------------
64if iscell(Series.RootPath)
65    msgbox_uvmat('ERROR','This function use only one input image series')
66    return
67end
68
69%% determine input image type
70FileType=[];%default
71MovieObject=[];
72FileExt=Series.FileExt;
73
74if isequal(lower(FileExt),'.avi')
75    hhh=which('mmreader');
76    if ~isequal(hhh,'')&& mmreader.isPlatformSupported()
77        MovieObject=mmreader(fullfile(RootPath,[RootFile FileExt]));
78        FileType='movie';
79    else
80        FileType='avi';
81    end
82elseif isequal(lower(FileExt),'.vol')
83    FileType='vol';
84else
85   form=imformats(FileExt(2:end));
86   if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
87       if isequal(Series.NomType,'*');
88           FileType='multimage';
89       else
90           FileType='image';
91       end
92   end
93end
94if isempty(FileType)
95    msgbox_uvmat('ERROR',['invalid file extension ' FileExt ': this function only accepts image or movie input'])
96    return
97end
98
99nbslice_i=Series.NbSlice; %number of slices
100siz=size(num_i1);
101nbaver_init=23;%approximate number of images used for the sliding background: to be adjusted later to include an integer number of bursts
102
103filebase=fullfile(Series.RootPath,Series.RootFile);
104dir_images=Series.RootPath;
105nom_type=Series.NomType;
106
107%% create dir of the new image
108% [dir_images,namebase]=fileparts(filebase);
109term='_background';
110
111[pp,subdir_ima]=fileparts(Series.RootPath);
112try
113    mkdir([dir_images term]);
114catch ME
115            msgbox_uvmat('ERROR',ME.message);
116            return
117end
118[xx,msg2] = fileattrib([dir_images term],'+w','g'); %yield writing access (+w) to user group (g)
119if ~strcmp(msg2,'')
120    msgbox_uvmat('ERROR',['pb of permission for ' subdir_ima term ': ' msg2])%error message for directory creation
121    return
122end
123filebase_b=fullfile([dir_images term],Series.RootFile);
124
125%% set processing parameters
126prompt = {'The number of positions (laser slices)';'volume scan mode (Yes/No)';...
127        'the luminosity rank chosen to define the background (0.1=for dense particle seeding, 0.5 (median) for sparse particles'};
128dlg_title = ['get (slice by slice) a  background , result in subdir ' subdir_ima term];
129num_lines= 3;
130def     = {'1';'No';'0.1'};
131answer = inputdlg(prompt,dlg_title,num_lines,def);
132vol_test=answer{2};
133if isequal(vol_test,'Yes')
134    nbfield2=1;%case of volume: no consecutive series at a given level
135    nbslice_i=siz(1);%number of slices
136else
137    nbfield2=siz(1); %nb of consecutive images at each level(burst)
138%     if siz(2)>1
139%        nbslice_i=str2num(answer{1})/(num_i1(1,2)-num_i1(1,1));% number of slices
140%     else
141        nbslice_i=1;
142%     end
143%     if ~isequal(floor(nbslice_i),nbslice_i)
144%         msgbox_uvmat('ERROR','the number of slices must be a multiple of the i increment')
145%         return
146%     end
147end
148lengthtot=siz(1)*siz(2);
149nbfield=floor(lengthtot/(nbfield2*nbslice_i));%total number of i indexes (adjusted to an integer number of slices)
150nbfield_slice=nbfield*nbfield2;% number of fields per slic
151rank=floor(str2num(answer{3})*nbfield_slice);
152if rank==0
153    rank=1;%rank selected in the sorted image series
154end
155
156%% prealocate memory for the background
157first_image=name_generator(filebase,num_i1(1),num_j1(1),Series.FileExt,Series.NomType);
158Afirst=read_image(first_image,FileType,num_i1(1),MovieObject);
159[npy,npx]=size(Afirst);
160try
161    Ak=zeros(npy,npx,nbfield_slice,'uint16'); %prealocate memory
162catch ME
163    msgbox_uvmat('ERROR',ME.message)
164    return
165end
166
167
168%MAIN LOOP ON SLICES
169%
170% nbfield_slice
171% nbslice_i
172% for islice=1:nbslice_i
173%     %% select the series of image indices at the level islice
174%     for ifield=1:nbfield
175%         for iburst=1:nbfield2
176%             indselect(iburst,ifield)=((ifield-1)*nbslice_i+(islice-1))*nbfield2+iburst;
177%         end
178%     end
179%     
180    %% read the first series of nbaver_ima images and sort by luminosity at each pixel
181    for ifile = 1:nbfield_slice
182%         ifile=indselect(ifield);
183        filename=name_generator(filebase,num_i1(ifile),num_j1(ifile),Series.FileExt,Series.NomType)
184        Aread=read_image(filename,FileType,num_i1(ifile),MovieObject);
185        Ak(:,:,ifile)=Aread;
186            %finish the waitbar
187        update_waitbar(hseries.waitbar,WaitbarPos,1)
188    end
189    Ak=sort(Ak,3);
190    C=Ak(:,:,rank);
191    [newname]=...
192        name_generator(filebase_b,num_i1(1),num_j1(1),'.png','none') % makes the new file name 
193    imwrite(C,newname,'BitDepth',16); % save the new image
194% end   
195   
196
197
198
199%------------------------------------------------------------------------
200%--read images and convert them to the uint16 format used for PIV
201function A=read_image(filename,type_ima,num,MovieObject)
202%------------------------------------------------------------------------
203%num is the view number needed for an avi movie
204switch type_ima
205    case 'movie'
206        A=read(MovieObject,num);
207    case 'avi'
208        mov=aviread(filename,num);
209        A=frame2im(mov(1));
210    case 'multimage'
211        A=imread(filename,num);
212    case 'image'   
213        A=imread(filename);
214end
215siz=size(A);
216if length(siz)==3;%color images
217    A=sum(double(A),3);
218end
219   
Note: See TracBrowser for help on using the repository browser.