1 | %'test_civ': test the civ fct on a simple shear produced by defomation of the input image |
---|
2 | % use Matlab signal processing toolbox |
---|
3 | %------------------------------------------------------------------------ |
---|
4 | % Method: |
---|
5 | %open a netcdf file with civ results. |
---|
6 | |
---|
7 | % function ParamOut=test_patch_tps (Param) |
---|
8 | % |
---|
9 | %%%%%%%%%%% GENERAL TO ALL SERIES ACTION FCTS %%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
10 | % |
---|
11 | %OUTPUT |
---|
12 | % ParamOut: sets input parameters when the function is selected, not activated(input Param.Action.RUN=0),=[] when the function is running |
---|
13 | % |
---|
14 | %INPUT: |
---|
15 | % In run mode, the input parameters are given as a Matlab structure Param copied from the GUI series. |
---|
16 | % In batch mode, Param is the name of the corresponding xml file containing the same information |
---|
17 | % when Param.Action.RUN=0 (as activated when the current Action is selected |
---|
18 | % in series), the function ouput paramOut set the activation of the needed GUI elements |
---|
19 | % |
---|
20 | % Param contains the elements:(use the menu bar command 'export/GUI config' in series to |
---|
21 | % see the current structure Param) |
---|
22 | % .InputTable: cell of input file names, (several lines for multiple input) |
---|
23 | % each line decomposed as {RootPath,SubDir,Rootfile,NomType,Extension} |
---|
24 | % .OutputSubDir: name of the subdirectory for data outputs |
---|
25 | % .OutputDirExt: directory extension for data outputs |
---|
26 | % .Action: .ActionName: name of the current activated function |
---|
27 | % .ActionPath: path of the current activated function |
---|
28 | % .ActionExt: fct extension ('.m', Matlab fct, '.sh', compiled Matlab fct |
---|
29 | % .RUN =0 for GUI input, =1 for function activation |
---|
30 | % .RunMode='local','background', 'cluster': type of function use |
---|
31 | % |
---|
32 | % .IndexRange: set the file or frame indices on which the action must be performed |
---|
33 | % .InputFields: sub structure describing the input fields withfields |
---|
34 | % .FieldName: name(s) of the field |
---|
35 | % .VelType: velocity type |
---|
36 | |
---|
37 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
38 | |
---|
39 | %======================================================================= |
---|
40 | % Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France |
---|
41 | % http://www.legi.grenoble-inp.fr |
---|
42 | % Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.fr |
---|
43 | % |
---|
44 | % This file is part of the toolbox UVMAT. |
---|
45 | % |
---|
46 | % UVMAT is free software; you can redistribute it and/or modify |
---|
47 | % it under the terms of the GNU General Public License as published |
---|
48 | % by the Free Software Foundation; either version 2 of the license, |
---|
49 | % or (at your option) any later version. |
---|
50 | % |
---|
51 | % UVMAT is distributed in the hope that it will be useful, |
---|
52 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
53 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
54 | % GNU General Public License (see LICENSE.txt) for more details. |
---|
55 | %======================================================================= |
---|
56 | |
---|
57 | function ParamOut=test_civ (Param) |
---|
58 | |
---|
59 | %%%%%%%%%%%%%%%%% INPUT PREPARATION MODE (no RUN) %%%%%%%%%%%%%%%%% |
---|
60 | if isstruct(Param) && isequal(Param.Action.RUN,0) |
---|
61 | ParamOut.AllowInputSort='off';% allow alphabetic sorting of the list of input file SubDir (options 'off'/'on', 'off' by default) |
---|
62 | ParamOut.WholeIndexRange='one';% prescribes the file index ranges from min to max (options 'off'/'on'/'one' (single input index), 'off' by default) |
---|
63 | ParamOut.NbSlice='off'; % edit box nbre of slices made active |
---|
64 | ParamOut.VelType='one';% menu for selecting the velocity type (options 'off'/'one'/'two', 'off' by default) |
---|
65 | ParamOut.FieldName='off';% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default) |
---|
66 | ParamOut.FieldTransform = 'off';%can use a transform function |
---|
67 | ParamOut.ProjObject='off';%cannot use projection object(option 'off'/'on', |
---|
68 | ParamOut.Mask='off';%can use mask option (option 'off'/'on', 'off' by default) |
---|
69 | ParamOut.OutputDirExt='.test_subpixel';%set the output dir extension |
---|
70 | ParamOut.OutputFileMode='NbSlice';% '=NbInput': 1 output file per input file index, '=NbInput_i': 1 file per input file index i, '=NbSlice': 1 file per slice |
---|
71 | |
---|
72 | %% root input file(s) and type |
---|
73 | % check the existence of the first file in the series |
---|
74 | first_j=[];% note that the function will propose to cover the whole range of indices |
---|
75 | if isfield(Param.IndexRange,'first_j'); first_j=Param.IndexRange.first_j; end |
---|
76 | PairString=''; |
---|
77 | if isfield(Param.IndexRange,'PairString'); PairString=Param.IndexRange.PairString; end |
---|
78 | [i1,i2,j1,j2] = get_file_index(Param.IndexRange.first_i,first_j,PairString); |
---|
79 | FirstFileName=fullfile_uvmat(Param.InputTable{1,1},Param.InputTable{1,2},Param.InputTable{1,3},... |
---|
80 | Param.InputTable{1,5},Param.InputTable{1,4},i1,i2,j1,j2); |
---|
81 | if ~exist(FirstFileName,'file') |
---|
82 | msgbox_uvmat('ERROR',['the input file ' FirstFileName ' does not exist']) |
---|
83 | return |
---|
84 | end |
---|
85 | |
---|
86 | %% check the validity of input file types |
---|
87 | Data=nc2struct(FirstFileName,[]); |
---|
88 | if isfield(Data,'CivStage') |
---|
89 | switch Data.CivStage |
---|
90 | case {1,2,3} |
---|
91 | CivStage='civ1'; |
---|
92 | |
---|
93 | otherwise |
---|
94 | CivStage='civ2'; |
---|
95 | end |
---|
96 | else |
---|
97 | msgbox_uvmat('ERROR','invalid file type input: test_filter_tps proceeds raw civ data') |
---|
98 | return |
---|
99 | end |
---|
100 | |
---|
101 | %% numbers of fields |
---|
102 | incr_j=1;%default |
---|
103 | if isfield(Param.IndexRange,'incr_j')&&~isempty(Param.IndexRange.incr_j) |
---|
104 | incr_j=Param.IndexRange.incr_j; |
---|
105 | end |
---|
106 | % if isempty(first_j)||isempty(last_j) |
---|
107 | % nbfield_j=1; |
---|
108 | % else |
---|
109 | % nbfield_j=numel(first_j:incr_j:last_j);%nb of fields for the j index (bursts or volume slices) |
---|
110 | % end |
---|
111 | first_i=1;last_i=1;incr_i=1;%default |
---|
112 | if isfield(Param.IndexRange,'MinIndex_i'); first_i=Param.IndexRange.MinIndex_i; end |
---|
113 | if isfield(Param.IndexRange,'MaxIndex_i'); last_i=Param.IndexRange.MaxIndex_i; end |
---|
114 | if isfield(Param.IndexRange,'incr_i')&&~isempty(Param.IndexRange.incr_i) |
---|
115 | incr_i=Param.IndexRange.incr_i; |
---|
116 | end |
---|
117 | nbfield_i=numel(first_i:incr_i:last_i);%nb of fields for the i index (bursts or volume slices) |
---|
118 | % nbfield=nbfield_j*nbfield_i; %total number of fields |
---|
119 | |
---|
120 | %% setting of intput parameters |
---|
121 | ListParam={'CivStage';'CorrSmooth';'Civ1_CorrBoxSize'}; |
---|
122 | DefaultValue={CivStage;1;[31 31]}; |
---|
123 | if isfield(Param,'ActionInput') |
---|
124 | ParamIn=Param.ActionInput; |
---|
125 | else |
---|
126 | ParamIn=[]; |
---|
127 | end |
---|
128 | [ParamOut.ActionInput,errormsg] = set_param_input(ListParam,DefaultValue,ParamIn); |
---|
129 | if ~isempty(errormsg) |
---|
130 | msgbox_uvmat('ERROR',errormsg) |
---|
131 | end |
---|
132 | return |
---|
133 | end |
---|
134 | %%%%%%%%%%%%%%%%% STOP HERE FOR PAMETER INPUT MODE %%%%%%%%%%%%%%%%% |
---|
135 | |
---|
136 | %% read input parameters from an xml file if input is a file name (batch mode) |
---|
137 | checkrun=1; |
---|
138 | RUNHandle=[]; |
---|
139 | WaitbarHandle=[]; |
---|
140 | if ischar(Param) |
---|
141 | Param=xml2struct(Param);% read Param as input file (batch case) |
---|
142 | checkrun=0; |
---|
143 | else |
---|
144 | hseries=findobj(allchild(0),'Tag','series'); |
---|
145 | RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series |
---|
146 | WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series |
---|
147 | end |
---|
148 | |
---|
149 | %% input preparation |
---|
150 | RootPath=Param.InputTable{:,1}; |
---|
151 | RootFile=Param.InputTable{:,3}; |
---|
152 | NomType=Param.InputTable{:,4}; |
---|
153 | [filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param); |
---|
154 | % if ~isempty(hdisp),delete(hdisp),end; |
---|
155 | %%%%%%%%%%%% |
---|
156 | % The cell array filecell is the list of input file names, while |
---|
157 | % filecell{iview,fileindex}: |
---|
158 | % iview: line in the table corresponding to a given file series |
---|
159 | % fileindex: file index within the file series, |
---|
160 | % 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 |
---|
161 | % i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices |
---|
162 | %%%%%%%%%%%% |
---|
163 | |
---|
164 | %% output path |
---|
165 | |
---|
166 | OutputPath=fullfile(Param.OutputPath,Param.Experiment,Param.Device); |
---|
167 | |
---|
168 | |
---|
169 | %% Prepare the structure of output netcdf file |
---|
170 | DataOut.ListGlobalAttribute={'Conventions','Program','CivStage'}; |
---|
171 | DataOut.Conventions='uvmat/civdata';% states the conventions used for the description of field variables and attributes |
---|
172 | if isfield(Param,'UvmatRevision') |
---|
173 | DataOut.Program=['test_subpixel, uvmat r' Param.UvmatRevision]; |
---|
174 | else |
---|
175 | DataOut.Program='test_subpixel'; |
---|
176 | end |
---|
177 | DataOut.CivStage=1; |
---|
178 | DataOut.CoordUnit='pixel'; |
---|
179 | DataOut.ListVarName={'Civ1_X','Civ1_Y','Civ1_U','Civ1_V','Civ1_C','Civ1_FF'}; |
---|
180 | DataOut.VarDimName={'nb_vec','nb_vec','nb_vec','nb_vec','nb_vec','nb_vec'}; |
---|
181 | DataOut.VarAttribute{1}.Role='coord_x'; |
---|
182 | DataOut.VarAttribute{2}.Role='coord_y'; |
---|
183 | DataOut.VarAttribute{3}.Role='vector_x'; |
---|
184 | DataOut.VarAttribute{4}.Role='vector_y'; |
---|
185 | DataOut.VarAttribute{5}.Role='ancillary'; |
---|
186 | DataOut.VarAttribute{6}.Role='errorflag'; |
---|
187 | |
---|
188 | %% MAIN LOOP |
---|
189 | |
---|
190 | Data=read_field(filecell{1,1},'civdata',Param.InputFields); |
---|
191 | |
---|
192 | %% create shifted input image |
---|
193 | |
---|
194 | A=read_image(Data.Civ1_ImageA); |
---|
195 | [npy,npx]=size(A); |
---|
196 | B=zeros(npy,npx); |
---|
197 | for iline=1:npy |
---|
198 | Ashift=interp(double(A(iline,:)),10); |
---|
199 | Ashift=circshift(Ashift,round(200*(iline-(npy/2))/npy)+10); |
---|
200 | Ashift(1:round(200*(iline-(npy/2))/npy)+10)=0; |
---|
201 | B(iline,:)=decimate(Ashift,10); |
---|
202 | end |
---|
203 | B=B+(rand(npy,npx)-0.5).*B; |
---|
204 | par_civ.ImageA=A; |
---|
205 | if isa(A,'uint8') |
---|
206 | par_civ.ImageB=uint8(B); |
---|
207 | BitDepth=8; |
---|
208 | else |
---|
209 | par_civ.ImageB=uint16(B); |
---|
210 | BitDepth=16; |
---|
211 | end |
---|
212 | y=linspace (0.5,npy-0.5,npy); |
---|
213 | |
---|
214 | %% Run civ_series |
---|
215 | %Param.ActionInput.ListCompareMode='PIV'; |
---|
216 | par_civ.CorrBoxSize=Param.ActionInput.Civ1_CorrBoxSize; |
---|
217 | par_civ.SearchRange=[15 5]; |
---|
218 | par_civ.SearchBoxShift=[0 0]; |
---|
219 | par_civ.Dx=Data.Civ1_Dx; |
---|
220 | par_civ.Dy=Data.Civ1_Dy; |
---|
221 | par_civ.Dy=Data.Civ1_Dy; |
---|
222 | par_civ.CorrSmooth=Param.ActionInput.CorrSmooth; |
---|
223 | % par_civ.MinCorr=0; |
---|
224 | % path_series=fileparts(which('series')); |
---|
225 | % addpath(fullfile(path_series,'series')) |
---|
226 | % [DataOut,errormsg,result_conv]= civ_series(Param) |
---|
227 | |
---|
228 | % par_civ: structure of input parameters, with fields: |
---|
229 | % .ImageA: first image for correlation (matrix) |
---|
230 | % .ImageB: second image for correlation(matrix) |
---|
231 | % .CorrBoxSize: 1,2 vector giving the size of the correlation box in x and y |
---|
232 | % .SearchBoxSize: 1,2 vector giving the size of the search box in x and y |
---|
233 | % .SearchBoxShift: 1,2 vector or 2 column matrix (for civ2) giving the shift of the search box in x and y |
---|
234 | % .CorrSmooth: =1 or 2 determines the choice of the sub-pixel determination of the correlation max |
---|
235 | % .ImageWidth: nb of pixels of the image in x |
---|
236 | % .Dx, Dy: mesh for the PIV calculation |
---|
237 | % .Grid: grid giving the PIV calculation points (alternative to .Dx .Dy): centres of the correlation boxes in Image A |
---|
238 | % .Mask: name of a mask file or mask image matrix itself |
---|
239 | % .MinIma: thresholds for image luminosity |
---|
240 | % .MaxIma |
---|
241 | % .CheckDeformation=1 for subpixel interpolation and image deformation (linear transform) |
---|
242 | % .DUDX: matrix of deformation obtained from patch at each grid point |
---|
243 | % .DUDY |
---|
244 | % .DVDX: |
---|
245 | % .DVDY |
---|
246 | |
---|
247 | [DataOut.Civ1_X,DataOut.Civ1_Y,DataOut.Civ1_U,DataOut.Civ1_V,DataOut.Civ1_C,DataOut.Civ1_FF,result_conv,errormsg] = civ (par_civ); |
---|
248 | |
---|
249 | if ~isempty(errormsg) |
---|
250 | disp_uvmat('ERROR',errormsg,checkrun) |
---|
251 | return |
---|
252 | end |
---|
253 | |
---|
254 | Uref=-10*(2*DataOut.Civ1_Y/npy)+10.1; |
---|
255 | Udiff=DataOut.Civ1_U-Uref; |
---|
256 | figure(1) |
---|
257 | clf |
---|
258 | h=histogram(DataOut.Civ1_U,200,'BinLimits',[-10 10]) |
---|
259 | xlabel('U(pixels)') |
---|
260 | grid on |
---|
261 | figure(2) |
---|
262 | clf |
---|
263 | h=histogram(Udiff,'BinLimits',[-1 1]) |
---|
264 | xlabel('Udiff (pixels)') |
---|
265 | grid on |
---|
266 | figure(3) |
---|
267 | clf |
---|
268 | h=histogram(DataOut.Civ1_V,'BinLimits',[-1 1]) |
---|
269 | xlabel('V (pixels)') |
---|
270 | grid on |
---|
271 | OutputFile=fullfile(OutputPath,[Param.OutputSubDir Param.OutputDirExt],[RootFile '_' num2str(Param.IndexRange.first_i) '.nc']) |
---|
272 | errormsg=struct2nc(OutputFile,DataOut) |
---|
273 | |
---|
274 | OutputImageA=fullfile(OutputPath,[Param.OutputSubDir Param.OutputDirExt],[RootFile '_' num2str(Param.IndexRange.first_i) 'a.png']) |
---|
275 | |
---|
276 | OutputImageB=fullfile(OutputPath,[Param.OutputSubDir Param.OutputDirExt],[RootFile '_' num2str(Param.IndexRange.first_i) 'b.png']) |
---|
277 | imwrite(par_civ.ImageA,OutputImageA,'BitDepth',BitDepth);% save the new image |
---|
278 | imwrite(par_civ.ImageB,OutputImageB,'BitDepth',BitDepth); |
---|
279 | % %plot rms difference and proportion of excluded vectors |
---|
280 | % figure(2) |
---|
281 | % clf |
---|
282 | % if CivStage==3% civ1 |
---|
283 | % ref=0.2; %recommanded value for diff rms |
---|
284 | % txt='civ1'; |
---|
285 | % else |
---|
286 | % ref=0.1; |
---|
287 | % txt='civ2'; |
---|
288 | % end |
---|
289 | % semilogx(FieldSmooth,DataOut.Diff_rms,'b+-',FieldSmooth,DataOut.NbExclude,'m+-',FieldSmooth,ref*ones(size(FieldSmooth)),'b--') |
---|
290 | % grid on |
---|
291 | % title( [filecell{1,1} ':' txt]) |
---|
292 | % legend({'rms vel. diff. ' ;' ratio excluded vectors';['recommended diff for' txt]},'Location','northwest') |
---|
293 | % xlabel('smoothing parameter') |
---|
294 | % ylabel('rms (pixels) and proportion of excluded vectors') |
---|
295 | % OutputFig=fullfile(OutputPath,OutputDir,'plot_rms_diff.png'); |
---|
296 | % saveas(2,OutputFig) |
---|
297 | |
---|
298 | |
---|
299 | |
---|
300 | |
---|
301 | |
---|
302 | |
---|
303 | |
---|