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

Last change on this file since 981 was 981, checked in by sommeria, 7 years ago

read cine updated

File size: 23.9 KB
RevLine 
[954]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%               
[839]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:
[954]21%
[839]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)
[954]30%                      each line decomposed as {RootPath,SubDir,Rootfile,NomType,Extension}.
31%                3 or 4 lines used as described above
[839]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
[954]38%             .RunMode='local','background', 'cluster': type of function  use         
[839]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%=======================================================================
[977]49% Copyright 2008-2017, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
[839]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)
[954]67
[839]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)
[863]70    ParamOut.AllowInputSort='off';% allow alphabetic sorting of the list of input file SubDir (options 'off'/'on', 'off' by default)
[839]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)
[863]77    ParamOut.ProjObject='on';%can use projection object(option 'off'/'on',
[839]78    ParamOut.Mask='off';%can use mask option   (option 'off'/'on', 'off' by default)
79    ParamOut.OutputDirExt='.vel3C';%set the output dir extension
[863]80    ParamOut.OutputSubDirMode='two'; % the two first input lines are used to define the output subfolder
[839]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
[863]82    %check the input files
[927]83    ParamOut.CheckOverwriteVisible='on'; % manage the overwrite of existing files (default=1)
[839]84    first_j=[];
[863]85    if size(Param.InputTable,1)<2
86        msgbox_uvmat('WARNING',['two or three input file series are needed'])
87    end
[839]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
[863]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
[839]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)));
[863]144    if diff_time>0
[839]145        disp_uvmat('WARNING',['times of series differ by (max) ' num2str(diff_time) ': the mean time is chosen in result'],checkrun)
[863]146    end
[839]147end
148if ~isempty(errormsg)
149    disp_uvmat('WARNING',errormsg,checkrun)
150end
[863]151time=mean(time,1); %averaged time taken for the merged field
[839]152if isfield(XmlData{1},'GeometryCalib')
[863]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
[839]164[filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
165
[862]166%% grid of physical positions (given by projection plane)
167if ~Param.CheckObject
[863]168    disp_uvmat('ERROR','a projection plane with interpolation is needed',checkrun)
169    return
[862]170end
[954]171ObjectData=Param.ProjObject;% Object attached to the GUI series
[863]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));
[862]178
[954]179%%%% MAIN LOOP ON FIELDS %%%%%
[863]180warning off
[880]181
[927]182CheckOverwrite=1;%default
183if isfield(Param,'CheckOverwrite')
184    CheckOverwrite=Param.CheckOverwrite;
185end
[954]186for field_index=1:NbField
[927]187   
[954]188    update_waitbar(WaitbarHandle,field_index/NbField)% waitbar to visualise progress (in RUN mode)
[927]189   
[954]190    %% generating the name of the output file for the merged field
191    i1=i1_series{1}(field_index);
[927]192    if ~isempty(i2_series{end})
[954]193        i2=i2_series{end}(field_index);
[927]194    else
195        i2=i1;
196    end
197    j1=1;
198    j2=1;
199    if ~isempty(j1_series{1})
[954]200        j1=j1_series{1}(field_index);
[927]201        if ~isempty(j2_series{end})
[954]202            j2=j2_series{end}(field_index);
[927]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   
[954]209    %% program stop if requested on the GUI
[839]210    if ~isempty(RUNHandle) && ~strcmp(get(RUNHandle,'BusyAction'),'queue')
211        disp('program stopped by user')
212        return
213    end
214   
[954]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   
[839]221    %%%%%%%%%%%%%%%% loop on views (input lines) %%%%%%%%%%%%%%%%
222    Data=cell(1,NbView);%initiate the set Data
[864]223   
224    %get Xphys,Yphys,Zphys from 1 or 2 stereo folders. Positions are taken
225    %at the middle between to time step
[954]226    ZItemp=zeros(size(XI,1),size(XI,2),2);
227    if field_index==1
[927]228        first_img=i1_series{1,1}(1,1); %id of the first image of the series
[954]229    end
[927]230   
[954]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
[981]234        idtemp=idtemp+1;       
[954]235        if NbView==3 % if there is only 1 stereo folder (2 cameras only), extract directly Xphys,Yphys and Zphys 
[981]236            InputFile_3=fullfile(Param.InputTable{3,1},Param.InputTable{3,2},[Param.InputTable{3,3} '_' int2str(first_img+indextemp-1) '.nc']);
[954]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           
[864]322        end
[981]323        if NbView>2
[954]324        ZItemp(:,:,idtemp)=griddata(Xphys,Yphys,Zphys,XI,YI); %interpolation on the choosen grid
[981]325        end
[864]326       
327    end
[954]328    ZI=mean(ZItemp,3); %mean between two the two times used for surface measurement
[878]329    Vtest=ZItemp(:,:,2)-ZItemp(:,:,1);
[864]330   
[863]331    [Xa,Ya]=px_XYZ(XmlData{1}.GeometryCalib,XI,YI,ZI);% set of image coordinates on view a
332    [Xb,Yb]=px_XYZ(XmlData{2}.GeometryCalib,XI,YI,ZI);% set of image coordinates on view b
333   
[954]334   
[863]335    for iview=1:2
[954]336        %% reading PIV input file(s)
337        [Data{iview},tild,errormsg]=read_civdata(filecell{iview,field_index},{'vec(U,V)'},'*');
[839]338        if ~isempty(errormsg)
339            disp_uvmat('ERROR',['ERROR in civ2vel_3C/read_field/' errormsg],checkrun)
340            return
341        end
342        % get the time defined in the current file if not already defined from the xml file
[863]343        if isfield(Data{iview},'Time')&& isequal(Data{iview}.Time,Data{1}.Time)
344            Time=Data{iview}.Time;
345        else
346            disp_uvmat('ERROR','Time undefined or not synchronous',checkrun)
347            return
[839]348        end
[863]349        if isfield(Data{iview},'Dt')&& isequal(Data{iview}.Dt,Data{1}.Dt)
350            Dt=Data{iview}.Dt;
351        else
352            disp_uvmat('ERROR','Dt undefined or not synchronous',checkrun)
353            return
[839]354        end
355    end
[954]356    %remove false vectors
[864]357    temp=find(Data{1}.FF==0);
358    X1=Data{1}.X(temp);
359    Y1=Data{1}.Y(temp);
360    U1=Data{1}.U(temp);
361    V1=Data{1}.V(temp);
362   
363    Ua=griddata(X1,Y1,U1,Xa,Ya);
364    Va=griddata(X1,Y1,V1,Xa,Ya);
[863]365   
[954]366    [Ua,Va,Xa,Ya]=Ud2U(XmlData{1}.GeometryCalib,Xa,Ya,Ua,Va); % convert Xd data to X
[878]367    [A]=get_coeff(XmlData{1}.GeometryCalib,Xa,Ya,XI,YI,ZI); %get coef A~
[864]368   
[954]369    %remove false vectors
[864]370    temp=find(Data{2}.FF==0);
371    X2=Data{2}.X(temp);
372    Y2=Data{2}.Y(temp);
373    U2=Data{2}.U(temp);
374    V2=Data{2}.V(temp);
375    Ub=griddata(X2,Y2,U2,Xb,Yb);
376    Vb=griddata(X2,Y2,V2,Xb,Yb);
[954]377   
378    [Ub,Vb,Xb,Yb]=Ud2U(XmlData{2}.GeometryCalib,Xb,Yb,Ub,Vb); % convert Xd data to X
[878]379    [B]=get_coeff(XmlData{2}.GeometryCalib,Xb,Yb,XI,YI,ZI); %get coef B~
380   
[954]381   
[878]382    % System to solve
[863]383    S=ones(size(XI,1),size(XI,2),3);
384    D=ones(size(XI,1),size(XI,2),3,3);
[954]385   
[863]386    S(:,:,1)=A(:,:,1,1).*Ua+A(:,:,2,1).*Va+B(:,:,1,1).*Ub+B(:,:,2,1).*Vb;
387    S(:,:,2)=A(:,:,1,2).*Ua+A(:,:,2,2).*Va+B(:,:,1,2).*Ub+B(:,:,2,2).*Vb;
388    S(:,:,3)=A(:,:,1,3).*Ua+A(:,:,2,3).*Va+B(:,:,1,3).*Ub+B(:,:,2,3).*Vb;
389    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);
390    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);
391    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);
392    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);
393    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);
394    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);
395    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);
396    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);
397    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);
398    for indj=1:size(XI,1)
399        for indi=1:size(XI,2)
[878]400            dxyz=(squeeze(D(indj,indi,:,:))*1000)\(squeeze(S(indj,indi,:))*1000); % solving...
[863]401            U(indj,indi)=dxyz(1);
402            V(indj,indi)=dxyz(2);
403            W(indj,indi)=dxyz(3);
404        end
[954]405    end
[863]406    Error=zeros(size(XI,1),size(XI,2),4);
407    Error(:,:,1)=A(:,:,1,1).*U+A(:,:,1,2).*V+A(:,:,1,3).*W-Ua;
408    Error(:,:,2)=A(:,:,2,1).*U+A(:,:,2,2).*V+A(:,:,2,3).*W-Va;
409    Error(:,:,3)=B(:,:,1,1).*U+B(:,:,1,2).*V+B(:,:,1,3).*W-Ub;
410    Error(:,:,4)=B(:,:,2,1).*U+B(:,:,2,2).*V+B(:,:,2,3).*W-Vb;
411   
[839]412    %% recording the merged field
[954]413    if field_index==1% initiate the structure at first index
[863]414        MergeData.ListGlobalAttribute={'Conventions','Time','Dt'};
[839]415        MergeData.Conventions='uvmat';
[863]416        MergeData.Time=Time;
417        MergeData.Dt=Dt;
418        MergeData.ListVarName={'coord_x','coord_y','Z','U','V','W','Error'};
419        MergeData.VarDimName={'coord_x','coord_y',{'coord_y','coord_x'},{'coord_y','coord_x'}...
[954]420            {'coord_y','coord_x'},{'coord_y','coord_x'},{'coord_y','coord_x'}};
[863]421        MergeData.coord_x=xI;
422        MergeData.coord_y=yI;
423    end
424    MergeData.U=U/Dt;
425    MergeData.V=V/Dt;
426    MergeData.W=W/Dt;
[927]427    MergeData.Z=ZI;
[878]428   
[954]429    %     mfx=(XmlData{1}.GeometryCalib.fx_fy(1)+XmlData{2}.GeometryCalib.fx_fy(1))/2;
430    %     mfy=(XmlData{1}.GeometryCalib.fx_fy(2)+XmlData{2}.GeometryCalib.fx_fy(2))/2;
[927]431    MergeData.Error=0.5*sqrt(sum(Error.^2,3));
[863]432    errormsg=struct2nc(OutputFile,MergeData);%save result file
433    if isempty(errormsg)
434        disp(['output file ' OutputFile ' written'])
435    else
436        disp(errormsg)
437    end
[839]438end
439
440
[878]441function [A]=get_coeff(Calib,X,Y,x,y,z) % compute A~ coefficients
[863]442R=(Calib.R)';%rotation matrix
443T_z=Calib.Tx_Ty_Tz(3);
444T=R(7)*x+R(8)*y+R(9)*z+T_z;
[878]445
[863]446A(:,:,1,1)=(R(1)-R(7)*X)./T;
447A(:,:,1,2)=(R(2)-R(8)*X)./T;
448A(:,:,1,3)=(R(3)-R(9)*X)./T;
449A(:,:,2,1)=(R(4)-R(7)*Y)./T;
450A(:,:,2,2)=(R(5)-R(8)*Y)./T;
451A(:,:,2,3)=(R(6)-R(9)*Y)./T;
[839]452
[954]453function [U,V,X,Y]=Ud2U(Calib,Xd,Yd,Ud,Vd)
454% convert image coordinates to view angles, after removal of  quadratic distorsion
455% input in pixel, output in radians
[878]456X1d=Xd-Ud/2;
457X2d=Xd+Ud/2;
458Y1d=Yd-Vd/2;
459Y2d=Yd+Vd/2;
[839]460
[878]461X1=(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);
462X2=(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);
463Y1=(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);
464Y2=(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);
[839]465
[878]466U=X2-X1;
467V=Y2-Y1;
468X=X1+U/2;
469Y=Y1+V/2;
470
471
472
473function [z,Xphy,Yphy,error]=shift2z(xmid, ymid, u, v,XmlData) % get H from stereo data
[864]474z=0;
475error=0;
[839]476
[864]477%% first image
478Calib_A=XmlData{1}.GeometryCalib;
479R=(Calib_A.R)';
480x_a=xmid- u/2;
[878]481y_a=ymid- v/2;
[864]482z_a=R(7)*x_a+R(8)*y_a+Calib_A.Tx_Ty_Tz(1,3);
483Xa=(R(1)*x_a+R(2)*y_a+Calib_A.Tx_Ty_Tz(1,1))./z_a;
484Ya=(R(4)*x_a+R(5)*y_a+Calib_A.Tx_Ty_Tz(1,2))./z_a;
[839]485
[864]486A_1_1=R(1)-R(7)*Xa;
487A_1_2=R(2)-R(8)*Xa;
488A_1_3=R(3)-R(9)*Xa;
489A_2_1=R(4)-R(7)*Ya;
490A_2_2=R(5)-R(8)*Ya;
491A_2_3=R(6)-R(9)*Ya;
492Det=A_1_1.*A_2_2-A_1_2.*A_2_1;
493Dxa=(A_1_2.*A_2_3-A_2_2.*A_1_3)./Det;
494Dya=(A_2_1.*A_1_3-A_1_1.*A_2_3)./Det;
495
496%% second image
[878]497%loading shift angle
498
[864]499Calib_B=XmlData{2}.GeometryCalib;
500R=(Calib_B.R)';
[878]501
502
[864]503x_b=xmid+ u/2;
504y_b=ymid+ v/2;
505z_b=R(7)*x_b+R(8)*y_b+Calib_B.Tx_Ty_Tz(1,3);
506Xb=(R(1)*x_b+R(2)*y_b+Calib_B.Tx_Ty_Tz(1,1))./z_b;
507Yb=(R(4)*x_b+R(5)*y_b+Calib_B.Tx_Ty_Tz(1,2))./z_b;
508B_1_1=R(1)-R(7)*Xb;
509B_1_2=R(2)-R(8)*Xb;
510B_1_3=R(3)-R(9)*Xb;
511B_2_1=R(4)-R(7)*Yb;
512B_2_2=R(5)-R(8)*Yb;
513B_2_3=R(6)-R(9)*Yb;
514Det=B_1_1.*B_2_2-B_1_2.*B_2_1;
515Dxb=(B_1_2.*B_2_3-B_2_2.*B_1_3)./Det;
516Dyb=(B_2_1.*B_1_3-B_1_1.*B_2_3)./Det;
517
518%% result
519Den=(Dxb-Dxa).*(Dxb-Dxa)+(Dyb-Dya).*(Dyb-Dya);
[927]520error=abs(((Dyb-Dya).*(-u)-(Dxb-Dxa).*(-v)))./Den;
[878]521% ex=-error.*(Dyb-Dya);
522% ey=-error.*(Dxb-Dxa);
[864]523
[878]524% z1=-u./(Dxb-Dxa);
525% z2=-v./(Dyb-Dya);
526z=((Dxb-Dxa).*(-u)+(Dyb-Dya).*(-v))./Den;
527
[864]528xnew(1,:)=Dxa.*z+x_a;
529xnew(2,:)=Dxb.*z+x_b;
530ynew(1,:)=Dya.*z+y_a;
531ynew(2,:)=Dyb.*z+y_b;
[878]532Xphy=mean(xnew,1);
533Yphy=mean(ynew,1);
[864]534
535
536
[878]537
Note: See TracBrowser for help on using the repository browser.