| 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 |
|---|
| 81 | ParamOut.ProjObject='off';%cannot use projection object(option 'off'/'on', |
|---|
| 82 | ParamOut.Mask='on';%can 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 | %% root input file(s) and type |
|---|
| 87 | % check the existence of the first file in the series |
|---|
| 88 | first_j=[];% note that the function will propose to cover the whole range of indices |
|---|
| 89 | if isfield(Param.IndexRange,'MinIndex_j'); first_j=Param.IndexRange.MinIndex_j; end |
|---|
| 90 | last_j=[]; |
|---|
| 91 | if isfield(Param.IndexRange,'MaxIndex_j'); last_j=Param.IndexRange.MaxIndex_j; end |
|---|
| 92 | PairString=''; |
|---|
| 93 | if isfield(Param.IndexRange,'PairString'); PairString=Param.IndexRange.PairString; end |
|---|
| 94 | [i1,i2,j1,j2] = get_file_index(Param.IndexRange.first_i,first_j,PairString); |
|---|
| 95 | FirstFileName=fullfile_uvmat(Param.InputTable{1,1},Param.InputTable{1,2},Param.InputTable{1,3},... |
|---|
| 96 | Param.InputTable{1,5},Param.InputTable{1,4},i1,i2,j1,j2); |
|---|
| 97 | if ~exist(FirstFileName,'file') |
|---|
| 98 | msgbox_uvmat('WARNING',['the first input file ' FirstFileName ' does not exist']) |
|---|
| 99 | else |
|---|
| 100 | [i1,i2,j1,j2] = get_file_index(Param.IndexRange.last_i,last_j,PairString); |
|---|
| 101 | LastFileName=fullfile_uvmat(Param.InputTable{1,1},Param.InputTable{1,2},Param.InputTable{1,3},... |
|---|
| 102 | Param.InputTable{1,5},Param.InputTable{1,4},i1,i2,j1,j2); |
|---|
| 103 | if ~exist(FirstFileName,'file') |
|---|
| 104 | msgbox_uvmat('WARNING',['the last input file ' LastFileName ' does not exist']) |
|---|
| 105 | end |
|---|
| 106 | end |
|---|
| 107 | |
|---|
| 108 | %% check the validity of input file types |
|---|
| 109 | FileInfo=get_file_info(FirstFileName); |
|---|
| 110 | FileType=FileInfo.FileType; |
|---|
| 111 | CheckImage=strcmp(FileInfo.FieldType,'image');% =1 for images |
|---|
| 112 | if ~CheckImage |
|---|
| 113 | msgbox_uvmat('ERROR',['invalid file type input: ' FileType ' not an image']) |
|---|
| 114 | return |
|---|
| 115 | end |
|---|
| 116 | |
|---|
| 117 | %% numbers of fields |
|---|
| 118 | NbSlice_i=1;%default |
|---|
| 119 | if isfield(Param.IndexRange,'NbSlice')&&~isempty(Param.IndexRange.NbSlice) |
|---|
| 120 | NbSlice_i=Param.IndexRange.NbSlice; |
|---|
| 121 | end |
|---|
| 122 | incr_j=1;%default |
|---|
| 123 | if isfield(Param.IndexRange,'incr_j')&&~isempty(Param.IndexRange.incr_j) |
|---|
| 124 | incr_j=Param.IndexRange.incr_j; |
|---|
| 125 | end |
|---|
| 126 | if isempty(first_j)||isempty(last_j) |
|---|
| 127 | nbfield_j=1; |
|---|
| 128 | else |
|---|
| 129 | nbfield_j=numel(first_j:incr_j:last_j);%nb of fields for the j index (bursts or volume slices) |
|---|
| 130 | end |
|---|
| 131 | first_i=1;last_i=1;incr_i=1;%default |
|---|
| 132 | if isfield(Param.IndexRange,'MinIndex_i'); first_i=Param.IndexRange.MinIndex_i; end |
|---|
| 133 | if isfield(Param.IndexRange,'MaxIndex_i'); last_i=Param.IndexRange.MaxIndex_i; end |
|---|
| 134 | if isfield(Param.IndexRange,'incr_i')&&~isempty(Param.IndexRange.incr_i) |
|---|
| 135 | incr_i=Param.IndexRange.incr_i; |
|---|
| 136 | end |
|---|
| 137 | nbfield_i=numel(first_i:incr_i:last_i);%nb of fields for the i index (bursts or volume slices) |
|---|
| 138 | nbfield=nbfield_j*nbfield_i; %total number of fields |
|---|
| 139 | nbfield_i=floor(nbfield/NbSlice_i);%total number of indexes in a slice (adjusted to an integer number of slices) |
|---|
| 140 | |
|---|
| 141 | %% setting of parameters specific to sub_background |
|---|
| 142 | CheckVolume='No'; |
|---|
| 143 | nbaver_init=23; %default number of images used for the sliding background: to be adjusted later to include an integer number of bursts |
|---|
| 144 | SaturationValue=0; |
|---|
| 145 | if nbfield_i~=1 && nbfield_j<=nbaver_init |
|---|
| 146 | nbaver=floor(nbaver_init/nbfield_j); % number of bursts used for the sliding background, |
|---|
| 147 | if isequal(mod(nbaver,2),0)% if nbaver is even |
|---|
| 148 | nbaver=nbaver+1;%put the number of burst to an odd number (so the middle burst is defined) |
|---|
| 149 | end |
|---|
| 150 | nbaver_init=nbaver*nbfield_j;%propose by default an integer number of bursts |
|---|
| 151 | end |
|---|
| 152 | BrightnessRankThreshold=0.1; |
|---|
| 153 | % CheckSubmedian='No'; |
|---|
| 154 | % SaturationCoeff=0; |
|---|
| 155 | if isfield(Param,'ActionInput') |
|---|
| 156 | if isfield(Param.ActionInput,'CheckVolume') && Param.ActionInput.CheckVolume |
|---|
| 157 | CheckVolume='Yes'; |
|---|
| 158 | end |
|---|
| 159 | if isfield(Param.ActionInput,'SlidingSequenceLength') |
|---|
| 160 | nbaver_init=Param.ActionInput.SlidingSequenceLength; |
|---|
| 161 | end |
|---|
| 162 | if isfield(Param.ActionInput,'BrightnessRankThreshold') |
|---|
| 163 | BrightnessRankThreshold=Param.ActionInput.BrightnessRankThreshold; |
|---|
| 164 | end |
|---|
| 165 | % if isfield(Param.ActionInput,'CheckSubmedian') && Param.ActionInput.CheckSubmedian |
|---|
| 166 | % CheckSubmedian='Yes'; |
|---|
| 167 | % end |
|---|
| 168 | if isfield(Param.ActionInput,'SaturationValue') |
|---|
| 169 | SaturationValue=Param.ActionInput.SaturationValue; |
|---|
| 170 | end |
|---|
| 171 | end |
|---|
| 172 | prompt = {'volume scan mode (Yes/No)';... |
|---|
| 173 | 'Number of images for the sliding background (MUST FIT IN COMPUTER MEMORY)';... |
|---|
| 174 | 'the luminosity rank chosen to define the background (0.1=for dense particle seeding, 0.5 (median) for sparse particles';... |
|---|
| 175 | 'image saturation level for rescaling( reduce the influence of particles brighter than this value), =0 for no rescaling' }; |
|---|
| 176 | dlg_title = 'get (slice by slice) a sliding background and substract to each image'; |
|---|
| 177 | num_lines= 4; |
|---|
| 178 | def = { CheckVolume;num2str(nbaver_init);num2str(BrightnessRankThreshold);num2str(SaturationValue)}; |
|---|
| 179 | answer = inputdlg(prompt,dlg_title,num_lines,def); |
|---|
| 180 | if isempty(answer) |
|---|
| 181 | return |
|---|
| 182 | end |
|---|
| 183 | %check input consistency |
|---|
| 184 | if strcmp(answer{1},'No') && ~isequal(NbSlice_i,1) |
|---|
| 185 | check=msgbox_uvmat('INPUT_Y-N',['confirm the multi-level splitting into ' num2str(NbSlice_i) ' slices']); |
|---|
| 186 | if ~strcmp(check,'Yes') |
|---|
| 187 | return |
|---|
| 188 | end |
|---|
| 189 | end |
|---|
| 190 | if strcmp(answer{1},'Yes') |
|---|
| 191 | step=2;%the sliding background is shifted by the length of one burst, assumed =2 for volume |
|---|
| 192 | ParamOut.NbSlice=1; %nbre of slices displayed |
|---|
| 193 | else |
|---|
| 194 | step=nbfield_j;%case of bursts: the sliding background is shifted by the length of one burst |
|---|
| 195 | end |
|---|
| 196 | ParamOut.ActionInput.SlidingSequenceLength=adjust_slidinglength(str2num(answer{2}),step); |
|---|
| 197 | ParamOut.ActionInput.CheckVolume=strcmp(answer{1},'Yes'); |
|---|
| 198 | ParamOut.ActionInput.BrightnessRankThreshold=str2double(answer{3}); |
|---|
| 199 | % ParamOut.ActionInput.CheckSubmedian=strcmp(answer{4},'Yes'); |
|---|
| 200 | ParamOut.ActionInput.SaturationValue=str2double(answer{4}); |
|---|
| 201 | % apply the image rescaling function 'level' (avoid the blinking effects of bright particles) |
|---|
| 202 | % answer=msgbox_uvmat('INPUT_Y-N','apply image rescaling function levels.m after sub_background'); |
|---|
| 203 | % ParamOut.ActionInput.CheckLevelTransform=strcmp(answer,'Yes'); |
|---|
| 204 | return |
|---|
| 205 | end |
|---|
| 206 | %%%%%%%%%%%%%%%%% STOP HERE FOR PAMETER INPUT MODE %%%%%%%%%%%%%%%%% |
|---|
| 207 | |
|---|
| 208 | %% read input parameters from an xml file if input is a file name (batch mode) |
|---|
| 209 | checkrun=1; |
|---|
| 210 | RUNHandle=[]; |
|---|
| 211 | WaitbarHandle=[]; |
|---|
| 212 | if ischar(Param) |
|---|
| 213 | Param=xml2struct(Param);% read Param as input file (batch case) |
|---|
| 214 | checkrun=0; |
|---|
| 215 | else |
|---|
| 216 | hseries=findobj(allchild(0),'Tag','series'); |
|---|
| 217 | RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series |
|---|
| 218 | WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series |
|---|
| 219 | end |
|---|
| 220 | |
|---|
| 221 | %% input preparation |
|---|
| 222 | NbSlice_i=Param.IndexRange.NbSlice; |
|---|
| 223 | if ~isequal(NbSlice_i,1) |
|---|
| 224 | display(['multi-level splitting into ' num2str(NbSlice_i) ' slices']); |
|---|
| 225 | end |
|---|
| 226 | RootPath=Param.InputTable(:,1); |
|---|
| 227 | RootFile=Param.InputTable(:,3); |
|---|
| 228 | SubDir=Param.InputTable(:,2); |
|---|
| 229 | NomType=Param.InputTable(:,4); |
|---|
| 230 | FileExt=Param.InputTable(:,5); |
|---|
| 231 | [filecell,i1_series,i2_series,j1_series]=get_file_series(Param);%series of file names organised as a single array |
|---|
| 232 | |
|---|
| 233 | %%%%%%%%%%%% |
|---|
| 234 | % The cell array filecell is the list of input file names, while |
|---|
| 235 | % filecell{iview,fileindex}: |
|---|
| 236 | % iview: line in the table corresponding to a given file series |
|---|
| 237 | % fileindex: file index within the file series, |
|---|
| 238 | % i1_series(iview,ref_j,ref_i)... are the corresponding arrays of indices i1,i2,j1,j2, depending on the input line iview and the two reference indices ref_i,ref_j |
|---|
| 239 | % i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices |
|---|
| 240 | %%%%%%%%%%%% |
|---|
| 241 | [FileInfo{1},MovieObject{1}]=get_file_info(filecell{1,1}); |
|---|
| 242 | FileType{1}=FileInfo{1}.FileType; |
|---|
| 243 | if ~isempty(j1_series{1}) |
|---|
| 244 | frame_index{1}=j1_series{1}; |
|---|
| 245 | else |
|---|
| 246 | frame_index{1}=i1_series{1}; |
|---|
| 247 | end |
|---|
| 248 | |
|---|
| 249 | |
|---|
| 250 | %% output file naming |
|---|
| 251 | FileExtOut='.png'; % write result as .png images for image inputsFileInfo.FileType='image' |
|---|
| 252 | if strcmp(FileInfo{1}.FileType,'image') |
|---|
| 253 | NomTypeOut=NomType{1}; |
|---|
| 254 | elseif isempty(j1_series{1}) |
|---|
| 255 | NomTypeOut='_1'; |
|---|
| 256 | else |
|---|
| 257 | NomTypeOut='_1_1';% caseof purely numerical indexing |
|---|
| 258 | end |
|---|
| 259 | OutputDir=[Param.OutputSubDir Param.OutputDirExt]; |
|---|
| 260 | OutputPath=fullfile(Param.OutputPath,Param.Experiment,Param.Device); |
|---|
| 261 | |
|---|
| 262 | %% file index parameters |
|---|
| 263 | % NbSlice_i: nbre of slices for i index in multi-level mode: equal to 1 for a single level |
|---|
| 264 | % the function sub_background is then relaunched by the GUI series for each |
|---|
| 265 | % slice, incrementing the first index i by 1 |
|---|
| 266 | % NbSlice_j: nbre of slices in volume mode |
|---|
| 267 | % nbfield : total number of images treated per slice |
|---|
| 268 | % step: shift of image index at each step of the sliding background (corresponding to the nbre of images in a burst) |
|---|
| 269 | % nbaver_ima: nbre of the images in the sliding sequence used for the background |
|---|
| 270 | % nbaver=nbaver_ima/step: nbre of bursts corresponding to nbaver_ima images. It has been adjusted so that nbaver is an odd integer |
|---|
| 271 | nbfield_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices) |
|---|
| 272 | nbfield_i=size(i1_series{1},2); %nb of fields for the i index |
|---|
| 273 | |
|---|
| 274 | if Param.ActionInput.CheckVolume% case of volume scan: the background images must be determined for each index j |
|---|
| 275 | step=2;% we assume the burst contains only one image pair |
|---|
| 276 | NbSlice_j=nbfield_j; |
|---|
| 277 | nbfield_series=nbfield_i; |
|---|
| 278 | else |
|---|
| 279 | step=nbfield_j;%case of bursts: the sliding background is shifted by the length of one burst |
|---|
| 280 | NbSlice_j=1; |
|---|
| 281 | nbfield_series=nbfield_i*nbfield_j; |
|---|
| 282 | end |
|---|
| 283 | nbfield=nbfield_j*nbfield_i; %total number of fields |
|---|
| 284 | [nbaver_ima,nbaver,step]=adjust_slidinglength(Param.ActionInput.SlidingSequenceLength,step); |
|---|
| 285 | if nbaver_ima > nbfield |
|---|
| 286 | display('number of images in a slice smaller than the proposed number of images for the sliding average') |
|---|
| 287 | return |
|---|
| 288 | end |
|---|
| 289 | halfnbaver=floor(nbaver/2); % half width (in unit of bursts) of the sliding background |
|---|
| 290 | |
|---|
| 291 | %% calculate absolute brightness rank |
|---|
| 292 | rank=floor(Param.ActionInput.BrightnessRankThreshold*nbaver_ima); |
|---|
| 293 | if rank==0 |
|---|
| 294 | rank=1;%rank selected in the sorted image series |
|---|
| 295 | end |
|---|
| 296 | |
|---|
| 297 | %% prealocate memory for the sliding background |
|---|
| 298 | try |
|---|
| 299 | Afirst=read_image(filecell{1,1},FileType{1},MovieObject{1},frame_index{1}(1)); |
|---|
| 300 | [npy,npx,nbcolor]=size(Afirst);% the argument nbcolor is important to get npx right for color images |
|---|
| 301 | if strcmp(class(Afirst),'uint8') % case of 8bit images |
|---|
| 302 | Ak=zeros(npy,npx,nbaver_ima,'uint8'); %prealocate memory |
|---|
| 303 | Asort=zeros(npy,npx,nbaver_ima,'uint8'); %prealocate memory |
|---|
| 304 | else |
|---|
| 305 | Ak=zeros(npy,npx,nbaver_ima,'uint16'); %prealocate memory |
|---|
| 306 | Asort=zeros(npy,npx,nbaver_ima,'uint16'); %prealocate memory |
|---|
| 307 | end |
|---|
| 308 | catch ME |
|---|
| 309 | msgbox_uvmat('ERROR',['sub_background/read_image/' ME.message]) |
|---|
| 310 | return |
|---|
| 311 | end |
|---|
| 312 | |
|---|
| 313 | %selection of frame indices |
|---|
| 314 | if Param.ActionInput.CheckVolume |
|---|
| 315 | nbfield=floor(nbfield/NbSlice_j)*NbSlice_j;% truncate the total number of frames in case of incomplete series |
|---|
| 316 | indselect=1:nbfield; |
|---|
| 317 | indselect=reshape(indselect,NbSlice_j,[]); |
|---|
| 318 | NbSlice=NbSlice_j; |
|---|
| 319 | else |
|---|
| 320 | NbSlice=NbSlice_i; |
|---|
| 321 | nbfield=floor(nbfield/NbSlice)*NbSlice;% truncate the total number of frames in case of incomplete series |
|---|
| 322 | indselect=reshape(1:nbfield,NbSlice,[]); |
|---|
| 323 | for j_slice=1:NbSlice |
|---|
| 324 | indselect(j_slice,:)=j_slice:step*NbSlice:nbfield;% select file indices of the slice |
|---|
| 325 | end |
|---|
| 326 | end |
|---|
| 327 | |
|---|
| 328 | %%%%%%% LOOP ON SLICES %%%%%%% |
|---|
| 329 | for j_slice=1:NbSlice |
|---|
| 330 | %% select the series of i indices to process |
|---|
| 331 | % indselect=j_slice:step*NbSlice_j:nbfield;% select file indices of the slice |
|---|
| 332 | % for ifield=1:step-1 |
|---|
| 333 | % indselect=[indselect;indselect(end,:)+NbSlice]; |
|---|
| 334 | % end |
|---|
| 335 | |
|---|
| 336 | %% read the first series of nbaver_ima images and sort by luminosity at each pixel |
|---|
| 337 | for ifield = 1:nbaver_ima |
|---|
| 338 | ifile=indselect(jslice,ifield); |
|---|
| 339 | filename=filecell{1,ifile}; |
|---|
| 340 | Aread=read_image(filename,FileType{1},MovieObject{1},frame_index{1}(ifile)); |
|---|
| 341 | if ndims(Aread)==3%color images |
|---|
| 342 | Aread=sum(double(Aread),3);% take the sum of color components |
|---|
| 343 | end |
|---|
| 344 | Ak(:,:,ifield)=Aread; |
|---|
| 345 | end |
|---|
| 346 | Asort=sort(Ak,3);%sort the luminosity of images at each point |
|---|
| 347 | B=Asort(:,:,rank);%background image |
|---|
| 348 | |
|---|
| 349 | %% substract the first background image to the first images |
|---|
| 350 | display( 'first background image will be substracted') |
|---|
| 351 | for ifield=1:step*(halfnbaver+1)% nbre of images treated by the first background image |
|---|
| 352 | Acor=double(Ak(:,:,ifield))-double(B);%substract background to the current image |
|---|
| 353 | Acor=(Acor>0).*Acor; % put to 0 the negative elements in Acor |
|---|
| 354 | ifile=indselect(jslice,ifield); |
|---|
| 355 | j1=1; |
|---|
| 356 | if ~isempty(j1_series{1}) |
|---|
| 357 | j1=j1_series{1}(ifile); |
|---|
| 358 | end |
|---|
| 359 | newname=fullfile_uvmat(OutputPath,OutputDir,RootFile{1},FileExtOut,NomTypeOut,i1_series{1}(ifile),[],j1); |
|---|
| 360 | |
|---|
| 361 | %write result file |
|---|
| 362 | if ~isequal(Param.ActionInput.SaturationValue,0) |
|---|
| 363 | C=levels(Acor,Param.ActionInput.SaturationValue); |
|---|
| 364 | imwrite(C,newname,'BitDepth',16); % save the new image |
|---|
| 365 | else |
|---|
| 366 | if ~isfield(FileInfo{1},'BitDepth') |
|---|
| 367 | FileInfo{1}.BitDepth=16; |
|---|
| 368 | end |
|---|
| 369 | if isequal(FileInfo{1}.BitDepth,16) |
|---|
| 370 | C=uint16(Acor); |
|---|
| 371 | imwrite(C,newname,'BitDepth',16); % save the new image |
|---|
| 372 | else |
|---|
| 373 | C=uint8(Acor); |
|---|
| 374 | imwrite(C,newname,'BitDepth',8); % save the new image |
|---|
| 375 | end |
|---|
| 376 | end |
|---|
| 377 | display([newname ' written']) |
|---|
| 378 | end |
|---|
| 379 | |
|---|
| 380 | %% repeat the operation on a sliding series of images |
|---|
| 381 | display('sliding background image will be substracted') |
|---|
| 382 | if nbfield_series > nbaver_ima |
|---|
| 383 | for ifield = step*(halfnbaver+1):step:nbfield_series-step*(halfnbaver+1)% ifield +iburst=index of the current processed image |
|---|
| 384 | update_waitbar(WaitbarHandle,ifield/nbfield_series) |
|---|
| 385 | if ~isempty(RUNHandle)&&~strcmp(get(RUNHandle,'BusyAction'),'queue') |
|---|
| 386 | disp('program stopped by user') |
|---|
| 387 | return |
|---|
| 388 | end |
|---|
| 389 | if nbaver_ima>step |
|---|
| 390 | Ak(:,:,1:nbaver_ima-step)=Ak(:,:,1+step:nbaver_ima);% shift the current image series by one burst (step) |
|---|
| 391 | end |
|---|
| 392 | %incorporate next burst in the current image series |
|---|
| 393 | for iburst=1:step |
|---|
| 394 | ifile=indselect(jslice,ifield+iburst+step*halfnbaver); |
|---|
| 395 | j1=1; |
|---|
| 396 | if ~isempty(j1_series{1}) |
|---|
| 397 | j1=j1_series{1}(ifile); |
|---|
| 398 | end |
|---|
| 399 | |
|---|
| 400 | filename=fullfile_uvmat(RootPath{1},SubDir{1},RootFile{1},FileExt{1},NomType{1},i1_series{1}(ifile),[],j1); |
|---|
| 401 | Aread=read_image(filename,FileType{1},MovieObject{1},frame_index{1}(ifile)); |
|---|
| 402 | if ndims(Aread)==3%case of color images |
|---|
| 403 | Aread=sum(double(Aread),3);% take the sum of color components |
|---|
| 404 | end |
|---|
| 405 | Ak(:,:,nbaver_ima-step+iburst)=Aread;% fill the last burst of the current image series by the new image |
|---|
| 406 | end |
|---|
| 407 | Asort=sort(Ak,3);%sort the new current image series by luminosity |
|---|
| 408 | B=Asort(:,:,rank);%current background image |
|---|
| 409 | %substract the background for the current burst |
|---|
| 410 | for iburst=1:step |
|---|
| 411 | 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 |
|---|
| 412 | Acor=(Acor>0).*Acor; % put to 0 the negative elements in Acor |
|---|
| 413 | ifile=indselect(jslice,ifield+iburst); |
|---|
| 414 | if ~isempty(j1_series{1}) |
|---|
| 415 | j1=j1_series{1}(ifile); |
|---|
| 416 | end |
|---|
| 417 | newname=fullfile_uvmat(OutputPath,OutputDir,RootFile{1},FileExtOut,NomTypeOut,i1_series{1}(ifile),[],j1); |
|---|
| 418 | %write result file |
|---|
| 419 | if ~isequal(Param.ActionInput.SaturationValue,0) |
|---|
| 420 | C=levels(Acor,Param.ActionInput.SaturationValue); |
|---|
| 421 | imwrite(C,newname,'BitDepth',16); % save the new image |
|---|
| 422 | else |
|---|
| 423 | if isequal(FileInfo{1}.BitDepth,16) |
|---|
| 424 | C=uint16(Acor); |
|---|
| 425 | imwrite(C,newname,'BitDepth',16); % save the new image |
|---|
| 426 | else |
|---|
| 427 | C=uint8(Acor); |
|---|
| 428 | imwrite(C,newname,'BitDepth',8); % save the new image |
|---|
| 429 | end |
|---|
| 430 | end |
|---|
| 431 | display([newname ' written']) |
|---|
| 432 | end |
|---|
| 433 | end |
|---|
| 434 | end |
|---|
| 435 | |
|---|
| 436 | %% substract the background from the last images |
|---|
| 437 | display('last background image will be substracted') |
|---|
| 438 | for ifield=nbfield_series-step*halfnbaver+1:nbfield_series |
|---|
| 439 | Acor=double(Ak(:,:,ifield-nbfield_series+step*(2*halfnbaver+1)))-double(B); |
|---|
| 440 | Acor=(Acor>0).*Acor; % put to 0 the negative elements in Acor |
|---|
| 441 | ifile=indselect(jslice,ifield); |
|---|
| 442 | if ~isempty(j1_series{1}) |
|---|
| 443 | j1=j1_series{1}(ifile); |
|---|
| 444 | end |
|---|
| 445 | newname=fullfile_uvmat(OutputPath,OutputDir,RootFile{1},FileExtOut,NomTypeOut,i1_series{1}(ifile),[],j1); |
|---|
| 446 | %write result file |
|---|
| 447 | if ~isequal(Param.ActionInput.SaturationValue,0) |
|---|
| 448 | C=levels(Acor,Param.ActionInput.SaturationValue); |
|---|
| 449 | imwrite(C,newname,'BitDepth',16); % save the new image |
|---|
| 450 | else |
|---|
| 451 | if isequal(FileInfo{1}.BitDepth,16) |
|---|
| 452 | C=uint16(Acor); |
|---|
| 453 | imwrite(C,newname,'BitDepth',16); % save the new image |
|---|
| 454 | else |
|---|
| 455 | C=uint8(Acor); |
|---|
| 456 | imwrite(C,newname,'BitDepth',8); % save the new image |
|---|
| 457 | end |
|---|
| 458 | end |
|---|
| 459 | display([newname ' written']) |
|---|
| 460 | end |
|---|
| 461 | end |
|---|
| 462 | |
|---|
| 463 | function C=levels(A,Coeff) |
|---|
| 464 | |
|---|
| 465 | % nblock_y=100;%2*Param.TransformInput.BlockSize; |
|---|
| 466 | % nblock_x=100;%2*Param.TransformInput.BlockSize; |
|---|
| 467 | % [npy,npx]=size(A); |
|---|
| 468 | % [X,Y]=meshgrid(1:npx,1:npy); |
|---|
| 469 | % |
|---|
| 470 | % %Backg=zeros(size(A)); |
|---|
| 471 | % %Aflagmin=sparse(imregionalmin(A));%Amin=1 for local image minima |
|---|
| 472 | % %Amin=A.*Aflagmin;%values of A at local minima |
|---|
| 473 | % % local background: find all the local minima in image subblocks |
|---|
| 474 | % if CheckSubmedian |
|---|
| 475 | % fctblock= inline('median(x(:))'); |
|---|
| 476 | % Backg=blkproc(A,[nblock_y nblock_x],fctblock);% take the median in blocks |
|---|
| 477 | % %B=imresize(Backg,size(A),'bilinear');% interpolate to the initial size image |
|---|
| 478 | % A=A-imresize(Backg,size(A),'bilinear');% substract background interpolated to the initial size image |
|---|
| 479 | % end |
|---|
| 480 | % fctblock= inline('mean(x(:))'); |
|---|
| 481 | % AMean=blkproc(A,[nblock_y nblock_x],fctblock);% take the mean in blocks |
|---|
| 482 | % fctblock= inline('var(x(:))'); |
|---|
| 483 | % AVar=blkproc(A,[nblock_y nblock_x],fctblock);% take the mean in blocks |
|---|
| 484 | % Avalue=AVar./AMean;% typical value of particle luminosity |
|---|
| 485 | % Avalue=imresize(Avalue,size(A),'bilinear');% interpolate to the initial size image |
|---|
| 486 | %C=uint16(1000*tanh(A./(Coeff*Avalue))); |
|---|
| 487 | C=uint16(Coeff*tanh(A./Coeff)); |
|---|
| 488 | %------------------------------------------ |
|---|
| 489 | % adjust the number of images used for the sliding average |
|---|
| 490 | function [nbaver_ima,nbaver,step_out]=adjust_slidinglength(nb_aver_in,step) |
|---|
| 491 | %nbaver_ima=str2double(nb_aver_in);%number of images for the sliding background |
|---|
| 492 | nbaver=ceil(nb_aver_in/step);%number of bursts for the sliding background |
|---|
| 493 | if isequal(mod(nbaver,2),0)% if nbaver is even |
|---|
| 494 | nbaver=nbaver+1;%set the number of bursts to an odd number (so the middle burst is defined) |
|---|
| 495 | end |
|---|
| 496 | step_out=step; |
|---|
| 497 | if nbaver>1 |
|---|
| 498 | nbaver_ima=nbaver*step;% correct the nbre of images corresponding to nbaver |
|---|
| 499 | else |
|---|
| 500 | nbaver_ima=nb_aver_in; |
|---|
| 501 | if isequal(mod(nbaver_ima,2),0)% if nbaver_ima is even |
|---|
| 502 | nbaver_ima=nbaver_ima+1;%set the number of bursts to an odd number (so the middle burst is defined) |
|---|
| 503 | end |
|---|
| 504 | step_out=1; |
|---|
| 505 | end |
|---|