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

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

phys: bug repair to account for a constant coordinate angle

File size: 11.2 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% DataOut.VarDimName{2}
97% DataOut.VarDimName{3}
98% DataOut.VarDimName{4}
99% DataOut.VarDimName{5}
100% DataOut.VarDimName{6}
101% DataOut.VarDimName{7}
102% DataOut.VarAttribute{1}
103% DataOut.VarAttribute{2}
104% DataOut.VarAttribute{3}
105% DataOut.VarAttribute{4}
106% DataOut.VarAttribute{5}
107% DataOut.VarAttribute{6}
108% DataOut.VarAttribute{7}
109%------------------------------------------------
110function DataOut=phys_1(Data,Calib)
111% for icell=1:length(Data)
112
113DataOut=Data;%default
114% DataOut.CoordUnit=Calib.CoordUnit; %put flag for physical coordinates
115if isfield(Calib,'SliceCoord') && isfield(Data,'ZIndex')&&~isempty(Data.ZIndex)&&~isnan(Data.ZIndex)
116    DataOut.PlaneCoord=Calib.SliceCoord(Data.ZIndex,:);% transfer the slice position
117    if isfield(Calib,'SliceAngle') % transfer the slice rotation angles
118        if isequal(size(Calib.SliceAngle,1),1)
119             DataOut.PlaneAngle=Calib.SliceAngle;
120        else
121            DataOut.PlaneAngle=Calib.SliceAngle(Data.ZIndex,:);
122        end
123    end
124end
125% The transform ACTS ONLY IF .CoordType='px'and Calib defined
126if isfield(Data,'CoordUnit')%&& isequal(Data.CoordType,'px')&& ~isempty(Calib)
127    if isfield(Calib,'CoordUnit')
128        DataOut.CoordUnit=Calib.CoordUnit;
129    else
130        DataOut.CoordUnit='cm'; %default
131    end
132    DataOut.TimeUnit='s';
133    %transform of X,Y coordinates for vector fields
134    test_z=0;
135    if isfield(Data,'ZIndex') && ~isempty(Data.ZIndex)&&~isnan(Data.ZIndex)
136        Z=Data.ZIndex;
137        test_z=1;
138    else
139        Z=0;
140    end
141    if isfield(Data,'X') &&isfield(Data,'Y')&&~isempty(Data.X) && ~isempty(Data.Y)
142        [DataOut.X,DataOut.Y,DataOut.Z]=phys_XYZ(Calib,Data.X,Data.Y,Z);
143        if test_z
144             DataOut.ListVarName=[DataOut.ListVarName(1:2) {'Z'} DataOut.ListVarName(3:end)];
145             DataOut.VarDimName=[DataOut.VarDimName(1:2) DataOut.VarDimName(1) DataOut.VarDimName(3:end)];
146             ZAttribute{1}.Role='coord_z';
147             DataOut.VarAttribute=[DataOut.VarAttribute(1:2) ZAttribute DataOut.VarAttribute(3:end)];
148        end
149        if isfield(Data,'U')&&isfield(Data,'V')&&~isempty(Data.U) && ~isempty(Data.V)&& isfield(Data,'dt')
150            if ~isempty(Data.dt)
151            [XOut_1,YOut_1]=phys_XYZ(Calib,Data.X-Data.U/2,Data.Y-Data.V/2,Z);
152            [XOut_2,YOut_2]=phys_XYZ(Calib,Data.X+Data.U/2,Data.Y+Data.V/2,Z);
153            DataOut.U=(XOut_2-XOut_1)/Data.dt;
154            DataOut.V=(YOut_2-YOut_1)/Data.dt;
155            end
156        end
157    end
158    %transform of an image or scalar: done in phys_ima
159     
160    %transform of spatial derivatives
161    if isfield(Data,'X') && ~isempty(Data.X) && isfield(Data,'DjUi') && ~isempty(Data.DjUi)...
162          && isfield(Data,'dt')   
163        if ~isempty(Data.dt)
164            % estimate the Jacobian matrix DXpx/DXphys
165            for ip=1:length(Data.X)
166                [Xp1,Yp1]=phys_XYZ(Calib,Data.X(ip)+0.5,Data.Y(ip),Z);
167                [Xm1,Ym1]=phys_XYZ(Calib,Data.X(ip)-0.5,Data.Y(ip),Z);
168                [Xp2,Yp2]=phys_XYZ(Calib,Data.X(ip),Data.Y(ip)+0.5,Z);
169                [Xm2,Ym2]=phys_XYZ(Calib,Data.X(ip),Data.Y(ip)-0.5,Z);
170            %Jacobian matrix DXpphys/DXpx
171               DjXi(1,1)=(Xp1-Xm1);
172               DjXi(2,1)=(Yp1-Ym1);
173               DjXi(1,2)=(Xp2-Xm2);
174               DjXi(2,2)=(Yp2-Ym2);
175               DjUi(:,:)=Data.DjUi(ip,:,:);
176               DjUi=(DjXi*DjUi')/DjXi;% =J-1*M*J , curvature effects (derivatives of J) neglected
177               DataOut.DjUi(ip,:,:)=DjUi';
178            end
179            DataOut.DjUi =  DataOut.DjUi/Data.dt;   %     min(Data.DjUi(:,1,1))=DUDX                         
180        end
181    end
182end
183
184%%%%%%%%%%%%%%%%%%%%
185function [A_out,Rangx,Rangy]=phys_Ima(A,CalibIn,ZIndex)
186xcorner=[];
187ycorner=[];
188npx=[];
189npy=[];
190dx=ones(1,length(A));
191dy=ones(1,length(A));
192for icell=1:length(A)
193    siz=size(A{icell});
194    npx=[npx siz(2)];
195    npy=[npy siz(1)];
196    Calib=CalibIn{icell};
197    xima=[0.5 siz(2)-0.5 0.5 siz(2)-0.5];%image coordinates of corners
198    yima=[0.5 0.5 siz(1)-0.5 siz(1)-0.5];
199    [xcorner_new,ycorner_new]=phys_XYZ(Calib,xima,yima,ZIndex);%corresponding physical coordinates
200    dx(icell)=(max(xcorner_new)-min(xcorner_new))/(siz(2)-1);
201    dy(icell)=(max(ycorner_new)-min(ycorner_new))/(siz(1)-1);
202    xcorner=[xcorner xcorner_new];
203    ycorner=[ycorner ycorner_new];
204end
205Rangx(1)=min(xcorner);
206Rangx(2)=max(xcorner);
207Rangy(2)=min(ycorner);
208Rangy(1)=max(ycorner);
209test_multi=(max(npx)~=min(npx)) || (max(npy)~=min(npy)); %different image lengths
210npX=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)
211npY=1+round((Rangy(1)-Rangy(2))/min(dy));
212x=linspace(Rangx(1),Rangx(2),npX);
213y=linspace(Rangy(1),Rangy(2),npY);
214[X,Y]=meshgrid(x,y);%grid in physical coordiantes
215vec_B=[];
216A_out={};
217for icell=1:length(A)
218    Calib=CalibIn{icell};
219    if isfield(Calib,'R') || isfield(Calib,'kc')|| test_multi ||~isequal(Calib,CalibIn{1})% the image needs to be interpolated to the new coordinates
220        zphys=0; %default
221        if isfield(Calib,'SliceCoord') %.Z= index of plane
222           SliceCoord=Calib.SliceCoord(ZIndex,:);
223           zphys=SliceCoord(3); %to generalize for non-parallel planes
224           if isfield(Calib,'InterfaceCoord') && isfield(Calib,'RefractionIndex')
225                H=Calib.InterfaceCoord(3);
226                if H>zphys
227                    zphys=H-(H-zphys)/Calib.RefractionIndex; %corrected z (virtual object)
228                end
229           end
230        end
231        [XIMA,YIMA]=px_XYZ(CalibIn{icell},X,Y,zphys);% image coordinates for each point in the real space grid
232        XIMA=reshape(round(XIMA),1,npX*npY);%indices reorganized in 'line'
233        YIMA=reshape(round(YIMA),1,npX*npY);
234        flagin=XIMA>=1 & XIMA<=npx(icell) & YIMA >=1 & YIMA<=npy(icell);%flagin=1 inside the original image
235        testuint8=isa(A{icell},'uint8');
236        testuint16=isa(A{icell},'uint16');
237        if numel(siz)==2 %(B/W images)
238            vec_A=reshape(A{icell},1,npx(icell)*npy(icell));%put the original image in line
239            %ind_in=find(flagin);
240            ind_out=find(~flagin);
241            ICOMB=((XIMA-1)*npy(icell)+(npy(icell)+1-YIMA));
242            ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A
243            %vec_B(ind_in)=vec_A(ICOMB);
244            vec_B(flagin)=vec_A(ICOMB);
245            vec_B(~flagin)=zeros(size(ind_out));
246%             vec_B(ind_out)=zeros(size(ind_out));
247            A_out{icell}=reshape(vec_B,npY,npX);%new image in real coordinates
248        elseif numel(siz)==3     
249            for icolor=1:siz(3)
250                vec_A=reshape(A{icell}(:,:,icolor),1,npx*npy);%put the original image in line
251               % ind_in=find(flagin);
252                ind_out=find(~flagin);
253                ICOMB=((XIMA-1)*npy+(npy+1-YIMA));
254                ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A
255                vec_B(flagin)=vec_A(ICOMB);
256                vec_B(~flagin)=zeros(size(ind_out));
257                A_out{icell}(:,:,icolor)=reshape(vec_B,npy,npx);%new image in real coordinates
258            end
259        end
260        if testuint8
261            A_out{icell}=uint8(A_out{icell});
262        end
263        if testuint16
264            A_out{icell}=uint16(A_out{icell});
265        end
266    else%     
267        A_out{icell}=A{icell};%no transform
268        Rangx=[0.5 npx-0.5];%image coordiantes of corners
269        Rangy=[npy-0.5 0.5];
270        [Rangx]=phys_XYZ(Calib,Rangx,[0.5 0.5],ZIndex);%case of translations without rotation and quadratic deformation
271        [xx,Rangy]=phys_XYZ(Calib,[0.5 0.5],Rangy,ZIndex);
272    end
273end
274
Note: See TracBrowser for help on using the repository browser.