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

Last change on this file since 1141 was 1141, checked in by sommeria, 8 weeks ago

smoothig parameter renormalised in filter_tps

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