1  %'RUN_STLIN': combine velocity fields for stereo PIV


2  % file_A,file_B: input velocity files


3  %vel_type: string ='civ1' or 'civ2'


4  function RUN_STLIN(file_A,file_B,vel_type,file_st,nx_patch,ny_patch,thresh_patch,fileAxml,fileBxml)


5 


6  [XmlDataA,error]=imadoc2struct(fileAxml);%read xml file associated to image series A


7  [XmlDataB,error]=imadoc2struct(fileBxml);%read xml file associated to image series B


8  npxA=[]; npyA=[]; pxB=[]; npyB=[];


9  if isfield(XmlDataA,'Camera') && isfield(XmlDataB,'Camera')


10  if isfield(XmlDataA.Camera,'ImageSize')&& isfield(XmlDataB.Camera,'ImageSize')


11  ImageSizeA=XmlDataA.Camera.ImageSize;


12  ImageSizeB=XmlDataB.Camera.ImageSize;


13  if ~isempty(ImageSizeA)&& ~isempty(ImageSizeB)


14  xindex=findstr(ImageSizeA,'x');


15  if length(xindex)>=2


16  npxA=str2num(ImageSizeA(1:xindex(1)1));


17  npyA=str2num(ImageSizeA(xindex(1)+1:xindex(2)1));


18  end


19  xindex=findstr(ImageSizeB,'x');


20  if length(xindex)>=2


21  npxB=str2num(ImageSizeB(1:xindex(1)1));


22  npyB=str2num(ImageSizeB(xindex(1)+1:xindex(2)1));


23  end


24  end


25  end


26  end


27  %%%%%%%% added for Duran


28  if isfield(XmlDataA,'Npx')&&isfield(XmlDataA,'Npy')&&isfield(XmlDataB,'Npx')&&isfield(XmlDataB,'Npy')


29  npxA=XmlDataA.Npx;


30  npyA=XmlDataA.Npy;


31  npxB=XmlDataB.Npx;


32  npyB=XmlDataB.Npy;


33  end


34  %%%%%%%% added for Duran


35  if isempty(npxA) isempty(npxB)


36  msgbox_uvmat('ERROR','The size of image A needs to be defined in the xml file ImaDoc')


37  return


38  elseif isempty(npxB)  isempty(npyB)


39  msgbox_uvmat('ERROR','The size of image B needs to be defined in the xml file ImaDoc')


40  return


41  end


42  if isfield(XmlDataA,'GeometryCalib')


43  tsaiA=XmlDataA.GeometryCalib;


44  else


45  msgbox_uvmat('ERROR','no geometric calibration available for image A')


46  return


47  end


48  if isfield(XmlDataB,'GeometryCalib')


49  tsaiB=XmlDataB.GeometryCalib;


50  else


51  msgbox_uvmat('ERROR','no geometric calibration available for image B')


52  return


53  end


54 


55  %corners of each image in px coordinates:


56  cornerA(:,1)=[0 0 npxA npxA]';%x positions


57  cornerA(:,2)=[0 npyA 0 npyA]';%y positions


58  cornerB(:,1)=[0 0 npxB npxB]';%x positions


59  cornerB(:,2)=[0 npyB 0 npyB]';%y positions


60  %corners of each image in phys coordinates:


61  [xyA(:,1),xyA(:,2)]=phys_XYZ(tsaiA,cornerA(:,1),cornerA(:,2));


62  [xyB(:,1),xyB(:,2)]=phys_XYZ(tsaiB,cornerB(:,1),cornerB(:,2));


63  max_x=max(max(xyA(:,1)),max(xyB(:,1)));%maximum on the 4 corners of the the images


64  min_x=min(min(xyA(:,1)),min(xyB(:,1)));%minimum on the 4 corners of the the images


65  max_y=max(max(xyA(:,2)),max(xyB(:,2)));


66  min_y=min(min(xyA(:,2)),min(xyB(:,2)));


