[454] | 1 | %'ima_levels': rescale the image intensity to reduce strong luminosity peaks (their blinking effects often perturbs PIV))
|
---|
[457] | 2 | % this function can be used as a template for applying a transform (here 'levels.m') to each image of a series
|
---|
[171] | 3 | %------------------------------------------------------------------------
|
---|
[454] | 4 | % function GUI_input=ima_levels(Param)
|
---|
[171] | 5 | %
|
---|
[604] | 6 | %------------------------------------------------------------------------
|
---|
[454] | 7 | %%%%%%%%%%% GENERAL TO ALL SERIES ACTION FCTS %%%%%%%%%%%%%%%%%%%%%%%%%%%
|
---|
[457] | 8 | %
|
---|
[171] | 9 | %OUTPUT
|
---|
[604] | 10 | % ParamOut: sets options in the GUI series.fig needed for the function
|
---|
[171] | 11 | %
|
---|
| 12 | %INPUT:
|
---|
[454] | 13 | % In run mode, the input parameters are given as a Matlab structure Param copied from the GUI series.
|
---|
| 14 | % In batch mode, Param is the name of the corresponding xml file containing the same information
|
---|
[604] | 15 | % when Param.Action.RUN=0 (as activated when the current Action is selected
|
---|
| 16 | % in series), the function ouput paramOut set the activation of the needed GUI elements
|
---|
[454] | 17 | %
|
---|
[604] | 18 | % Param contains the elements:(use the menu bar command 'export/GUI config' in series to
|
---|
| 19 | % see the current structure Param)
|
---|
[454] | 20 | % .InputTable: cell of input file names, (several lines for multiple input)
|
---|
| 21 | % each line decomposed as {RootPath,SubDir,Rootfile,NomType,Extension}
|
---|
| 22 | % .OutputSubDir: name of the subdirectory for data outputs
|
---|
[604] | 23 | % .OutputDirExt: directory extension for data outputs
|
---|
[454] | 24 | % .Action: .ActionName: name of the current activated function
|
---|
| 25 | % .ActionPath: path of the current activated function
|
---|
[604] | 26 | % .ActionExt: fct extension ('.m', Matlab fct, '.sh', compiled Matlab fct
|
---|
| 27 | % .RUN =0 for GUI input, =1 for function activation
|
---|
| 28 | % .RunMode='local','background', 'cluster': type of function use
|
---|
| 29 | %
|
---|
[454] | 30 | % .IndexRange: set the file or frame indices on which the action must be performed
|
---|
| 31 | % .FieldTransform: .TransformName: name of the selected transform function
|
---|
| 32 | % .TransformPath: path of the selected transform function
|
---|
| 33 | % .InputFields: sub structure describing the input fields withfields
|
---|
[604] | 34 | % .FieldName: name(s) of the field
|
---|
[454] | 35 | % .VelType: velocity type
|
---|
| 36 | % .FieldName_1: name of the second field in case of two input series
|
---|
| 37 | % .VelType_1: velocity type of the second field in case of two input series
|
---|
[604] | 38 | % .Coord_y: name of y coordinate variable
|
---|
| 39 | % .Coord_x: name of x coordinate variable
|
---|
[454] | 40 | % .ProjObject: %sub structure describing a projection object (read from ancillary GUI set_object)
|
---|
| 41 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
---|
[810] | 42 |
|
---|
| 43 | %=======================================================================
|
---|
[1126] | 44 | % Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
|
---|
[810] | 45 | % http://www.legi.grenoble-inp.fr
|
---|
[1127] | 46 | % Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.fr
|
---|
[810] | 47 | %
|
---|
| 48 | % This file is part of the toolbox UVMAT.
|
---|
| 49 | %
|
---|
| 50 | % UVMAT is free software; you can redistribute it and/or modify
|
---|
| 51 | % it under the terms of the GNU General Public License as published
|
---|
| 52 | % by the Free Software Foundation; either version 2 of the license,
|
---|
| 53 | % or (at your option) any later version.
|
---|
| 54 | %
|
---|
| 55 | % UVMAT is distributed in the hope that it will be useful,
|
---|
| 56 | % but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 57 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 58 | % GNU General Public License (see LICENSE.txt) for more details.
|
---|
| 59 | %=======================================================================
|
---|
| 60 |
|
---|
[592] | 61 | function ParamOut=ima_levels (Param)
|
---|
[454] | 62 |
|
---|
[606] | 63 | %% set the input elements needed on the GUI series when the action is selected in the menu ActionName or InputTable refreshed
|
---|
[592] | 64 | if isstruct(Param) && isequal(Param.Action.RUN,0)
|
---|
[604] | 65 | ParamOut.NbViewMax=1;% max nbre of input file series (default , no limitation)
|
---|
[592] | 66 | ParamOut.AllowInputSort='off';% allow alphabetic sorting of the list of input file SubDir (options 'off'/'on', 'off' by default)
|
---|
| 67 | ParamOut.WholeIndexRange='off';% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default)
|
---|
| 68 | ParamOut.NbSlice='off'; %nbre of slices ('off' by default)
|
---|
| 69 | ParamOut.VelType='off';% menu for selecting the velocity type (options 'off'/'one'/'two', 'off' by default)
|
---|
| 70 | ParamOut.FieldName='one';% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default)
|
---|
| 71 | ParamOut.FieldTransform = 'off';%can use a transform function
|
---|
| 72 | ParamOut.ProjObject='off';%can use projection object(option 'off'/'on',
|
---|
| 73 | ParamOut.Mask='off';%can use mask option (option 'off'/'on', 'off' by default)
|
---|
| 74 | ParamOut.OutputDirExt='.lev';%set the output dir extension
|
---|
[604] | 75 | 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
|
---|
| 76 | %check the type of the existence and type of the first input file:
|
---|
| 77 | Param.IndexRange.last_i=Param.IndexRange.first_i;%keep only the first index in the series
|
---|
[606] | 78 | if isfield(Param.IndexRange,'first_j')
|
---|
[604] | 79 | Param.IndexRange.last_j=Param.IndexRange.first_j;
|
---|
[606] | 80 | end
|
---|
[604] | 81 | filecell=get_file_series(Param);
|
---|
| 82 | if ~exist(filecell{1,1},'file')
|
---|
| 83 | msgbox_uvmat('WARNING','the first input file does not exist')
|
---|
| 84 | else
|
---|
[784] | 85 | FileInfo=get_file_info(filecell{1,1});
|
---|
[783] | 86 | FileType=FileInfo.FileType;
|
---|
[604] | 87 | if isempty(find(strcmp(FileType,{'image','multimage','mmreader','video'})));% =1 for images
|
---|
| 88 | msgbox_uvmat('ERROR',['bad input file type for ' mfilename ': an image is needed'])
|
---|
| 89 | end
|
---|
| 90 | end
|
---|
[592] | 91 | return
|
---|
[64] | 92 | end
|
---|
| 93 |
|
---|
[457] | 94 | %%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%%
|
---|
[592] | 95 | %% read input parameters from an xml file if input is a file name (batch mode)
|
---|
[624] | 96 | ParamOut=[];
|
---|
[904] | 97 | RUNHandle=[];
|
---|
| 98 | WaitbarHandle=[];
|
---|
[592] | 99 | checkrun=1;
|
---|
[457] | 100 | if ischar(Param)
|
---|
[592] | 101 | Param=xml2struct(Param);% read Param as input file (batch case)
|
---|
| 102 | checkrun=0;
|
---|
[904] | 103 | else% interactive mode in Matlab
|
---|
| 104 | hseries=findobj(allchild(0),'Tag','series');
|
---|
| 105 | RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series
|
---|
| 106 | WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series
|
---|
[442] | 107 | end
|
---|
[64] | 108 |
|
---|
[624] | 109 | %% subdirectory for output files
|
---|
| 110 | SubdirOut=[Param.OutputSubDir Param.OutputDirExt];
|
---|
| 111 |
|
---|
[604] | 112 | %% root input file names and nomenclature type (cell arrays with one element)
|
---|
[454] | 113 | RootPath=Param.InputTable(:,1);
|
---|
| 114 | RootFile=Param.InputTable(:,3);
|
---|
| 115 | SubDir=Param.InputTable(:,2);
|
---|
| 116 | NomType=Param.InputTable(:,4);
|
---|
| 117 | FileExt=Param.InputTable(:,5);
|
---|
| 118 |
|
---|
[604] | 119 |
|
---|
| 120 | %% get the set of input file names (cell array filecell), and file indices
|
---|
[457] | 121 | [filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
|
---|
| 122 | % filecell{iview,fileindex}: cell array representing the list of file names
|
---|
| 123 | % iview: line in the table corresponding to a given file series
|
---|
| 124 | % fileindex: file index within the file series,
|
---|
| 125 | % 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
|
---|
| 126 | % i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices
|
---|
[628] | 127 | nbfield_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices)
|
---|
[457] | 128 | nbfield_i=size(i1_series{1},2); %nb of fields for the i index
|
---|
| 129 | nbfield=nbfield_j*nbfield_i; %total number of fields
|
---|
[784] | 130 | [FileInfo{1},VideoObject{1}]=get_file_info(filecell{1,1});% type of input file
|
---|
[783] | 131 | FileType{1}=FileInfo{1}.FileType;
|
---|
[442] | 132 |
|
---|
[604] | 133 | %% frame index for movie or multimage file input
|
---|
[454] | 134 | if ~isempty(j1_series{1})
|
---|
| 135 | frame_index{1}=j1_series{1};
|
---|
| 136 | else
|
---|
| 137 | frame_index{1}=i1_series{1};
|
---|
[442] | 138 | end
|
---|
[454] | 139 |
|
---|
| 140 | %% calibration data and timing: read the ImaDoc files
|
---|
| 141 | %not relevant for this function
|
---|
| 142 |
|
---|
| 143 | %% check coincidence in time for several input file series
|
---|
| 144 | %not relevant for this function
|
---|
| 145 |
|
---|
| 146 | %% coordinate transform or other user defined transform
|
---|
| 147 | %not relevant for this function
|
---|
| 148 |
|
---|
| 149 | %%%%%%%%%%%% END STANDARD PART %%%%%%%%%%%%
|
---|
| 150 | % EDIT FROM HERE
|
---|
| 151 |
|
---|
[604] | 152 | %% Extension and indexing nomenclature for output file
|
---|
| 153 | FileExtOut='.png'; % write result as .png images for image inputs
|
---|
| 154 | if strcmpi(NomType{1}(end),'a')
|
---|
| 155 | NomTypeOut=NomType{1};
|
---|
| 156 | else
|
---|
| 157 | NomTypeOut='_1_1';
|
---|
| 158 | end
|
---|
[64] | 159 |
|
---|
[454] | 160 | %% Set field names and velocity types
|
---|
| 161 | %not relevant for this function
|
---|
[64] | 162 |
|
---|
[454] | 163 | %% Initiate output fields
|
---|
| 164 | %not relevant for this function
|
---|
| 165 |
|
---|
| 166 | %% set processing parameters
|
---|
| 167 | % not needed for this function
|
---|
| 168 |
|
---|
[953] | 169 | %% coordinate transform or other user defined transform
|
---|
| 170 | transform_fct='';%default fct handle
|
---|
| 171 | if isfield(Param,'FieldTransform')&&~isempty(Param.FieldTransform.TransformName)
|
---|
| 172 | currentdir=pwd;
|
---|
| 173 | cd(Param.FieldTransform.TransformPath)
|
---|
| 174 | transform_fct=str2func(Param.FieldTransform.TransformName);
|
---|
| 175 | cd (currentdir)
|
---|
| 176 | if isfield(Param,'TransformInput')
|
---|
| 177 | for iview=1:NbView
|
---|
| 178 | XmlData{iview}.TransformInput=Param.TransformInput;
|
---|
| 179 | end
|
---|
| 180 | end
|
---|
| 181 | end
|
---|
[454] | 182 |
|
---|
| 183 | %% main loop on images
|
---|
| 184 | j1=[];%default
|
---|
[64] | 185 | for ifile=1:nbfield
|
---|
[628] | 186 | update_waitbar(WaitbarHandle,ifile/nbfield)
|
---|
[904] | 187 | if ~isempty(RUNHandle) && ~strcmp(get(RUNHandle,'BusyAction'),'queue')
|
---|
[624] | 188 | disp('program stopped by user')
|
---|
| 189 | return
|
---|
[64] | 190 | end
|
---|
[604] | 191 | if ~isempty(j1_series)&&~isequal(j1_series,{[]})
|
---|
| 192 | j1=j1_series{1}(ifile);
|
---|
| 193 | end
|
---|
| 194 | filename=fullfile_uvmat(RootPath{1},SubDir{1},RootFile{1},FileExt{1},NomType{1},i1_series{1}(ifile),[],j1);
|
---|
| 195 | A=read_image(filename,FileType{1},VideoObject{1},frame_index{1}(ifile));
|
---|
| 196 | if ndims(A)==3;%color images
|
---|
| 197 | A=sum(double(Aread),3);% take the sum of color components
|
---|
| 198 | end
|
---|
| 199 | % operation on images
|
---|
| 200 | A=levels(A);
|
---|
| 201 | % write output file
|
---|
| 202 | filename_new=fullfile_uvmat(RootPath{1},SubdirOut,RootFile{1},FileExtOut,NomTypeOut,i1_series{1}(ifile),[],j1);
|
---|
| 203 | imwrite(A,filename_new)
|
---|
| 204 | display([filename_new ' written'])
|
---|
[64] | 205 | end
|
---|
[442] | 206 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
---|
[64] | 207 |
|
---|
[454] | 208 |
|
---|
[64] | 209 | function C=levels(A)
|
---|
| 210 | %whos A;
|
---|
| 211 | B=double(A(:,:,1));
|
---|
| 212 | windowsize=round(min(size(B,1),size(B,2))/20);
|
---|
| 213 | windowsize=floor(windowsize/2)*2+1;
|
---|
| 214 | ix=[1/2-windowsize/2:-1/2+windowsize/2];%
|
---|
| 215 | %del=np/3;
|
---|
| 216 | %fct=exp(-(ix/del).^2);
|
---|
| 217 | fct2=cos(ix/(windowsize-1)/2*pi/2);
|
---|
| 218 | %Mfiltre=(ones(5,5)/5^2);
|
---|
| 219 | %Mfiltre=fct2';
|
---|
| 220 | Mfiltre=fct2'*fct2;
|
---|
| 221 | Mfiltre=Mfiltre/(sum(sum(Mfiltre)));
|
---|
| 222 |
|
---|
[71] | 223 | C=filter2(Mfiltre,B);
|
---|
[64] | 224 | C(:,1:windowsize)=C(:,windowsize)*ones(1,windowsize);
|
---|
| 225 | C(:,end-windowsize+1:end)=C(:,end-windowsize+1)*ones(1,windowsize);
|
---|
| 226 | C(1:windowsize,:)=ones(windowsize,1)*C(windowsize,:);
|
---|
| 227 | C(end-windowsize+1:end,:)=ones(windowsize,1)*C(end-windowsize,:);
|
---|
| 228 | C=tanh(B./(2*C));
|
---|
| 229 | [n,c]=hist(reshape(C,1,[]),100);
|
---|
| 230 | % figure;plot(c,n);
|
---|
| 231 |
|
---|
| 232 | [m,i]=max(n);
|
---|
| 233 | c_max=c(i);
|
---|
| 234 | [dummy,index]=sort(abs(c-c(i)));
|
---|
| 235 | n=n(index);
|
---|
| 236 | c=c(index);
|
---|
| 237 | i_select = find(cumsum(n)<0.95*sum(n));
|
---|
| 238 | if isempty(i_select)
|
---|
| 239 | i_select = 1:length(c);
|
---|
| 240 | end
|
---|
| 241 | c_select=c(i_select);
|
---|
| 242 | n_select=n(i_select);
|
---|
| 243 | cmin=min(c_select);
|
---|
| 244 | cmax=max(c_select);
|
---|
| 245 | C=(C-cmin)/(cmax-cmin)*256;
|
---|
| 246 | C=uint8(C);
|
---|