1 | %'ima_levels': rescale the image intensity to reduce strong luminosity peaks (their blinking effects often perturbs PIV))
|
---|
2 | % this function can be used as a template for applying a transform (here 'levels.m') to each image of a series
|
---|
3 | %------------------------------------------------------------------------
|
---|
4 | % function GUI_input=ima_levels(Param)
|
---|
5 | %
|
---|
6 | %%%%%%%%%%% GENERAL TO ALL SERIES ACTION FCTS %%%%%%%%%%%%%%%%%%%%%%%%%%%
|
---|
7 | %
|
---|
8 | % This function is used in four modes by the GUI series:
|
---|
9 | % 1) config GUI: with no input argument, the function determine the suitable GUI configuration
|
---|
10 | % 2) interactive input: the function is used to interactively introduce input parameters, and then stops
|
---|
11 | % 3) RUN: the function itself runs, when an appropriate input structure Param has been introduced.
|
---|
12 | % 4) BATCH: the function itself proceeds in BATCH mode, using an xml file 'Param' as input.
|
---|
13 | %
|
---|
14 | %OUTPUT
|
---|
15 | % GUI_input=list of options in the GUI series.fig needed for the function
|
---|
16 | %
|
---|
17 | %INPUT:
|
---|
18 | % In run mode, the input parameters are given as a Matlab structure Param copied from the GUI series.
|
---|
19 | % In batch mode, Param is the name of the corresponding xml file containing the same information
|
---|
20 | % In the absence of input (as activated when the current Action is selected
|
---|
21 | % in series), the function ouput GUI_input set the activation of the needed GUI elements
|
---|
22 | %
|
---|
23 | % Param contains the elements:(use the menu bar command 'export/GUI config' in series to see the current structure Param)
|
---|
24 | % .InputTable: cell of input file names, (several lines for multiple input)
|
---|
25 | % each line decomposed as {RootPath,SubDir,Rootfile,NomType,Extension}
|
---|
26 | % .OutputSubDir: name of the subdirectory for data outputs
|
---|
27 | % .OutputDir: directory for data outputs, including path
|
---|
28 | % .Action: .ActionName: name of the current activated function
|
---|
29 | % .ActionPath: path of the current activated function
|
---|
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 | % .TransformHandle: corresponding function handle
|
---|
34 | % .InputFields: sub structure describing the input fields withfields
|
---|
35 | % .FieldName: name of the field
|
---|
36 | % .VelType: velocity type
|
---|
37 | % .FieldName_1: name of the second field in case of two input series
|
---|
38 | % .VelType_1: velocity type of the second field in case of two input series
|
---|
39 | % .ProjObject: %sub structure describing a projection object (read from ancillary GUI set_object)
|
---|
40 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
---|
41 |
|
---|
42 | function ParamOut=ima_levels (Param)
|
---|
43 |
|
---|
44 | %% set the input elements needed on the GUI series when the action is selected in the menu ActionName
|
---|
45 | if isstruct(Param) && isequal(Param.Action.RUN,0)
|
---|
46 | ParamOut.NbViewMax=1;% max nbre of input file series (default='' , no limitation)
|
---|
47 | ParamOut.AllowInputSort='off';% allow alphabetic sorting of the list of input file SubDir (options 'off'/'on', 'off' by default)
|
---|
48 | ParamOut.WholeIndexRange='off';% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default)
|
---|
49 | ParamOut.NbSlice='off'; %nbre of slices ('off' by default)
|
---|
50 | ParamOut.VelType='off';% menu for selecting the velocity type (options 'off'/'one'/'two', 'off' by default)
|
---|
51 | ParamOut.FieldName='one';% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default)
|
---|
52 | ParamOut.FieldTransform = 'off';%can use a transform function
|
---|
53 | ParamOut.ProjObject='off';%can use projection object(option 'off'/'on',
|
---|
54 | ParamOut.Mask='off';%can use mask option (option 'off'/'on', 'off' by default)
|
---|
55 | ParamOut.OutputDirExt='.lev';%set the output dir extension
|
---|
56 | return
|
---|
57 | end
|
---|
58 |
|
---|
59 | %%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%%
|
---|
60 | %% read input parameters from an xml file if input is a file name (batch mode)
|
---|
61 | checkrun=1;
|
---|
62 | if ischar(Param)
|
---|
63 | Param=xml2struct(Param);% read Param as input file (batch case)
|
---|
64 | checkrun=0;
|
---|
65 | end
|
---|
66 |
|
---|
67 | ParamOut=Param; %default output
|
---|
68 | if ~isfield(Param,'InputFields')
|
---|
69 | Param.InputFields.FieldName='';
|
---|
70 | end
|
---|
71 | OutputSubDir=[Param.OutputSubDir Param.OutputDirExt];% subdirectory for output files
|
---|
72 |
|
---|
73 | %% root input file(s) and type
|
---|
74 | RootPath=Param.InputTable(:,1);
|
---|
75 | RootFile=Param.InputTable(:,3);
|
---|
76 | SubDir=Param.InputTable(:,2);
|
---|
77 | NomType=Param.InputTable(:,4);
|
---|
78 | FileExt=Param.InputTable(:,5);
|
---|
79 | OutputSubDir=[Param.OutputSubDir Param.OutputDirExt];% subdirectory for output files
|
---|
80 |
|
---|
81 | % get the set of input file names (cell array filecell), and the lists of
|
---|
82 | % input file or frame indices i1_series,i2_series,j1_series,j2_series
|
---|
83 | [filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
|
---|
84 | % filecell{iview,fileindex}: cell array representing the list of file names
|
---|
85 | % iview: line in the table corresponding to a given file series
|
---|
86 | % fileindex: file index within the file series,
|
---|
87 | % 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
|
---|
88 | % i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices
|
---|
89 | % set of frame indices used for movie or multimage input
|
---|
90 | % numbers of slices and file indices
|
---|
91 |
|
---|
92 | NbSlice=1;%default
|
---|
93 | if isfield(Param.IndexRange,'NbSlice')
|
---|
94 | NbSlice=Param.IndexRange.NbSlice;
|
---|
95 | end
|
---|
96 | nbview=numel(i1_series);%number of input file series (lines in InputTable)
|
---|
97 | nbfield_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices)
|
---|
98 | nbfield_i=size(i1_series{1},2); %nb of fields for the i index
|
---|
99 | nbfield=nbfield_j*nbfield_i; %total number of fields
|
---|
100 | nbfield_i=floor(nbfield/NbSlice);%total number of indexes in a slice (adjusted to an integer number of slices)
|
---|
101 | nbfield=nbfield_i*NbSlice; %total number of fields after adjustement
|
---|
102 |
|
---|
103 | %determine the file type on each line from the first input file
|
---|
104 | ImageTypeOptions={'image','multimage','mmreader','video'};%allowed input file types(images)
|
---|
105 |
|
---|
106 | [FileType{1},FileInfo{1},MovieObject{1}]=get_file_type(filecell{1,1});
|
---|
107 | CheckImage{1}=~isempty(find(strcmp(FileType,ImageTypeOptions)));% =1 for images
|
---|
108 | if ~isempty(j1_series{1})
|
---|
109 | frame_index{1}=j1_series{1};
|
---|
110 | else
|
---|
111 | frame_index{1}=i1_series{1};
|
---|
112 | end
|
---|
113 |
|
---|
114 | %% calibration data and timing: read the ImaDoc files
|
---|
115 | %not relevant for this function
|
---|
116 |
|
---|
117 | %% check coincidence in time for several input file series
|
---|
118 | %not relevant for this function
|
---|
119 |
|
---|
120 | %% coordinate transform or other user defined transform
|
---|
121 | %not relevant for this function
|
---|
122 |
|
---|
123 | %%%%%%%%%%%% END STANDARD PART %%%%%%%%%%%%
|
---|
124 | % EDIT FROM HERE
|
---|
125 |
|
---|
126 | %% check the validity of input file types
|
---|
127 | if CheckImage{1}
|
---|
128 | FileExtOut='.png'; % write result as .png images for image inputs
|
---|
129 | if strcmp(lower(NomType{1}(end)),'a')
|
---|
130 | NomTypeOut=NomType{1};
|
---|
131 | else
|
---|
132 | NomTypeOut='_1_1';
|
---|
133 | end
|
---|
134 | else
|
---|
135 | msgbox_uvmat('ERROR',['invalid file type input: ' FileType{1} ' not an image'])
|
---|
136 | return
|
---|
137 | end
|
---|
138 |
|
---|
139 | %% Set field names and velocity types
|
---|
140 | %not relevant for this function
|
---|
141 |
|
---|
142 | %% Initiate output fields
|
---|
143 | %not relevant for this function
|
---|
144 |
|
---|
145 | %% set processing parameters
|
---|
146 | % not needed for this function
|
---|
147 |
|
---|
148 | %% update the xml file
|
---|
149 | % not needed for this function
|
---|
150 |
|
---|
151 | %% main loop on images
|
---|
152 | j1=[];%default
|
---|
153 | for ifile=1:nbfield
|
---|
154 | if checkrun
|
---|
155 | stopstate=get(Param.RUNHandle,'BusyAction');
|
---|
156 | update_waitbar(Param.WaitbarHandle,ifile/nbfield)
|
---|
157 | else
|
---|
158 | stopstate='queue';
|
---|
159 | end
|
---|
160 | if isequal(stopstate,'queue') % enable STOP command
|
---|
161 | if ~isempty(j1_series)&&~isequal(j1_series,{[]})
|
---|
162 | j1=j1_series{1}(ifile);
|
---|
163 | end
|
---|
164 | filename=fullfile_uvmat(RootPath{1},SubDir{1},RootFile{1},FileExt{1},NomType{1},i1_series{1}(ifile),[],j1);
|
---|
165 | A=read_image(filename,FileType{1},MovieObject{1},frame_index{1}(ifile));
|
---|
166 | if ndims(A)==3;%color images
|
---|
167 | A=sum(double(Aread),3);% take the sum of color components
|
---|
168 | end
|
---|
169 | % operation on images
|
---|
170 | A=levels(A);
|
---|
171 | filename_new=fullfile_uvmat(RootPath{1},OutputSubDir,RootFile{1},FileExtOut,NomTypeOut,i1_series{1}(ifile),[],j1);
|
---|
172 | imwrite(A,filename_new)
|
---|
173 | display([filename_new ' written'])
|
---|
174 | end
|
---|
175 | end
|
---|
176 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
---|
177 |
|
---|
178 |
|
---|
179 | function C=levels(A)
|
---|
180 | %whos A;
|
---|
181 | B=double(A(:,:,1));
|
---|
182 | windowsize=round(min(size(B,1),size(B,2))/20);
|
---|
183 | windowsize=floor(windowsize/2)*2+1;
|
---|
184 | ix=[1/2-windowsize/2:-1/2+windowsize/2];%
|
---|
185 | %del=np/3;
|
---|
186 | %fct=exp(-(ix/del).^2);
|
---|
187 | fct2=cos(ix/(windowsize-1)/2*pi/2);
|
---|
188 | %Mfiltre=(ones(5,5)/5^2);
|
---|
189 | %Mfiltre=fct2';
|
---|
190 | Mfiltre=fct2'*fct2;
|
---|
191 | Mfiltre=Mfiltre/(sum(sum(Mfiltre)));
|
---|
192 |
|
---|
193 | C=filter2(Mfiltre,B);
|
---|
194 | C(:,1:windowsize)=C(:,windowsize)*ones(1,windowsize);
|
---|
195 | C(:,end-windowsize+1:end)=C(:,end-windowsize+1)*ones(1,windowsize);
|
---|
196 | C(1:windowsize,:)=ones(windowsize,1)*C(windowsize,:);
|
---|
197 | C(end-windowsize+1:end,:)=ones(windowsize,1)*C(end-windowsize,:);
|
---|
198 | C=tanh(B./(2*C));
|
---|
199 | [n,c]=hist(reshape(C,1,[]),100);
|
---|
200 | % figure;plot(c,n);
|
---|
201 |
|
---|
202 | [m,i]=max(n);
|
---|
203 | c_max=c(i);
|
---|
204 | [dummy,index]=sort(abs(c-c(i)));
|
---|
205 | n=n(index);
|
---|
206 | c=c(index);
|
---|
207 | i_select = find(cumsum(n)<0.95*sum(n));
|
---|
208 | if isempty(i_select)
|
---|
209 | i_select = 1:length(c);
|
---|
210 | end
|
---|
211 | c_select=c(i_select);
|
---|
212 | n_select=n(i_select);
|
---|
213 | cmin=min(c_select);
|
---|
214 | cmax=max(c_select);
|
---|
215 | C=(C-cmin)/(cmax-cmin)*256;
|
---|
216 | C=uint8(C);
|
---|