67  array_realx=[min_x:(max_xmin_x)/(nx_patch1):max_x];


68  array_realy=[min_y:(max_ymin_y)/(ny_patch1):max_y];


69  [grid_realx,grid_realy]=meshgrid(array_realx,array_realy);


70  grid_real(:,1)=reshape(grid_realx,nx_patch*ny_patch,1);


71  grid_real(:,2)=reshape(grid_realy,nx_patch*ny_patch,1);


72  grid_real(:,3)=zeros(nx_patch*ny_patch,1);


73  [grid_imaA(:,1),grid_imaA(:,2)]=px_XYZ(tsaiA,grid_real(:,1),grid_real(:,2));


74  [grid_imaB(:,1),grid_imaB(:,2)]=px_XYZ(tsaiB,grid_real(:,1),grid_real(:,2));


75 


76  flagA=grid_imaA(:,1)>0 & grid_imaA(:,1)<npxA & grid_imaA(:,2)>0 & grid_imaA(:,2)<npyA;


77  flagB=grid_imaB(:,1)>0 & grid_imaB(:,1)<npxB & grid_imaB(:,2)>0 & grid_imaB(:,2)<npyB;


78  ind_good=find(flagA==1&flagB==1);


79  XimaA=grid_imaA(ind_good,1);


80  YimaA=grid_imaA(ind_good,2);


81  XimaB=grid_imaB(ind_good,1);


82  YimaB=grid_imaB(ind_good,2);


83  grid_real_x=grid_real(ind_good,1);


84  grid_real_y=grid_real(ind_good,2);


85 


86  % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


87  % %read the velocity fields


88  % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


89  %read field A


90  [Field,VelTypeOut]=read_civxdata(file_A,[],vel_type);


91  %removes false vectors


92  if isfield(Field,'FF')


93  Field.X=Field.X(find(Field.FF==0));


94  Field.Y=Field.Y(find(Field.FF==0));


95  Field.U=Field.U(find(Field.FF==0));


96  Field.V=Field.V(find(Field.FF==0));


97  end


98  %interpolate on the grid common to both images in phys coordinates


99  dXa= griddata_uvmat(Field.X,Field.Y,Field.U,XimaA,YimaA);


100  dYa= griddata_uvmat(Field.X,Field.Y,Field.V,XimaA,YimaA);


101  dt=Field.dt;


102  time=Field.Time;


103 


104  %read field B


105  [Field,VelTypeOut]=read_civxdata(file_B,[],vel_type);


106  if ~isequal(Field.dt,dt)


107  msgbox_uvmat('ERROR','different time intervals for the two velocity fields ')


108  return


109  end


110  if ~isequal(Field.Time,time)


111  msgbox_uvmat('ERROR','different times for the two velocity fields ')


112  return


113  end


114  %removes false vectors


115  if isfield(Field,'FF')


116  Field.X=Field.X(find(Field.FF==0));


117  Field.Y=Field.Y(find(Field.FF==0));


118  Field.U=Field.U(find(Field.FF==0));


119  Field.V=Field.V(find(Field.FF==0));


120  end


121  %interpolate on XimaB


122  dXb=griddata_uvmat(Field.X,Field.Y,Field.U,XimaB,YimaB);


123  dYb=griddata_uvmat(Field.X,Field.Y,Field.V,XimaB,YimaB);


124  %eliminate NotaNumber


125  ind_Nan=find(and(~isnan(dXa),~isnan(dXb)));


126  dXa=dXa(ind_Nan);


127  dYa=dYa(ind_Nan);


128  dXb=dXb(ind_Nan);


129  dYb=dYb(ind_Nan);


130  grid_phys1(:,1)=grid_real_x(ind_Nan);


131  grid_phys1(:,2)=grid_real_y(ind_Nan);


132 


133  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


134  %compute the differential coefficients of the geometric calibration


135  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


136  [A11,A12,A13,A21,A22,A23]=pxcm_tsai(tsaiA,grid_phys1);


