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

Last change on this file since 545 was 545, checked in by sommeria, 11 years ago

bug repaired in phys

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 first field in phys coordinates
7% DataOut_1: structure representing the second  field in phys coordinates
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% Data_1, CalibData_1: same as Data, CalibData for the second field.
17
18function DataOut=phys(DataIn,XmlData,DataIn_1,XmlData_1)
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)
27%% set GUI config
28DataOut=[];
29DataOut_1=[]; %default second  output field
30if strcmp(DataIn,'*')
31    if isfield(XmlData,'GeometryCalib')&& isfield(XmlData.GeometryCalib,'CoordUnit')
32        DataOut.CoordUnit=XmlData.GeometryCalib.CoordUnit;
33    end
34    return
35end
36
37%% analyse input and set default output
38DataOut=DataIn;%default first output field
39if nargin>=2 % nargin =nbre of input variables
40    if isfield(XmlData,'GeometryCalib')
41        Calib{1}=XmlData.GeometryCalib;
42    else
43        Calib{1}=[];
44    end
45    if nargin>=3  %two input fields
46        DataOut_1=DataIn_1;%default second output field
47        if nargin>=4 && isfield(XmlData_1,'GeometryCalib')
48            Calib{2}=XmlData_1.GeometryCalib;
49        else
50            Calib{2}=Calib{1};
51        end
52    end
53end
54
55%% get the z index defining the section plane
56if isfield(DataIn,'ZIndex')&&~isempty(DataIn.ZIndex)&&~isnan(DataIn.ZIndex)
57    ZIndex=DataIn.ZIndex;
58else
59    ZIndex=1;
60end
61
62%% transform first field
63iscalar=0;% counter of scalar fields
64if  ~isempty(Calib{1})
65    if ~isfield(Calib{1},'CalibrationType')||~isfield(Calib{1},'CoordUnit')
66        return %bad calib parameter input
67    end
68    if ~(isfield(DataIn,'CoordUnit')&& strcmp(DataIn.CoordUnit,'pixel'))
69        return % transform only fields in pixel coordinates
70    end
71    DataOut=phys_1(DataIn,Calib{1},ZIndex);% transform coordiantes and velocity components
72    %case of images or scalar: in case of two input fields, we need to project the transform  on the same regular grid
73    if isfield(DataIn,'A') && isfield(DataIn,'AX') && ~isempty(DataIn.AX) && isfield(DataIn,'AY')&&...
74                                           ~isempty(DataIn.AY) && length(DataIn.A)>1
75        iscalar=1;
76        A{1}=DataIn.A;
77    end
78end
79
80%% document the selected  plane position and angle if relevant
81if 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
90end
91
92%% transform second field if relevant
93if ~isempty(DataOut_1)
94    if isfield(DataIn_1,'ZIndex') && ~isequal(DataIn_1.ZIndex,ZIndex)
95        DataOut_1.Txt='different plane indices for the two input fields';
96        return
97    end
98    if ~isfield(Calib{2},'CalibrationType')||~isfield(Calib{2},'CoordUnit')
99        return %bad calib parameter input
100    end
101    if ~(isfield(DataIn_1,'CoordUnit')&& strcmp(DataIn_1.CoordUnit,'pixel'))
102        return % transform only fields in pixel coordinates
103    end
104    DataOut_1=phys_1(DataOut_1,Calib{2},ZIndex);
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
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
121        iscalar=iscalar+1;
122        Calib{iscalar}=Calib{2};
123        A{iscalar}=DataIn_1.A;
124    end
125end
126
127%% transform the scalar(s) or image(s)
128if iscalar~=0
129    [A,AX,AY]=phys_Ima(A,Calib,ZIndex);%TODO : introduire interp2_uvmat ds phys_ima
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
139    if iscalar==2
140        DataOut_1.A=A{2};
141        DataOut_1.AX=AX;
142        DataOut_1.AY=AY;
143    end
144end
145
146% subtract fields
147if ~isempty(DataOut_1)
148DataOut=sub_field(DataOut,[],DataOut_1);
149end
150%------------------------------------------------
151%--- transform a single field
152function DataOut=phys_1(Data,Calib,ZIndex)
153%------------------------------------------------
154%% set default output
155DataOut=Data;%default
156DataOut.CoordUnit=Calib.CoordUnit;% the output coord unit is set by the calibration parameters
157
158%% transform  X,Y coordinates for velocity fields (transform of an image or scalar done in phys_ima)
159if 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;
164    end
165    if isfield(Data,'Dt')&&~isempty(Data.Dt)
166        Dt=Data.Dt;
167    end
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;
173    end
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
177end
178
179%% suppress tps
180list_tps={'Coord_tps'  'U_tps'  'V_tps'  'SubRange'  'NbSites'};
181ind_remove=[];
182for 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});
187    end
188end
189if isfield(DataOut,'VarAttribute')&&isfield(DataOut.VarAttribute{3},'VarIndex_tps')
190    DataOut.VarAttribute{3}=rmfield(DataOut.VarAttribute{3},'VarIndex_tps');
191end
192
193if ~isempty(ind_remove)
194    DataOut.ListVarName(ind_remove)=[];
195    DataOut.VarDimName(ind_remove)=[];
196    DataOut.VarAttribute(ind_remove)=[];
197end
198   
199%% transform of spatial derivatives: TODO check the case with plane angles
200if 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);
207        %Jacobian matrix DXpphys/DXpx
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';
215    end
216    DataOut.DjUi =  DataOut.DjUi/Dt;   %     min(Data.DjUi(:,1,1))=DUDX
217end
218
219
220%%%%%%%%%%%%%%%%%%%%
221function [A_out,Rangx,Rangy]=phys_Ima(A,CalibIn,ZIndex)
222xcorner=[];
223ycorner=[];
224npx=[];
225npy=[];
226dx=ones(1,length(A));
227dy=ones(1,length(A));
228for icell=1:length(A)
229    siz=size(A{icell});
230    npx=[npx siz(2)];
231    npy=[npy siz(1)];
232    Calib=CalibIn{icell};
233    xima=[0.5 siz(2)-0.5 0.5 siz(2)-0.5];%image coordinates of corners
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
236    dx(icell)=(max(xcorner_new)-min(xcorner_new))/(siz(2)-1);
237    dy(icell)=(max(ycorner_new)-min(ycorner_new))/(siz(1)-1);
238    xcorner=[xcorner xcorner_new];
239    ycorner=[ycorner ycorner_new];
240end
241Rangx(1)=min(xcorner);
242Rangx(2)=max(xcorner);
243Rangy(2)=min(ycorner);
244Rangy(1)=max(ycorner);
245test_multi=(max(npx)~=min(npx)) || (max(npy)~=min(npy)); %different image lengths
246npX=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)
247npY=1+round((Rangy(1)-Rangy(2))/min(dy));
248x=linspace(Rangx(1),Rangx(2),npX);
249y=linspace(Rangy(1),Rangy(2),npY);
250[X,Y]=meshgrid(x,y);%grid in physical coordiantes
251vec_B=[];
252A_out={};
253for icell=1:length(A)
254    Calib=CalibIn{icell};
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
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
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
274        end
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
279        testuint8=isa(A{icell},'uint8');
280        testuint16=isa(A{icell},'uint16');
281        if numel(siz)==2 %(B/W images)
282            vec_A=reshape(A{icell},1,npx(icell)*npy(icell));%put the original image in line
283            %ind_in=find(flagin);
284            ind_out=find(~flagin);
285            ICOMB=((XIMA-1)*npy(icell)+(npy(icell)+1-YIMA));
286            ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A
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));
291            A_out{icell}=reshape(vec_B,npY,npX);%new image in real coordinates
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
295               % ind_in=find(flagin);
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
299                vec_B(flagin)=vec_A(ICOMB);
300                vec_B(~flagin)=zeros(size(ind_out));
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});
309        end     
310    end
311end
312
Note: See TracBrowser for help on using the repository browser.