source: trunk/src/series/civ2vel_3C.m @ 977

Last change on this file since 977 was 977, checked in by g7moreau, 4 years ago
  • Update Copyright 2008-2017 notice
File size: 23.9 KB
Line 
1%'civ2vel_3C': combine velocity fields from two cameras to get the three velocity components
2% used with the GUI 'series':
3%           first input line =raw PIV camera 1 (image coordinates)
4%           second input line=raw PIV camera 2 (image coordinates)
5% Three modes:
6%   1) no additional input: measurements assumed in the reference plane (laser sheet)
7%   2) measurement surface obtained by stereoscopic comparison of the images of the two cameras.
8%           third input line =surface z(x,y) given by correlation between camera 1 and 2 (expressed in phys apparent coordinates)
9%   3)  surface z(x,y) given by adding the displacements of each camera with a third intermediate camera (#3) used to reduce the
10% to reduce the angle for stereoscopic view.
11%           third input line =correlation between camera 1 and 3 (expressed in phys apparent coordinates)
12%           fourth input line =corelation between camera 2 and 3 (expressed in phys apparent coordinates)
13%               
14%------------------------------------------------------------------------
15% function ParamOut=civ2vel_3C(Param)
16%
17%OUTPUT
18% ParamOut: sets options in the GUI series.fig needed for the function
19%
20%INPUT:
21%
22% In run mode, the input parameters are given as a Matlab structure Param copied from the GUI series.
23% In batch mode, Param is the name of the corresponding xml file containing the same information
24% when Param.Action.RUN=0 (as activated when the current Action is selected
25% in series), the function ouput paramOut set the activation of the needed GUI elements
26%
27% Param contains the elements:(use the menu bar command 'export/GUI config' in series to
28% see the current structure Param)
29%    .InputTable: cell of input file names, (several lines for multiple input)
30%                      each line decomposed as {RootPath,SubDir,Rootfile,NomType,Extension}.
31%                3 or 4 lines used as described above
32%    .OutputSubDir: name of the subdirectory for data outputs
33%    .OutputDirExt: directory extension for data outputs
34%    .Action: .ActionName: name of the current activated function
35%             .ActionPath:   path of the current activated function
36%             .ActionExt: fct extension ('.m', Matlab fct, '.sh', compiled   Matlab fct
37%             .RUN =0 for GUI input, =1 for function activation
38%             .RunMode='local','background', 'cluster': type of function  use         
39%    .IndexRange: set the file or frame indices on which the action must be performed
40%    .InputFields: sub structure describing the input fields withfields
41%              .FieldName: name(s) of the field
42%              .VelType: velocity type
43%              .FieldName_1: name of the second field in case of two input series
44%              .VelType_1: velocity type of the second field in case of two input series
45%              .Coord_y: name of y coordinate variable
46%              .Coord_x: name of x coordinate variable'
47
48%=======================================================================
49% Copyright 2008-2017, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
50%   http://www.legi.grenoble-inp.fr
51%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
52%
53%     This file is part of the toolbox UVMAT.
54%
55%     UVMAT is free software; you can redistribute it and/or modify
56%     it under the terms of the GNU General Public License as published
57%     by the Free Software Foundation; either version 2 of the license,
58%     or (at your option) any later version.
59%
60%     UVMAT is distributed in the hope that it will be useful,
61%     but WITHOUT ANY WARRANTY; without even the implied warranty of
62%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
63%     GNU General Public License (see LICENSE.txt) for more details.
64%=======================================================================
65
66function ParamOut=civ2vel_3C(Param)
67
68%% set the input elements needed on the GUI series when the function is selected in the menu ActionName or InputTable refreshed
69if isstruct(Param) && isequal(Param.Action.RUN,0)
70    ParamOut.AllowInputSort='off';% allow alphabetic sorting of the list of input file SubDir (options 'off'/'on', 'off' by default)
71    ParamOut.WholeIndexRange='off';% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default)
72    ParamOut.NbSlice='off'; %nbre of slices ('off' by default)
73    ParamOut.VelType='one';% menu for selecting the velocity type (options 'off'/'one'/'two',  'off' by default)
74    ParamOut.FieldName='off';% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default)
75    ParamOut.FieldTransform = 'off';%use the phys  transform function without choice
76    %ParamOut.TransformPath=fullfile(fileparts(which('uvmat')),'transform_field');% path to transform functions (needed for compilation only)
77    ParamOut.ProjObject='on';%can use projection object(option 'off'/'on',
78    ParamOut.Mask='off';%can use mask option   (option 'off'/'on', 'off' by default)
79    ParamOut.OutputDirExt='.vel3C';%set the output dir extension
80    ParamOut.OutputSubDirMode='two'; % the two first input lines are used to define the output subfolder
81    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
82    %check the input files
83    ParamOut.CheckOverwriteVisible='on'; % manage the overwrite of existing files (default=1)
84    first_j=[];
85    if size(Param.InputTable,1)<2
86        msgbox_uvmat('WARNING',['two or three input file series are needed'])
87    end
88    if isfield(Param.IndexRange,'first_j'); first_j=Param.IndexRange.first_j; end
89    PairString='';
90    if isfield(Param.IndexRange,'PairString'); PairString=Param.IndexRange.PairString; end
91    [i1,i2,j1,j2] = get_file_index(Param.IndexRange.first_i,first_j,PairString);
92    FirstFileName=fullfile_uvmat(Param.InputTable{1,1},Param.InputTable{1,2},Param.InputTable{1,3},...
93        Param.InputTable{1,5},Param.InputTable{1,4},i1,i2,j1,j2);
94    if ~exist(FirstFileName,'file')
95        msgbox_uvmat('WARNING',['the first input file ' FirstFileName ' does not exist'])
96    elseif isequal(size(Param.InputTable,1),1) && ~isfield(Param,'ProjObject')
97        msgbox_uvmat('WARNING','You may need a projection object of type plane for merge_proj')
98    end
99    return
100end
101
102%%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%%
103ParamOut=[]; %default output
104%% read input parameters from an xml file if input is a file name (batch mode)
105checkrun=1;
106if ischar(Param)
107    Param=xml2struct(Param);% read Param as input file (batch case)
108    checkrun=0;
109end
110hseries=findobj(allchild(0),'Tag','series');
111RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series
112WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series
113
114
115%% root input file(s) name, type and index series
116RootPath=Param.InputTable(:,1);
117RootFile=Param.InputTable(:,3);
118SubDir=Param.InputTable(:,2);
119NomType=Param.InputTable(:,4);
120FileExt=Param.InputTable(:,5);
121hdisp=disp_uvmat('WAITING...','checking the file series',checkrun);
122[filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
123if ~isempty(hdisp),delete(hdisp),end;
124%%%%%%%%%%%%
125% The cell array filecell is the list of input file names, while
126% filecell{iview,fileindex}:
127%        iview: line in the table corresponding to a given file series
128%        fileindex: file index within  the file series,
129% 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
130% i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices
131%%%%%%%%%%%%
132NbView=numel(i1_series);%number of input file series (lines in InputTable)
133NbField_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices)
134NbField_i=size(i1_series{1},2); %nb of fields for the i index
135NbField=NbField_j*NbField_i; %total number of fields
136
137%% define the directory for result file (with path=RootPath{1})
138OutputDir=[Param.OutputSubDir Param.OutputDirExt];% subdirectory for output files
139
140%% calibration data and timing: read the ImaDoc files
141[XmlData,NbSlice_calib,time,errormsg]=read_multimadoc(RootPath,SubDir,RootFile,FileExt,i1_series,i2_series,j1_series,j2_series);
142if size(time,1)>1
143    diff_time=max(max(diff(time)));
144    if diff_time>0
145        disp_uvmat('WARNING',['times of series differ by (max) ' num2str(diff_time) ': the mean time is chosen in result'],checkrun)
146    end
147end
148if ~isempty(errormsg)
149    disp_uvmat('WARNING',errormsg,checkrun)
150end
151time=mean(time,1); %averaged time taken for the merged field
152if isfield(XmlData{1},'GeometryCalib')
153    tsaiA=XmlData{1}.GeometryCalib;
154else
155    disp_uvmat('ERROR','no geometric calibration available for image A',checkrun)
156    return
157end
158if isfield(XmlData{2},'GeometryCalib')
159    tsaiB=XmlData{2}.GeometryCalib;
160else
161    disp_uvmat('ERROR','no geometric calibration available for image B',checkrun)
162    return
163end
164[filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
165
166%% grid of physical positions (given by projection plane)
167if ~Param.CheckObject
168    disp_uvmat('ERROR','a projection plane with interpolation is needed',checkrun)
169    return
170end
171ObjectData=Param.ProjObject;% Object attached to the GUI series
172xI=ObjectData.RangeX(1):ObjectData.DX:ObjectData.RangeX(2);
173yI=ObjectData.RangeY(1):ObjectData.DY:ObjectData.RangeY(2);
174[XI,YI]=meshgrid(xI,yI);
175U=zeros(size(XI,1),size(XI,2));
176V=zeros(size(XI,1),size(XI,2));
177W=zeros(size(XI,1),size(XI,2));
178
179%%%% MAIN LOOP ON FIELDS %%%%%
180warning off
181
182CheckOverwrite=1;%default
183if isfield(Param,'CheckOverwrite')
184    CheckOverwrite=Param.CheckOverwrite;
185end
186for field_index=1:NbField
187   
188    update_waitbar(WaitbarHandle,field_index/NbField)% waitbar to visualise progress (in RUN mode)
189   
190    %% generating the name of the output file for the merged field
191    i1=i1_series{1}(field_index);
192    if ~isempty(i2_series{end})
193        i2=i2_series{end}(field_index);
194    else
195        i2=i1;
196    end
197    j1=1;
198    j2=1;
199    if ~isempty(j1_series{1})
200        j1=j1_series{1}(field_index);
201        if ~isempty(j2_series{end})
202            j2=j2_series{end}(field_index);
203        else
204            j2=j1;
205        end
206    end
207    OutputFile=fullfile_uvmat(RootPath{1},OutputDir,RootFile{1},'.nc','_1-2',i1,i2,j1,j2);
208   
209    %% program stop if requested on the GUI
210    if ~isempty(RUNHandle) && ~strcmp(get(RUNHandle,'BusyAction'),'queue')
211        disp('program stopped by user')
212        return
213    end
214   
215    %% check existence of the output file
216    if (~CheckOverwrite && exist(OutputFile,'file'))
217        disp('existing output file already exists, skip to next field')
218        continue% skip iteration if the mode overwrite is desactivated and the result file already exists
219    end
220   
221    %%%%%%%%%%%%%%%% loop on views (input lines) %%%%%%%%%%%%%%%%
222    Data=cell(1,NbView);%initiate the set Data
223   
224    %get Xphys,Yphys,Zphys from 1 or 2 stereo folders. Positions are taken
225    %at the middle between to time step
226    ZItemp=zeros(size(XI,1),size(XI,2),2);
227    if field_index==1
228        first_img=i1_series{1,1}(1,1); %id of the first image of the series
229    end
230   
231    idtemp=0;
232    % get the surface shape corresponding to the PIV measurements
233    for indextemp=field_index:field_index+1;%TODO: generalise to field index intervals>1 for PIV
234        idtemp=idtemp+1;
235        InputFile_3=fullfile(Param.InputTable{3,1},Param.InputTable{3,2},[Param.InputTable{3,3} '_' int2str(first_img+indextemp-1) '.nc']);
236        if NbView==3 % if there is only 1 stereo folder (2 cameras only), extract directly Xphys,Yphys and Zphys 
237            % Data{1}: =raw PIV camera 1 only
238            % Data{2}: =raw PIV camera 2 only
239            % Data{3}: =correlation between camera 1 and 2
240            [Data{3},tild,errormsg] = nc2struct(InputFile_3);%read input file       
241            if  exist('Data{3}.Civ3_FF','var') % FF is present, remove wrong vector
242                temp=find(Data{3}.Civ3_FF==0);
243                Zphys=Data{3}.Zphys(temp);
244                Yphys=Data{3}.Yphys(temp);
245                Xphys=Data{3}.Xphys(temp);
246            else
247                Zphys=Data{3}.Zphys;
248                Yphys=Data{3}.Yphys;
249                Xphys=Data{3}.Xphys;
250            end
251           
252        elseif NbView==4 % is there is 2 stereo folders, get global U and V and compute Zphys
253            % Data{1}: =raw PIV camera 1 only (left)
254            % Data{2}: =raw PIV camera 2 only (right) (no PIV done with middle camera)
255            % Data{3}: =corelation between camera 1 and 3 (left and middle)
256            % Data{4}: =corelation between camera 2 and 3 (right and middle)
257            % test if the second camera (3) is the same for both folders
258            for i=3:4
259                indpt(i)=strfind(Param.InputTable{i,2},'.'); % indice of the "." is the folder name 1
260                indline(i)=strfind(Param.InputTable{i,2},'-'); % indice of the "-" is the folder name1
261                camname{i}=Param.InputTable{i,2}(indline(i)+1:indpt(i)-1);% extract the second camera name
262            end       
263            if strcmp(camname{3},camname{4})==0
264                disp_uvmat('ERROR','The 2 stereo folders should have the same camera for the second position',checkrun)
265                return
266            end     
267            [Data{3},tild,errormsg] = nc2struct(InputFile_3);       
268            if exist('Data{3}.Civ3_FF','var') % if FF is present, remove wrong vector
269                temp=find(Data{3}.Civ3_FF==0);
270                Xmid3=Data{3}.Xmid(temp);
271                Ymid3=Data{3}.Ymid(temp);
272                U3=Data{3}.Uphys(temp);
273                V3=Data{3}.Vphys(temp);
274            else
275                Xmid3=Data{3}.Xmid;
276                Ymid3=Data{3}.Ymid;
277                U3=Data{3}.Uphys;
278                V3=Data{3}.Vphys;
279            end
280            %temporary grid of merging the 2 stereos data
281            [xq,yq] = meshgrid(min(Xmid3+(U3)/2):(max(Xmid3+(U3)/2)-min(Xmid3+(U3)/2))/128:max(Xmid3+(U3)/2),min(Ymid3+(V3)/2):(max(Ymid3+(V3)/2)-min(Ymid3+(V3)/2))/128:max(Ymid3+(V3)/2));
282           
283            %1st folder : interpolate the first camera points on the second (common) camera
284            %(Dalsa 3)
285            x3Q=griddata(Xmid3+(U3)/2,Ymid3+(V3)/2,Xmid3-(U3)/2,xq,yq);
286            y3Q=griddata(Xmid3+(U3)/2,Ymid3+(V3)/2,Ymid3-(V3)/2,xq,yq);
287           
288            InputFile_4=fullfile(Param.InputTable{4,1},Param.InputTable{4,2},[Param.InputTable{4,3} '_' int2str(first_img+indextemp-1) '.nc']);
289            [Data{4},tild,errormsg] = nc2struct(InputFile_4);
290            if exist('Data{4}.Civ3_FF','var') % if FF is present, remove wrong vector
291                temp=find(Data{4}.Civ3_FF==0);
292                Xmid4=Data{4}.Xmid(temp);
293                Ymid4=Data{4}.Ymid(temp);
294                U4=Data{4}.Uphys(temp);
295                V4=Data{4}.Vphys(temp);
296            else
297                Xmid4=Data{4}.Xmid;
298                Ymid4=Data{4}.Ymid;
299                U4=Data{4}.Uphys;
300                V4=Data{4}.Vphys;
301            end
302            %2nd folder :interpolate the first camera (Dalsa2) points on the second (common) camera
303            %(Dalsa 3)
304            x4Q=griddata(Xmid4+(U4)/2,Ymid4+(V4)/2,Xmid4-(U4)/2,xq,yq);
305            y4Q=griddata(Xmid4+(U4)/2,Ymid4+(V4)/2,Ymid4-(V4)/2,xq,yq);
306           
307            %add the displacements of the two camera pairs
308            xmid=reshape((x4Q+x3Q)/2,length(xq(:,1)).*length(xq(1,:)),1);
309            ymid=reshape((y4Q+y3Q)/2,length(yq(:,1)).*length(yq(1,:)),1);
310            u=reshape(x4Q-x3Q,length(xq(:,1)).*length(xq(1,:)),1);
311            v=reshape(y4Q-y3Q,length(yq(:,1)).*length(yq(1,:)),1);
312           
313            % get the surface z(x,y) from the combined displacement
314            [Zphys,Xphys,Yphys,error]=shift2z(xmid, ymid, u, v,XmlData); %get Xphy,Yphy and Zphys
315            %remove NaN
316            tempNaN=isnan(Zphys);tempind=find(tempNaN==1);
317            Zphys(tempind)=[];
318            Xphys(tempind)=[];
319            Yphys(tempind)=[];
320            error(tempind)=[];
321           
322        end
323       
324        ZItemp(:,:,idtemp)=griddata(Xphys,Yphys,Zphys,XI,YI); %interpolation on the choosen grid
325       
326    end
327    ZI=mean(ZItemp,3); %mean between two the two times used for surface measurement
328    Vtest=ZItemp(:,:,2)-ZItemp(:,:,1);
329   
330    [Xa,Ya]=px_XYZ(XmlData{1}.GeometryCalib,XI,YI,ZI);% set of image coordinates on view a
331    [Xb,Yb]=px_XYZ(XmlData{2}.GeometryCalib,XI,YI,ZI);% set of image coordinates on view b
332   
333   
334    for iview=1:2
335        %% reading PIV input file(s)
336        [Data{iview},tild,errormsg]=read_civdata(filecell{iview,field_index},{'vec(U,V)'},'*');
337        if ~isempty(errormsg)
338            disp_uvmat('ERROR',['ERROR in civ2vel_3C/read_field/' errormsg],checkrun)
339            return
340        end
341        % get the time defined in the current file if not already defined from the xml file
342        if isfield(Data{iview},'Time')&& isequal(Data{iview}.Time,Data{1}.Time)
343            Time=Data{iview}.Time;
344        else
345            disp_uvmat('ERROR','Time undefined or not synchronous',checkrun)
346            return
347        end
348        if isfield(Data{iview},'Dt')&& isequal(Data{iview}.Dt,Data{1}.Dt)
349            Dt=Data{iview}.Dt;
350        else
351            disp_uvmat('ERROR','Dt undefined or not synchronous',checkrun)
352            return
353        end
354    end
355    %remove false vectors
356    temp=find(Data{1}.FF==0);
357    X1=Data{1}.X(temp);
358    Y1=Data{1}.Y(temp);
359    U1=Data{1}.U(temp);
360    V1=Data{1}.V(temp);
361   
362    Ua=griddata(X1,Y1,U1,Xa,Ya);
363    Va=griddata(X1,Y1,V1,Xa,Ya);
364   
365    [Ua,Va,Xa,Ya]=Ud2U(XmlData{1}.GeometryCalib,Xa,Ya,Ua,Va); % convert Xd data to X
366    [A]=get_coeff(XmlData{1}.GeometryCalib,Xa,Ya,XI,YI,ZI); %get coef A~
367   
368    %remove false vectors
369    temp=find(Data{2}.FF==0);
370    X2=Data{2}.X(temp);
371    Y2=Data{2}.Y(temp);
372    U2=Data{2}.U(temp);
373    V2=Data{2}.V(temp);
374    Ub=griddata(X2,Y2,U2,Xb,Yb);
375    Vb=griddata(X2,Y2,V2,Xb,Yb);
376   
377    [Ub,Vb,Xb,Yb]=Ud2U(XmlData{2}.GeometryCalib,Xb,Yb,Ub,Vb); % convert Xd data to X
378    [B]=get_coeff(XmlData{2}.GeometryCalib,Xb,Yb,XI,YI,ZI); %get coef B~
379   
380   
381    % System to solve
382    S=ones(size(XI,1),size(XI,2),3);
383    D=ones(size(XI,1),size(XI,2),3,3);
384   
385    S(:,:,1)=A(:,:,1,1).*Ua+A(:,:,2,1).*Va+B(:,:,1,1).*Ub+B(:,:,2,1).*Vb;
386    S(:,:,2)=A(:,:,1,2).*Ua+A(:,:,2,2).*Va+B(:,:,1,2).*Ub+B(:,:,2,2).*Vb;
387    S(:,:,3)=A(:,:,1,3).*Ua+A(:,:,2,3).*Va+B(:,:,1,3).*Ub+B(:,:,2,3).*Vb;
388    D(:,:,1,1)=A(:,:,1,1).*A(:,:,1,1)+A(:,:,2,1).*A(:,:,2,1)+B(:,:,1,1).*B(:,:,1,1)+B(:,:,2,1).*B(:,:,2,1);
389    D(:,:,1,2)=A(:,:,1,1).*A(:,:,1,2)+A(:,:,2,1).*A(:,:,2,2)+B(:,:,1,1).*B(:,:,1,2)+B(:,:,2,1).*B(:,:,2,2);
390    D(:,:,1,3)=A(:,:,1,1).*A(:,:,1,3)+A(:,:,2,1).*A(:,:,2,3)+B(:,:,1,1).*B(:,:,1,3)+B(:,:,2,1).*B(:,:,2,3);
391    D(:,:,2,1)=A(:,:,1,2).*A(:,:,1,1)+A(:,:,2,2).*A(:,:,2,1)+B(:,:,1,2).*B(:,:,1,1)+B(:,:,2,2).*B(:,:,2,1);
392    D(:,:,2,2)=A(:,:,1,2).*A(:,:,1,2)+A(:,:,2,2).*A(:,:,2,2)+B(:,:,1,2).*B(:,:,1,2)+B(:,:,2,2).*B(:,:,2,2);
393    D(:,:,2,3)=A(:,:,1,2).*A(:,:,1,3)+A(:,:,2,2).*A(:,:,2,3)+B(:,:,1,2).*B(:,:,1,3)+B(:,:,2,2).*B(:,:,2,3);
394    D(:,:,3,1)=A(:,:,1,3).*A(:,:,1,1)+A(:,:,2,3).*A(:,:,2,1)+B(:,:,1,3).*B(:,:,1,1)+B(:,:,2,3).*B(:,:,2,1);
395    D(:,:,3,2)=A(:,:,1,3).*A(:,:,1,2)+A(:,:,2,3).*A(:,:,2,2)+B(:,:,1,3).*B(:,:,1,2)+B(:,:,2,3).*B(:,:,2,2);
396    D(:,:,3,3)=A(:,:,1,3).*A(:,:,1,3)+A(:,:,2,3).*A(:,:,2,3)+B(:,:,1,3).*B(:,:,1,3)+B(:,:,2,3).*B(:,:,2,3);
397    for indj=1:size(XI,1)
398        for indi=1:size(XI,2)
399            dxyz=(squeeze(D(indj,indi,:,:))*1000)\(squeeze(S(indj,indi,:))*1000); % solving...
400            U(indj,indi)=dxyz(1);
401            V(indj,indi)=dxyz(2);
402            W(indj,indi)=dxyz(3);
403        end
404    end
405    Error=zeros(size(XI,1),size(XI,2),4);
406    Error(:,:,1)=A(:,:,1,1).*U+A(:,:,1,2).*V+A(:,:,1,3).*W-Ua;
407    Error(:,:,2)=A(:,:,2,1).*U+A(:,:,2,2).*V+A(:,:,2,3).*W-Va;
408    Error(:,:,3)=B(:,:,1,1).*U+B(:,:,1,2).*V+B(:,:,1,3).*W-Ub;
409    Error(:,:,4)=B(:,:,2,1).*U+B(:,:,2,2).*V+B(:,:,2,3).*W-Vb;
410   
411    %% recording the merged field
412    if field_index==1% initiate the structure at first index
413        MergeData.ListGlobalAttribute={'Conventions','Time','Dt'};
414        MergeData.Conventions='uvmat';
415        MergeData.Time=Time;
416        MergeData.Dt=Dt;
417        MergeData.ListVarName={'coord_x','coord_y','Z','U','V','W','Error'};
418        MergeData.VarDimName={'coord_x','coord_y',{'coord_y','coord_x'},{'coord_y','coord_x'}...
419            {'coord_y','coord_x'},{'coord_y','coord_x'},{'coord_y','coord_x'}};
420        MergeData.coord_x=xI;
421        MergeData.coord_y=yI;
422    end
423    MergeData.U=U/Dt;
424    MergeData.V=V/Dt;
425    MergeData.W=W/Dt;
426    MergeData.Z=ZI;
427   
428    %     mfx=(XmlData{1}.GeometryCalib.fx_fy(1)+XmlData{2}.GeometryCalib.fx_fy(1))/2;
429    %     mfy=(XmlData{1}.GeometryCalib.fx_fy(2)+XmlData{2}.GeometryCalib.fx_fy(2))/2;
430    MergeData.Error=0.5*sqrt(sum(Error.^2,3));
431    errormsg=struct2nc(OutputFile,MergeData);%save result file
432    if isempty(errormsg)
433        disp(['output file ' OutputFile ' written'])
434    else
435        disp(errormsg)
436    end
437end
438
439
440function [A]=get_coeff(Calib,X,Y,x,y,z) % compute A~ coefficients
441R=(Calib.R)';%rotation matrix
442T_z=Calib.Tx_Ty_Tz(3);
443T=R(7)*x+R(8)*y+R(9)*z+T_z;
444
445A(:,:,1,1)=(R(1)-R(7)*X)./T;
446A(:,:,1,2)=(R(2)-R(8)*X)./T;
447A(:,:,1,3)=(R(3)-R(9)*X)./T;
448A(:,:,2,1)=(R(4)-R(7)*Y)./T;
449A(:,:,2,2)=(R(5)-R(8)*Y)./T;
450A(:,:,2,3)=(R(6)-R(9)*Y)./T;
451
452function [U,V,X,Y]=Ud2U(Calib,Xd,Yd,Ud,Vd)
453% convert image coordinates to view angles, after removal of  quadratic distorsion
454% input in pixel, output in radians
455X1d=Xd-Ud/2;
456X2d=Xd+Ud/2;
457Y1d=Yd-Vd/2;
458Y2d=Yd+Vd/2;
459
460X1=(X1d-Calib.Cx_Cy(1))./Calib.fx_fy(1).*(1 + Calib.kc.*Calib.fx_fy(1).^(-2).*(X1d-Calib.Cx_Cy(1)).^2 + Calib.kc.*Calib.fx_fy(2).^(-2).*(Y1d-Calib.Cx_Cy(2)).^2 ).^(-1);
461X2=(X2d-Calib.Cx_Cy(1))./Calib.fx_fy(1).*(1 + Calib.kc.*Calib.fx_fy(1).^(-2).*(X2d-Calib.Cx_Cy(1)).^2 + Calib.kc.*Calib.fx_fy(2).^(-2).*(Y2d-Calib.Cx_Cy(2)).^2 ).^(-1);
462Y1=(Y1d-Calib.Cx_Cy(2))./Calib.fx_fy(2).*(1 + Calib.kc.*Calib.fx_fy(1).^(-2).*(X1d-Calib.Cx_Cy(1)).^2 + Calib.kc.*Calib.fx_fy(2).^(-2).*(Y1d-Calib.Cx_Cy(2)).^2 ).^(-1);
463Y2=(Y2d-Calib.Cx_Cy(2))./Calib.fx_fy(2).*(1 + Calib.kc.*Calib.fx_fy(1).^(-2).*(X2d-Calib.Cx_Cy(1)).^2 + Calib.kc.*Calib.fx_fy(2).^(-2).*(Y2d-Calib.Cx_Cy(2)).^2 ).^(-1);
464
465U=X2-X1;
466V=Y2-Y1;
467X=X1+U/2;
468Y=Y1+V/2;
469
470
471
472function [z,Xphy,Yphy,error]=shift2z(xmid, ymid, u, v,XmlData) % get H from stereo data
473z=0;
474error=0;
475
476%% first image
477Calib_A=XmlData{1}.GeometryCalib;
478R=(Calib_A.R)';
479x_a=xmid- u/2;
480y_a=ymid- v/2;
481z_a=R(7)*x_a+R(8)*y_a+Calib_A.Tx_Ty_Tz(1,3);
482Xa=(R(1)*x_a+R(2)*y_a+Calib_A.Tx_Ty_Tz(1,1))./z_a;
483Ya=(R(4)*x_a+R(5)*y_a+Calib_A.Tx_Ty_Tz(1,2))./z_a;
484
485A_1_1=R(1)-R(7)*Xa;
486A_1_2=R(2)-R(8)*Xa;
487A_1_3=R(3)-R(9)*Xa;
488A_2_1=R(4)-R(7)*Ya;
489A_2_2=R(5)-R(8)*Ya;
490A_2_3=R(6)-R(9)*Ya;
491Det=A_1_1.*A_2_2-A_1_2.*A_2_1;
492Dxa=(A_1_2.*A_2_3-A_2_2.*A_1_3)./Det;
493Dya=(A_2_1.*A_1_3-A_1_1.*A_2_3)./Det;
494
495%% second image
496%loading shift angle
497
498Calib_B=XmlData{2}.GeometryCalib;
499R=(Calib_B.R)';
500
501
502x_b=xmid+ u/2;
503y_b=ymid+ v/2;
504z_b=R(7)*x_b+R(8)*y_b+Calib_B.Tx_Ty_Tz(1,3);
505Xb=(R(1)*x_b+R(2)*y_b+Calib_B.Tx_Ty_Tz(1,1))./z_b;
506Yb=(R(4)*x_b+R(5)*y_b+Calib_B.Tx_Ty_Tz(1,2))./z_b;
507B_1_1=R(1)-R(7)*Xb;
508B_1_2=R(2)-R(8)*Xb;
509B_1_3=R(3)-R(9)*Xb;
510B_2_1=R(4)-R(7)*Yb;
511B_2_2=R(5)-R(8)*Yb;
512B_2_3=R(6)-R(9)*Yb;
513Det=B_1_1.*B_2_2-B_1_2.*B_2_1;
514Dxb=(B_1_2.*B_2_3-B_2_2.*B_1_3)./Det;
515Dyb=(B_2_1.*B_1_3-B_1_1.*B_2_3)./Det;
516
517%% result
518Den=(Dxb-Dxa).*(Dxb-Dxa)+(Dyb-Dya).*(Dyb-Dya);
519error=abs(((Dyb-Dya).*(-u)-(Dxb-Dxa).*(-v)))./Den;
520% ex=-error.*(Dyb-Dya);
521% ey=-error.*(Dxb-Dxa);
522
523% z1=-u./(Dxb-Dxa);
524% z2=-v./(Dyb-Dya);
525z=((Dxb-Dxa).*(-u)+(Dyb-Dya).*(-v))./Den;
526
527xnew(1,:)=Dxa.*z+x_a;
528xnew(2,:)=Dxb.*z+x_b;
529ynew(1,:)=Dya.*z+y_a;
530ynew(2,:)=Dyb.*z+y_b;
531Xphy=mean(xnew,1);
532Yphy=mean(ynew,1);
533
534
535
536
Note: See TracBrowser for help on using the repository browser.