137  [B11,B12,B13,B21,B22,B23]=pxcm_tsai(tsaiB,grid_phys1);


138 


139  C1=A11.*A22A12.*A21;


140  C2=A13.*A22A12.*A23;


141  C3=A13.*A21A11.*A23;


142  D1=B11.*B22B12.*B21;


143  D2=B13.*B22B12.*B23;


144  D3=B13.*B21B11.*B23;


145  A1=(A22.*D1.*(C1.*D3C3.*D1)+A21.*D1.*(C2.*D1C1.*D2));


146  A2=(A12.*D1.*(C3.*D1C1.*D3)+A11.*D1.*(C1.*D2C2.*D1));


147  B1=(B22.*C1.*(C3.*D1C1.*D3)+B21.*C1.*(C1.*D2C2.*D1));


148  B2=(B12.*C1.*(C1.*D3C3.*D1)+B11.*C1.*(C2.*D1C1.*D2));


149  Lambda=(A1.*dXa+A2.*dYa+B1.*dXb+B2.*dYb)./(A1.*A1+A2.*A2+B1.*B1+B2.*B2);


150 


151  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


152  %Projection for compatible displacements


153  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


154  Ua=dXaLambda.*A1;


155  Va=dYaLambda.*A2;


156  Ub=dXbLambda.*B1;


157  Vb=dYbLambda.*B2;


158 


159  %%%%%%%%%%%%%%%%%%%%%%%%%%%%


160  %Calculations of displacements and error


161  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


162  U=(A22.*D2.*UaA12.*D2.*VaB22.*C2.*Ub+B12.*C2.*Vb)./(C1.*D2C2.*D1);


163  V=(A21.*D3.*UaA11.*D3.*VaB21.*C3.*Ub+B11.*C3.*Vb)./(C3.*D1C1.*D3);


164  W=(A22.*D1.*UaA12.*D1.*VaB22.*C1.*Ub+B12.*C1.*Vb)./(C2.*D1C1.*D2);


165  W1=(A21.*D1.*Ua+A11.*D1.*Va+B21.*C1.*UbB11.*C1.*Vb)./(C1.*D3C3.*D1);


166 


167  error=sqrt((A1.*dXa+A2.*dYa+B1.*dXb+B2.*dYb).*(A1.*dXa+A2.*dYa+B1.*dXb+B2.*dYb)./(A1.*A1+A2.*A2+B1.*B1+B2.*B2));


168 


169  ind_error=(find(error<thresh_patch));


170  U=U(ind_error);


171  V=V(ind_error);


172  W=W(ind_error);%correction for water interface


173  error=error(ind_error);


174 


175  %create nc grid file


176  Result.ListGlobalAttribute={'nb_coord','nb_dim','constant_pixcm','absolut_time_T0','hart','dt','civ'};


177  Result.nb_coord=3;%grid file, no velocity


178  Result.nb_dim=2;


179  Result.constant_pixcm=0;%no linear correspondance with images


180  Result.absolut_time_T0=time;%absolute time of the field


181  Result.hart=0;


182  Result.dt=dt;%time interval for image correlation (put by default)


183  % cte.title='grid';


184  Result.civ=0;%not a civ file (no direct correspondance with an image)


185  % Result.ListDimName={'nb_vectors'}


186  % Result.DimValue=length(U);


187  Result.ListVarName={'vec_X','vec_Y','vec_U','vec_V','vec_W','vec_E'};


188  Result.VarDimName={'nb_vectors','nb_vectors','nb_vectors','nb_vectors','nb_vectors','nb_vectors'}


189  Result.vec_X= grid_phys1(ind_error,1);


190  Result.vec_Y= grid_phys1(ind_error,2);


191  Result.vec_U=U/dt;


192  Result.vec_V=V/dt;


193  Result.vec_W=W/dt;


194  Result.vec_E=error;


195  % error=write_netcdf(file_st,cte,fieldlabels,grid_phys);


196  error=struct2nc(file_st,Result);


