[457] | 1 | %'sub_background': substract a sliding background to an image series |
---|
| 2 | % This is an example of action on a series of input images |
---|
[169] | 3 | %------------------------------------------------------------------------ |
---|
[24] | 4 | % Method: |
---|
| 5 | %calculate the background image by sorting the luminosity of each point |
---|
| 6 | % over a sliding sub-sequence of 'nbaver_ima' images. |
---|
| 7 | % The luminosity value of rank 'rank' is selected as the |
---|
| 8 | % 'background'. rank=nbimages/2 gives the median value. Smaller values are appropriate |
---|
| 9 | % for a dense set of particles. The extrem value rank=1 gives the true minimum |
---|
| 10 | % luminosity, but it can be polluted by noise. |
---|
| 11 | % Organization of image indices: |
---|
[454] | 12 | % The program is working on a series of images, |
---|
| 13 | % The processing can be done over groups of nbfield2 consecutive files in slices (parameter NbSlice) |
---|
| 14 | % In the mode 'volume', nbfield2=1 (1 image at each level)and NbSlice (=nbfield_j) |
---|
| 15 | % Else nbfield2=nbfield_j =nbre of images in a burst (j index) |
---|
[451] | 16 | |
---|
[457] | 17 | % function GUI_config=sub_background(Param) |
---|
[169] | 18 | % |
---|
[451] | 19 | %%%%%%%%%%% GENERAL TO ALL SERIES ACTION FCTS %%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
[457] | 20 | % |
---|
| 21 | % This function is used in four modes by the GUI series: |
---|
| 22 | % 1) config GUI: with no input argument, the function determine the suitable GUI configuration |
---|
| 23 | % 2) interactive input: the function is used to interactively introduce input parameters, and then stops |
---|
| 24 | % 3) RUN: the function itself runs, when an appropriate input structure Param has been introduced. |
---|
| 25 | % 4) BATCH: the function itself proceeds in BATCH mode, using an xml file 'Param' as input. |
---|
| 26 | % |
---|
[451] | 27 | %OUTPUT |
---|
[454] | 28 | % GUI_series_config=list of options in the GUI series.fig needed for the function |
---|
[451] | 29 | % |
---|
| 30 | %INPUT: |
---|
| 31 | % In run mode, the input parameters are given as a Matlab structure Param copied from the GUI series. |
---|
| 32 | % In batch mode, Param is the name of the corresponding xml file containing the same information |
---|
| 33 | % In the absence of input (as activated when the current Action is selected |
---|
[454] | 34 | % in series), the function ouput GUI_series_config set the activation of the needed GUI elements |
---|
[451] | 35 | % |
---|
| 36 | % Param contains the elements:(use the menu bar command 'export/GUI config' in series to see the current structure Param) |
---|
| 37 | % .InputTable: cell of input file names, (several lines for multiple input) |
---|
| 38 | % each line decomposed as {RootPath,SubDir,Rootfile,NomType,Extension} |
---|
| 39 | % .OutputSubDir: name of the subdirectory for data outputs |
---|
| 40 | % .OutputDir: directory for data outputs, including path |
---|
| 41 | % .Action: .ActionName: name of the current activated function |
---|
| 42 | % .ActionPath: path of the current activated function |
---|
| 43 | % .IndexRange: set the file or frame indices on which the action must be performed |
---|
| 44 | % .FieldTransform: .TransformName: name of the selected transform function |
---|
| 45 | % .TransformPath: path of the selected transform function |
---|
| 46 | % .TransformHandle: corresponding function handle |
---|
| 47 | % .InputFields: sub structure describing the input fields withfields |
---|
| 48 | % .FieldName: name of the field |
---|
| 49 | % .VelType: velocity type |
---|
| 50 | % .FieldName_1: name of the second field in case of two input series |
---|
| 51 | % .VelType_1: velocity type of the second field in case of two input series |
---|
| 52 | % .ProjObject: %sub structure describing a projection object (read from ancillary GUI set_object) |
---|
| 53 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
| 54 | |
---|
| 55 | |
---|
[455] | 56 | function GUI_config=sub_background (Param) |
---|
[24] | 57 | |
---|
[451] | 58 | %% set the input elements needed on the GUI series when the action is selected in the menu ActionName |
---|
| 59 | if ~exist('Param','var') % case with no input parameter |
---|
[454] | 60 | GUI_config={'NbViewMax';1;...% max nbre of input file series (default='' , no limitation) |
---|
[451] | 61 | 'AllowInputSort';'off';...% allow alphabetic sorting of the list of input files (options 'off'/'on', 'off' by default) |
---|
[457] | 62 | 'WholeIndexRange';'on';...% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default) |
---|
[24] | 63 | 'NbSlice';'on'; ...%nbre of slices ('off' by default) |
---|
[451] | 64 | 'VelType';'off';...% menu for selecting the velocity type (options 'off'/'one'/'two', 'off' by default) |
---|
| 65 | 'FieldName';'off';...% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default) |
---|
| 66 | 'FieldTransform'; 'off';...%can use a transform function |
---|
| 67 | 'ProjObject';'off';...%can use projection object(option 'off'/'on', |
---|
| 68 | 'Mask';'off';...%can use mask option (option 'off'/'on', 'off' by default) |
---|
[454] | 69 | 'OutputDirExt';'.sback';...%set the output dir extension |
---|
[24] | 70 | ''}; |
---|
[451] | 71 | return |
---|
[24] | 72 | end |
---|
| 73 | |
---|
[451] | 74 | %%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%% |
---|
[457] | 75 | %% select different modes, RUN, parameter input, BATCH |
---|
[451] | 76 | % BATCH case: read the xml file for batch case |
---|
[457] | 77 | if ischar(Param) |
---|
| 78 | if strcmp(Param,'input?') |
---|
| 79 | checkrun=1;% will inly search input parameters (preparation of BATCH mode) |
---|
| 80 | else |
---|
[454] | 81 | Param=xml2struct(Param); |
---|
| 82 | checkrun=0; |
---|
[457] | 83 | end |
---|
| 84 | % RUN case: parameters introduced as the input structure Param |
---|
[454] | 85 | else |
---|
[374] | 86 | hseries=guidata(Param.hseries);%handles of the GUI series |
---|
[451] | 87 | WaitbarPos=get(hseries.waitbar_frame,'Position');%position of the waitbar on the GUI series |
---|
[457] | 88 | checkrun=2; % indicate the RUN option is used |
---|
[374] | 89 | end |
---|
[457] | 90 | |
---|
| 91 | %% root input file(s) and type |
---|
| 92 | RootPath=Param.InputTable(:,1); |
---|
| 93 | RootFile=Param.InputTable(:,3); |
---|
| 94 | SubDir=Param.InputTable(:,2); |
---|
| 95 | NomType=Param.InputTable(:,4); |
---|
| 96 | FileExt=Param.InputTable(:,5); |
---|
| 97 | |
---|
[451] | 98 | % get the set of input file names (cell array filecell), and the lists of |
---|
| 99 | % input file or frame indices i1_series,i2_series,j1_series,j2_series |
---|
| 100 | [filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param); |
---|
| 101 | % filecell{iview,fileindex}: cell array representing the list of file names |
---|
| 102 | % iview: line in the table corresponding to a given file series |
---|
| 103 | % fileindex: file index within the file series, |
---|
| 104 | % 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 |
---|
| 105 | % i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices |
---|
| 106 | % set of frame indices used for movie or multimage input |
---|
[457] | 107 | % numbers of slices and file indices |
---|
[394] | 108 | |
---|
[451] | 109 | NbSlice=1;%default |
---|
[457] | 110 | if isfield(Param.IndexRange,'NbSlice')&&~isempty(Param.IndexRange.NbSlice) |
---|
[451] | 111 | NbSlice=Param.IndexRange.NbSlice; |
---|
[54] | 112 | end |
---|
[454] | 113 | nbview=numel(i1_series);%number of input file series (lines in InputTable) |
---|
| 114 | nbfield_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices) |
---|
| 115 | nbfield_i=size(i1_series{1},2); %nb of fields for the i index |
---|
| 116 | nbfield=nbfield_j*nbfield_i; %total number of fields |
---|
| 117 | nbfield_i=floor(nbfield/NbSlice);%total number of indexes in a slice (adjusted to an integer number of slices) |
---|
| 118 | nbfield=nbfield_i*NbSlice; %total number of fields after adjustement |
---|
[451] | 119 | |
---|
| 120 | %determine the file type on each line from the first input file |
---|
[454] | 121 | ImageTypeOptions={'image','multimage','mmreader','video'};%allowed input file types(images) |
---|
[451] | 122 | |
---|
[454] | 123 | [FileType{1},FileInfo{1},MovieObject{1}]=get_file_type(filecell{1,1}); |
---|
| 124 | CheckImage{1}=~isempty(find(strcmp(FileType,ImageTypeOptions)));% =1 for images |
---|
| 125 | if ~isempty(j1_series{1}) |
---|
| 126 | frame_index{1}=j1_series{1}; |
---|
| 127 | else |
---|
| 128 | frame_index{1}=i1_series{1}; |
---|
| 129 | end |
---|
[451] | 130 | |
---|
| 131 | %% calibration data and timing: read the ImaDoc files |
---|
| 132 | %not relevant here |
---|
| 133 | |
---|
| 134 | %% check coincidence in time for several input file series |
---|
| 135 | %not relevant here |
---|
| 136 | |
---|
| 137 | %% coordinate transform or other user defined transform |
---|
| 138 | %not relevant here |
---|
| 139 | |
---|
| 140 | %%%%%%%%%%%% END STANDARD PART %%%%%%%%%%%% |
---|
| 141 | % EDIT FROM HERE |
---|
| 142 | |
---|
| 143 | %% check the validity of input file types |
---|
[454] | 144 | if CheckImage{1} |
---|
[451] | 145 | FileExtOut='.png'; % write result as .png images for image inputs |
---|
[454] | 146 | if strcmp(lower(NomType{1}(end)),'a') |
---|
| 147 | NomTypeOut=NomType{1};%case of letter appendix |
---|
| 148 | else |
---|
| 149 | NomTypeOut='_1_1';% caseof purely numerical indexing |
---|
| 150 | end |
---|
[451] | 151 | else |
---|
| 152 | msgbox_uvmat('ERROR',['invalid file type input: ' FileType{1} ' not an image']) |
---|
[54] | 153 | return |
---|
| 154 | end |
---|
| 155 | |
---|
[451] | 156 | %% Set field names and velocity types |
---|
| 157 | %not relevant here |
---|
| 158 | |
---|
| 159 | %% Initiate output fields |
---|
| 160 | %not relevant here |
---|
| 161 | |
---|
| 162 | %%% SPECIFIC PART BEGINS HERE |
---|
| 163 | NbSlice=Param.IndexRange.NbSlice; %number of slices |
---|
[454] | 164 | %siz=size(i1_series); |
---|
[24] | 165 | nbaver_init=23;%approximate number of images used for the sliding background: to be adjusted later to include an integer number of bursts |
---|
[394] | 166 | j1=[];%default |
---|
[24] | 167 | |
---|
[214] | 168 | %% adjust the proposed number of images in the sliding average to include an integer number of bursts |
---|
[454] | 169 | if nbfield_i~=1 |
---|
| 170 | nbaver=floor(nbaver_init/nbfield_j); % number of bursts used for the sliding background, |
---|
[24] | 171 | if isequal(floor(nbaver/2),nbaver) |
---|
| 172 | nbaver=nbaver+1;%put the number of burst to an odd number (so the middle burst is defined) |
---|
| 173 | end |
---|
[454] | 174 | nbaver_init=nbaver*nbfield_j;%propose by default an integer number of bursts |
---|
[24] | 175 | end |
---|
| 176 | |
---|
[457] | 177 | %% input of specific parameters |
---|
| 178 | if checkrun %get specific parameters interactively |
---|
[454] | 179 | prompt = {'volume scan mode (Yes/No)';'Number of images for the sliding background (MUST FIT IN COMPUTER MEMORY)';... |
---|
| 180 | 'the luminosity rank chosen to define the background (0.1=for dense particle seeding, 0.5 (median) for sparse particles'}; |
---|
| 181 | dlg_title = ['get (slice by slice) a sliding background and substract to each image, result in subdir ' Param.OutputDir]; |
---|
| 182 | num_lines= 3; |
---|
| 183 | def = { 'No';num2str(nbaver_init);'0.1'}; |
---|
| 184 | answer = inputdlg(prompt,dlg_title,num_lines,def); |
---|
| 185 | |
---|
| 186 | %check input consistency |
---|
| 187 | if strcmp(answer{1},'No') && ~isequal(NbSlice,1) |
---|
| 188 | check=msgbox_uvmat('INPUT_Y-N',['confirm the multi-level splitting into ' num2str(NbSlice) ' slices']); |
---|
| 189 | if ~strcmp(check,'Yes') |
---|
| 190 | return |
---|
| 191 | end |
---|
| 192 | end |
---|
| 193 | if strcmp(answer{1},'Yes') |
---|
| 194 | step=1; |
---|
| 195 | else |
---|
| 196 | step=nbfield_j;%case of bursts: the sliding background is shifted by the length of one burst |
---|
| 197 | end |
---|
| 198 | nbaver_ima=str2num(answer{2});%number of images for the sliding background |
---|
| 199 | nbaver=ceil(nbaver_ima/step);%number of bursts for the sliding background |
---|
| 200 | if isequal(floor(nbaver/2),nbaver) |
---|
| 201 | nbaver=nbaver+1;%set the number of bursts to an odd number (so the middle burst is defined) |
---|
| 202 | end |
---|
| 203 | nbaver_ima=nbaver*step; |
---|
| 204 | if nbaver_ima > nbfield |
---|
| 205 | msgbox_uvmat('ERROR','number of images in a slice smaller than the proposed number of images for the sliding average') |
---|
| 206 | return |
---|
| 207 | end |
---|
| 208 | GUI_config.CheckVolume=strcmp(answer{1},'Yes'); |
---|
| 209 | GUI_config.SlidingSequenceSize=nbaver_ima; |
---|
| 210 | GUI_config.BrightnessRankThreshold=str2num(answer{3}); |
---|
| 211 | |
---|
| 212 | % apply the image rescaling function 'level' (avoid the blinking effects of bright particles) |
---|
| 213 | answer=msgbox_uvmat('INPUT_Y-N','apply image rescaling function levels.m after sub_background'); |
---|
| 214 | GUI_config.CheckLevelTransform=strcmp(answer,'Yes'); |
---|
[457] | 215 | if checkrun==2 |
---|
| 216 | return |
---|
| 217 | end |
---|
| 218 | %%%%%%%%%%%%%%%%%%%%%% STOP HERE FOR PAMETER INPUT MODE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
[24] | 219 | else |
---|
[454] | 220 | GUI_config=Param.Specific; |
---|
| 221 | if isequal(GUI_config.CheckVolume,1) |
---|
| 222 | step=1; |
---|
[24] | 223 | else |
---|
[454] | 224 | step=nbfield_j;%case of bursts: the sliding background is shifted by the length of one burst |
---|
[24] | 225 | end |
---|
[454] | 226 | nbaver_ima=GUI_config.SlidingSequenceSize;%number of images for the sliding background |
---|
| 227 | nbaver=ceil(nbaver_ima/step);%number of bursts for the sliding background |
---|
| 228 | if isequal(floor(nbaver/2),nbaver) |
---|
| 229 | nbaver=nbaver+1;%set the number of bursts to an odd number (so the middle burst is defined) |
---|
| 230 | end |
---|
| 231 | nbaver_ima=nbaver*step; |
---|
| 232 | if nbaver_ima > nbfield |
---|
| 233 | msgbox_uvmat('ERROR','number of images in a slice smaller than the proposed number of images for the sliding average') |
---|
[24] | 234 | return |
---|
| 235 | end |
---|
| 236 | end |
---|
[454] | 237 | |
---|
| 238 | % calculate absolute brightness rank |
---|
| 239 | rank=floor(GUI_config.BrightnessRankThreshold*nbaver_ima); |
---|
[24] | 240 | if rank==0 |
---|
| 241 | rank=1;%rank selected in the sorted image series |
---|
| 242 | end |
---|
| 243 | |
---|
[239] | 244 | %% prealocate memory for the sliding background |
---|
[394] | 245 | try |
---|
[454] | 246 | Afirst=read_image(filecell{1,1},FileType{1},MovieObject{1},frame_index{1}(1)); |
---|
| 247 | [npy,npx]=size(Afirst); |
---|
| 248 | if strcmp(class(Afirst),'uint8') % case of 8bit images |
---|
| 249 | Ak=zeros(npy,npx,nbaver_ima,'uint8'); %prealocate memory |
---|
| 250 | Asort=zeros(npy,npx,nbaver_ima,'uint8'); %prealocate memory |
---|
| 251 | else |
---|
| 252 | Ak=zeros(npy,npx,nbaver_ima,'uint16'); %prealocate memory |
---|
| 253 | Asort=zeros(npy,npx,nbaver_ima,'uint16'); %prealocate memory |
---|
| 254 | end |
---|
[239] | 255 | catch ME |
---|
| 256 | msgbox_uvmat('ERROR',ME.message) |
---|
| 257 | return |
---|
| 258 | end |
---|
| 259 | |
---|
[442] | 260 | %% update the xml file |
---|
[454] | 261 | % SubDirBase=regexprep(Param.InputTable{1,2},'\..*','');%take the root part of SubDir, before the first dot '.' |
---|
| 262 | % filexml=fullfile(RootPath{1},[SubDirBase '.xml']); |
---|
| 263 | % if ~exist(filexml,'file') && exist([fullfile(RootPath{1},SubDir{1},RootFile{1}) '.xml'],'file')% xml inside the image directory |
---|
| 264 | % copyfile([filebase '.xml'],filexml);% copy the .xml file |
---|
| 265 | % end |
---|
| 266 | % if exist(filexml,'file') |
---|
| 267 | % t=xmltree(filexml); |
---|
| 268 | % %update information on the first image name in the series |
---|
| 269 | % uid_Heading=find(t,'ImaDoc/Heading'); |
---|
| 270 | % if isempty(uid_Heading) |
---|
| 271 | % [t,uid_Heading]=add(t,1,'element','Heading'); |
---|
| 272 | % end |
---|
| 273 | % uid_ImageName=find(t,'ImaDoc/Heading/ImageName'); |
---|
| 274 | % if ~isempty(j1_series{1}) |
---|
| 275 | % j1=j1_series{1}(1); |
---|
| 276 | % end |
---|
| 277 | % ImageName=fullfile_uvmat([dir_images term],'',RootFile{1},'.png',NomType,i1_series(1,1),[],j1); |
---|
| 278 | % [pth,ImageName]=fileparts(ImageName); |
---|
| 279 | % ImageName=[ImageName '.png']; |
---|
| 280 | % if isempty(uid_ImageName) |
---|
| 281 | % [t,uid_ImageName]=add(t,uid_Heading,'element','ImageName'); |
---|
| 282 | % end |
---|
| 283 | % uid_value=children(t,uid_ImageName); |
---|
| 284 | % if isempty(uid_value) |
---|
| 285 | % t=add(t,uid_ImageName,'chardata',ImageName);%indicate name of the first image, with ;png extension |
---|
| 286 | % else |
---|
| 287 | % t=set(t,uid_value(1),'value',ImageName);%indicate name of the first image, with ;png extension |
---|
| 288 | % end |
---|
| 289 | % |
---|
| 290 | % %add information about image transform |
---|
| 291 | % [t,new_uid]=add(t,1,'element','ImageTransform'); |
---|
| 292 | % [t,NameFunction_uid]=add(t,new_uid,'element','NameFunction'); |
---|
| 293 | % [t]=add(t,NameFunction_uid,'chardata','sub_background'); |
---|
| 294 | % if GUI_config.CheckLevel |
---|
| 295 | % [t,NameFunction_uid]=add(t,new_uid,'element','NameFunction'); |
---|
| 296 | % [t]=add(t,NameFunction_uid,'chardata','levels'); |
---|
| 297 | % end |
---|
| 298 | % [t,NbSlice_uid]=add(t,new_uid,'element','NbSlice'); |
---|
| 299 | % [t]=add(t,new_uid,'chardata',num2str(NbSlice)); |
---|
| 300 | % [t,NbSlidingImages_uid]=add(t,new_uid,'element','NbSlidingImages'); |
---|
| 301 | % [t]=add(t,NbSlidingImages_uid,'chardata',num2str(nbaver)); |
---|
| 302 | % [t,LuminosityRank_uid]=add(t,new_uid,'element','RankBackground'); |
---|
| 303 | % [t]=add(t,LuminosityRank_uid,'chardata',num2str(rank));% luminosity rank almong the nbaver sliding images |
---|
| 304 | % save(t,filexml) |
---|
| 305 | % end |
---|
[24] | 306 | %copy the mask |
---|
[442] | 307 | % if exist([filebase '_1mask_1'],'file') |
---|
| 308 | % copyfile([filebase '_1mask_1'],[filebase_b '_1mask_1']);% copy the mask file |
---|
| 309 | % end |
---|
[24] | 310 | |
---|
| 311 | %MAIN LOOP ON SLICES |
---|
[451] | 312 | for islice=1:NbSlice |
---|
[214] | 313 | %% select the series of image indices at the level islice |
---|
[454] | 314 | indselect=islice:NbSlice*step:nbfield;% select file indices of the slice |
---|
| 315 | for ifield=1:step-1 |
---|
| 316 | indselect=[indselect;indselect(end,:)+1]; |
---|
[394] | 317 | end |
---|
[214] | 318 | |
---|
| 319 | %% read the first series of nbaver_ima images and sort by luminosity at each pixel |
---|
[24] | 320 | for ifield = 1:nbaver_ima |
---|
[54] | 321 | ifile=indselect(ifield); |
---|
[394] | 322 | filename=filecell{1,ifile}; |
---|
[454] | 323 | Aread=read_image(filename,FileType{1},MovieObject{1},frame_index{1}(ifile)); |
---|
| 324 | if ndims(Aread)==3;%color images |
---|
| 325 | Aread=sum(double(Aread),3);% take the sum of color components |
---|
| 326 | end |
---|
[394] | 327 | Ak(:,:,ifield)=Aread; |
---|
[24] | 328 | end |
---|
| 329 | Asort=sort(Ak,3);%sort the luminosity of images at each point |
---|
| 330 | B=Asort(:,:,rank);%background image |
---|
[394] | 331 | display( 'first background image will be substracted') |
---|
[454] | 332 | nbfirst=(ceil(nbaver/2))*step; |
---|
[24] | 333 | for ifield=1:nbfirst |
---|
[394] | 334 | Acor=double(Ak(:,:,ifield))-double(B);%substract background to the current image |
---|
| 335 | Acor=(Acor>0).*Acor; % put to 0 the negative elements in Acor |
---|
| 336 | ifile=indselect(ifield); |
---|
| 337 | if ~isempty(j1_series{1}) |
---|
| 338 | j1=j1_series{1}(ifile); |
---|
| 339 | end |
---|
[454] | 340 | newname=fullfile_uvmat(RootPath{1},Param.OutputSubDir,RootFile{1},FileExtOut,NomTypeOut,i1_series{1}(ifile),[],j1); |
---|
| 341 | |
---|
| 342 | %write result file |
---|
| 343 | if GUI_config.CheckLevelTransform |
---|
| 344 | C=levels(Acor); |
---|
[394] | 345 | imwrite(C,newname,'BitDepth',8); % save the new image |
---|
| 346 | else |
---|
[454] | 347 | if isequal(FileInfo{1}.BitDepth,16) |
---|
| 348 | C=uint16(Acor); |
---|
| 349 | imwrite(C,newname,'BitDepth',16); % save the new image |
---|
| 350 | else |
---|
| 351 | C=uint8(Acor); |
---|
| 352 | imwrite(C,newname,'BitDepth',8); % save the new image |
---|
| 353 | end |
---|
[394] | 354 | end |
---|
[454] | 355 | display([newname ' written']) |
---|
[24] | 356 | end |
---|
[214] | 357 | |
---|
[454] | 358 | %% repeat the operation on a sliding series of images |
---|
[214] | 359 | display('sliding background image will be substracted') |
---|
[454] | 360 | if nbfield_i > nbaver_ima |
---|
| 361 | for ifield = step*ceil(nbaver/2)+1:step:nbfield_i-step*floor(nbaver/2) |
---|
[442] | 362 | if checkrun |
---|
| 363 | stopstate=get(hseries.RUN,'BusyAction'); |
---|
[454] | 364 | update_waitbar(hseries.waitbar_frame,WaitbarPos,(ifield+(islice-1)*nbfield_i)/(nbfield_i*NbSlice)) |
---|
[442] | 365 | else |
---|
| 366 | stopstate='queue'; |
---|
| 367 | end |
---|
| 368 | if isequal(stopstate,'queue')% enable STOP command |
---|
[240] | 369 | Ak(:,:,1:nbaver_ima-step)=Ak(:,:,1+step:nbaver_ima);% shift the current image series by one burst (step) |
---|
[24] | 370 | %incorporate next burst in the current image series |
---|
| 371 | for iburst=1:step |
---|
| 372 | ifile=indselect(ifield+step*floor(nbaver/2)+iburst-1); |
---|
[454] | 373 | filename=fullfile_uvmat(RootPath{1},SubDir{1},RootFile{1},FileExt{1},NomType{1},i1_series{1}(ifile),[],j1_series{1}(ifile)); |
---|
| 374 | Aread=read_image(filename,FileType{1},MovieObject{1},i1_series{1}(ifile)); |
---|
| 375 | if ndims(Aread)==3;%color images |
---|
| 376 | Aread=sum(double(Aread),3);% take the sum of color components |
---|
| 377 | end |
---|
[24] | 378 | Ak(:,:,nbaver_ima-step+iburst)=Aread; |
---|
| 379 | end |
---|
| 380 | Asort=sort(Ak,3);%sort the new current image series by luminosity |
---|
| 381 | B=Asort(:,:,rank);%current background image |
---|
| 382 | for iburst=1:step |
---|
| 383 | index=step*floor(nbaver/2)+iburst; |
---|
| 384 | Acor=double(Ak(:,:,index))-double(B); |
---|
| 385 | Acor=(Acor>0).*Acor; % put to 0 the negative elements in Acor |
---|
| 386 | ifile=indselect(ifield+iburst-1); |
---|
[394] | 387 | if ~isempty(j1_series{1}) |
---|
| 388 | j1=j1_series{1}(ifile); |
---|
| 389 | end |
---|
[454] | 390 | newname=fullfile_uvmat(RootPath{1},Param.OutputSubDir,RootFile{1},FileExtOut,NomTypeOut,i1_series{1}(ifile),[],j1); |
---|
| 391 | %write result file |
---|
| 392 | if GUI_config.CheckLevelTransform |
---|
| 393 | C=levels(Acor); |
---|
[214] | 394 | imwrite(C,newname,'BitDepth',8); % save the new image |
---|
| 395 | else |
---|
[454] | 396 | if isequal(FileInfo{1}.BitDepth,16) |
---|
| 397 | C=uint16(Acor); |
---|
| 398 | imwrite(C,newname,'BitDepth',16); % save the new image |
---|
| 399 | else |
---|
| 400 | C=uint8(Acor); |
---|
| 401 | imwrite(C,newname,'BitDepth',8); % save the new image |
---|
| 402 | end |
---|
[214] | 403 | end |
---|
[454] | 404 | display([newname ' written']) |
---|
| 405 | |
---|
[214] | 406 | end |
---|
[24] | 407 | else |
---|
| 408 | return |
---|
| 409 | end |
---|
| 410 | end |
---|
| 411 | end |
---|
[394] | 412 | |
---|
| 413 | %% substract the background from the last images |
---|
[214] | 414 | display('last background image will be substracted') |
---|
[454] | 415 | ifield=nbfield_i-(step*ceil(nbaver/2))+1:nbfield_i; |
---|
| 416 | for ifield=nbfield_i-(step*floor(nbaver/2))+1:nbfield_i |
---|
| 417 | index=ifield-nbfield_i+step*(2*floor(nbaver/2)+1); |
---|
[394] | 418 | Acor=double(Ak(:,:,index))-double(B); |
---|
| 419 | Acor=(Acor>0).*Acor; % put to 0 the negative elements in Acor |
---|
| 420 | ifile=indselect(ifield); |
---|
| 421 | if ~isempty(j1_series{1}) |
---|
| 422 | j1=j1_series{1}(ifile); |
---|
| 423 | end |
---|
[454] | 424 | newname=fullfile_uvmat(RootPath{1},Param.OutputSubDir,RootFile{1},FileExtOut,NomTypeOut,i1_series{1}(ifile),[],j1); |
---|
| 425 | |
---|
| 426 | %write result file |
---|
| 427 | if GUI_config.CheckLevelTransform |
---|
| 428 | C=levels(Acor); |
---|
[394] | 429 | imwrite(C,newname,'BitDepth',8); % save the new image |
---|
| 430 | else |
---|
[454] | 431 | if isequal(FileInfo{1}.BitDepth,16) |
---|
| 432 | C=uint16(Acor); |
---|
| 433 | imwrite(C,newname,'BitDepth',16); % save the new image |
---|
| 434 | else |
---|
| 435 | C=uint8(Acor); |
---|
| 436 | imwrite(C,newname,'BitDepth',8); % save the new image |
---|
| 437 | end |
---|
[394] | 438 | end |
---|
[454] | 439 | display([newname ' written']) |
---|
[394] | 440 | end |
---|
[24] | 441 | end |
---|
| 442 | |
---|
| 443 | %finish the waitbar |
---|
[442] | 444 | if checkrun |
---|
| 445 | update_waitbar(hseries.waitbar,WaitbarPos,1) |
---|
| 446 | end |
---|
[24] | 447 | |
---|
[214] | 448 | |
---|
| 449 | function C=levels(A) |
---|
| 450 | %whos A; |
---|
| 451 | B=double(A(:,:,1)); |
---|
| 452 | windowsize=round(min(size(B,1),size(B,2))/20); |
---|
| 453 | windowsize=floor(windowsize/2)*2+1; |
---|
[239] | 454 | ix=1/2-windowsize/2:-1/2+windowsize/2;% |
---|
[214] | 455 | %del=np/3; |
---|
| 456 | %fct=exp(-(ix/del).^2); |
---|
| 457 | fct2=cos(ix/(windowsize-1)/2*pi/2); |
---|
| 458 | %Mfiltre=(ones(5,5)/5^2); |
---|
| 459 | %Mfiltre=fct2'; |
---|
| 460 | Mfiltre=fct2'*fct2; |
---|
| 461 | Mfiltre=Mfiltre/(sum(sum(Mfiltre))); |
---|
| 462 | |
---|
| 463 | C=filter2(Mfiltre,B); |
---|
| 464 | C(:,1:windowsize)=C(:,windowsize)*ones(1,windowsize); |
---|
| 465 | C(:,end-windowsize+1:end)=C(:,end-windowsize+1)*ones(1,windowsize); |
---|
| 466 | C(1:windowsize,:)=ones(windowsize,1)*C(windowsize,:); |
---|
| 467 | C(end-windowsize+1:end,:)=ones(windowsize,1)*C(end-windowsize,:); |
---|
| 468 | C=tanh(B./(2*C)); |
---|
| 469 | [n,c]=hist(reshape(C,1,[]),100); |
---|
| 470 | % figure;plot(c,n); |
---|
| 471 | |
---|
| 472 | [m,i]=max(n); |
---|
| 473 | c_max=c(i); |
---|
| 474 | [dummy,index]=sort(abs(c-c(i))); |
---|
| 475 | n=n(index); |
---|
| 476 | c=c(index); |
---|
| 477 | i_select = find(cumsum(n)<0.95*sum(n)); |
---|
| 478 | if isempty(i_select) |
---|
| 479 | i_select = 1:length(c); |
---|
| 480 | end |
---|
| 481 | c_select=c(i_select); |
---|
| 482 | n_select=n(i_select); |
---|
| 483 | cmin=min(c_select); |
---|
| 484 | cmax=max(c_select); |
---|
| 485 | C=(C-cmin)/(cmax-cmin)*256; |
---|
| 486 | C=uint8(C); |
---|