[574] | 1 | %'phys': transforms image (Unit='pixel') to real world (phys) coordinates using geometric calibration parameters. It acts if the input field contains the tag 'CoordTUnit' with value 'pixel'
|
---|
[40] | 2 |
|
---|
[574] | 3 | %------------------------------------------------------------------------
|
---|
| 4 | %%%% Use the general syntax for transform fields %%%%
|
---|
[40] | 5 | % OUTPUT:
|
---|
[574] | 6 | % DataOut: output field structure
|
---|
[40] | 7 |
|
---|
| 8 | %INPUT:
|
---|
[574] | 9 | % DataIn: first input field structure
|
---|
| 10 | % XmlData: first input parameter structure,
|
---|
| 11 | % .GeometryCalib: substructure of the calibration parameters
|
---|
| 12 | % DataIn_1: optional second input field structure
|
---|
| 13 | % XmlData_1: optional second input parameter structure
|
---|
| 14 | % .GeometryCalib: substructure of the calibration parameters
|
---|
| 15 | %------------------------------------------------------------------------
|
---|
| 16 | function DataOut=phys(DataIn,XmlData,DataIn_1,XmlData_1)
|
---|
| 17 | %------------------------------------------------------------------------
|
---|
[40] | 18 |
|
---|
| 19 | % A FAIRE: 1- verifier si DataIn est une 'field structure'(.ListVarName'):
|
---|
| 20 | % chercher ListVarAttribute, for each field (cell of variables):
|
---|
| 21 | % .CoordType: 'phys' or 'px' (default==phys, no transform)
|
---|
| 22 | % .scale_factor: =dt (to transform displacement into velocity) default=1
|
---|
| 23 | % .covariance: 'scalar', 'coord', 'D_i': covariant (like velocity), 'D^i': contravariant (like gradient), 'D^jD_i' (like strain tensor)
|
---|
| 24 | % (default='coord' if .Role='coord_x,_y...,
|
---|
| 25 | % 'D_i' if '.Role='vector_x,...',
|
---|
| 26 | % 'scalar', else (thenno change except scale factor)
|
---|
[574] | 27 |
|
---|
[507] | 28 | DataOut=[];
|
---|
| 29 | DataOut_1=[]; %default second output field
|
---|
| 30 | if strcmp(DataIn,'*')
|
---|
| 31 | if isfield(XmlData,'GeometryCalib')&& isfield(XmlData.GeometryCalib,'CoordUnit')
|
---|
[557] | 32 | DataOut.CoordUnit=XmlData.GeometryCalib.CoordUnit;% states that the output is in unit defined by GeometryCalib, then erased all projection objects with different units
|
---|
[507] | 33 | end
|
---|
| 34 | return
|
---|
| 35 | end
|
---|
| 36 |
|
---|
[396] | 37 | %% analyse input and set default output
|
---|
[507] | 38 | DataOut=DataIn;%default first output field
|
---|
[396] | 39 | if nargin>=2 % nargin =nbre of input variables
|
---|
[507] | 40 | if isfield(XmlData,'GeometryCalib')
|
---|
| 41 | Calib{1}=XmlData.GeometryCalib;
|
---|
[396] | 42 | else
|
---|
| 43 | Calib{1}=[];
|
---|
[40] | 44 | end
|
---|
[396] | 45 | if nargin>=3 %two input fields
|
---|
[507] | 46 | DataOut_1=DataIn_1;%default second output field
|
---|
| 47 | if nargin>=4 && isfield(XmlData_1,'GeometryCalib')
|
---|
| 48 | Calib{2}=XmlData_1.GeometryCalib;
|
---|
[396] | 49 | else
|
---|
| 50 | Calib{2}=Calib{1};
|
---|
| 51 | end
|
---|
| 52 | end
|
---|
| 53 | end
|
---|
| 54 |
|
---|
| 55 | %% get the z index defining the section plane
|
---|
[507] | 56 | if isfield(DataIn,'ZIndex')&&~isempty(DataIn.ZIndex)&&~isnan(DataIn.ZIndex)
|
---|
| 57 | ZIndex=DataIn.ZIndex;
|
---|
[40] | 58 | else
|
---|
[396] | 59 | ZIndex=1;
|
---|
[40] | 60 | end
|
---|
[396] | 61 |
|
---|
| 62 | %% transform first field
|
---|
| 63 | iscalar=0;% counter of scalar fields
|
---|
| 64 | if ~isempty(Calib{1})
|
---|
| 65 | if ~isfield(Calib{1},'CalibrationType')||~isfield(Calib{1},'CoordUnit')
|
---|
| 66 | return %bad calib parameter input
|
---|
[40] | 67 | end
|
---|
[507] | 68 | if ~(isfield(DataIn,'CoordUnit')&& strcmp(DataIn.CoordUnit,'pixel'))
|
---|
[396] | 69 | return % transform only fields in pixel coordinates
|
---|
| 70 | end
|
---|
[507] | 71 | DataOut=phys_1(DataIn,Calib{1},ZIndex);% transform coordiantes and velocity components
|
---|
[396] | 72 | %case of images or scalar: in case of two input fields, we need to project the transform on the same regular grid
|
---|
[507] | 73 | if isfield(DataIn,'A') && isfield(DataIn,'AX') && ~isempty(DataIn.AX) && isfield(DataIn,'AY')&&...
|
---|
| 74 | ~isempty(DataIn.AY) && length(DataIn.A)>1
|
---|
[40] | 75 | iscalar=1;
|
---|
[507] | 76 | A{1}=DataIn.A;
|
---|
[40] | 77 | end
|
---|
| 78 | end
|
---|
[396] | 79 |
|
---|
| 80 | %% document the selected plane position and angle if relevant
|
---|
| 81 | if isfield(Calib{1},'SliceCoord')&&size(Calib{1}.SliceCoord,1)>=ZIndex
|
---|
| 82 | DataOut.PlaneCoord=Calib{1}.SliceCoord(ZIndex,:);% transfer the slice position corresponding to index ZIndex
|
---|
| 83 | if isfield(Calib{1},'SliceAngle') % transfer the slice rotation angles
|
---|
| 84 | if isequal(size(Calib{1}.SliceAngle,1),1)% case of a unique angle
|
---|
| 85 | DataOut.PlaneAngle=Calib{1}.SliceAngle;
|
---|
| 86 | else % case of multiple planes with different angles: select the plane with index ZIndex
|
---|
| 87 | DataOut.PlaneAngle=Calib{1}.SliceAngle(ZIndex,:);
|
---|
| 88 | end
|
---|
| 89 | end
|
---|
[40] | 90 | end
|
---|
[396] | 91 |
|
---|
| 92 | %% transform second field if relevant
|
---|
| 93 | if ~isempty(DataOut_1)
|
---|
[507] | 94 | if isfield(DataIn_1,'ZIndex') && ~isequal(DataIn_1.ZIndex,ZIndex)
|
---|
[396] | 95 | DataOut_1.Txt='different plane indices for the two input fields';
|
---|
| 96 | return
|
---|
[40] | 97 | end
|
---|
[396] | 98 | if ~isfield(Calib{2},'CalibrationType')||~isfield(Calib{2},'CoordUnit')
|
---|
| 99 | return %bad calib parameter input
|
---|
| 100 | end
|
---|
[507] | 101 | if ~(isfield(DataIn_1,'CoordUnit')&& strcmp(DataIn_1.CoordUnit,'pixel'))
|
---|
[396] | 102 | return % transform only fields in pixel coordinates
|
---|
| 103 | end
|
---|
[433] | 104 | DataOut_1=phys_1(DataOut_1,Calib{2},ZIndex);
|
---|
[396] | 105 | if isfield(Calib{1},'SliceCoord')
|
---|
| 106 | if ~(isfield(Calib{2},'SliceCoord') && isequal(Calib{2}.SliceCoord,Calib{1}.SliceCoord))
|
---|
| 107 | DataOut_1.Txt='different plane positions for the two input fields';
|
---|
| 108 | return
|
---|
| 109 | end
|
---|
| 110 | DataOut_1.PlaneCoord=DataOut.PlaneCoord;% same plane position for the two input fields
|
---|
| 111 | if isfield(Calib{1},'SliceAngle')
|
---|
| 112 | if ~(isfield(Calib{2},'SliceAngle') && isequal(Calib{2}.SliceAngle,Calib{1}.SliceAngle))
|
---|
| 113 | DataOut_1.Txt='different plane angles for the two input fields';
|
---|
| 114 | return
|
---|
| 115 | end
|
---|
| 116 | DataOut_1.PlaneAngle=DataOut.PlaneAngle; % same plane angle for the two input fields
|
---|
| 117 | end
|
---|
| 118 | end
|
---|
[507] | 119 | if isfield(DataIn_1,'A')&&isfield(DataIn_1,'AX')&&~isempty(DataIn_1.AX) && isfield(DataIn_1,'AY')&&...
|
---|
| 120 | ~isempty(DataIn_1.AY)&&length(DataIn_1.A)>1
|
---|
[396] | 121 | iscalar=iscalar+1;
|
---|
| 122 | Calib{iscalar}=Calib{2};
|
---|
[507] | 123 | A{iscalar}=DataIn_1.A;
|
---|
[396] | 124 | end
|
---|
[40] | 125 | end
|
---|
[396] | 126 |
|
---|
| 127 | %% transform the scalar(s) or image(s)
|
---|
[40] | 128 | if iscalar~=0
|
---|
| 129 | [A,AX,AY]=phys_Ima(A,Calib,ZIndex);%TODO : introduire interp2_uvmat ds phys_ima
|
---|
[396] | 130 | if iscalar==1 && ~isempty(DataOut_1) % case for which only the second field is a scalar
|
---|
| 131 | DataOut_1.A=A{1};
|
---|
| 132 | DataOut_1.AX=AX;
|
---|
| 133 | DataOut_1.AY=AY;
|
---|
| 134 | else
|
---|
| 135 | DataOut.A=A{1};
|
---|
| 136 | DataOut.AX=AX;
|
---|
| 137 | DataOut.AY=AY;
|
---|
| 138 | end
|
---|
[40] | 139 | if iscalar==2
|
---|
| 140 | DataOut_1.A=A{2};
|
---|
| 141 | DataOut_1.AX=AX;
|
---|
| 142 | DataOut_1.AY=AY;
|
---|
| 143 | end
|
---|
| 144 | end
|
---|
| 145 |
|
---|
[518] | 146 | % subtract fields
|
---|
| 147 | if ~isempty(DataOut_1)
|
---|
| 148 | DataOut=sub_field(DataOut,[],DataOut_1);
|
---|
| 149 | end
|
---|
[40] | 150 | %------------------------------------------------
|
---|
[396] | 151 | %--- transform a single field
|
---|
| 152 | function DataOut=phys_1(Data,Calib,ZIndex)
|
---|
| 153 | %------------------------------------------------
|
---|
| 154 | %% set default output
|
---|
| 155 | DataOut=Data;%default
|
---|
| 156 | DataOut.CoordUnit=Calib.CoordUnit;% the output coord unit is set by the calibration parameters
|
---|
[40] | 157 |
|
---|
[396] | 158 | %% transform X,Y coordinates for velocity fields (transform of an image or scalar done in phys_ima)
|
---|
| 159 | if isfield(Data,'X') &&isfield(Data,'Y')&&~isempty(Data.X) && ~isempty(Data.Y)
|
---|
| 160 | [DataOut.X,DataOut.Y]=phys_XYZ(Calib,Data.X,Data.Y,ZIndex);
|
---|
| 161 | Dt=1; %default
|
---|
| 162 | if isfield(Data,'dt')&&~isempty(Data.dt)
|
---|
| 163 | Dt=Data.dt;
|
---|
[209] | 164 | end
|
---|
[396] | 165 | if isfield(Data,'Dt')&&~isempty(Data.Dt)
|
---|
| 166 | Dt=Data.Dt;
|
---|
[40] | 167 | end
|
---|
[396] | 168 | if isfield(Data,'U')&&isfield(Data,'V')&&~isempty(Data.U) && ~isempty(Data.V)
|
---|
| 169 | [XOut_1,YOut_1]=phys_XYZ(Calib,Data.X-Data.U/2,Data.Y-Data.V/2,ZIndex);
|
---|
| 170 | [XOut_2,YOut_2]=phys_XYZ(Calib,Data.X+Data.U/2,Data.Y+Data.V/2,ZIndex);
|
---|
| 171 | DataOut.U=(XOut_2-XOut_1)/Dt;
|
---|
| 172 | DataOut.V=(YOut_2-YOut_1)/Dt;
|
---|
[40] | 173 | end
|
---|
[494] | 174 | % if ~strcmp(Calib.CalibrationType,'rescale') && isfield(Data,'X_tps') && isfield(Data,'Y_tps')
|
---|
| 175 | % [DataOut.X_tps,DataOut.Y_tps]=phys_XYZ(Calib,Data.X,Data.Y,ZIndex);
|
---|
| 176 | % end
|
---|
| 177 | end
|
---|
| 178 |
|
---|
| 179 | %% suppress tps
|
---|
| 180 | list_tps={'Coord_tps' 'U_tps' 'V_tps' 'SubRange' 'NbSites'};
|
---|
| 181 | ind_remove=[];
|
---|
| 182 | for ilist=1:numel(list_tps)
|
---|
| 183 | ind_tps=find(strcmp(list_tps{ilist},Data.ListVarName));
|
---|
| 184 | if ~isempty(ind_tps)
|
---|
| 185 | ind_remove=[ind_remove ind_tps];
|
---|
| 186 | DataOut=rmfield(DataOut,list_tps{ilist});
|
---|
[40] | 187 | end
|
---|
[396] | 188 | end
|
---|
[631] | 189 | if isfield(DataOut,'VarAttribute') && numel(DataOut.VarAttribute)>=3 && isfield(DataOut.VarAttribute{3},'VarIndex_tps')
|
---|
[518] | 190 | DataOut.VarAttribute{3}=rmfield(DataOut.VarAttribute{3},'VarIndex_tps');
|
---|
| 191 | end
|
---|
[631] | 192 | if isfield(DataOut,'VarAttribute')&& numel(DataOut.VarAttribute)>=4 && isfield(DataOut.VarAttribute{4},'VarIndex_tps')
|
---|
| 193 | DataOut.VarAttribute{4}=rmfield(DataOut.VarAttribute{4},'VarIndex_tps');
|
---|
| 194 | end
|
---|
[518] | 195 | if ~isempty(ind_remove)
|
---|
| 196 | DataOut.ListVarName(ind_remove)=[];
|
---|
| 197 | DataOut.VarDimName(ind_remove)=[];
|
---|
| 198 | DataOut.VarAttribute(ind_remove)=[];
|
---|
| 199 | end
|
---|
[494] | 200 |
|
---|
[396] | 201 | %% transform of spatial derivatives: TODO check the case with plane angles
|
---|
[518] | 202 | if isfield(Data,'X') && ~isempty(Data.X) && isfield(Data,'DjUi') && ~isempty(Data.DjUi)
|
---|
| 203 | % estimate the Jacobian matrix DXpx/DXphys
|
---|
| 204 | for ip=1:length(Data.X)
|
---|
| 205 | [Xp1,Yp1]=phys_XYZ(Calib,Data.X(ip)+0.5,Data.Y(ip),ZIndex);
|
---|
| 206 | [Xm1,Ym1]=phys_XYZ(Calib,Data.X(ip)-0.5,Data.Y(ip),ZIndex);
|
---|
| 207 | [Xp2,Yp2]=phys_XYZ(Calib,Data.X(ip),Data.Y(ip)+0.5,ZIndex);
|
---|
| 208 | [Xm2,Ym2]=phys_XYZ(Calib,Data.X(ip),Data.Y(ip)-0.5,ZIndex);
|
---|
[396] | 209 | %Jacobian matrix DXpphys/DXpx
|
---|
[518] | 210 | DjXi(1,1)=(Xp1-Xm1);
|
---|
| 211 | DjXi(2,1)=(Yp1-Ym1);
|
---|
| 212 | DjXi(1,2)=(Xp2-Xm2);
|
---|
| 213 | DjXi(2,2)=(Yp2-Ym2);
|
---|
| 214 | DjUi(:,:)=Data.DjUi(ip,:,:);
|
---|
| 215 | DjUi=(DjXi*DjUi')/DjXi;% =J-1*M*J , curvature effects (derivatives of J) neglected
|
---|
| 216 | DataOut.DjUi(ip,:,:)=DjUi';
|
---|
[40] | 217 | end
|
---|
[518] | 218 | DataOut.DjUi = DataOut.DjUi/Dt; % min(Data.DjUi(:,1,1))=DUDX
|
---|
[40] | 219 | end
|
---|
| 220 |
|
---|
[396] | 221 |
|
---|
[40] | 222 | %%%%%%%%%%%%%%%%%%%%
|
---|
| 223 | function [A_out,Rangx,Rangy]=phys_Ima(A,CalibIn,ZIndex)
|
---|
| 224 | xcorner=[];
|
---|
| 225 | ycorner=[];
|
---|
| 226 | npx=[];
|
---|
| 227 | npy=[];
|
---|
[116] | 228 | dx=ones(1,length(A));
|
---|
| 229 | dy=ones(1,length(A));
|
---|
[40] | 230 | for icell=1:length(A)
|
---|
| 231 | siz=size(A{icell});
|
---|
| 232 | npx=[npx siz(2)];
|
---|
| 233 | npy=[npy siz(1)];
|
---|
| 234 | Calib=CalibIn{icell};
|
---|
[116] | 235 | xima=[0.5 siz(2)-0.5 0.5 siz(2)-0.5];%image coordinates of corners
|
---|
[40] | 236 | yima=[0.5 0.5 siz(1)-0.5 siz(1)-0.5];
|
---|
| 237 | [xcorner_new,ycorner_new]=phys_XYZ(Calib,xima,yima,ZIndex);%corresponding physical coordinates
|
---|
[79] | 238 | dx(icell)=(max(xcorner_new)-min(xcorner_new))/(siz(2)-1);
|
---|
| 239 | dy(icell)=(max(ycorner_new)-min(ycorner_new))/(siz(1)-1);
|
---|
[40] | 240 | xcorner=[xcorner xcorner_new];
|
---|
| 241 | ycorner=[ycorner ycorner_new];
|
---|
| 242 | end
|
---|
| 243 | Rangx(1)=min(xcorner);
|
---|
| 244 | Rangx(2)=max(xcorner);
|
---|
| 245 | Rangy(2)=min(ycorner);
|
---|
| 246 | Rangy(1)=max(ycorner);
|
---|
[116] | 247 | test_multi=(max(npx)~=min(npx)) || (max(npy)~=min(npy)); %different image lengths
|
---|
[79] | 248 | npX=1+round((Rangx(2)-Rangx(1))/min(dx));% nbre of pixels in the new image (use the finest resolution min(dx) in the set of images)
|
---|
| 249 | npY=1+round((Rangy(1)-Rangy(2))/min(dy));
|
---|
| 250 | x=linspace(Rangx(1),Rangx(2),npX);
|
---|
| 251 | y=linspace(Rangy(1),Rangy(2),npY);
|
---|
[40] | 252 | [X,Y]=meshgrid(x,y);%grid in physical coordiantes
|
---|
| 253 | vec_B=[];
|
---|
| 254 | A_out={};
|
---|
| 255 | for icell=1:length(A)
|
---|
| 256 | Calib=CalibIn{icell};
|
---|
[396] | 257 | % rescaling of the image coordinates without change of the image array
|
---|
| 258 | if strcmp(Calib.CalibrationType,'rescale') && isequal(Calib,CalibIn{1})
|
---|
| 259 | A_out{icell}=A{icell};%no transform
|
---|
| 260 | Rangx=[0.5 npx-0.5];%image coordiantes of corners
|
---|
| 261 | Rangy=[npy-0.5 0.5];
|
---|
| 262 | [Rangx]=phys_XYZ(Calib,Rangx,[0.5 0.5],ZIndex);%case of translations without rotation and quadratic deformation
|
---|
| 263 | [xx,Rangy]=phys_XYZ(Calib,[0.5 0.5],Rangy,ZIndex);
|
---|
| 264 | else
|
---|
| 265 | % the image needs to be interpolated to the new coordinates
|
---|
[40] | 266 | zphys=0; %default
|
---|
| 267 | if isfield(Calib,'SliceCoord') %.Z= index of plane
|
---|
| 268 | SliceCoord=Calib.SliceCoord(ZIndex,:);
|
---|
| 269 | zphys=SliceCoord(3); %to generalize for non-parallel planes
|
---|
[202] | 270 | if isfield(Calib,'InterfaceCoord') && isfield(Calib,'RefractionIndex')
|
---|
| 271 | H=Calib.InterfaceCoord(3);
|
---|
| 272 | if H>zphys
|
---|
| 273 | zphys=H-(H-zphys)/Calib.RefractionIndex; %corrected z (virtual object)
|
---|
| 274 | end
|
---|
| 275 | end
|
---|
[40] | 276 | end
|
---|
[79] | 277 | [XIMA,YIMA]=px_XYZ(CalibIn{icell},X,Y,zphys);% image coordinates for each point in the real space grid
|
---|
| 278 | XIMA=reshape(round(XIMA),1,npX*npY);%indices reorganized in 'line'
|
---|
| 279 | YIMA=reshape(round(YIMA),1,npX*npY);
|
---|
| 280 | flagin=XIMA>=1 & XIMA<=npx(icell) & YIMA >=1 & YIMA<=npy(icell);%flagin=1 inside the original image
|
---|
[40] | 281 | testuint8=isa(A{icell},'uint8');
|
---|
| 282 | testuint16=isa(A{icell},'uint16');
|
---|
| 283 | if numel(siz)==2 %(B/W images)
|
---|
[79] | 284 | vec_A=reshape(A{icell},1,npx(icell)*npy(icell));%put the original image in line
|
---|
[209] | 285 | %ind_in=find(flagin);
|
---|
[40] | 286 | ind_out=find(~flagin);
|
---|
[79] | 287 | ICOMB=((XIMA-1)*npy(icell)+(npy(icell)+1-YIMA));
|
---|
[40] | 288 | ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A
|
---|
[209] | 289 | %vec_B(ind_in)=vec_A(ICOMB);
|
---|
| 290 | vec_B(flagin)=vec_A(ICOMB);
|
---|
| 291 | vec_B(~flagin)=zeros(size(ind_out));
|
---|
| 292 | % vec_B(ind_out)=zeros(size(ind_out));
|
---|
[79] | 293 | A_out{icell}=reshape(vec_B,npY,npX);%new image in real coordinates
|
---|
[40] | 294 | elseif numel(siz)==3
|
---|
| 295 | for icolor=1:siz(3)
|
---|
| 296 | vec_A=reshape(A{icell}(:,:,icolor),1,npx*npy);%put the original image in line
|
---|
[209] | 297 | % ind_in=find(flagin);
|
---|
[40] | 298 | ind_out=find(~flagin);
|
---|
| 299 | ICOMB=((XIMA-1)*npy+(npy+1-YIMA));
|
---|
| 300 | ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A
|
---|
[209] | 301 | vec_B(flagin)=vec_A(ICOMB);
|
---|
| 302 | vec_B(~flagin)=zeros(size(ind_out));
|
---|
[40] | 303 | A_out{icell}(:,:,icolor)=reshape(vec_B,npy,npx);%new image in real coordinates
|
---|
| 304 | end
|
---|
| 305 | end
|
---|
| 306 | if testuint8
|
---|
| 307 | A_out{icell}=uint8(A_out{icell});
|
---|
| 308 | end
|
---|
| 309 | if testuint16
|
---|
| 310 | A_out{icell}=uint16(A_out{icell});
|
---|
[396] | 311 | end
|
---|
[40] | 312 | end
|
---|
| 313 | end
|
---|
| 314 |
|
---|