[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
|
---|
[545] | 189 | if isfield(DataOut,'VarAttribute')&&isfield(DataOut.VarAttribute{3},'VarIndex_tps')
|
---|
[518] | 190 | DataOut.VarAttribute{3}=rmfield(DataOut.VarAttribute{3},'VarIndex_tps');
|
---|
| 191 | end
|
---|
| 192 |
|
---|
| 193 | if ~isempty(ind_remove)
|
---|
| 194 | DataOut.ListVarName(ind_remove)=[];
|
---|
| 195 | DataOut.VarDimName(ind_remove)=[];
|
---|
| 196 | DataOut.VarAttribute(ind_remove)=[];
|
---|
| 197 | end
|
---|
[494] | 198 |
|
---|
[396] | 199 | %% transform of spatial derivatives: TODO check the case with plane angles
|
---|
[518] | 200 | if isfield(Data,'X') && ~isempty(Data.X) && isfield(Data,'DjUi') && ~isempty(Data.DjUi)
|
---|
| 201 | % estimate the Jacobian matrix DXpx/DXphys
|
---|
| 202 | for ip=1:length(Data.X)
|
---|
| 203 | [Xp1,Yp1]=phys_XYZ(Calib,Data.X(ip)+0.5,Data.Y(ip),ZIndex);
|
---|
| 204 | [Xm1,Ym1]=phys_XYZ(Calib,Data.X(ip)-0.5,Data.Y(ip),ZIndex);
|
---|
| 205 | [Xp2,Yp2]=phys_XYZ(Calib,Data.X(ip),Data.Y(ip)+0.5,ZIndex);
|
---|
| 206 | [Xm2,Ym2]=phys_XYZ(Calib,Data.X(ip),Data.Y(ip)-0.5,ZIndex);
|
---|
[396] | 207 | %Jacobian matrix DXpphys/DXpx
|
---|
[518] | 208 | DjXi(1,1)=(Xp1-Xm1);
|
---|
| 209 | DjXi(2,1)=(Yp1-Ym1);
|
---|
| 210 | DjXi(1,2)=(Xp2-Xm2);
|
---|
| 211 | DjXi(2,2)=(Yp2-Ym2);
|
---|
| 212 | DjUi(:,:)=Data.DjUi(ip,:,:);
|
---|
| 213 | DjUi=(DjXi*DjUi')/DjXi;% =J-1*M*J , curvature effects (derivatives of J) neglected
|
---|
| 214 | DataOut.DjUi(ip,:,:)=DjUi';
|
---|
[40] | 215 | end
|
---|
[518] | 216 | DataOut.DjUi = DataOut.DjUi/Dt; % min(Data.DjUi(:,1,1))=DUDX
|
---|
[40] | 217 | end
|
---|
| 218 |
|
---|
[396] | 219 |
|
---|
[40] | 220 | %%%%%%%%%%%%%%%%%%%%
|
---|
| 221 | function [A_out,Rangx,Rangy]=phys_Ima(A,CalibIn,ZIndex)
|
---|
| 222 | xcorner=[];
|
---|
| 223 | ycorner=[];
|
---|
| 224 | npx=[];
|
---|
| 225 | npy=[];
|
---|
[116] | 226 | dx=ones(1,length(A));
|
---|
| 227 | dy=ones(1,length(A));
|
---|
[40] | 228 | for icell=1:length(A)
|
---|
| 229 | siz=size(A{icell});
|
---|
| 230 | npx=[npx siz(2)];
|
---|
| 231 | npy=[npy siz(1)];
|
---|
| 232 | Calib=CalibIn{icell};
|
---|
[116] | 233 | xima=[0.5 siz(2)-0.5 0.5 siz(2)-0.5];%image coordinates of corners
|
---|
[40] | 234 | yima=[0.5 0.5 siz(1)-0.5 siz(1)-0.5];
|
---|
| 235 | [xcorner_new,ycorner_new]=phys_XYZ(Calib,xima,yima,ZIndex);%corresponding physical coordinates
|
---|
[79] | 236 | dx(icell)=(max(xcorner_new)-min(xcorner_new))/(siz(2)-1);
|
---|
| 237 | dy(icell)=(max(ycorner_new)-min(ycorner_new))/(siz(1)-1);
|
---|
[40] | 238 | xcorner=[xcorner xcorner_new];
|
---|
| 239 | ycorner=[ycorner ycorner_new];
|
---|
| 240 | end
|
---|
| 241 | Rangx(1)=min(xcorner);
|
---|
| 242 | Rangx(2)=max(xcorner);
|
---|
| 243 | Rangy(2)=min(ycorner);
|
---|
| 244 | Rangy(1)=max(ycorner);
|
---|
[116] | 245 | test_multi=(max(npx)~=min(npx)) || (max(npy)~=min(npy)); %different image lengths
|
---|
[79] | 246 | 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)
|
---|
| 247 | npY=1+round((Rangy(1)-Rangy(2))/min(dy));
|
---|
| 248 | x=linspace(Rangx(1),Rangx(2),npX);
|
---|
| 249 | y=linspace(Rangy(1),Rangy(2),npY);
|
---|
[40] | 250 | [X,Y]=meshgrid(x,y);%grid in physical coordiantes
|
---|
| 251 | vec_B=[];
|
---|
| 252 | A_out={};
|
---|
| 253 | for icell=1:length(A)
|
---|
| 254 | Calib=CalibIn{icell};
|
---|
[396] | 255 | % rescaling of the image coordinates without change of the image array
|
---|
| 256 | if strcmp(Calib.CalibrationType,'rescale') && isequal(Calib,CalibIn{1})
|
---|
| 257 | A_out{icell}=A{icell};%no transform
|
---|
| 258 | Rangx=[0.5 npx-0.5];%image coordiantes of corners
|
---|
| 259 | Rangy=[npy-0.5 0.5];
|
---|
| 260 | [Rangx]=phys_XYZ(Calib,Rangx,[0.5 0.5],ZIndex);%case of translations without rotation and quadratic deformation
|
---|
| 261 | [xx,Rangy]=phys_XYZ(Calib,[0.5 0.5],Rangy,ZIndex);
|
---|
| 262 | else
|
---|
| 263 | % the image needs to be interpolated to the new coordinates
|
---|
[40] | 264 | zphys=0; %default
|
---|
| 265 | if isfield(Calib,'SliceCoord') %.Z= index of plane
|
---|
| 266 | SliceCoord=Calib.SliceCoord(ZIndex,:);
|
---|
| 267 | zphys=SliceCoord(3); %to generalize for non-parallel planes
|
---|
[202] | 268 | if isfield(Calib,'InterfaceCoord') && isfield(Calib,'RefractionIndex')
|
---|
| 269 | H=Calib.InterfaceCoord(3);
|
---|
| 270 | if H>zphys
|
---|
| 271 | zphys=H-(H-zphys)/Calib.RefractionIndex; %corrected z (virtual object)
|
---|
| 272 | end
|
---|
| 273 | end
|
---|
[40] | 274 | end
|
---|
[79] | 275 | [XIMA,YIMA]=px_XYZ(CalibIn{icell},X,Y,zphys);% image coordinates for each point in the real space grid
|
---|
| 276 | XIMA=reshape(round(XIMA),1,npX*npY);%indices reorganized in 'line'
|
---|
| 277 | YIMA=reshape(round(YIMA),1,npX*npY);
|
---|
| 278 | flagin=XIMA>=1 & XIMA<=npx(icell) & YIMA >=1 & YIMA<=npy(icell);%flagin=1 inside the original image
|
---|
[40] | 279 | testuint8=isa(A{icell},'uint8');
|
---|
| 280 | testuint16=isa(A{icell},'uint16');
|
---|
| 281 | if numel(siz)==2 %(B/W images)
|
---|
[79] | 282 | vec_A=reshape(A{icell},1,npx(icell)*npy(icell));%put the original image in line
|
---|
[209] | 283 | %ind_in=find(flagin);
|
---|
[40] | 284 | ind_out=find(~flagin);
|
---|
[79] | 285 | ICOMB=((XIMA-1)*npy(icell)+(npy(icell)+1-YIMA));
|
---|
[40] | 286 | ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A
|
---|
[209] | 287 | %vec_B(ind_in)=vec_A(ICOMB);
|
---|
| 288 | vec_B(flagin)=vec_A(ICOMB);
|
---|
| 289 | vec_B(~flagin)=zeros(size(ind_out));
|
---|
| 290 | % vec_B(ind_out)=zeros(size(ind_out));
|
---|
[79] | 291 | A_out{icell}=reshape(vec_B,npY,npX);%new image in real coordinates
|
---|
[40] | 292 | elseif numel(siz)==3
|
---|
| 293 | for icolor=1:siz(3)
|
---|
| 294 | vec_A=reshape(A{icell}(:,:,icolor),1,npx*npy);%put the original image in line
|
---|
[209] | 295 | % ind_in=find(flagin);
|
---|
[40] | 296 | ind_out=find(~flagin);
|
---|
| 297 | ICOMB=((XIMA-1)*npy+(npy+1-YIMA));
|
---|
| 298 | ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A
|
---|
[209] | 299 | vec_B(flagin)=vec_A(ICOMB);
|
---|
| 300 | vec_B(~flagin)=zeros(size(ind_out));
|
---|
[40] | 301 | A_out{icell}(:,:,icolor)=reshape(vec_B,npy,npx);%new image in real coordinates
|
---|
| 302 | end
|
---|
| 303 | end
|
---|
| 304 | if testuint8
|
---|
| 305 | A_out{icell}=uint8(A_out{icell});
|
---|
| 306 | end
|
---|
| 307 | if testuint16
|
---|
| 308 | A_out{icell}=uint16(A_out{icell});
|
---|
[396] | 309 | end
|
---|
[40] | 310 | end
|
---|
| 311 | end
|
---|
| 312 |
|
---|