| 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 | % In the mode 'volume', nbfield2=1 (1 image at each level)and NbSlice (=NbField_j) |
|---|
| 13 | % Else nbfield2=NbField_j =nbre of images in a burst (j index) |
|---|
| 14 | |
|---|
| 15 | % function GUI_config=sub_background(Param) |
|---|
| 16 | % |
|---|
| 17 | %%%%%%%%%%% GENERAL TO ALL SERIES ACTION FCTS %%%%%%%%%%%%%%%%%%%%%%%%%%% |
|---|
| 18 | % |
|---|
| 19 | %OUTPUT |
|---|
| 20 | % ParamOut: sets options in the GUI series.fig needed for the function |
|---|
| 21 | % |
|---|
| 22 | %INPUT: |
|---|
| 23 | % In run mode, the input parameters are given as a Matlab structure Param copied from the GUI series. |
|---|
| 24 | % In batch mode, Param is the name of the corresponding xml file containing the same information |
|---|
| 25 | % when Param.Action.RUN=0 (as activated when the current Action is selected |
|---|
| 26 | % in series), the function ouput paramOut set the activation of the needed GUI elements |
|---|
| 27 | % |
|---|
| 28 | % Param contains the elements:(use the menu bar command 'export/GUI config' in series to |
|---|
| 29 | % see the current structure Param) |
|---|
| 30 | % .InputTable: cell of input file names, (several lines for multiple input) |
|---|
| 31 | % each line decomposed as {RootPath,SubDir,Rootfile,NomType,Extension} |
|---|
| 32 | % .OutputSubDir: name of the subdirectory for data outputs |
|---|
| 33 | % .OutputDirExt: directory extension for data outputs |
|---|
| 34 | % .Action: .ActionName: name of the current activated function |
|---|
| 35 | % .ActionPath: path of the current activated function |
|---|
| 36 | % .ActionExt: fct extension ('.m', Matlab fct, '.sh', compiled Matlab fct |
|---|
| 37 | % .RUN =0 for GUI input, =1 for function activation |
|---|
| 38 | % .RunMode='local','background', 'cluster': type of function use |
|---|
| 39 | % |
|---|
| 40 | % .IndexRange: set the file or frame indices on which the action must be performed |
|---|
| 41 | % .FieldTransform: .TransformName: name of the selected transform function |
|---|
| 42 | % .TransformPath: path of the selected transform function |
|---|
| 43 | % .InputFields: sub structure describing the input fields withfields |
|---|
| 44 | % .FieldName: name(s) of the field |
|---|
| 45 | % .VelType: velocity type |
|---|
| 46 | % .FieldName_1: name of the second field in case of two input series |
|---|
| 47 | % .VelType_1: velocity type of the second field in case of two input series |
|---|
| 48 | % .Coord_y: name of y coordinate variable |
|---|
| 49 | % .Coord_x: name of x coordinate variable |
|---|
| 50 | % .ProjObject: %sub structure describing a projection object (read from ancillary GUI set_object) |
|---|
| 51 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|---|
| 52 | |
|---|
| 53 | %======================================================================= |
|---|
| 54 | % Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France |
|---|
| 55 | % http://www.legi.grenoble-inp.fr |
|---|
| 56 | % Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.fr |
|---|
| 57 | % |
|---|
| 58 | % This file is part of the toolbox UVMAT. |
|---|
| 59 | % |
|---|
| 60 | % UVMAT is free software; you can redistribute it and/or modify |
|---|
| 61 | % it under the terms of the GNU General Public License as published |
|---|
| 62 | % by the Free Software Foundation; either version 2 of the license, |
|---|
| 63 | % or (at your option) any later version. |
|---|
| 64 | % |
|---|
| 65 | % UVMAT is distributed in the hope that it will be useful, |
|---|
| 66 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 67 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 68 | % GNU General Public License (see LICENSE.txt) for more details. |
|---|
| 69 | %======================================================================= |
|---|
| 70 | |
|---|
| 71 | function ParamOut=sub_background (Param) |
|---|
| 72 | |
|---|
| 73 | %%%%%%%%%%%%%%%%% INPUT PREPARATION MODE (no RUN) %%%%%%%%%%%%%%%%% |
|---|
| 74 | if isstruct(Param) && isequal(Param.Action.RUN,0) |
|---|
| 75 | ParamOut.AllowInputSort='off';% allow alphabetic sorting of the list of input file SubDir (options 'off'/'on', 'off' by default) |
|---|
| 76 | ParamOut.WholeIndexRange='on';% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default) |
|---|
| 77 | ParamOut.NbSlice='on'; % edit box nbre of slices made active |
|---|
| 78 | ParamOut.VelType='off';% menu for selecting the velocity type (options 'off'/'one'/'two', 'off' by default) |
|---|
| 79 | ParamOut.FieldName='off';% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default) |
|---|
| 80 | ParamOut.FieldTransform = 'off';%can use a transform function (option 'off'/'on','off' by default) |
|---|
| 81 | ParamOut.ProjObject='off';%cannot use projection object(option 'off'/'on','off' by default) |
|---|
| 82 | ParamOut.Mask='off';%cannot use mask option (option 'off'/'on', 'off' by default) |
|---|
| 83 | ParamOut.OutputDirExt='.sback';%set the output dir extension |
|---|
| 84 | 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 |
|---|
| 85 | |
|---|
| 86 | %% check the validity of input file types |
|---|
| 87 | if isfield(Param,'SeriesData')&& isfield(Param.SeriesData,'FileInfo') |
|---|
| 88 | if ~strcmp(Param.SeriesData.FileInfo{1}.FieldType,'image') |
|---|
| 89 | msgbox_uvmat('ERROR','invalid file type input: not an image series') |
|---|
| 90 | return |
|---|
| 91 | end |
|---|
| 92 | end |
|---|
| 93 | |
|---|
| 94 | %% numbers of fields |
|---|
| 95 | NbSlice_i=1;%default |
|---|
| 96 | if isfield(Param.IndexRange,'NbSlice')&&~isempty(Param.IndexRange.NbSlice) |
|---|
| 97 | NbSlice_i=Param.IndexRange.NbSlice; |
|---|
| 98 | end |
|---|
| 99 | incr_j=1;%default |
|---|
| 100 | if isfield(Param.IndexRange,'incr_j')&&~isempty(Param.IndexRange.incr_j) |
|---|
| 101 | incr_j=Param.IndexRange.incr_j; |
|---|
| 102 | end |
|---|
| 103 | if isfield(Param.IndexRange,'first_j')&&~isempty(Param.IndexRange.first_j) |
|---|
| 104 | NbField_j=numel(Param.IndexRange.first_j:incr_j:Param.IndexRange.last_j);%nb of fields for the j index (bursts or volume slices) |
|---|
| 105 | else |
|---|
| 106 | NbField_j=1; |
|---|
| 107 | end |
|---|
| 108 | first_i=1;last_i=1;incr_i=1;%default |
|---|
| 109 | if isfield(Param.IndexRange,'MinIndex_i'); first_i=Param.IndexRange.MinIndex_i; end |
|---|
| 110 | if isfield(Param.IndexRange,'MaxIndex_i'); last_i=Param.IndexRange.MaxIndex_i; end |
|---|
| 111 | if isfield(Param.IndexRange,'incr_i')&&~isempty(Param.IndexRange.incr_i) |
|---|
| 112 | incr_i=Param.IndexRange.incr_i; |
|---|
| 113 | end |
|---|
| 114 | nbfield_i=numel(first_i:incr_i:last_i);%nb of fields for the i index (bursts or volume slices) |
|---|
| 115 | nbfield=NbField_j*nbfield_i; %total number of fields |
|---|
| 116 | nbfield_i=floor(nbfield/NbSlice_i);%total number of indexes in a slice (adjusted to an integer number of slices) |
|---|
| 117 | |
|---|
| 118 | %% setting of parameters specific to sub_background |
|---|
| 119 | CheckVolume='No'; |
|---|
| 120 | nbaver_init=23; %default number of images used for the sliding background: to be adjusted later to include an integer number of bursts |
|---|
| 121 | SaturationValue=0; |
|---|
| 122 | if nbfield_i~=1 && NbField_j<=nbaver_init |
|---|
| 123 | nbaver=floor(nbaver_init/NbField_j); % number of bursts used for the sliding background, |
|---|
| 124 | if isequal(mod(nbaver,2),0)% if nbaver is even |
|---|
| 125 | nbaver=nbaver+1;%put the number of burst to an odd number (so the middle burst is defined) |
|---|
| 126 | end |
|---|
| 127 | nbaver_init=nbaver*NbField_j;%propose by default an integer number of bursts |
|---|
| 128 | end |
|---|
| 129 | BrightnessRankThreshold=0.1; |
|---|
| 130 | if isfield(Param,'ActionInput') |
|---|
| 131 | if isfield(Param.ActionInput,'CheckVolume') && Param.ActionInput.CheckVolume |
|---|
| 132 | CheckVolume='Yes'; |
|---|
| 133 | end |
|---|
| 134 | if isfield(Param.ActionInput,'SlidingSequenceLength') |
|---|
| 135 | nbaver_init=Param.ActionInput.SlidingSequenceLength; |
|---|
| 136 | end |
|---|
| 137 | if isfield(Param.ActionInput,'BrightnessRankThreshold') |
|---|
| 138 | BrightnessRankThreshold=Param.ActionInput.BrightnessRankThreshold; |
|---|
| 139 | end |
|---|
| 140 | if isfield(Param.ActionInput,'SaturationValue') |
|---|
| 141 | SaturationValue=Param.ActionInput.SaturationValue; |
|---|
| 142 | end |
|---|
| 143 | end |
|---|
| 144 | prompt = {'volume scan mode (Yes/No)';... |
|---|
| 145 | 'Number of images for the sliding background (MUST FIT IN COMPUTER MEMORY)';... |
|---|
| 146 | 'the luminosity rank chosen to define the background (0.1=for dense particle seeding, 0.5 (median) for sparse particles';... |
|---|
| 147 | 'image saturation level for rescaling( reduce the influence of particles brighter than this value), =0 for no rescaling' }; |
|---|
| 148 | dlg_title = 'get (slice by slice) a sliding background and substract to each image'; |
|---|
| 149 | num_lines= 4; |
|---|
| 150 | def = { CheckVolume;num2str(nbaver_init);num2str(BrightnessRankThreshold);num2str(SaturationValue)}; |
|---|
| 151 | answer = inputdlg(prompt,dlg_title,num_lines,def); |
|---|
| 152 | if isempty(answer) |
|---|
| 153 | return |
|---|
| 154 | end |
|---|
| 155 | %check input consistency |
|---|
| 156 | if strcmp(answer{1},'No') && ~isequal(NbSlice_i,1) |
|---|
| 157 | check=msgbox_uvmat('INPUT_Y-N',['confirm the multi-level splitting into ' num2str(NbSlice_i) ' slices']); |
|---|
| 158 | if ~strcmp(check,'Yes') |
|---|
| 159 | return |
|---|
| 160 | end |
|---|
| 161 | end |
|---|
| 162 | if strcmp(answer{1},'Yes') |
|---|
| 163 | step=2;%the sliding background is shifted by the length of one burst, assumed =2 for volume |
|---|
| 164 | ParamOut.NbSlice=1; %nbre of slices displayed |
|---|
| 165 | else |
|---|
| 166 | step=NbField_j;%case of bursts: the sliding background is shifted by the length of one burst |
|---|
| 167 | end |
|---|
| 168 | ParamOut.ActionInput.SlidingSequenceLength=adjust_slidinglength(str2double(answer{2}),step); |
|---|
| 169 | ParamOut.ActionInput.CheckVolume=strcmp(answer{1},'Yes'); |
|---|
| 170 | ParamOut.ActionInput.BrightnessRankThreshold=str2double(answer{3}); |
|---|
| 171 | % ParamOut.ActionInput.CheckSubmedian=strcmp(answer{4},'Yes'); |
|---|
| 172 | ParamOut.ActionInput.SaturationValue=str2double(answer{4}); |
|---|
| 173 | % apply the image rescaling function 'level' (avoid the blinking effects of bright particles) |
|---|
| 174 | % answer=msgbox_uvmat('INPUT_Y-N','apply image rescaling function levels.m after sub_background'); |
|---|
| 175 | % ParamOut.ActionInput.CheckLevelTransform=strcmp(answer,'Yes'); |
|---|
| 176 | return |
|---|
| 177 | end |
|---|
| 178 | %%%%%%%%%%%%%%%%% STOP HERE FOR PAMETER INPUT MODE %%%%%%%%%%%%%%%%% |
|---|
| 179 | |
|---|
| 180 | %% read input parameters from an xml file if input is a file name (batch mode) |
|---|
| 181 | % checkrun=1; |
|---|
| 182 | RUNHandle=[]; |
|---|
| 183 | % WaitbarHandle=[]; |
|---|
| 184 | if ischar(Param) |
|---|
| 185 | Param=xml2struct(Param);% read Param as input file (batch case) |
|---|
| 186 | else |
|---|
| 187 | hseries=findobj(allchild(0),'Tag','series'); |
|---|
| 188 | RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series |
|---|
| 189 | % WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series |
|---|
| 190 | end |
|---|
| 191 | |
|---|
| 192 | %% input preparation |
|---|
| 193 | NbSlice_i=Param.IndexRange.NbSlice; |
|---|
| 194 | if ~isequal(NbSlice_i,1) |
|---|
| 195 | disp(['multi-level splitting into ' num2str(NbSlice_i) ' slices']); |
|---|
| 196 | end |
|---|
| 197 | RootPath=Param.InputTable{1,1}; |
|---|
| 198 | RootFile=Param.InputTable{1,3}; |
|---|
| 199 | SubDir=Param.InputTable{1,2}; |
|---|
| 200 | NomType=Param.InputTable{1,4}; |
|---|
| 201 | FileExt=Param.InputTable{1,5}; |
|---|
| 202 | %[filecell,i1_series,i2_series,j1_series]=get_file_series(Param);%series of file names organised as a single array |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | %% file index parameters |
|---|
| 206 | % NbSlice_i: nbre of slices for i index in multi-level mode: equal to 1 for a single level |
|---|
| 207 | % the function sub_background is then relaunched by the GUI series for each |
|---|
| 208 | % slice, incrementing the first index i by 1 |
|---|
| 209 | % NbSlice_j: nbre of slices in volume mode |
|---|
| 210 | % nbfield : total number of images treated per slice |
|---|
| 211 | % step: shift of image index at each step of the sliding background (corresponding to the nbre of images in a burst) |
|---|
| 212 | % nbaver_ima: nbre of the images in the sliding sequence used for the background |
|---|
| 213 | % nbaver=nbaver_ima/step: nbre of bursts corresponding to nbaver_ima images. It has been adjusted so that nbaver is an odd integer |
|---|
| 214 | i_indices=Param.IndexRange.first_i:Param.IndexRange.incr_i:Param.IndexRange.last_i; |
|---|
| 215 | if isfield(Param.IndexRange,'first_j') |
|---|
| 216 | j_indices=Param.IndexRange.first_j:Param.IndexRange.incr_j:Param.IndexRange.last_j; |
|---|
| 217 | else |
|---|
| 218 | j_indices=1; |
|---|
| 219 | end |
|---|
| 220 | nbfield_i=numel(i_indices); %nb of fields for the i index (bursts or volume slices) |
|---|
| 221 | NbField_j=numel(j_indices); %nb of fields for the j index |
|---|
| 222 | j_indices=j_indices'*ones(1,nbfield_i); |
|---|
| 223 | i_indices=ones(NbField_j,1)*i_indices; |
|---|
| 224 | |
|---|
| 225 | if Param.ActionInput.CheckVolume% case of volume scan: the background images must be determined for each index j |
|---|
| 226 | step=2;% we assume the burst contains only one image pair |
|---|
| 227 | NbSlice_j=NbField_j; |
|---|
| 228 | nbfield_series=nbfield_i; |
|---|
| 229 | else |
|---|
| 230 | if Param.ActionInput.SlidingSequenceLength<NbField_j |
|---|
| 231 | step=1; |
|---|
| 232 | else |
|---|
| 233 | step=NbField_j;%case of bursts: the sliding background is shifted by the length of one burst |
|---|
| 234 | end |
|---|
| 235 | NbSlice_j=1; |
|---|
| 236 | nbfield_series=nbfield_i*NbField_j; |
|---|
| 237 | end |
|---|
| 238 | nbfield=NbField_j*nbfield_i; %total number of fields |
|---|
| 239 | [nbaver_ima,nbaver,step]=adjust_slidinglength(Param.ActionInput.SlidingSequenceLength,step); |
|---|
| 240 | if nbaver_ima > nbfield |
|---|
| 241 | disp('number of images in a slice smaller than the proposed number of images for the sliding average') |
|---|
| 242 | return |
|---|
| 243 | end |
|---|
| 244 | halfnbaver=floor(nbaver/2); % half width (in unit of bursts) of the sliding background |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | %% File relabeling documented by the xml file |
|---|
| 248 | CheckRelabel=isfield(Param,'FileSeries' ); |
|---|
| 249 | |
|---|
| 250 | %% Input file info |
|---|
| 251 | if CheckRelabel |
|---|
| 252 | [RootFileOut,frame_index]=index2filename(Param.FileSeries,Param.IndexRange.first_i,j_indices(1),NbField_j); |
|---|
| 253 | FirstFileName=fullfile(RootPath,SubDir,RootFileOut); |
|---|
| 254 | else |
|---|
| 255 | FirstFileName=fullfile_uvmat(RootPath,SubDir,RootFile,FileExt,NomType,Param.IndexRange.first_i,[],j_indices(1));%get first file name |
|---|
| 256 | RootFileOut=RootFile; |
|---|
| 257 | end |
|---|
| 258 | [FileInfo,MovieObject]=get_file_info(FirstFileName); |
|---|
| 259 | FileType=FileInfo.FileType; |
|---|
| 260 | if ~CheckRelabel |
|---|
| 261 | if isfield(FileInfo,'NumberOfFrames') && FileInfo.NumberOfFrames >1 |
|---|
| 262 | if isempty(regexp(NomType,'1$', 'once'))% no file indexing |
|---|
| 263 | frame_index=i_indices;% the index i denotes the frame number in a movie, no index j |
|---|
| 264 | else |
|---|
| 265 | frame_index=j_indices;% the index j denotes the frame number in a movie |
|---|
| 266 | MovieObject=[]; %not a single video object |
|---|
| 267 | end |
|---|
| 268 | else |
|---|
| 269 | frame_index=ones(1,nbfield); |
|---|
| 270 | end |
|---|
| 271 | end |
|---|
| 272 | |
|---|
| 273 | %% output file naming |
|---|
| 274 | FileExtOut='.png'; % write result as .png images for image inputsFileInfo.FileType='image' |
|---|
| 275 | if strcmp(FileInfo.FileType,'image') |
|---|
| 276 | NomTypeOut=NomType; |
|---|
| 277 | elseif NbField_j==1 |
|---|
| 278 | NomTypeOut='_1'; |
|---|
| 279 | else |
|---|
| 280 | NomTypeOut='_1_1';% case of purely numerical indexing |
|---|
| 281 | end |
|---|
| 282 | OutputDir=[Param.OutputSubDir Param.OutputDirExt]; |
|---|
| 283 | OutputPath=fullfile(Param.OutputPath,Param.Experiment,Param.Device); |
|---|
| 284 | |
|---|
| 285 | %% calculate absolute brightness rank |
|---|
| 286 | rank=floor(Param.ActionInput.BrightnessRankThreshold*nbaver_ima); |
|---|
| 287 | if rank==0 |
|---|
| 288 | rank=1;%rank selected in the sorted image series |
|---|
| 289 | end |
|---|
| 290 | |
|---|
| 291 | %% prealocate memory for the sliding background |
|---|
| 292 | Ak=zeros(FileInfo.Height,FileInfo.Width,nbaver_ima,['uint' num2str(FileInfo.BitDepth)]); %prealocate memory |
|---|
| 293 | |
|---|
| 294 | %% selection of frame indices |
|---|
| 295 | if Param.ActionInput.CheckVolume |
|---|
| 296 | nbfield=floor(nbfield/NbSlice_j)*NbSlice_j;% truncate the total number of frames in case of incomplete series |
|---|
| 297 | indselect=1:nbfield; |
|---|
| 298 | indselect=reshape(indselect,NbSlice_j,[]); |
|---|
| 299 | NbSlice=NbSlice_j; |
|---|
| 300 | else |
|---|
| 301 | NbSlice=NbSlice_i; |
|---|
| 302 | nbfield=floor(nbfield/NbSlice)*NbSlice;% truncate the total number of frames in case of incomplete series |
|---|
| 303 | indselect=reshape(1:nbfield,NbSlice,[]); |
|---|
| 304 | for j_slice=1:NbSlice |
|---|
| 305 | indselect(j_slice,:)=j_slice:NbSlice:nbfield;% select file indices of the slice |
|---|
| 306 | end |
|---|
| 307 | end |
|---|
| 308 | |
|---|
| 309 | %%%%%%% LOOP ON SLICES %%%%%%% |
|---|
| 310 | for j_slice=1:NbSlice |
|---|
| 311 | |
|---|
| 312 | %% read the first series of nbaver_ima images and sort by luminosity at each pixel |
|---|
| 313 | for ifield = 1:nbaver_ima |
|---|
| 314 | ifile=indselect(j_slice,ifield); |
|---|
| 315 | %filename=filecell{1,ifile}; |
|---|
| 316 | if CheckRelabel |
|---|
| 317 | [filename,FrameIndex]=index2filename(Param.FileSeries,i_indices(ifile),j_indices(ifile),NbField_j); |
|---|
| 318 | filename=fullfile(RootPath,SubDir,filename); |
|---|
| 319 | else |
|---|
| 320 | filename=fullfile_uvmat(RootPath,SubDir,RootFile,FileExt,NomType,i_indices(ifile),[],j_indices(ifile)); |
|---|
| 321 | FrameIndex=frame_index(ifile); |
|---|
| 322 | end |
|---|
| 323 | Aread=read_image(filename,FileType,MovieObject,FrameIndex); |
|---|
| 324 | if ndims(Aread)==3%color images |
|---|
| 325 | Aread=sum(double(Aread),3);% take the sum of color components |
|---|
| 326 | end |
|---|
| 327 | Ak(:,:,ifield)=Aread; |
|---|
| 328 | end |
|---|
| 329 | Asort=sort(Ak,3);%sort the luminosity of images at each point |
|---|
| 330 | B=Asort(:,:,rank);%background image |
|---|
| 331 | |
|---|
| 332 | %% substract the first background image to the first images |
|---|
| 333 | disp( 'first background image will be substracted') |
|---|
| 334 | for ifield=1:step*(halfnbaver+1)% nbre of images treated by the first background image |
|---|
| 335 | Acor=double(Ak(:,:,ifield))-double(B);%substract background to the current image |
|---|
| 336 | Acor=(Acor>0).*Acor; % put to 0 the negative elements in Acor |
|---|
| 337 | ifile=indselect(j_slice,ifield); |
|---|
| 338 | newname=fullfile_uvmat(OutputPath,OutputDir,RootFileOut,FileExtOut,NomTypeOut,i_indices(ifile),[],j_indices(ifile)); |
|---|
| 339 | |
|---|
| 340 | %write result file |
|---|
| 341 | if ~isequal(Param.ActionInput.SaturationValue,0) |
|---|
| 342 | C=levels(Acor,Param.ActionInput.SaturationValue); |
|---|
| 343 | imwrite(C,newname,'BitDepth',16); % save the new image |
|---|
| 344 | else |
|---|
| 345 | if isequal(FileInfo.BitDepth,16) |
|---|
| 346 | C=uint16(Acor); |
|---|
| 347 | imwrite(C,newname,'BitDepth',16); % save the new image |
|---|
| 348 | else |
|---|
| 349 | C=uint8(Acor); |
|---|
| 350 | imwrite(C,newname,'BitDepth',8); % save the new image |
|---|
| 351 | end |
|---|
| 352 | end |
|---|
| 353 | disp([newname ' written']) |
|---|
| 354 | end |
|---|
| 355 | |
|---|
| 356 | %% repeat the operation on a sliding series of images |
|---|
| 357 | disp('sliding background image will be substracted') |
|---|
| 358 | if nbfield_series > nbaver_ima |
|---|
| 359 | for ifield = step*(halfnbaver+1):step:nbfield_series-step*(halfnbaver+1)% ifield +iburst=index of the current processed image |
|---|
| 360 | % update_waitbar(WaitbarHandle,ifield/nbfield_series) |
|---|
| 361 | if ~isempty(RUNHandle)&&~strcmp(get(RUNHandle,'BusyAction'),'queue') |
|---|
| 362 | disp('program stopped by user') |
|---|
| 363 | return |
|---|
| 364 | end |
|---|
| 365 | if nbaver_ima>step |
|---|
| 366 | Ak(:,:,1:nbaver_ima-step)=Ak(:,:,1+step:nbaver_ima);% shift the current image series by one burst (step) |
|---|
| 367 | end |
|---|
| 368 | %incorporate next burst in the current image series |
|---|
| 369 | for iburst=1:step |
|---|
| 370 | ifile=indselect(j_slice,ifield+iburst+step*halfnbaver); |
|---|
| 371 | if CheckRelabel |
|---|
| 372 | [filename,FrameIndex]=index2filename(Param.FileSeries,i_indices(ifile),j_indices(ifile),NbField_j); |
|---|
| 373 | filename=fullfile(RootPath,SubDir,filename); |
|---|
| 374 | else |
|---|
| 375 | filename=fullfile_uvmat(RootPath,SubDir,RootFile,FileExt,NomType,i_indices(ifile),[],j_indices(ifile)); |
|---|
| 376 | FrameIndex=frame_index(ifile); |
|---|
| 377 | end |
|---|
| 378 | Aread=read_image(filename,FileType,MovieObject,FrameIndex); |
|---|
| 379 | if ndims(Aread)==3%case of color images |
|---|
| 380 | Aread=sum(double(Aread),3);% take the sum of color components |
|---|
| 381 | end |
|---|
| 382 | Ak(:,:,nbaver_ima-step+iburst)=Aread;% fill the last burst of the current image series by the new image |
|---|
| 383 | end |
|---|
| 384 | Asort=sort(Ak,3);%sort the new current image series by luminosity |
|---|
| 385 | B=Asort(:,:,rank);%current background image |
|---|
| 386 | %substract the background for the current burst |
|---|
| 387 | for iburst=1:step |
|---|
| 388 | Acor=double(Ak(:,:,step*halfnbaver+iburst))-double(B); %the current image has been already read ans stored as index step*halfnbaver+iburst in the current series |
|---|
| 389 | Acor=(Acor>0).*Acor; % put to 0 the negative elements in Acor |
|---|
| 390 | ifile=indselect(j_slice,ifield+iburst); |
|---|
| 391 | newname=fullfile_uvmat(OutputPath,OutputDir,RootFileOut,FileExtOut,NomTypeOut,i_indices(ifile),[],j_indices(ifile)); |
|---|
| 392 | %write result file |
|---|
| 393 | if ~isequal(Param.ActionInput.SaturationValue,0) |
|---|
| 394 | C=levels(Acor,Param.ActionInput.SaturationValue); |
|---|
| 395 | imwrite(C,newname,'BitDepth',16); % save the new image |
|---|
| 396 | else |
|---|
| 397 | if isequal(FileInfo.BitDepth,16) |
|---|
| 398 | C=uint16(Acor); |
|---|
| 399 | imwrite(C,newname,'BitDepth',16); % save the new image |
|---|
| 400 | else |
|---|
| 401 | C=uint8(Acor); |
|---|
| 402 | imwrite(C,newname,'BitDepth',8); % save the new image |
|---|
| 403 | end |
|---|
| 404 | end |
|---|
| 405 | disp([newname ' written']) |
|---|
| 406 | end |
|---|
| 407 | end |
|---|
| 408 | end |
|---|
| 409 | |
|---|
| 410 | %% substract the background from the last images |
|---|
| 411 | disp('last background image will be substracted') |
|---|
| 412 | for ifield=nbfield_series-step*halfnbaver+1:nbfield_series |
|---|
| 413 | Acor=double(Ak(:,:,ifield-nbfield_series+step*(2*halfnbaver+1)))-double(B); |
|---|
| 414 | Acor=(Acor>0).*Acor; % put to 0 the negative elements in Acor |
|---|
| 415 | ifile=indselect(j_slice,ifield); |
|---|
| 416 | newname=fullfile_uvmat(OutputPath,OutputDir,RootFileOut,FileExtOut,NomTypeOut,i_indices(ifile),[],j_indices(ifile)); |
|---|
| 417 | %write result file |
|---|
| 418 | if ~isequal(Param.ActionInput.SaturationValue,0) |
|---|
| 419 | C=levels(Acor,Param.ActionInput.SaturationValue); |
|---|
| 420 | imwrite(C,newname,'BitDepth',16); % save the new image |
|---|
| 421 | else |
|---|
| 422 | if isequal(FileInfo.BitDepth,16) |
|---|
| 423 | C=uint16(Acor); |
|---|
| 424 | imwrite(C,newname,'BitDepth',16); % save the new image |
|---|
| 425 | else |
|---|
| 426 | C=uint8(Acor); |
|---|
| 427 | imwrite(C,newname,'BitDepth',8); % save the new image |
|---|
| 428 | end |
|---|
| 429 | end |
|---|
| 430 | disp([newname ' written']) |
|---|
| 431 | end |
|---|
| 432 | end |
|---|
| 433 | |
|---|
| 434 | function C=levels(A,Coeff) |
|---|
| 435 | |
|---|
| 436 | % nblock_y=100;%2*Param.TransformInput.BlockSize; |
|---|
| 437 | % nblock_x=100;%2*Param.TransformInput.BlockSize; |
|---|
| 438 | % [npy,npx]=size(A); |
|---|
| 439 | % [X,Y]=meshgrid(1:npx,1:npy); |
|---|
| 440 | % |
|---|
| 441 | % %Backg=zeros(size(A)); |
|---|
| 442 | % %Aflagmin=sparse(imregionalmin(A));%Amin=1 for local image minima |
|---|
| 443 | % %Amin=A.*Aflagmin;%values of A at local minima |
|---|
| 444 | % % local background: find all the local minima in image subblocks |
|---|
| 445 | % if CheckSubmedian |
|---|
| 446 | % fctblock= inline('median(x(:))'); |
|---|
| 447 | % Backg=blkproc(A,[nblock_y nblock_x],fctblock);% take the median in blocks |
|---|
| 448 | % %B=imresize(Backg,size(A),'bilinear');% interpolate to the initial size image |
|---|
| 449 | % A=A-imresize(Backg,size(A),'bilinear');% substract background interpolated to the initial size image |
|---|
| 450 | % end |
|---|
| 451 | % fctblock= inline('mean(x(:))'); |
|---|
| 452 | % AMean=blkproc(A,[nblock_y nblock_x],fctblock);% take the mean in blocks |
|---|
| 453 | % fctblock= inline('var(x(:))'); |
|---|
| 454 | % AVar=blkproc(A,[nblock_y nblock_x],fctblock);% take the mean in blocks |
|---|
| 455 | % Avalue=AVar./AMean;% typical value of particle luminosity |
|---|
| 456 | % Avalue=imresize(Avalue,size(A),'bilinear');% interpolate to the initial size image |
|---|
| 457 | %C=uint16(1000*tanh(A./(Coeff*Avalue))); |
|---|
| 458 | C=uint16(Coeff*tanh(A./Coeff)); |
|---|
| 459 | %------------------------------------------ |
|---|
| 460 | % adjust the number of images used for the sliding average |
|---|
| 461 | function [nbaver_ima,nbaver,step_out]=adjust_slidinglength(nb_aver_in,step) |
|---|
| 462 | %nbaver_ima=str2double(nb_aver_in);%number of images for the sliding background |
|---|
| 463 | nbaver=ceil(nb_aver_in/step);%number of bursts for the sliding background |
|---|
| 464 | if isequal(mod(nbaver,2),0)% if nbaver is even |
|---|
| 465 | nbaver=nbaver+1;%set the number of bursts to an odd number (so the middle burst is defined) |
|---|
| 466 | end |
|---|
| 467 | step_out=step; |
|---|
| 468 | if nbaver>1 |
|---|
| 469 | nbaver_ima=nbaver*step;% correct the nbre of images corresponding to nbaver |
|---|
| 470 | else |
|---|
| 471 | nbaver_ima=nb_aver_in; |
|---|
| 472 | if isequal(mod(nbaver_ima,2),0)% if nbaver_ima is even |
|---|
| 473 | nbaver_ima=nbaver_ima+1;%set the number of bursts to an odd number (so the middle burst is defined) |
|---|
| 474 | end |
|---|
| 475 | step_out=1; |
|---|
| 476 | end |
|---|