source: trunk/src/series/test_civ.m @ 1179

Last change on this file since 1179 was 1179, checked in by sommeria, 4 weeks ago

a few bug repairs and cleaning

File size: 12.4 KB
Line 
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
57function ParamOut=test_civ (Param)
58
59%%%%%%%%%%%%%%%%%    INPUT PREPARATION MODE (no RUN)    %%%%%%%%%%%%%%%%%
60if 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
133end
134%%%%%%%%%%%%%%%%%    STOP HERE FOR PAMETER INPUT MODE   %%%%%%%%%%%%%%%%%
135
136%% read input parameters from an xml file if input is a file name (batch mode)
137checkrun=1;
138RUNHandle=[];
139WaitbarHandle=[];
140if ischar(Param)
141    Param=xml2struct(Param);% read Param as input file (batch case)
142    checkrun=0;
143else
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
147end
148
149%% input preparation
150RootPath=Param.InputTable{:,1};
151RootFile=Param.InputTable{:,3};
152NomType=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
166OutputPath=fullfile(Param.OutputPath,Param.Experiment,Param.Device);
167
168
169%% Prepare the structure of output netcdf file
170DataOut.ListGlobalAttribute={'Conventions','Program','CivStage'};
171DataOut.Conventions='uvmat/civdata';% states the conventions used for the description of field variables and attributes
172if isfield(Param,'UvmatRevision')
173    DataOut.Program=['test_subpixel, uvmat r' Param.UvmatRevision];
174else
175    DataOut.Program='test_subpixel';
176end
177DataOut.CivStage=1;
178DataOut.CoordUnit='pixel';
179DataOut.ListVarName={'Civ1_X','Civ1_Y','Civ1_U','Civ1_V','Civ1_C','Civ1_FF'};
180DataOut.VarDimName={'nb_vec','nb_vec','nb_vec','nb_vec','nb_vec','nb_vec'};
181DataOut.VarAttribute{1}.Role='coord_x';
182DataOut.VarAttribute{2}.Role='coord_y';
183DataOut.VarAttribute{3}.Role='vector_x';
184DataOut.VarAttribute{4}.Role='vector_y';
185DataOut.VarAttribute{5}.Role='ancillary';
186DataOut.VarAttribute{6}.Role='errorflag';
187
188%% MAIN LOOP
189
190Data=read_field(filecell{1,1},'civdata',Param.InputFields);
191
192%% create shifted input image
193
194A=read_image(Data.Civ1_ImageA);
195[npy,npx]=size(A);
196B=zeros(npy,npx);
197for 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);
202end
203B=B+(rand(npy,npx)-0.5).*B;
204par_civ.ImageA=A;
205if isa(A,'uint8')
206    par_civ.ImageB=uint8(B);
207    BitDepth=8;
208else
209    par_civ.ImageB=uint16(B);
210    BitDepth=16;
211end
212y=linspace (0.5,npy-0.5,npy);
213
214%% Run civ_series
215%Param.ActionInput.ListCompareMode='PIV';
216par_civ.CorrBoxSize=Param.ActionInput.Civ1_CorrBoxSize;
217par_civ.SearchRange=[15 5];
218par_civ.SearchBoxShift=[0 0];
219par_civ.Dx=Data.Civ1_Dx;
220par_civ.Dy=Data.Civ1_Dy;
221par_civ.Dy=Data.Civ1_Dy;
222par_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
249if ~isempty(errormsg)
250    disp_uvmat('ERROR',errormsg,checkrun)
251    return
252end
253
254Uref=-10*(2*DataOut.Civ1_Y/npy)+10.1;
255Udiff=DataOut.Civ1_U-Uref;
256figure(1)
257clf
258h=histogram(DataOut.Civ1_U,200,'BinLimits',[-10 10])
259xlabel('U(pixels)')
260grid on
261figure(2)
262clf
263h=histogram(Udiff,'BinLimits',[-1 1])
264xlabel('Udiff (pixels)')
265grid on
266figure(3)
267clf
268h=histogram(DataOut.Civ1_V,'BinLimits',[-1 1])
269xlabel('V (pixels)')
270grid on
271OutputFile=fullfile(OutputPath,[Param.OutputSubDir Param.OutputDirExt],[RootFile '_' num2str(Param.IndexRange.first_i) '.nc'])
272errormsg=struct2nc(OutputFile,DataOut)
273
274OutputImageA=fullfile(OutputPath,[Param.OutputSubDir Param.OutputDirExt],[RootFile '_' num2str(Param.IndexRange.first_i) 'a.png'])
275
276OutputImageB=fullfile(OutputPath,[Param.OutputSubDir Param.OutputDirExt],[RootFile '_' num2str(Param.IndexRange.first_i) 'b.png'])
277imwrite(par_civ.ImageA,OutputImageA,'BitDepth',BitDepth);% save the new image
278imwrite(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 
Note: See TracBrowser for help on using the repository browser.