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

Last change on this file since 811 was 810, checked in by g7moreau, 10 years ago
  • Add license
File size: 14.7 KB
RevLine 
[695]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'
2%------------------------------------------------------------------------
3%%%%  Use the general syntax for transform fields %%%%
4% OUTPUT:
5% DataOut:   output field structure
[810]6%
[695]7%INPUT:
8% DataIn:  first input field structure
9% XmlData: first input parameter structure,
10%        .GeometryCalib: substructure of the calibration parameters
11% DataIn_1: optional second input field structure
12% XmlData_1: optional second input parameter structure
13%         .GeometryCalib: substructure of the calibration parameters
[810]14
15%=======================================================================
16% Copyright 2008-2014, LEGI UMR 5519 / CNRS UJF G-INP, Grenoble, France
17%   http://www.legi.grenoble-inp.fr
18%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
19%
20%     This file is part of the toolbox UVMAT.
21%
22%     UVMAT is free software; you can redistribute it and/or modify
23%     it under the terms of the GNU General Public License as published
24%     by the Free Software Foundation; either version 2 of the license,
25%     or (at your option) any later version.
26%
27%     UVMAT is distributed in the hope that it will be useful,
28%     but WITHOUT ANY WARRANTY; without even the implied warranty of
29%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30%     GNU General Public License (see LICENSE.txt) for more details.
31%=======================================================================
32
[695]33function DataOut=phys(DataIn,XmlData,DataIn_1,XmlData_1)
34%------------------------------------------------------------------------
35
36% A FAIRE: 1- verifier si DataIn est une 'field structure'(.ListVarName'):
37% chercher ListVarAttribute, for each field (cell of variables):
38%   .CoordType: 'phys' or 'px'   (default==phys, no transform)
39%   .scale_factor: =dt (to transform displacement into velocity) default=1
40%   .covariance: 'scalar', 'coord', 'D_i': covariant (like velocity), 'D^i': contravariant (like gradient), 'D^jD_i' (like strain tensor)
41%   (default='coord' if .Role='coord_x,_y...,
42%            'D_i' if '.Role='vector_x,...',
43%              'scalar', else (thenno change except scale factor)
44
45DataOut=[];
46DataOut_1=[]; %default second  output field
[753]47if isfield(DataIn,'Action') && isfield(DataIn.Action,'RUN') && isequal(DataIn.Action.RUN,0)
[695]48    if isfield(XmlData,'GeometryCalib')&& isfield(XmlData.GeometryCalib,'CoordUnit')
49        DataOut.CoordUnit=XmlData.GeometryCalib.CoordUnit;% states that the output is in unit defined by GeometryCalib, then erased all projection objects with different units
50    end
51    return
52end
53
54%% analyse input and set default output
55DataOut=DataIn;%default first output field
56if nargin>=2 % nargin =nbre of input variables
57    if isfield(XmlData,'GeometryCalib')
58        Calib{1}=XmlData.GeometryCalib;
59    else
60        Calib{1}=[];
61    end
62    if nargin>=3  %two input fields
63        DataOut_1=DataIn_1;%default second output field
64        if nargin>=4 && isfield(XmlData_1,'GeometryCalib')
65            Calib{2}=XmlData_1.GeometryCalib;
66        else
67            Calib{2}=Calib{1};
68        end
69    end
70end
71
72%% get the z index defining the section plane
73if isfield(DataIn,'ZIndex')&&~isempty(DataIn.ZIndex)&&~isnan(DataIn.ZIndex)
74    ZIndex=DataIn.ZIndex;
75else
76    ZIndex=1;
77end
78
79%% transform first field
80iscalar=0;% counter of scalar fields
81if  ~isempty(Calib{1})
82    if ~isfield(Calib{1},'CalibrationType')||~isfield(Calib{1},'CoordUnit')
83        return %bad calib parameter input
84    end
85    if ~(isfield(DataIn,'CoordUnit')&& strcmp(DataIn.CoordUnit,'pixel'))
86        return % transform only fields in pixel coordinates
87    end
88    DataOut=phys_1(DataIn,Calib{1},ZIndex);% transform coordiantes and velocity components
89    %case of images or scalar: in case of two input fields, we need to project the transform  on the same regular grid
[782]90    if isfield(DataIn,'A') && isfield(DataIn,'Coord_x') && ~isempty(DataIn.Coord_x) && isfield(DataIn,'Coord_y')&&...
91                                           ~isempty(DataIn.Coord_y) && length(DataIn.A)>1
[695]92        iscalar=1;
93        A{1}=DataIn.A;
94    end
95end
96
97%% document the selected  plane position and angle if relevant
98if isfield(Calib{1},'SliceCoord')&&size(Calib{1}.SliceCoord,1)>=ZIndex
99    DataOut.PlaneCoord=Calib{1}.SliceCoord(ZIndex,:);% transfer the slice position corresponding to index ZIndex
100    if isfield(Calib{1},'SliceAngle') % transfer the slice rotation angles
101        if isequal(size(Calib{1}.SliceAngle,1),1)% case of a unique angle
102            DataOut.PlaneAngle=Calib{1}.SliceAngle;
103        else  % case of multiple planes with different angles: select the plane with index ZIndex
104            DataOut.PlaneAngle=Calib{1}.SliceAngle(ZIndex,:);
105        end
106    end
107end
108
109%% transform second field if relevant
110if ~isempty(DataOut_1)
111    if isfield(DataIn_1,'ZIndex') && ~isequal(DataIn_1.ZIndex,ZIndex)
112        DataOut_1.Txt='different plane indices for the two input fields';
113        return
114    end
115    if ~isfield(Calib{2},'CalibrationType')||~isfield(Calib{2},'CoordUnit')
116        return %bad calib parameter input
117    end
118    if ~(isfield(DataIn_1,'CoordUnit')&& strcmp(DataIn_1.CoordUnit,'pixel'))
119        return % transform only fields in pixel coordinates
120    end
121    DataOut_1=phys_1(DataOut_1,Calib{2},ZIndex);
122    if isfield(Calib{1},'SliceCoord')
123        if ~(isfield(Calib{2},'SliceCoord') && isequal(Calib{2}.SliceCoord,Calib{1}.SliceCoord))
124            DataOut_1.Txt='different plane positions for the two input fields';
125            return
126        end       
127        DataOut_1.PlaneCoord=DataOut.PlaneCoord;% same plane position for the two input fields
128        if isfield(Calib{1},'SliceAngle')
129            if ~(isfield(Calib{2},'SliceAngle') && isequal(Calib{2}.SliceAngle,Calib{1}.SliceAngle))
130                DataOut_1.Txt='different plane angles for the two input fields';
131                return
132            end
133            DataOut_1.PlaneAngle=DataOut.PlaneAngle; % same plane angle for the two input fields
134        end
135    end
[782]136    if isfield(DataIn_1,'A')&&isfield(DataIn_1,'Coord_x')&&~isempty(DataIn_1.Coord_x) && isfield(DataIn_1,'Coord_y')&&...
137            ~isempty(DataIn_1.Coord_y)&&length(DataIn_1.A)>1
[695]138        iscalar=iscalar+1;
139        Calib{iscalar}=Calib{2};
140        A{iscalar}=DataIn_1.A;
141    end
142end
143
144%% transform the scalar(s) or image(s)
145if iscalar~=0
[782]146    [A,Coord_x,Coord_y]=phys_Ima(A,Calib,ZIndex);%TODO : introduire interp2_uvmat ds phys_ima
[695]147    if iscalar==1 && ~isempty(DataOut_1) % case for which only the second field is a scalar
148         DataOut_1.A=A{1};
[782]149         DataOut_1.Coord_x=Coord_x;
150         DataOut_1.Coord_y=Coord_y;
[695]151    else
152        DataOut.A=A{1};
[782]153        DataOut.Coord_x=Coord_x;
154        DataOut.Coord_y=Coord_y;
[695]155    end
156    if iscalar==2
157        DataOut_1.A=A{2};
[782]158        DataOut_1.Coord_x=Coord_x;
159        DataOut_1.Coord_y=Coord_y;
[695]160    end
161end
162
163% subtract fields
164if ~isempty(DataOut_1)
[782]165    DataOut=sub_field(DataOut,[],DataOut_1);
[695]166end
167%------------------------------------------------
168%--- transform a single field
169function DataOut=phys_1(Data,Calib,ZIndex)
170%------------------------------------------------
171%% set default output
172DataOut=Data;%default
173DataOut.CoordUnit=Calib.CoordUnit;% the output coord unit is set by the calibration parameters
174
175%% transform  X,Y coordinates for velocity fields (transform of an image or scalar done in phys_ima)
176if isfield(Data,'X') &&isfield(Data,'Y')&&~isempty(Data.X) && ~isempty(Data.Y)
177  [DataOut.X,DataOut.Y]=phys_XYZ(Calib,Data.X,Data.Y,ZIndex);
178    Dt=1; %default
179    if isfield(Data,'dt')&&~isempty(Data.dt)
180        Dt=Data.dt;
181    end
182    if isfield(Data,'Dt')&&~isempty(Data.Dt)
183        Dt=Data.Dt;
184    end
185    if isfield(Data,'U')&&isfield(Data,'V')&&~isempty(Data.U) && ~isempty(Data.V)
186        [XOut_1,YOut_1]=phys_XYZ(Calib,Data.X-Data.U/2,Data.Y-Data.V/2,ZIndex);
187        [XOut_2,YOut_2]=phys_XYZ(Calib,Data.X+Data.U/2,Data.Y+Data.V/2,ZIndex);
188        DataOut.U=(XOut_2-XOut_1)/Dt;
189        DataOut.V=(YOut_2-YOut_1)/Dt;
190    end
191%     if ~strcmp(Calib.CalibrationType,'rescale') && isfield(Data,'X_tps') && isfield(Data,'Y_tps')
192%         [DataOut.X_tps,DataOut.Y_tps]=phys_XYZ(Calib,Data.X,Data.Y,ZIndex);
193%     end
194end
195
196%% suppress tps
197list_tps={'Coord_tps'  'U_tps'  'V_tps'  'SubRange'  'NbSites'};
198ind_remove=[];
199for ilist=1:numel(list_tps)
200    ind_tps=find(strcmp(list_tps{ilist},Data.ListVarName));
201    if ~isempty(ind_tps)
202        ind_remove=[ind_remove ind_tps];
203        DataOut=rmfield(DataOut,list_tps{ilist});
204    end
205end
206if isfield(DataOut,'VarAttribute') && numel(DataOut.VarAttribute)>=3 && isfield(DataOut.VarAttribute{3},'VarIndex_tps')
207    DataOut.VarAttribute{3}=rmfield(DataOut.VarAttribute{3},'VarIndex_tps');
208end
209if isfield(DataOut,'VarAttribute')&& numel(DataOut.VarAttribute)>=4 && isfield(DataOut.VarAttribute{4},'VarIndex_tps')
210    DataOut.VarAttribute{4}=rmfield(DataOut.VarAttribute{4},'VarIndex_tps');
211end
212if ~isempty(ind_remove)
213    DataOut.ListVarName(ind_remove)=[];
214    DataOut.VarDimName(ind_remove)=[];
215    DataOut.VarAttribute(ind_remove)=[];
216end
217   
218%% transform of spatial derivatives: TODO check the case with plane angles
219if isfield(Data,'X') && ~isempty(Data.X) && isfield(Data,'DjUi') && ~isempty(Data.DjUi)
220    % estimate the Jacobian matrix DXpx/DXphys
221    for ip=1:length(Data.X)
222        [Xp1,Yp1]=phys_XYZ(Calib,Data.X(ip)+0.5,Data.Y(ip),ZIndex);
223        [Xm1,Ym1]=phys_XYZ(Calib,Data.X(ip)-0.5,Data.Y(ip),ZIndex);
224        [Xp2,Yp2]=phys_XYZ(Calib,Data.X(ip),Data.Y(ip)+0.5,ZIndex);
225        [Xm2,Ym2]=phys_XYZ(Calib,Data.X(ip),Data.Y(ip)-0.5,ZIndex);
226        %Jacobian matrix DXpphys/DXpx
227        DjXi(1,1)=(Xp1-Xm1);
228        DjXi(2,1)=(Yp1-Ym1);
229        DjXi(1,2)=(Xp2-Xm2);
230        DjXi(2,2)=(Yp2-Ym2);
231        DjUi(:,:)=Data.DjUi(ip,:,:);
232        DjUi=(DjXi*DjUi')/DjXi;% =J-1*M*J , curvature effects (derivatives of J) neglected
233        DataOut.DjUi(ip,:,:)=DjUi';
234    end
235    DataOut.DjUi =  DataOut.DjUi/Dt;   %     min(Data.DjUi(:,1,1))=DUDX
236end
237
238
239%%%%%%%%%%%%%%%%%%%%
240function [A_out,Rangx,Rangy]=phys_Ima(A,CalibIn,ZIndex)
241xcorner=[];
242ycorner=[];
243npx=[];
244npy=[];
245dx=ones(1,numel(A));
246dy=ones(1,numel(A));
247for icell=1:numel(A)
248    siz=size(A{icell});
249    npx=[npx siz(2)];
250    npy=[npy siz(1)];
251    Calib=CalibIn{icell};
252    xima=[0.5 siz(2)-0.5 0.5 siz(2)-0.5];%image coordinates of corners
253    yima=[0.5 0.5 siz(1)-0.5 siz(1)-0.5];
254    [xcorner_new,ycorner_new]=phys_XYZ(Calib,xima,yima,ZIndex);%corresponding physical coordinates
255    dx(icell)=(max(xcorner_new)-min(xcorner_new))/(siz(2)-1);
256    dy(icell)=(max(ycorner_new)-min(ycorner_new))/(siz(1)-1);
257    xcorner=[xcorner xcorner_new];
258    ycorner=[ycorner ycorner_new];
259end
260Rangx(1)=min(xcorner);
261Rangx(2)=max(xcorner);
262Rangy(2)=min(ycorner);
263Rangy(1)=max(ycorner);
264test_multi=(max(npx)~=min(npx)) || (max(npy)~=min(npy)); %different image lengths
265npX=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)
266npY=1+round((Rangy(1)-Rangy(2))/min(dy));
267x=linspace(Rangx(1),Rangx(2),npX);
268y=linspace(Rangy(1),Rangy(2),npY);
269[X,Y]=meshgrid(x,y);%grid in physical coordiantes
270%vec_B=[];
271A_out=cell(1,numel(A));
272for icell=1:length(A)
273    Calib=CalibIn{icell};
274    % rescaling of the image coordinates without change of the image array
275    if strcmp(Calib.CalibrationType,'rescale') && isequal(Calib,CalibIn{1})
276        A_out{icell}=A{icell};%no transform
277        Rangx=[0.5 npx-0.5];%image coordiantes of corners
278        Rangy=[npy-0.5 0.5];
279        [Rangx]=phys_XYZ(Calib,Rangx,[0.5 0.5],ZIndex);%case of translations without rotation and quadratic deformation
280        [xx,Rangy]=phys_XYZ(Calib,[0.5 0.5],Rangy,ZIndex);
281    else         
282        % the image needs to be interpolated to the new coordinates
283        zphys=0; %default
284        if isfield(Calib,'SliceCoord') %.Z= index of plane
285           SliceCoord=Calib.SliceCoord(ZIndex,:);
286           zphys=SliceCoord(3); %to generalize for non-parallel planes
287           if isfield(Calib,'InterfaceCoord') && isfield(Calib,'RefractionIndex')
288                H=Calib.InterfaceCoord(3);
289                if H>zphys
290                    zphys=H-(H-zphys)/Calib.RefractionIndex; %corrected z (virtual object)
291                end
292           end
293        end
[742]294        xima=0.5:npx-0.5;%image coordinates of corners
[695]295        yima=npy-0.5:-1:0.5;
[742]296        [XIMA_init,YIMA_init]=meshgrid(xima,yima);%grid of initial image in px coordinates
[695]297        [XIMA,YIMA]=px_XYZ(CalibIn{icell},X,Y,zphys);% image coordinates for each point in the real
298        %[XPHYS_init,YPHYS_init]=phys_XYZ(Calib,XIMA_init,YIMA_init,ZIndex);
[742]299        testuint8=isa(A{icell},'uint8');
300        testuint16=isa(A{icell},'uint16');
[695]301        if ndims(A{icell})==2 %(B/W images)
302        A_out{icell}=interp2(XIMA_init,YIMA_init,double(A{icell}),XIMA,YIMA);
303%         [Rangx]=phys_XYZ(Calib,Rangx,[0.5 0.5],ZIndex);%case of translations without rotation and quadratic deformation
304%         [XIMA_init,YIMA_init]=px_XYZ(CalibIn{icell},X,Y,zphys);% image coordinates for each point in the real space grid
305%         
306%         XIMA=reshape(round(XIMA),1,npX*npY);%indices reorganized in 'line'
307%         YIMA=reshape(round(YIMA),1,npX*npY);
308%         flagin=XIMA>=1 & XIMA<=npx(icell) & YIMA >=1 & YIMA<=npy(icell);%flagin=1 inside the original image
309
310%         if numel(siz)==2 %(B/W images)
311%             vec_A=reshape(A{icell},1,npx(icell)*npy(icell));%put the original image in line
312%             %ind_in=find(flagin);
313%             ind_out=find(~flagin);
314%             ICOMB=((XIMA-1)*npy(icell)+(npy(icell)+1-YIMA));
315%             ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A
316%             %vec_B(ind_in)=vec_A(ICOMB);
317%             vec_B(flagin)=vec_A(ICOMB);
318%             vec_B(~flagin)=zeros(size(ind_out));
319% %             vec_B(ind_out)=zeros(size(ind_out));
320%             A_out{icell}=reshape(vec_B,npY,npX);%new image in real coordinates
321         elseif ndims(A{icell})==3     
322             for icolor=1:size(A{icell},3)
323                 A{icell}=double(A{icell});
324                 A_out{icell}(:,:,icolor)=interp2(XIMA_init,YIMA_init,A{icell}(:,:,icolor),XIMA,YIMA);
325%                 vec_A=reshape(A{icell}(:,:,icolor),1,npx*npy);%put the original image in line
326%                % ind_in=find(flagin);
327%                 ind_out=find(~flagin);
328%                 ICOMB=((XIMA-1)*npy+(npy+1-YIMA));
329%                 ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A
330%                 vec_B(flagin)=vec_A(ICOMB);
331%                 vec_B(~flagin)=zeros(size(ind_out));
332%                 A_out{icell}(:,:,icolor)=reshape(vec_B,npy,npx);%new image in real coordinates
333             end
334         end
335        if testuint8
336            A_out{icell}=uint8(A_out{icell});
337        end
338        if testuint16
339            A_out{icell}=uint16(A_out{icell});
340        end     
341    end
342end
343
Note: See TracBrowser for help on using the repository browser.