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