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

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

function update_obj suppressed, not used anymore

File size: 12.2 KB
RevLine 
[40]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:
[396]6% DataOut:   structure representing the first field in phys coordinates
7% DataOut_1: structure representing the second  field in phys coordinates
[40]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)
[396]16% Data_1, CalibData_1: same as Data, CalibData for the second field.
[40]17
18function [DataOut,DataOut_1]=phys(varargin)
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)
[396]27%% analyse input and set default output
28DataOut=varargin{1};%default first output field
29DataOut_1=[]; %default second  output field
30if nargin>=2 % nargin =nbre of input variables
31    if isfield(varargin{2},'GeometryCalib')
32        Calib{1}=varargin{2}.GeometryCalib;
33    else
34        Calib{1}=[];
[40]35    end
[396]36    if nargin>=3  %two input fields
37        DataOut_1=varargin{3};%default second output field
38        if nargin>=4 && isfield(varargin{4},'GeometryCalib')
[433]39            Calib{2}=varargin{4}.GeometryCalib;
[396]40        else
41            Calib{2}=Calib{1};
42        end
43    end
44end
45
46%% get the z index defining the section plane
47if isfield(varargin{1},'ZIndex')&&~isempty(varargin{1}.ZIndex)&&~isnan(varargin{1}.ZIndex)
48    ZIndex=varargin{1}.ZIndex;
[40]49else
[396]50    ZIndex=1;
[40]51end
[396]52
53%% transform first field
54iscalar=0;% counter of scalar fields
55if  ~isempty(Calib{1})
56    if ~isfield(Calib{1},'CalibrationType')||~isfield(Calib{1},'CoordUnit')
57        return %bad calib parameter input
[40]58    end
[396]59    if ~(isfield(varargin{1},'CoordUnit')&& strcmp(varargin{1}.CoordUnit,'pixel'))
60        return % transform only fields in pixel coordinates
61    end
62    DataOut=phys_1(varargin{1},Calib{1},ZIndex);% transform coordiantes and velocity components
63    %case of images or scalar: in case of two input fields, we need to project the transform  on the same regular grid
64    if isfield(varargin{1},'A') && isfield(varargin{1},'AX') && ~isempty(varargin{1}.AX) && isfield(varargin{1},'AY')&&...
65                                           ~isempty(varargin{1}.AY) && length(varargin{1}.A)>1
[40]66        iscalar=1;
[396]67        A{1}=varargin{1}.A;
[40]68    end
69end
[396]70
71%% document the selected  plane position and angle if relevant
72if isfield(Calib{1},'SliceCoord')&&size(Calib{1}.SliceCoord,1)>=ZIndex
73    DataOut.PlaneCoord=Calib{1}.SliceCoord(ZIndex,:);% transfer the slice position corresponding to index ZIndex
74    if isfield(Calib{1},'SliceAngle') % transfer the slice rotation angles
75        if isequal(size(Calib{1}.SliceAngle,1),1)% case of a unique angle
76            DataOut.PlaneAngle=Calib{1}.SliceAngle;
77        else  % case of multiple planes with different angles: select the plane with index ZIndex
78            DataOut.PlaneAngle=Calib{1}.SliceAngle(ZIndex,:);
79        end
80    end
[40]81end
[396]82
83%% transform second field if relevant
84if ~isempty(DataOut_1)
85    if isfield(varargin{3},'ZIndex') && ~isequal(varargin{3}.ZIndex,ZIndex)
86        DataOut_1.Txt='different plane indices for the two input fields';
87        return
[40]88    end
[396]89    if ~isfield(Calib{2},'CalibrationType')||~isfield(Calib{2},'CoordUnit')
90        return %bad calib parameter input
91    end
92    if ~(isfield(varargin{3},'CoordUnit')&& strcmp(varargin{3}.CoordUnit,'pixel'))
93        return % transform only fields in pixel coordinates
94    end
[433]95    DataOut_1=phys_1(DataOut_1,Calib{2},ZIndex);
[396]96    if isfield(Calib{1},'SliceCoord')
97        if ~(isfield(Calib{2},'SliceCoord') && isequal(Calib{2}.SliceCoord,Calib{1}.SliceCoord))
98            DataOut_1.Txt='different plane positions for the two input fields';
99            return
100        end       
101        DataOut_1.PlaneCoord=DataOut.PlaneCoord;% same plane position for the two input fields
102        if isfield(Calib{1},'SliceAngle')
103            if ~(isfield(Calib{2},'SliceAngle') && isequal(Calib{2}.SliceAngle,Calib{1}.SliceAngle))
104                DataOut_1.Txt='different plane angles for the two input fields';
105                return
106            end
107            DataOut_1.PlaneAngle=DataOut.PlaneAngle; % same plane angle for the two input fields
108        end
109    end
110    if isfield(varargin{3},'A')&&isfield(varargin{3},'AX')&&~isempty(varargin{3}.AX) && isfield(varargin{3},'AY')&&...
111            ~isempty(varargin{3}.AY)&&length(varargin{3}.A)>1
112        iscalar=iscalar+1;
113        Calib{iscalar}=Calib{2};
114        A{iscalar}=varargin{3}.A;
115    end
[40]116end
[396]117
118%% transform the scalar(s) or image(s)
[40]119if iscalar~=0
120    [A,AX,AY]=phys_Ima(A,Calib,ZIndex);%TODO : introduire interp2_uvmat ds phys_ima
[396]121    if iscalar==1 && ~isempty(DataOut_1) % case for which only the second field is a scalar
122         DataOut_1.A=A{1};
123         DataOut_1.AX=AX;
124         DataOut_1.AY=AY;
125    else
126        DataOut.A=A{1};
127        DataOut.AX=AX;
128        DataOut.AY=AY;
129    end
[40]130    if iscalar==2
131        DataOut_1.A=A{2};
132        DataOut_1.AX=AX;
133        DataOut_1.AY=AY;
134    end
135end
136
137%------------------------------------------------
[396]138%--- transform a single field
139function DataOut=phys_1(Data,Calib,ZIndex)
140%------------------------------------------------
141%% set default output
142DataOut=Data;%default
143DataOut.CoordUnit=Calib.CoordUnit;% the output coord unit is set by the calibration parameters
[40]144
[396]145%% transform  X,Y coordinates for velocity fields (transform of an image or scalar done in phys_ima)
146if isfield(Data,'X') &&isfield(Data,'Y')&&~isempty(Data.X) && ~isempty(Data.Y)
147  [DataOut.X,DataOut.Y]=phys_XYZ(Calib,Data.X,Data.Y,ZIndex);
148    Dt=1; %default
149    if isfield(Data,'dt')&&~isempty(Data.dt)
150        Dt=Data.dt;
[209]151    end
[396]152    if isfield(Data,'Dt')&&~isempty(Data.Dt)
153        Dt=Data.Dt;
[40]154    end
[396]155    if isfield(Data,'U')&&isfield(Data,'V')&&~isempty(Data.U) && ~isempty(Data.V)
156        [XOut_1,YOut_1]=phys_XYZ(Calib,Data.X-Data.U/2,Data.Y-Data.V/2,ZIndex);
157        [XOut_2,YOut_2]=phys_XYZ(Calib,Data.X+Data.U/2,Data.Y+Data.V/2,ZIndex);
158        DataOut.U=(XOut_2-XOut_1)/Dt;
159        DataOut.V=(YOut_2-YOut_1)/Dt;
[40]160    end
[396]161    if ~strcmp(Calib.CalibrationType,'rescale') && isfield(Data,'X_tps') && isfield(Data,'Y_tps')
162        [DataOut.X_tps,DataOut.Y_tps]=phys_XYZ(Calib,Data.X,Data.Y,ZIndex);
[40]163    end
[396]164end
165
166%% transform of spatial derivatives: TODO check the case with plane angles
167if isfield(Data,'X') && ~isempty(Data.X) && isfield(Data,'DjUi') && ~isempty(Data.DjUi)...
168      && isfield(Data,'dt')   
169    if ~isempty(Data.dt)
170        % estimate the Jacobian matrix DXpx/DXphys
171        for ip=1:length(Data.X)
172            [Xp1,Yp1]=phys_XYZ(Calib,Data.X(ip)+0.5,Data.Y(ip),ZIndex);
173            [Xm1,Ym1]=phys_XYZ(Calib,Data.X(ip)-0.5,Data.Y(ip),ZIndex);
174            [Xp2,Yp2]=phys_XYZ(Calib,Data.X(ip),Data.Y(ip)+0.5,ZIndex);
175            [Xm2,Ym2]=phys_XYZ(Calib,Data.X(ip),Data.Y(ip)-0.5,ZIndex);
176        %Jacobian matrix DXpphys/DXpx
177           DjXi(1,1)=(Xp1-Xm1);
178           DjXi(2,1)=(Yp1-Ym1);
179           DjXi(1,2)=(Xp2-Xm2);
180           DjXi(2,2)=(Yp2-Ym2);
181           DjUi(:,:)=Data.DjUi(ip,:,:);
182           DjUi=(DjXi*DjUi')/DjXi;% =J-1*M*J , curvature effects (derivatives of J) neglected
183           DataOut.DjUi(ip,:,:)=DjUi';
[40]184        end
[396]185        DataOut.DjUi =  DataOut.DjUi/Dt;   %     min(Data.DjUi(:,1,1))=DUDX                         
[40]186    end
187end
188
[396]189
[40]190%%%%%%%%%%%%%%%%%%%%
191function [A_out,Rangx,Rangy]=phys_Ima(A,CalibIn,ZIndex)
192xcorner=[];
193ycorner=[];
194npx=[];
195npy=[];
[116]196dx=ones(1,length(A));
197dy=ones(1,length(A));
[40]198for icell=1:length(A)
199    siz=size(A{icell});
200    npx=[npx siz(2)];
201    npy=[npy siz(1)];
202    Calib=CalibIn{icell};
[116]203    xima=[0.5 siz(2)-0.5 0.5 siz(2)-0.5];%image coordinates of corners
[40]204    yima=[0.5 0.5 siz(1)-0.5 siz(1)-0.5];
205    [xcorner_new,ycorner_new]=phys_XYZ(Calib,xima,yima,ZIndex);%corresponding physical coordinates
[79]206    dx(icell)=(max(xcorner_new)-min(xcorner_new))/(siz(2)-1);
207    dy(icell)=(max(ycorner_new)-min(ycorner_new))/(siz(1)-1);
[40]208    xcorner=[xcorner xcorner_new];
209    ycorner=[ycorner ycorner_new];
210end
211Rangx(1)=min(xcorner);
212Rangx(2)=max(xcorner);
213Rangy(2)=min(ycorner);
214Rangy(1)=max(ycorner);
[116]215test_multi=(max(npx)~=min(npx)) || (max(npy)~=min(npy)); %different image lengths
[79]216npX=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)
217npY=1+round((Rangy(1)-Rangy(2))/min(dy));
218x=linspace(Rangx(1),Rangx(2),npX);
219y=linspace(Rangy(1),Rangy(2),npY);
[40]220[X,Y]=meshgrid(x,y);%grid in physical coordiantes
221vec_B=[];
222A_out={};
223for icell=1:length(A)
224    Calib=CalibIn{icell};
[396]225    % rescaling of the image coordinates without change of the image array
226    if strcmp(Calib.CalibrationType,'rescale') && isequal(Calib,CalibIn{1})
227        A_out{icell}=A{icell};%no transform
228        Rangx=[0.5 npx-0.5];%image coordiantes of corners
229        Rangy=[npy-0.5 0.5];
230        [Rangx]=phys_XYZ(Calib,Rangx,[0.5 0.5],ZIndex);%case of translations without rotation and quadratic deformation
231        [xx,Rangy]=phys_XYZ(Calib,[0.5 0.5],Rangy,ZIndex);
232    else         
233        % the image needs to be interpolated to the new coordinates
[40]234        zphys=0; %default
235        if isfield(Calib,'SliceCoord') %.Z= index of plane
236           SliceCoord=Calib.SliceCoord(ZIndex,:);
237           zphys=SliceCoord(3); %to generalize for non-parallel planes
[202]238           if isfield(Calib,'InterfaceCoord') && isfield(Calib,'RefractionIndex')
239                H=Calib.InterfaceCoord(3);
240                if H>zphys
241                    zphys=H-(H-zphys)/Calib.RefractionIndex; %corrected z (virtual object)
242                end
243           end
[40]244        end
[79]245        [XIMA,YIMA]=px_XYZ(CalibIn{icell},X,Y,zphys);% image coordinates for each point in the real space grid
246        XIMA=reshape(round(XIMA),1,npX*npY);%indices reorganized in 'line'
247        YIMA=reshape(round(YIMA),1,npX*npY);
248        flagin=XIMA>=1 & XIMA<=npx(icell) & YIMA >=1 & YIMA<=npy(icell);%flagin=1 inside the original image
[40]249        testuint8=isa(A{icell},'uint8');
250        testuint16=isa(A{icell},'uint16');
251        if numel(siz)==2 %(B/W images)
[79]252            vec_A=reshape(A{icell},1,npx(icell)*npy(icell));%put the original image in line
[209]253            %ind_in=find(flagin);
[40]254            ind_out=find(~flagin);
[79]255            ICOMB=((XIMA-1)*npy(icell)+(npy(icell)+1-YIMA));
[40]256            ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A
[209]257            %vec_B(ind_in)=vec_A(ICOMB);
258            vec_B(flagin)=vec_A(ICOMB);
259            vec_B(~flagin)=zeros(size(ind_out));
260%             vec_B(ind_out)=zeros(size(ind_out));
[79]261            A_out{icell}=reshape(vec_B,npY,npX);%new image in real coordinates
[40]262        elseif numel(siz)==3     
263            for icolor=1:siz(3)
264                vec_A=reshape(A{icell}(:,:,icolor),1,npx*npy);%put the original image in line
[209]265               % ind_in=find(flagin);
[40]266                ind_out=find(~flagin);
267                ICOMB=((XIMA-1)*npy+(npy+1-YIMA));
268                ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A
[209]269                vec_B(flagin)=vec_A(ICOMB);
270                vec_B(~flagin)=zeros(size(ind_out));
[40]271                A_out{icell}(:,:,icolor)=reshape(vec_B,npy,npx);%new image in real coordinates
272            end
273        end
274        if testuint8
275            A_out{icell}=uint8(A_out{icell});
276        end
277        if testuint16
278            A_out{icell}=uint16(A_out{icell});
[396]279        end     
[40]280    end
281end
282
Note: See TracBrowser for help on using the repository browser.