source: trunk/src/transform_field/phys.m @ 157

Last change on this file since 157 was 157, checked in by sommeria, 13 years ago

bug repair on phys and phys_polar

File size: 12.9 KB
Line 
1%'phys': transforms image (px) to real world (phys) coordinates using geometric calibration parameters
2% DataOut=phys(Data,CalibData) , transform one input field
3% [DataOut,DataOut_1]=phys(Data,CalibData,Data_1,CalibData_1), transform two input fields
4
5% OUTPUT:
6% DataOut:   structure representing the modified field
7% DataOut_1: structure representing the second modified field
8
9%INPUT:
10% Data:  structure of input data
11%       with fields .A (image or scalar matrix), AX, AY
12%       .X,.Y,.U,.V, .DjUi
13%       .ZIndex: index of plane in multilevel case
14%       .CoordType='phys' or 'px', The function ACTS ONLY IF .CoordType='px'
15% CalibData: structure containing calibration parameters or a subtree Calib.GeometryCalib =calibration data (tsai parameters)
16
17function [DataOut,DataOut_1]=phys(varargin)
18% A FAIRE: 1- verifier si DataIn est une 'field structure'(.ListVarName'):
19% chercher ListVarAttribute, for each field (cell of variables):
20%   .CoordType: 'phys' or 'px'   (default==phys, no transform)
21%   .scale_factor: =dt (to transform displacement into velocity) default=1
22%   .covariance: 'scalar', 'coord', 'D_i': covariant (like velocity), 'D^i': contravariant (like gradient), 'D^jD_i' (like strain tensor)
23%   (default='coord' if .Role='coord_x,_y...,
24%            'D_i' if '.Role='vector_x,...',
25%              'scalar', else (thenno change except scale factor)
26Calib{1}=[];
27if nargin==2||nargin==4 % nargin =nbre of input variables
28    Data=varargin{1};
29    DataOut=Data;%default
30    DataOut_1=[];%default
31    CalibData=varargin{2};
32    if isfield(CalibData,'GeometryCalib')
33        Calib{1}=CalibData.GeometryCalib;
34    end
35    Calib{2}=Calib{1};
36else
37    DataOut.Txt='wrong input: need two or four structures';
38end
39test_1=0;
40if nargin==4
41    test_1=1;
42    Data_1=varargin{3};
43    DataOut_1=Data_1;%default
44    CalibData_1=varargin{4};
45    if isfield(CalibData_1,'GeometryCalib')
46        Calib{2}=CalibData_1.GeometryCalib;
47    end
48end
49iscalar=0;
50if  ~isempty(Calib{1})
51    DataOut=phys_1(Data,Calib{1});
52    %case of images or scalar: in case of two input fields, we need to project the transform of on the same regular grid
53    if isfield(Data,'A') && isfield(Data,'AX') && ~isempty(Data.AX) && isfield(Data,'AY')&&...
54                                           ~isempty(Data.AY) && length(Data.A)>1
55        iscalar=1;
56        A{1}=Data.A;
57    end
58end
59%transform of X,Y coordinates for vector fields
60if isfield(Data,'ZIndex')&&~isempty(Data.ZIndex)&&~isnan(Data.ZIndex)
61    ZIndex=Data.ZIndex;
62else
63    ZIndex=1;
64end
65if test_1
66    DataOut_1=phys_1(Data_1,Calib{2});
67    if isfield(Data_1,'A')&&isfield(Data_1,'AX')&&~isempty(Data_1.AX) && isfield(Data_1,'AY')&&...
68                                       ~isempty(Data_1.AY)&&length(Data_1.A)>1
69          iscalar=iscalar+1;
70          Calib{iscalar}=Calib{2};
71          A{iscalar}=Data_1.A;
72          if isfield(Data_1,'ZIndex') && ~isequal(Data_1.ZIndex,ZIndex)
73              DataOut.Txt='inconsistent plane indexes in the two input fields';
74          end
75          if iscalar==1% case for which only the second field is a scalar
76               [A,AX,AY]=phys_Ima(A,Calib,ZIndex);
77               DataOut_1.A=A{1};
78               DataOut_1.AX=AX;
79               DataOut_1.AY=AY;
80               return
81          end
82    end
83end
84if iscalar~=0
85    [A,AX,AY]=phys_Ima(A,Calib,ZIndex);%TODO : introduire interp2_uvmat ds phys_ima
86    DataOut.A=A{1};
87    DataOut.AX=AX;
88    DataOut.AY=AY;
89    if iscalar==2
90        DataOut_1.A=A{2};
91        DataOut_1.AX=AX;
92        DataOut_1.AY=AY;
93    end
94end
95
96%------------------------------------------------
97function DataOut=phys_1(Data,Calib)
98% for icell=1:length(Data)
99
100DataOut=Data;%default
101DataOut.CoordType='phys'; %put flag for physical coordinates
102if isfield(Calib,'SliceCoord')
103    DataOut.PlaneCoord=Calib.SliceCoord;%to generalise for any plane
104end
105% The transform ACTS ONLY IF .CoordType='px'and Calib defined
106if isfield(Data,'CoordType')&& isequal(Data.CoordType,'px')&& ~isempty(Calib)
107    if isfield(Calib,'CoordUnit')
108        DataOut.CoordUnit=Calib.CoordUnit;
109    else
110        DataOut.CoordUnit='cm'; %default
111    end
112    DataOut.TimeUnit='s';
113    %transform of X,Y coordinates for vector fields
114    if isfield(Data,'ZIndex') && ~isempty(Data.ZIndex)&&~isnan(Data.ZIndex)
115        Z=Data.ZIndex;
116    else
117        Z=0;
118    end
119    if isfield(Data,'X') &&isfield(Data,'Y')&&~isempty(Data.X) && ~isempty(Data.Y)
120        [DataOut.X,DataOut.Y,DataOut.Z]=phys_XYZ(Calib,Data.X,Data.Y,Z);
121        if isfield(Data,'U')&&isfield(Data,'V')&&~isempty(Data.U) && ~isempty(Data.V)&& isfield(Data,'dt')
122            if ~isempty(Data.dt)
123            [XOut_1,YOut_1]=phys_XYZ(Calib,Data.X-Data.U/2,Data.Y-Data.V/2,Z);
124            [XOut_2,YOut_2]=phys_XYZ(Calib,Data.X+Data.U/2,Data.Y+Data.V/2,Z);
125            DataOut.U=(XOut_2-XOut_1)/Data.dt;
126            DataOut.V=(YOut_2-YOut_1)/Data.dt;
127            end
128        end
129    end
130    %transform of an image or scalar: done in phys_ima
131     
132    %transform of spatial derivatives
133    if isfield(Data,'X') && ~isempty(Data.X) && isfield(Data,'DjUi') && ~isempty(Data.DjUi)...
134          && isfield(Data,'dt')   
135        if ~isempty(Data.dt)
136            % estimate the Jacobian matrix DXpx/DXphys
137            for ip=1:length(Data.X)
138                [Xp1,Yp1]=phys_XYZ(Calib,Data.X(ip)+0.5,Data.Y(ip),Z);
139                [Xm1,Ym1]=phys_XYZ(Calib,Data.X(ip)-0.5,Data.Y(ip),Z);
140                [Xp2,Yp2]=phys_XYZ(Calib,Data.X(ip),Data.Y(ip)+0.5,Z);
141                [Xm2,Ym2]=phys_XYZ(Calib,Data.X(ip),Data.Y(ip)-0.5,Z);
142            %Jacobian matrix DXpphys/DXpx
143               DjXi(1,1)=(Xp1-Xm1);
144               DjXi(2,1)=(Yp1-Ym1);
145               DjXi(1,2)=(Xp2-Xm2);
146               DjXi(2,2)=(Yp2-Ym2);
147               DjUi(:,:)=Data.DjUi(ip,:,:);
148               DjUi=(DjXi*DjUi')/DjXi;% =J-1*M*J , curvature effects (derivatives of J) neglected
149               DataOut.DjUi(ip,:,:)=DjUi';
150            end
151            DataOut.DjUi =  DataOut.DjUi/Data.dt;   %     min(Data.DjUi(:,1,1))=DUDX                         
152        end
153    end
154end
155
156%%%%%%%%%%%%%%%%%%%%
157function [A_out,Rangx,Rangy]=phys_Ima(A,CalibIn,ZIndex)
158
159if ndims(A)==3
160    A=mean(A,3);
161end
162
163
164xcorner=[];
165ycorner=[];
166npx=[];
167npy=[];
168dx=ones(1,length(A));
169dy=ones(1,length(A));
170for icell=1:length(A)
171    siz=size(A{icell});
172    npx=[npx siz(2)];
173    npy=[npy siz(1)];
174    Calib=CalibIn{icell};
175    xima=[0.5 siz(2)-0.5 0.5 siz(2)-0.5];%image coordinates of corners
176    yima=[0.5 0.5 siz(1)-0.5 siz(1)-0.5];
177    [xcorner_new,ycorner_new]=phys_XYZ(Calib,xima,yima,ZIndex);%corresponding physical coordinates
178    dx(icell)=(max(xcorner_new)-min(xcorner_new))/(siz(2)-1);
179    dy(icell)=(max(ycorner_new)-min(ycorner_new))/(siz(1)-1);
180    xcorner=[xcorner xcorner_new];
181    ycorner=[ycorner ycorner_new];
182end
183Rangx(1)=min(xcorner);
184Rangx(2)=max(xcorner);
185Rangy(2)=min(ycorner);
186Rangy(1)=max(ycorner);
187test_multi=(max(npx)~=min(npx)) || (max(npy)~=min(npy)); %different image lengths
188npX=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)
189npY=1+round((Rangy(1)-Rangy(2))/min(dy));
190x=linspace(Rangx(1),Rangx(2),npX);
191y=linspace(Rangy(1),Rangy(2),npY);
192[X,Y]=meshgrid(x,y);%grid in physical coordiantes
193vec_B=[];
194A_out={};
195for icell=1:length(A)
196    Calib=CalibIn{icell};
197    if isfield(Calib,'R') || isfield(Calib,'kc')|| test_multi ||~isequal(Calib,CalibIn{1})% the image needs to be interpolated to the new coordinates
198        zphys=0; %default
199        if isfield(Calib,'SliceCoord') %.Z= index of plane
200           SliceCoord=Calib.SliceCoord(ZIndex,:);
201           zphys=SliceCoord(3); %to generalize for non-parallel planes
202        end
203        [XIMA,YIMA]=px_XYZ(CalibIn{icell},X,Y,zphys);% image coordinates for each point in the real space grid
204        XIMA=reshape(round(XIMA),1,npX*npY);%indices reorganized in 'line'
205        YIMA=reshape(round(YIMA),1,npX*npY);
206        flagin=XIMA>=1 & XIMA<=npx(icell) & YIMA >=1 & YIMA<=npy(icell);%flagin=1 inside the original image
207        testuint8=isa(A{icell},'uint8');
208        testuint16=isa(A{icell},'uint16');
209        if numel(siz)==2 %(B/W images)
210            vec_A=reshape(A{icell},1,npx(icell)*npy(icell));%put the original image in line
211            ind_in=find(flagin);
212            ind_out=find(~flagin);
213            ICOMB=((XIMA-1)*npy(icell)+(npy(icell)+1-YIMA));
214            ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A
215            vec_B(ind_in)=vec_A(ICOMB);
216            vec_B(ind_out)=zeros(size(ind_out));
217            A_out{icell}=reshape(vec_B,npY,npX);%new image in real coordinates
218        elseif numel(siz)==3     
219            for icolor=1:siz(3)
220                vec_A=reshape(A{icell}(:,:,icolor),1,npx*npy);%put the original image in line
221                ind_in=find(flagin);
222                ind_out=find(~flagin);
223                ICOMB=((XIMA-1)*npy+(npy+1-YIMA));
224                ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A
225                vec_B(ind_in)=vec_A(ICOMB);
226                vec_B(ind_out)=zeros(size(ind_out));
227                A_out{icell}(:,:,icolor)=reshape(vec_B,npy,npx);%new image in real coordinates
228            end
229        end
230        if testuint8
231            A_out{icell}=uint8(A_out{icell});
232        end
233        if testuint16
234            A_out{icell}=uint16(A_out{icell});
235        end
236    else%     
237        A_out{icell}=A{icell};%no transform
238        Rangx=[0.5 npx-0.5];%image coordiantes of corners
239        Rangy=[npy-0.5 0.5];
240        [Rangx]=phys_XYZ(Calib,Rangx,[0.5 0.5],ZIndex);%case of translations without rotation and quadratic deformation
241        [xx,Rangy]=phys_XYZ(Calib,[0.5 0.5],Rangy,ZIndex);
242    end
243end
244
245%------------------------------------------------------------------------
246%'phys_XYZ':transforms image (px) to real world (phys) coordinates using geometric calibration parameters
247% function [Xphys,Yphys]=phys_XYZ(Calib,X,Y,Z)
248%
249%OUTPUT:
250%
251%INPUT:
252%Z: index of plane
253function [Xphys,Yphys,Zphys]=phys_XYZ(Calib,X,Y,Z)
254%------------------------------------------------------------------------
255if exist('Z','var')&& isequal(Z,round(Z))&& Z>0 && isfield(Calib,'SliceCoord')&&length(Calib.SliceCoord)>=Z
256    Zindex=Z;
257    Zphys=Calib.SliceCoord(Zindex,3);%GENERALISER AUX CAS AVEC ANGLE
258else
259    Zphys=0;
260end
261if ~exist('X','var')||~exist('Y','var')
262    Xphys=[];
263    Yphys=[];%default
264    return
265end
266%coordinate transform
267if ~isfield(Calib,'fx_fy')
268     Calib.fx_fy=[1 1];
269end
270if ~isfield(Calib,'Tx_Ty_Tz')
271     Calib.Tx_Ty_Tz=[0 0 1];
272end
273if ~isfield(Calib,'Cx_Cy')
274     Calib.Cx_Cy=[0 0];
275end
276if ~isfield(Calib,'kc')
277     Calib.kc=0;
278end
279if isfield(Calib,'R')
280    R=(Calib.R)';
281    Tx=Calib.Tx_Ty_Tz(1);
282    Ty=Calib.Tx_Ty_Tz(2);
283    Tz=Calib.Tx_Ty_Tz(3);
284    f=Calib.fx_fy(1);%dpy=1; sx=1
285    dpx=Calib.fx_fy(2)/Calib.fx_fy(1);
286    Dx=R(5)*R(7)-R(4)*R(8);
287    Dy=R(1)*R(8)-R(2)*R(7);
288    D0=f*(R(2)*R(4)-R(1)*R(5));
289    Z11=R(6)*R(8)-R(5)*R(9);
290    Z12=R(2)*R(9)-R(3)*R(8); 
291    Z21=R(4)*R(9)-R(6)*R(7);
292    Z22=R(3)*R(7)-R(1)*R(9);
293    Zx0=R(3)*R(5)-R(2)*R(6);
294    Zy0=R(1)*R(6)-R(3)*R(4);
295    A11=R(8)*Ty-R(5)*Tz+Z11*Zphys;
296    A12=R(2)*Tz-R(8)*Tx+Z12*Zphys;
297    A21=-R(7)*Ty+R(4)*Tz+Z21*Zphys;
298    A22=-R(1)*Tz+R(7)*Tx+Z22*Zphys;
299    X0=f*(R(5)*Tx-R(2)*Ty+Zx0*Zphys);
300    Y0=f*(-R(4)*Tx+R(1)*Ty+Zy0*Zphys);
301        %px to camera:
302    Xd=dpx*(X-Calib.Cx_Cy(1)); % sensor coordinates
303    Yd=(Y-Calib.Cx_Cy(2));
304    dist_fact=1+Calib.kc*(Xd.*Xd+Yd.*Yd)/(f*f); %distortion factor
305    Xu=Xd./dist_fact;%undistorted sensor coordinates
306    Yu=Yd./dist_fact;
307    denom=Dx*Xu+Dy*Yu+D0;
308    % denom2=denom.*denom;
309    Xphys=(A11.*Xu+A12.*Yu+X0)./denom;%world coordinates
310    Yphys=(A21.*Xu+A22.*Yu+Y0)./denom;
311%     Xd=(X-Calib.Cx_Cy(1))/Calib.fx_fy(1); % sensor coordinates
312%     Yd=(Y-Calib.Cx_Cy(2))/Calib.fx_fy(2);
313%     dist_fact=1+Calib.kc*(Xd.*Xd+Yd.*Yd); %distortion factor
314%     Xu=Xd./dist_fact;%undistorted sensor coordinates
315%     Yu=Yd./dist_fact;
316%     A11=R(7)*Xu-R(1);
317%     A12=R(8)*Xu-R(2);
318%     A21=R(7)*Yu-R(4);
319%     A22=R(8)*Yu-R(5);
320%     B1=(R(3)-R(9)*Xu)*Zphys-Tz*Xu+Tx;
321%     B2=(R(6)-R(9)*Yu)*Zphys-Tz*Yu+Ty;
322%     deter=A12.*A21-A11.*A22;
323%     Xphys=(A21.*B1-A11.*B2)./deter;
324%     Yphys=(-A22.*B1+A12.*B2)./deter;
325else
326    Xphys=-Calib.Tx_Ty_Tz(1)+X/Calib.fx_fy(1);
327    Yphys=-Calib.Tx_Ty_Tz(2)+Y/Calib.fx_fy(2);
328end
329
330%'px_XYZ': transform phys coordinates to image coordinates (px)
331%
332% OUPUT:
333% X,Y: array of coordinates in the image cooresponding to the input physical positions
334%                    (origin at lower leftcorner, unit=pixel)
335
336% INPUT:
337% Calib: structure containing the calibration parameters (read from the ImaDoc .xml file)
338% Xphys, Yphys: array of x,y physical coordinates
339% [Zphys]: corresponding array of z physical coordinates (0 by default)
340
341
342
343
Note: See TracBrowser for help on using the repository browser.