197  display([file_st ' written'])


198 


199 


200 


201  %'pxcm_tsai': find differentials of the Tsai calibration


202  function [A11,A12,A13,A21,A22,A23]=pxcm_tsai(a,var_phys)


203  R=(a.R)';


204 


205  x=var_phys(:,1);


206  y=var_phys(:,2);


207 


208  if isfield(a,'PlanePos')


209  prompt={'Plane 1 Index','Plane 2 Index'};


210  Rep=inputdlg(prompt,'Target displacement test');


211  Z1=str2double(Rep(1));


212  Z2=str2double(Rep(2));


213  z=(a.PlanePos(Z2,3)+a.PlanePos(Z1,3))/2


214  else


215  z=0;


216  end


217 


218  %transform coeff for differentiels


219  a.C11=R(1)*R(8)R(2)*R(7);


220  a.C12=R(2)*R(7)R(1)*R(8);


221  a.C21=R(4)*R(8)R(5)*R(7);


222  a.C22=R(5)*R(7)R(4)*R(8);


223  a.C1x=R(3)*R(7)R(9)*R(1);


224  a.C1y=R(3)*R(8)R(9)*R(2);


225  a.C2x=R(6)*R(7)R(9)*R(4);


226  a.C2y=R(6)*R(8)R(9)*R(5);


227 


228  % %dependence in x,y


229  % denom=(R(7)*x+R(8)*y+R(9)*z+a.Tz).*(R(7)*x+R(8)*y+R(9)*z+a.Tz);


230  % A11=(a.f*a.sx*(a.C11*ya.C1x*z+R(1)*a.TzR(7)*a.Tx)./denom)/a.dpx;


231  % A12=(a.f*a.sx*(a.C12*xa.C1y*z+R(2)*a.TzR(8)*a.Tx)./denom)/a.dpx;


232  % A21=(a.f*a.sx*(a.C21*ya.C2x*z+R(4)*a.TzR(7)*a.Ty)./denom)/a.dpy;


233  % A22=(a.f*(a.C22*xa.C2y*z+R(5)*a.TzR(8)*a.Ty)./denom)/a.dpy;


234  % A13=(a.f*(a.C1x*x+a.C1y*y+R(3)*a.TzR(9)*a.Tx)./denom)/a.dpx;


235  % A23=(a.f*(a.C2x*x+a.C2y*y+R(6)*a.TzR(9)*a.Ty)./denom)/a.dpy;


236 


237  %dependence in x,y


238  denom=(R(7)*x+R(8)*y+R(9)*z+a.Tx_Ty_Tz(3)).*(R(7)*x+R(8)*y+R(9)*z+a.Tx_Ty_Tz(3));


239  A11=(a.fx_fy(1)*(a.C11*ya.C1x*z+R(1)*a.Tx_Ty_Tz(3)R(7)*a.Tx_Ty_Tz(1))./denom);


240  A12=(a.fx_fy(1)*(a.C12*xa.C1y*z+R(2)*a.Tx_Ty_Tz(3)R(8)*a.Tx_Ty_Tz(1))./denom);


241  A21=(a.fx_fy(1)*(a.C21*ya.C2x*z+R(4)*a.Tx_Ty_Tz(3)R(7)*a.Tx_Ty_Tz(2))./denom);


242  A22=(a.fx_fy(2)*(a.C22*xa.C2y*z+R(5)*a.Tx_Ty_Tz(3)R(8)*a.Tx_Ty_Tz(2))./denom);


243  A13=(a.fx_fy(2)*(a.C1x*x+a.C1y*y+R(3)*a.Tx_Ty_Tz(3)R(9)*a.Tx_Ty_Tz(1))./denom);


244  A23=(a.fx_fy(2)*(a.C2x*x+a.C2y*y+R(6)*a.Tx_Ty_Tz(3)R(9)*a.Tx_Ty_Tz(2))./denom);


245 


246 


247 


248 


249 


250 


251 


252 

