[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 | %======================================================================= |
---|
[924] | 54 | % Copyright 2008-2016, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France |
---|
[810] | 55 | % http://www.legi.grenoble-inp.fr |
---|
| 56 | % Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.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 | |
---|
[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 |
---|
| 81 | ParamOut.ProjObject='off';%can use projection object(option 'off'/'on', |
---|
| 82 | ParamOut.Mask='off';%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 |
---|
| 109 | ImageTypeOptions={'image','multimage','mmreader','video'};%allowed input file types(images) |
---|
[784] | 110 | FileInfo=get_file_info(FirstFileName); |
---|
[783] | 111 | FileType=FileInfo.FileType; |
---|
[592] | 112 | CheckImage=~isempty(find(strcmp(FileType,ImageTypeOptions), 1));% =1 for images |
---|
| 113 | if ~CheckImage |
---|
[642] | 114 | msgbox_uvmat('ERROR',['invalid file type input: ' FileType ' not an image']) |
---|
[451] | 115 | return |
---|
[592] | 116 | end |
---|
| 117 | |
---|
[676] | 118 | %% numbers of fields |
---|
[897] | 119 | NbSlice_i=1;%default |
---|
[676] | 120 | if isfield(Param.IndexRange,'NbSlice')&&~isempty(Param.IndexRange.NbSlice) |
---|
[897] | 121 | NbSlice_i=Param.IndexRange.NbSlice; |
---|
[592] | 122 | end |
---|
[774] | 123 | incr_j=1;%default |
---|
| 124 | if isfield(Param.IndexRange,'incr_j')&&~isempty(Param.IndexRange.incr_j) |
---|
| 125 | incr_j=Param.IndexRange.incr_j; |
---|
| 126 | end |
---|
| 127 | if isempty(first_j)||isempty(last_j) |
---|
| 128 | nbfield_j=1; |
---|
| 129 | else |
---|
| 130 | nbfield_j=numel(first_j:incr_j:last_j);%nb of fields for the j index (bursts or volume slices) |
---|
| 131 | end |
---|
[897] | 132 | first_i=1;last_i=1;incr_i=1;%default |
---|
[917] | 133 | if isfield(Param.IndexRange,'MinIndex_i'); first_i=Param.IndexRange.MinIndex_i; end |
---|
| 134 | if isfield(Param.IndexRange,'MaxIndex_i'); last_i=Param.IndexRange.MaxIndex_i; end |
---|
[774] | 135 | if isfield(Param.IndexRange,'incr_i')&&~isempty(Param.IndexRange.incr_i) |
---|
| 136 | incr_i=Param.IndexRange.incr_i; |
---|
| 137 | end |
---|
| 138 | nbfield_i=numel(first_i:incr_i:last_i);%nb of fields for the i index (bursts or volume slices) |
---|
[676] | 139 | nbfield=nbfield_j*nbfield_i; %total number of fields |
---|
[897] | 140 | nbfield_i=floor(nbfield/NbSlice_i);%total number of indexes in a slice (adjusted to an integer number of slices) |
---|
[592] | 141 | |
---|
[676] | 142 | %% setting of parameters specific to sub_background |
---|
| 143 | nbaver_init=23; %default number of images used for the sliding background: to be adjusted later to include an integer number of bursts |
---|
[592] | 144 | if nbfield_i~=1 |
---|
| 145 | nbaver=floor(nbaver_init/nbfield_j); % number of bursts used for the sliding background, |
---|
[854] | 146 | if isequal(mod(nbaver,2),0)% if nbaver is even |
---|
[592] | 147 | nbaver=nbaver+1;%put the number of burst to an odd number (so the middle burst is defined) |
---|
| 148 | end |
---|
| 149 | nbaver_init=nbaver*nbfield_j;%propose by default an integer number of bursts |
---|
| 150 | end |
---|
| 151 | |
---|
[676] | 152 | prompt = {'volume scan mode (Yes/No)';'Number of images for the sliding background (MUST FIT IN COMPUTER MEMORY)';... |
---|
| 153 | 'the luminosity rank chosen to define the background (0.1=for dense particle seeding, 0.5 (median) for sparse particles'}; |
---|
| 154 | dlg_title = 'get (slice by slice) a sliding background and substract to each image'; |
---|
| 155 | num_lines= 3; |
---|
| 156 | def = { 'No';num2str(nbaver_init);'0.1'}; |
---|
| 157 | answer = inputdlg(prompt,dlg_title,num_lines,def); |
---|
[917] | 158 | if isempty(answer) |
---|
| 159 | return |
---|
| 160 | end |
---|
[676] | 161 | %check input consistency |
---|
[897] | 162 | if strcmp(answer{1},'No') && ~isequal(NbSlice_i,1) |
---|
| 163 | check=msgbox_uvmat('INPUT_Y-N',['confirm the multi-level splitting into ' num2str(NbSlice_i) ' slices']); |
---|
[676] | 164 | if ~strcmp(check,'Yes') |
---|
| 165 | return |
---|
[592] | 166 | end |
---|
| 167 | end |
---|
[676] | 168 | if strcmp(answer{1},'Yes') |
---|
[897] | 169 | step=2;%the sliding background is shifted by the length of one burst, assumed =2 for volume ;ode |
---|
| 170 | ParamOut.NbSlice=1; %nbre of slices displayed |
---|
[676] | 171 | else |
---|
| 172 | step=nbfield_j;%case of bursts: the sliding background is shifted by the length of one burst |
---|
| 173 | end |
---|
[854] | 174 | nbaver_ima=str2double(answer{2});%number of images for the sliding background |
---|
[676] | 175 | nbaver=ceil(nbaver_ima/step);%number of bursts for the sliding background |
---|
[854] | 176 | if isequal(mod(nbaver,2),0)% if nbaver is even |
---|
[676] | 177 | nbaver=nbaver+1;%set the number of bursts to an odd number (so the middle burst is defined) |
---|
| 178 | end |
---|
| 179 | nbaver_ima=nbaver*step;% correct the nbre of images corresponding to nbaver |
---|
[599] | 180 | ParamOut.ActionInput.CheckVolume=strcmp(answer{1},'Yes'); |
---|
| 181 | ParamOut.ActionInput.SlidingSequenceLength=nbaver_ima; |
---|
[854] | 182 | ParamOut.ActionInput.BrightnessRankThreshold=str2double(answer{3}); |
---|
[592] | 183 | |
---|
| 184 | % apply the image rescaling function 'level' (avoid the blinking effects of bright particles) |
---|
| 185 | answer=msgbox_uvmat('INPUT_Y-N','apply image rescaling function levels.m after sub_background'); |
---|
[599] | 186 | ParamOut.ActionInput.CheckLevelTransform=strcmp(answer,'Yes'); |
---|
[592] | 187 | return |
---|
[24] | 188 | end |
---|
[676] | 189 | %%%%%%%%%%%%%%%%% STOP HERE FOR PAMETER INPUT MODE %%%%%%%%%%%%%%%%% |
---|
[24] | 190 | |
---|
[592] | 191 | %% read input parameters from an xml file if input is a file name (batch mode) |
---|
| 192 | checkrun=1; |
---|
[904] | 193 | RUNHandle=[]; |
---|
| 194 | WaitbarHandle=[]; |
---|
[457] | 195 | if ischar(Param) |
---|
[592] | 196 | Param=xml2struct(Param);% read Param as input file (batch case) |
---|
| 197 | checkrun=0; |
---|
[904] | 198 | else |
---|
[624] | 199 | hseries=findobj(allchild(0),'Tag','series'); |
---|
| 200 | RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series |
---|
| 201 | WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series |
---|
[904] | 202 | end |
---|
[676] | 203 | |
---|
| 204 | %% input preparation |
---|
[897] | 205 | NbSlice_i=Param.IndexRange.NbSlice; |
---|
| 206 | if ~isequal(NbSlice_i,1) |
---|
| 207 | display(['multi-level splitting into ' num2str(NbSlice_i) ' slices']); |
---|
[592] | 208 | end |
---|
[457] | 209 | RootPath=Param.InputTable(:,1); |
---|
| 210 | RootFile=Param.InputTable(:,3); |
---|
| 211 | SubDir=Param.InputTable(:,2); |
---|
| 212 | NomType=Param.InputTable(:,4); |
---|
| 213 | FileExt=Param.InputTable(:,5); |
---|
[897] | 214 | %hdisp=disp_uvmat('WAITING...','checking the file series',checkrun); |
---|
| 215 | [filecell,i1_series,i2_series,j1_series]=get_file_series(Param); |
---|
| 216 | % if ~isempty(hdisp),delete(hdisp),end; |
---|
[676] | 217 | %%%%%%%%%%%% |
---|
| 218 | % The cell array filecell is the list of input file names, while |
---|
| 219 | % filecell{iview,fileindex}: |
---|
| 220 | % iview: line in the table corresponding to a given file series |
---|
| 221 | % fileindex: file index within the file series, |
---|
| 222 | % 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 |
---|
| 223 | % i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices |
---|
| 224 | %%%%%%%%%%%% |
---|
[784] | 225 | [FileInfo{1},MovieObject{1}]=get_file_info(filecell{1,1}); |
---|
[783] | 226 | FileType{1}=FileInfo{1}.FileType; |
---|
[592] | 227 | if ~isempty(j1_series{1}) |
---|
| 228 | frame_index{1}=j1_series{1}; |
---|
| 229 | else |
---|
| 230 | frame_index{1}=i1_series{1}; |
---|
| 231 | end |
---|
[451] | 232 | |
---|
[897] | 233 | |
---|
| 234 | %% output file naming |
---|
[592] | 235 | FileExtOut='.png'; % write result as .png images for image inputs |
---|
| 236 | if strcmp(lower(NomType{1}(end)),'a') |
---|
| 237 | NomTypeOut=NomType{1};%case of letter appendix |
---|
[802] | 238 | elseif isempty(j1_series{1}) |
---|
[592] | 239 | NomTypeOut='_1'; |
---|
[454] | 240 | else |
---|
[592] | 241 | NomTypeOut='_1_1';% caseof purely numerical indexing |
---|
[454] | 242 | end |
---|
[592] | 243 | OutputDir=[Param.OutputSubDir Param.OutputDirExt]; |
---|
[451] | 244 | |
---|
[897] | 245 | %% file index parameters |
---|
| 246 | % NbSlice_i: nbre of slices for i index: different of of 1 for multi-level, |
---|
| 247 | % the function sub_background is then relaunched by the GUI series for each |
---|
| 248 | % slice, incrementing the first index i by 1 |
---|
| 249 | % NbSlice_j: nbre of slices in volume mode |
---|
| 250 | % nbfield : total number of images treated per slice |
---|
| 251 | % step: shift of image index at each step of the sliding background (corresponding to the nbre of images in a burst) |
---|
| 252 | % nbaver_ima: nbre of the images in the sliding sequence used for the background |
---|
| 253 | % nbaver=nbaver_ima/step: nbre of bursts corresponding to nbaver_ima images. It has been adjusted so that nbaver is an odd integer |
---|
| 254 | nbfield_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices) |
---|
| 255 | nbfield_i=size(i1_series{1},2); %nb of fields for the i index |
---|
[917] | 256 | |
---|
[897] | 257 | if Param.ActionInput.CheckVolume |
---|
| 258 | step=2;% we assume the burst contains only one image pair |
---|
| 259 | NbSlice_j=nbfield_j; |
---|
| 260 | NbSlice=nbfield_j; |
---|
| 261 | nbfield_series=nbfield_i; |
---|
| 262 | else |
---|
[592] | 263 | step=nbfield_j;%case of bursts: the sliding background is shifted by the length of one burst |
---|
[897] | 264 | NbSlice_j=1; |
---|
| 265 | NbSlice=NbSlice_i; |
---|
[917] | 266 | %nbfield_i=floor(nbfield_i/NbSlice_i);%total number of indexes in a slice (adjusted to an integer number of slices) |
---|
| 267 | %nbfield=nbfield_i*NbSlice_i; %total number of fields after adjustement |
---|
| 268 | nbfield_series=nbfield_i*nbfield_j; |
---|
[54] | 269 | end |
---|
[917] | 270 | nbfield=nbfield_j*nbfield_i; %total number of fields |
---|
[599] | 271 | nbaver_ima=Param.ActionInput.SlidingSequenceLength;%number of images for the sliding background |
---|
[592] | 272 | nbaver=ceil(nbaver_ima/step);%number of bursts for the sliding background |
---|
[854] | 273 | if isequal(mod(nbaver,2),0) |
---|
[592] | 274 | nbaver=nbaver+1;%set the number of bursts to an odd number (so the middle burst is defined) |
---|
[24] | 275 | end |
---|
[592] | 276 | nbaver_ima=nbaver*step; |
---|
| 277 | if nbaver_ima > nbfield |
---|
| 278 | display('number of images in a slice smaller than the proposed number of images for the sliding average') |
---|
| 279 | return |
---|
[24] | 280 | end |
---|
[897] | 281 | halfnbaver=floor(nbaver/2); % half width (in unit of bursts) of the sliding background |
---|
[454] | 282 | |
---|
[897] | 283 | %% calculate absolute brightness rank |
---|
[599] | 284 | rank=floor(Param.ActionInput.BrightnessRankThreshold*nbaver_ima); |
---|
[24] | 285 | if rank==0 |
---|
| 286 | rank=1;%rank selected in the sorted image series |
---|
| 287 | end |
---|
| 288 | |
---|
[239] | 289 | %% prealocate memory for the sliding background |
---|
[394] | 290 | try |
---|
[454] | 291 | Afirst=read_image(filecell{1,1},FileType{1},MovieObject{1},frame_index{1}(1)); |
---|
[700] | 292 | [npy,npx,nbcolor]=size(Afirst);% the argument nbcolor is important to get npx right for color images |
---|
[454] | 293 | if strcmp(class(Afirst),'uint8') % case of 8bit images |
---|
| 294 | Ak=zeros(npy,npx,nbaver_ima,'uint8'); %prealocate memory |
---|
| 295 | Asort=zeros(npy,npx,nbaver_ima,'uint8'); %prealocate memory |
---|
| 296 | else |
---|
| 297 | Ak=zeros(npy,npx,nbaver_ima,'uint16'); %prealocate memory |
---|
| 298 | Asort=zeros(npy,npx,nbaver_ima,'uint16'); %prealocate memory |
---|
| 299 | end |
---|
[239] | 300 | catch ME |
---|
[592] | 301 | msgbox_uvmat('ERROR',['sub_background/read_image/' ME.message]) |
---|
[239] | 302 | return |
---|
| 303 | end |
---|
| 304 | |
---|
[24] | 305 | |
---|
[897] | 306 | %%%%%%% LOOP ON SLICES FOR VOLUME SCAN %%%%%%% |
---|
| 307 | for j_slice=1:NbSlice_j |
---|
| 308 | %% select the series of i indices to process |
---|
| 309 | indselect=j_slice:step*NbSlice_j:nbfield;% select file indices of the slice |
---|
| 310 | for ifield=1:step-1 |
---|
| 311 | indselect=[indselect;indselect(end,:)+NbSlice_j]; |
---|
[394] | 312 | end |
---|
[214] | 313 | |
---|
[897] | 314 | %% read the first series of nbaver_ima images and sort by luminosity at each pixel |
---|
| 315 | for ifield = 1:nbaver_ima |
---|
| 316 | ifile=indselect(ifield); |
---|
| 317 | filename=filecell{1,ifile}; |
---|
| 318 | Aread=read_image(filename,FileType{1},MovieObject{1},frame_index{1}(ifile)); |
---|
| 319 | if ndims(Aread)==3;%color images |
---|
| 320 | Aread=sum(double(Aread),3);% take the sum of color components |
---|
[454] | 321 | end |
---|
[897] | 322 | Ak(:,:,ifield)=Aread; |
---|
[24] | 323 | end |
---|
[897] | 324 | Asort=sort(Ak,3);%sort the luminosity of images at each point |
---|
| 325 | B=Asort(:,:,rank);%background image |
---|
| 326 | |
---|
| 327 | %% substract the first background image to the first images |
---|
| 328 | display( 'first background image will be substracted') |
---|
| 329 | for ifield=1:step*(halfnbaver+1);% nbre of images treated by the first background image |
---|
| 330 | Acor=double(Ak(:,:,ifield))-double(B);%substract background to the current image |
---|
| 331 | Acor=(Acor>0).*Acor; % put to 0 the negative elements in Acor |
---|
| 332 | ifile=indselect(ifield); |
---|
| 333 | j1=1; |
---|
| 334 | if ~isempty(j1_series{1}) |
---|
| 335 | j1=j1_series{1}(ifile); |
---|
[676] | 336 | end |
---|
[897] | 337 | newname=fullfile_uvmat(RootPath{1},OutputDir,RootFile{1},FileExtOut,NomTypeOut,i1_series{1}(ifile),[],j1); |
---|
| 338 | |
---|
| 339 | %write result file |
---|
| 340 | if Param.ActionInput.CheckLevelTransform |
---|
| 341 | C=levels(Acor); |
---|
| 342 | imwrite(C,newname,'BitDepth',8); % save the new image |
---|
| 343 | else |
---|
| 344 | if isequal(FileInfo{1}.BitDepth,16) |
---|
| 345 | C=uint16(Acor); |
---|
| 346 | imwrite(C,newname,'BitDepth',16); % save the new image |
---|
| 347 | else |
---|
| 348 | C=uint8(Acor); |
---|
| 349 | imwrite(C,newname,'BitDepth',8); % save the new image |
---|
[442] | 350 | end |
---|
[676] | 351 | end |
---|
[897] | 352 | display([newname ' written']) |
---|
| 353 | end |
---|
| 354 | |
---|
| 355 | %% repeat the operation on a sliding series of images |
---|
| 356 | display('sliding background image will be substracted') |
---|
| 357 | if nbfield_series > nbaver_ima |
---|
| 358 | for ifield = step*(halfnbaver+1):step:nbfield_series-step*(halfnbaver+1)% ifield +iburst=index of the current processed image |
---|
| 359 | update_waitbar(WaitbarHandle,ifield/nbfield_series) |
---|
[904] | 360 | if ~isempty(RUNHandle)&&~strcmp(get(RUNHandle,'BusyAction'),'queue') |
---|
[897] | 361 | disp('program stopped by user') |
---|
| 362 | return |
---|
[676] | 363 | end |
---|
[897] | 364 | if nbaver_ima>step |
---|
| 365 | Ak(:,:,1:nbaver_ima-step)=Ak(:,:,1+step:nbaver_ima);% shift the current image series by one burst (step) |
---|
| 366 | end |
---|
| 367 | %incorporate next burst in the current image series |
---|
| 368 | for iburst=1:step |
---|
| 369 | ifile=indselect(ifield+iburst+step*halfnbaver); |
---|
| 370 | j1=1; |
---|
| 371 | if ~isempty(j1_series{1}) |
---|
| 372 | j1=j1_series{1}(ifile); |
---|
| 373 | end |
---|
| 374 | filename=fullfile_uvmat(RootPath{1},SubDir{1},RootFile{1},FileExt{1},NomType{1},i1_series{1}(ifile),[],j1); |
---|
| 375 | Aread=read_image(filename,FileType{1},MovieObject{1},i1_series{1}(ifile)); |
---|
| 376 | if ndims(Aread)==3;%color images |
---|
| 377 | Aread=sum(double(Aread),3);% take the sum of color components |
---|
| 378 | end |
---|
| 379 | Ak(:,:,nbaver_ima-step+iburst)=Aread;% fill the last burst of the current image series by the new image |
---|
| 380 | end |
---|
| 381 | Asort=sort(Ak,3);%sort the new current image series by luminosity |
---|
| 382 | B=Asort(:,:,rank);%current background image |
---|
| 383 | %substract the background for the current burst |
---|
| 384 | for iburst=1:step |
---|
| 385 | 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 |
---|
| 386 | Acor=(Acor>0).*Acor; % put to 0 the negative elements in Acor |
---|
| 387 | ifile=indselect(ifield+iburst); |
---|
| 388 | if ~isempty(j1_series{1}) |
---|
| 389 | j1=j1_series{1}(ifile); |
---|
| 390 | end |
---|
| 391 | newname=fullfile_uvmat(RootPath{1},OutputDir,RootFile{1},FileExtOut,NomTypeOut,i1_series{1}(ifile),[],j1); |
---|
| 392 | %write result file |
---|
| 393 | if Param.ActionInput.CheckLevelTransform |
---|
| 394 | C=levels(Acor); |
---|
| 395 | imwrite(C,newname,'BitDepth',8); % save the new image |
---|
[676] | 396 | else |
---|
[897] | 397 | if isequal(FileInfo{1}.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 |
---|
[214] | 404 | end |
---|
[897] | 405 | display([newname ' written']) |
---|
[24] | 406 | end |
---|
[676] | 407 | end |
---|
[24] | 408 | end |
---|
[897] | 409 | |
---|
| 410 | %% substract the background from the last images |
---|
| 411 | display('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(ifield); |
---|
| 416 | if ~isempty(j1_series{1}) |
---|
| 417 | j1=j1_series{1}(ifile); |
---|
| 418 | end |
---|
| 419 | newname=fullfile_uvmat(RootPath{1},OutputDir,RootFile{1},FileExtOut,NomTypeOut,i1_series{1}(ifile),[],j1); |
---|
| 420 | %write result file |
---|
| 421 | if Param.ActionInput.CheckLevelTransform |
---|
| 422 | C=levels(Acor); |
---|
| 423 | imwrite(C,newname,'BitDepth',8); % save the new image |
---|
[616] | 424 | else |
---|
[897] | 425 | if isequal(FileInfo{1}.BitDepth,16) |
---|
| 426 | C=uint16(Acor); |
---|
| 427 | imwrite(C,newname,'BitDepth',16); % save the new image |
---|
| 428 | else |
---|
| 429 | C=uint8(Acor); |
---|
| 430 | imwrite(C,newname,'BitDepth',8); % save the new image |
---|
| 431 | end |
---|
[394] | 432 | end |
---|
[897] | 433 | display([newname ' written']) |
---|
[394] | 434 | end |
---|
[24] | 435 | end |
---|
| 436 | |
---|
[214] | 437 | |
---|
| 438 | function C=levels(A) |
---|
| 439 | %whos A; |
---|
| 440 | B=double(A(:,:,1)); |
---|
| 441 | windowsize=round(min(size(B,1),size(B,2))/20); |
---|
| 442 | windowsize=floor(windowsize/2)*2+1; |
---|
[239] | 443 | ix=1/2-windowsize/2:-1/2+windowsize/2;% |
---|
[214] | 444 | %del=np/3; |
---|
| 445 | %fct=exp(-(ix/del).^2); |
---|
| 446 | fct2=cos(ix/(windowsize-1)/2*pi/2); |
---|
| 447 | %Mfiltre=(ones(5,5)/5^2); |
---|
| 448 | %Mfiltre=fct2'; |
---|
| 449 | Mfiltre=fct2'*fct2; |
---|
| 450 | Mfiltre=Mfiltre/(sum(sum(Mfiltre))); |
---|
| 451 | |
---|
| 452 | C=filter2(Mfiltre,B); |
---|
| 453 | C(:,1:windowsize)=C(:,windowsize)*ones(1,windowsize); |
---|
| 454 | C(:,end-windowsize+1:end)=C(:,end-windowsize+1)*ones(1,windowsize); |
---|
| 455 | C(1:windowsize,:)=ones(windowsize,1)*C(windowsize,:); |
---|
| 456 | C(end-windowsize+1:end,:)=ones(windowsize,1)*C(end-windowsize,:); |
---|
| 457 | C=tanh(B./(2*C)); |
---|
| 458 | [n,c]=hist(reshape(C,1,[]),100); |
---|
| 459 | % figure;plot(c,n); |
---|
| 460 | |
---|
| 461 | [m,i]=max(n); |
---|
| 462 | c_max=c(i); |
---|
| 463 | [dummy,index]=sort(abs(c-c(i))); |
---|
| 464 | n=n(index); |
---|
| 465 | c=c(index); |
---|
| 466 | i_select = find(cumsum(n)<0.95*sum(n)); |
---|
| 467 | if isempty(i_select) |
---|
| 468 | i_select = 1:length(c); |
---|
| 469 | end |
---|
| 470 | c_select=c(i_select); |
---|
| 471 | n_select=n(i_select); |
---|
| 472 | cmin=min(c_select); |
---|
| 473 | cmax=max(c_select); |
---|
| 474 | C=(C-cmin)/(cmax-cmin)*256; |
---|
[810] | 475 | C=uint8(C); |
---|