[239] | 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 | % |
---|
| 34 | function 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) |
---|
| 38 | if ~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 |
---|
| 57 | end |
---|
| 58 | |
---|
| 59 | %---------------------------------------------------------------- |
---|
| 60 | %% initiate the waitbar |
---|
| 61 | hseries=guidata(Series.hseries);%handles of the GUI series |
---|
| 62 | WaitbarPos=get(hseries.waitbar_frame,'Position'); |
---|
| 63 | %----------------------------------------------------------------- |
---|
| 64 | if iscell(Series.RootPath) |
---|
| 65 | msgbox_uvmat('ERROR','This function use only one input image series') |
---|
| 66 | return |
---|
| 67 | end |
---|
| 68 | |
---|
| 69 | %% determine input image type |
---|
| 70 | FileType=[];%default |
---|
| 71 | MovieObject=[]; |
---|
| 72 | FileExt=Series.FileExt; |
---|
| 73 | |
---|
| 74 | if 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 |
---|
| 82 | elseif isequal(lower(FileExt),'.vol') |
---|
| 83 | FileType='vol'; |
---|
| 84 | else |
---|
| 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 |
---|
| 93 | end |
---|
| 94 | if isempty(FileType) |
---|
| 95 | msgbox_uvmat('ERROR',['invalid file extension ' FileExt ': this function only accepts image or movie input']) |
---|
| 96 | return |
---|
| 97 | end |
---|
| 98 | |
---|
| 99 | nbslice_i=Series.NbSlice; %number of slices |
---|
| 100 | siz=size(num_i1); |
---|
| 101 | nbaver_init=23;%approximate number of images used for the sliding background: to be adjusted later to include an integer number of bursts |
---|
| 102 | |
---|
| 103 | filebase=fullfile(Series.RootPath,Series.RootFile); |
---|
| 104 | dir_images=Series.RootPath; |
---|
| 105 | nom_type=Series.NomType; |
---|
| 106 | |
---|
| 107 | %% create dir of the new image |
---|
| 108 | % [dir_images,namebase]=fileparts(filebase); |
---|
| 109 | term='_background'; |
---|
| 110 | |
---|
| 111 | [pp,subdir_ima]=fileparts(Series.RootPath); |
---|
| 112 | try |
---|
| 113 | mkdir([dir_images term]); |
---|
| 114 | catch ME |
---|
| 115 | msgbox_uvmat('ERROR',ME.message); |
---|
| 116 | return |
---|
| 117 | end |
---|
| 118 | [xx,msg2] = fileattrib([dir_images term],'+w','g'); %yield writing access (+w) to user group (g) |
---|
| 119 | if ~strcmp(msg2,'') |
---|
| 120 | msgbox_uvmat('ERROR',['pb of permission for ' subdir_ima term ': ' msg2])%error message for directory creation |
---|
| 121 | return |
---|
| 122 | end |
---|
| 123 | filebase_b=fullfile([dir_images term],Series.RootFile); |
---|
| 124 | |
---|
| 125 | %% set processing parameters |
---|
| 126 | prompt = {'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'}; |
---|
| 128 | dlg_title = ['get (slice by slice) a background , result in subdir ' subdir_ima term]; |
---|
| 129 | num_lines= 3; |
---|
| 130 | def = {'1';'No';'0.1'}; |
---|
| 131 | answer = inputdlg(prompt,dlg_title,num_lines,def); |
---|
| 132 | vol_test=answer{2}; |
---|
| 133 | if 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 |
---|
| 136 | else |
---|
| 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 |
---|
| 147 | end |
---|
| 148 | lengthtot=siz(1)*siz(2); |
---|
| 149 | nbfield=floor(lengthtot/(nbfield2*nbslice_i));%total number of i indexes (adjusted to an integer number of slices) |
---|
| 150 | nbfield_slice=nbfield*nbfield2;% number of fields per slic |
---|
| 151 | rank=floor(str2num(answer{3})*nbfield_slice); |
---|
| 152 | if rank==0 |
---|
| 153 | rank=1;%rank selected in the sorted image series |
---|
| 154 | end |
---|
| 155 | |
---|
| 156 | %% prealocate memory for the background |
---|
| 157 | first_image=name_generator(filebase,num_i1(1),num_j1(1),Series.FileExt,Series.NomType); |
---|
| 158 | Afirst=read_image(first_image,FileType,num_i1(1),MovieObject); |
---|
| 159 | [npy,npx]=size(Afirst); |
---|
| 160 | try |
---|
| 161 | Ak=zeros(npy,npx,nbfield_slice,'uint16'); %prealocate memory |
---|
| 162 | catch ME |
---|
| 163 | msgbox_uvmat('ERROR',ME.message) |
---|
| 164 | return |
---|
| 165 | end |
---|
| 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) |
---|
[245] | 184 | try |
---|
| 185 | Aread=read_image(filename,FileType,num_i1(ifile),MovieObject); |
---|
| 186 | catch ME |
---|
| 187 | msgbox_uvmat('ERROR',ME.message) |
---|
| 188 | return |
---|
| 189 | end |
---|
[239] | 190 | Ak(:,:,ifile)=Aread; |
---|
| 191 | %finish the waitbar |
---|
| 192 | update_waitbar(hseries.waitbar,WaitbarPos,1) |
---|
| 193 | end |
---|
| 194 | Ak=sort(Ak,3); |
---|
| 195 | C=Ak(:,:,rank); |
---|
| 196 | [newname]=... |
---|
| 197 | name_generator(filebase_b,num_i1(1),num_j1(1),'.png','none') % makes the new file name |
---|
| 198 | imwrite(C,newname,'BitDepth',16); % save the new image |
---|
| 199 | % end |
---|
| 200 | |
---|
| 201 | |
---|
| 202 | |
---|
| 203 | |
---|
| 204 | %------------------------------------------------------------------------ |
---|
| 205 | %--read images and convert them to the uint16 format used for PIV |
---|
| 206 | function A=read_image(filename,type_ima,num,MovieObject) |
---|
| 207 | %------------------------------------------------------------------------ |
---|
| 208 | %num is the view number needed for an avi movie |
---|
| 209 | switch type_ima |
---|
| 210 | case 'movie' |
---|
| 211 | A=read(MovieObject,num); |
---|
| 212 | case 'avi' |
---|
| 213 | mov=aviread(filename,num); |
---|
| 214 | A=frame2im(mov(1)); |
---|
| 215 | case 'multimage' |
---|
| 216 | A=imread(filename,num); |
---|
| 217 | case 'image' |
---|
| 218 | A=imread(filename); |
---|
| 219 | end |
---|
| 220 | siz=size(A); |
---|
| 221 | if length(siz)==3;%color images |
---|
| 222 | A=sum(double(A),3); |
---|
| 223 | end |
---|
| 224 | |
---|