source: trunk/src/transform_field/phys_polar.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: 14.2 KB
RevLine 
[40]1%transform image coordinates (px) to physical coordinates
2% then transform to polar coordinates:
3%[DataOut,DataOut_1]=phys_polar(varargin)
4%
5% OUTPUT:
6% DataOut: structure of modified data field: .X=radius, .Y=azimuth angle, .U, .V are radial and azimuthal velocity components
7% DataOut_1:  second data field (if two fields are in input)
8%
9%INPUT:
10% Data:  structure of input data (like UvData)
11% CalibData= structure containing the field .GeometryCalib with calibration parameters
12% Data_1:  second input field (not mandatory)
13% CalibData_1= calibration parameters for the second field
14
15function [DataOut,DataOut_1]=phys_polar(varargin)
16Calib{1}=[];
17if nargin==2||nargin==4
18    Data=varargin{1};
19    DataOut=Data;%default
20    DataOut_1=[];%default
21    CalibData=varargin{2};
22    if isfield(CalibData,'GeometryCalib')
23        Calib{1}=CalibData.GeometryCalib;
24    end
25    Calib{2}=Calib{1};
26else
27    DataOut.Txt='wrong input: need two or four structures';
28end
29test_1=0;
[93]30if nargin==4% case of two input fields
[40]31    test_1=1;
32    Data_1=varargin{3};
33    DataOut_1=Data_1;%default
34    CalibData_1=varargin{4};
35    if isfield(CalibData_1,'GeometryCalib')
36        Calib{2}=CalibData_1.GeometryCalib;
37    end
38end
39
40%parameters for polar coordinates (taken from the calibration data of the first field)
41%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42origin_xy=[0 0];%center for the polar coordinates in the original x,y coordinates
43if isfield(Calib{1},'PolarCentre') && isnumeric(Calib{1}.PolarCentre)
44    if isequal(length(Calib{1}.PolarCentre),2);
45        origin_xy= Calib{1}.PolarCentre;
46    end
47end
48radius_offset=0;%reference radius used to offset the radial coordinate r
49angle_offset=0; %reference angle used as new origin of the polar angle (= axis Ox by default)
50if isfield(Calib{1},'PolarReferenceRadius') && isnumeric(Calib{1}.PolarReferenceRadius)
51    radius_offset=Calib{1}.PolarReferenceRadius;
52end
53if radius_offset > 0
54    angle_scale=radius_offset; %the azimuth is rescale in terms of the length along the reference radius
55else
56    angle_scale=180/pi; %polar angle in degrees
57end
58if isfield(Calib{1},'PolarReferenceAngle') && isnumeric(Calib{1}.PolarReferenceAngle)
59    angle_offset=Calib{1}.PolarReferenceAngle; %offset angle (in unit of the final angle, degrees or arc length along the reference radius))
60end
61% new x coordinate = radius-radius_offset;
62% new y coordinate = theta*angle_scale-angle_offset
63
64%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65
66iscalar=0;
[93]67%transform first field to cartesian phys coordiantes
[40]68if  ~isempty(Calib{1})
69    DataOut=phys_1(Data,Calib{1},origin_xy,radius_offset,angle_offset,angle_scale);
70    %case of images or scalar
71    if isfield(Data,'A')&isfield(Data,'AX')&~isempty(Data.AX) & isfield(Data,'AY')&...
72                                           ~isempty(Data.AY)&length(Data.A)>1
73        iscalar=1;
74        A{1}=Data.A;
75    end
76    %transform of X,Y coordinates for vector fields
77    if isfield(Data,'ZIndex')&~isempty(Data.ZIndex)
78        ZIndex=Data.ZIndex;
79    else
80        ZIndex=0;
81    end
82end
[93]83%transform second field (if exists) to cartesian phys coordiantes
[40]84if test_1
85    DataOut_1=phys_1(Data_1,Calib{2},origin_xy,radius_offset,angle_offset,angle_scale);
86    if isfield(Data_1,'A')&isfield(Data_1,'AX')&~isempty(Data_1.AX) & isfield(Data_1,'AY')&...
87                                       ~isempty(Data_1.AY)&length(Data_1.A)>1
88          iscalar=iscalar+1;
89          Calib{iscalar}=Calib{2};
90          A{iscalar}=Data_1.A;
91          if isfield(Data_1,'ZIndex')&~isequal(Data_1.ZIndex,ZIndex)
92              DataOut.Txt='inconsistent plane indexes in the two input fields';
93          end
94          if iscalar==1% case for which only the second field is a scalar
95               [A,AX,AY]=phys_Ima(A,Calib,ZIndex,origin_xy,radius_offset,angle_offset,angle_scale);
96               DataOut_1.A=A{1};
97               DataOut_1.AX=AX;
98               DataOut_1.AY=AY;
99               return
100          end
101    end
102end
103if iscalar~=0
104    [A,AX,AY]=phys_Ima(A,Calib,ZIndex,origin_xy,radius_offset,angle_offset,angle_scale);%
105    DataOut.A=A{1};
106    DataOut.AX=AX;
107    DataOut.AY=AY;
108    if iscalar==2
109        DataOut_1.A=A{2};
110        DataOut_1.AX=AX;
111        DataOut_1.AY=AY;
112    end
113end
114
115%------------------------------------------------
116function DataOut=phys_1(Data,Calib,origin_xy,radius_offset,angle_offset,angle_scale)
117
118DataOut=Data;
119DataOut.CoordType='phys'; %put flag for physical coordinates
120if isfield(Calib,'CoordUnit')
121    DataOut.CoordUnit=Calib.CoordUnit;
122else
123    DataOut.CoordUnit='cm'; %default
124end
125DataOut.TimeUnit='s';
126%perform a geometry transform if Calib contains a field .GeometryCalib
127if isfield(Data,'CoordType') && isequal(Data.CoordType,'px') && ~isempty(Calib)
128    if isfield(Data,'CoordUnit')
129         DataOut=rmfield(DataOut,'CoordUnit');
130    end
131    %transform of X,Y coordinates for vector fields
132    if isfield(Data,'ZIndex')&~isempty(Data.ZIndex)
133        Z=Data.ZIndex;
134    else
135        Z=0;
136    end
137    if isfield(Data,'X') &isfield(Data,'Y')&~isempty(Data.X) & ~isempty(Data.Y)
138        [DataOut.X,DataOut.Y,DataOut.Z]=phys_XYZ(Calib,Data.X,Data.Y,Z); %transform from pixels to physical
139        DataOut.X=DataOut.X-origin_xy(1);%origin of coordinates at the tank center
140        DataOut.Y=DataOut.Y-origin_xy(2);%origin of coordinates at the tank center
141        [theta,DataOut.X] = cart2pol(DataOut.X,DataOut.Y);%theta  and X are the polar coordinates angle and radius
142          %shift and renormalize the polar coordinates
143        DataOut.X=DataOut.X-radius_offset;%
144        DataOut.Y=theta*angle_scale-angle_offset;% normalized angle: distance along reference radius
145        %transform velocity field if exists
146        if isfield(Data,'U')&isfield(Data,'V')&~isempty(Data.U) & ~isempty(Data.V)& isfield(Data,'dt')
147            if ~isempty(Data.dt)
148            [XOut_1,YOut_1]=phys_XYZ(Calib,Data.X-Data.U/2,Data.Y-Data.V/2,Z);
149            [XOut_2,YOut_2]=phys_XYZ(Calib,Data.X+Data.U/2,Data.Y+Data.V/2,Z);
150            UX=(XOut_2-XOut_1)/Data.dt;
151            VY=(YOut_2-YOut_1)/Data.dt;     
152            %transform u,v into polar coordiantes
153            DataOut.U=UX.*cos(theta)+VY.*sin(theta);%radial velocity
154            DataOut.V=(-UX.*sin(theta)+VY.*cos(theta));%./(DataOut.X)%+radius_ref);%angular velocity calculated
155            %shift and renormalize the angular velocity
156            end
157        end
[93]158        %transform of spatial derivatives
159        if isfield(Data,'X') && ~isempty(Data.X) && isfield(Data,'DjUi') && ~isempty(Data.DjUi)...
160                && isfield(Data,'dt')
161            if ~isempty(Data.dt)
162                % estimate the Jacobian matrix DXpx/DXphys
163                for ip=1:length(Data.X)
164                    [Xp1,Yp1]=phys_XYZ(Calib,Data.X(ip)+0.5,Data.Y(ip),Z);
165                    [Xm1,Ym1]=phys_XYZ(Calib,Data.X(ip)-0.5,Data.Y(ip),Z);
166                    [Xp2,Yp2]=phys_XYZ(Calib,Data.X(ip),Data.Y(ip)+0.5,Z);
167                    [Xm2,Ym2]=phys_XYZ(Calib,Data.X(ip),Data.Y(ip)-0.5,Z);
168                    %Jacobian matrix DXpphys/DXpx
169                    DjXi(1,1)=(Xp1-Xm1);
170                    DjXi(2,1)=(Yp1-Ym1);
171                    DjXi(1,2)=(Xp2-Xm2);
172                    DjXi(2,2)=(Yp2-Ym2);
173                    DjUi(:,:)=Data.DjUi(ip,:,:);
174                    DjUi=(DjXi*DjUi')/DjXi;% =J-1*M*J , curvature effects (derivatives of J) neglected
175                    DataOut.DjUi(ip,:,:)=DjUi';
176                end
177                DataOut.DjUi =  DataOut.DjUi/Data.dt;   %     min(Data.DjUi(:,1,1))=DUDX
178            end
179        end
[40]180    end
181end
182
[157]183%------------------------------------------------------------------------
184%'phys_XYZ':transforms image (px) to real world (phys) coordinates using geometric calibration parameters
185% function [Xphys,Yphys]=phys_XYZ(Calib,X,Y,Z)
186%
187%OUTPUT:
188%
189%INPUT:
190%Z: index of plane
191function [Xphys,Yphys,Zphys]=phys_XYZ(Calib,X,Y,Z)
192%------------------------------------------------------------------------
193if exist('Z','var')&& isequal(Z,round(Z))&& Z>0 && isfield(Calib,'SliceCoord')&&length(Calib.SliceCoord)>=Z
194    Zindex=Z;
195    Zphys=Calib.SliceCoord(Zindex,3);%GENERALISER AUX CAS AVEC ANGLE
196else
197    Zphys=0;
198end
199if ~exist('X','var')||~exist('Y','var')
200    Xphys=[];
201    Yphys=[];%default
202    return
203end
204%coordinate transform
205if ~isfield(Calib,'fx_fy')
206     Calib.fx_fy=[1 1];
207end
208if ~isfield(Calib,'Tx_Ty_Tz')
209     Calib.Tx_Ty_Tz=[0 0 1];
210end
211if ~isfield(Calib,'Cx_Cy')
212     Calib.Cx_Cy=[0 0];
213end
214if ~isfield(Calib,'kc')
215     Calib.kc=0;
216end
217if isfield(Calib,'R')
218    R=(Calib.R)';
219    Tx=Calib.Tx_Ty_Tz(1);
220    Ty=Calib.Tx_Ty_Tz(2);
221    Tz=Calib.Tx_Ty_Tz(3);
222    f=Calib.fx_fy(1);%dpy=1; sx=1
223    dpx=Calib.fx_fy(2)/Calib.fx_fy(1);
224    Dx=R(5)*R(7)-R(4)*R(8);
225    Dy=R(1)*R(8)-R(2)*R(7);
226    D0=f*(R(2)*R(4)-R(1)*R(5));
227    Z11=R(6)*R(8)-R(5)*R(9);
228    Z12=R(2)*R(9)-R(3)*R(8); 
229    Z21=R(4)*R(9)-R(6)*R(7);
230    Z22=R(3)*R(7)-R(1)*R(9);
231    Zx0=R(3)*R(5)-R(2)*R(6);
232    Zy0=R(1)*R(6)-R(3)*R(4);
233    A11=R(8)*Ty-R(5)*Tz+Z11*Zphys;
234    A12=R(2)*Tz-R(8)*Tx+Z12*Zphys;
235    A21=-R(7)*Ty+R(4)*Tz+Z21*Zphys;
236    A22=-R(1)*Tz+R(7)*Tx+Z22*Zphys;
237    X0=f*(R(5)*Tx-R(2)*Ty+Zx0*Zphys);
238    Y0=f*(-R(4)*Tx+R(1)*Ty+Zy0*Zphys);
239        %px to camera:
240    Xd=dpx*(X-Calib.Cx_Cy(1)); % sensor coordinates
241    Yd=(Y-Calib.Cx_Cy(2));
242    dist_fact=1+Calib.kc*(Xd.*Xd+Yd.*Yd)/(f*f); %distortion factor
243    Xu=Xd./dist_fact;%undistorted sensor coordinates
244    Yu=Yd./dist_fact;
245    denom=Dx*Xu+Dy*Yu+D0;
246    % denom2=denom.*denom;
247    Xphys=(A11.*Xu+A12.*Yu+X0)./denom;%world coordinates
248    Yphys=(A21.*Xu+A22.*Yu+Y0)./denom;
249%     Xd=(X-Calib.Cx_Cy(1))/Calib.fx_fy(1); % sensor coordinates
250%     Yd=(Y-Calib.Cx_Cy(2))/Calib.fx_fy(2);
251%     dist_fact=1+Calib.kc*(Xd.*Xd+Yd.*Yd); %distortion factor
252%     Xu=Xd./dist_fact;%undistorted sensor coordinates
253%     Yu=Yd./dist_fact;
254%     A11=R(7)*Xu-R(1);
255%     A12=R(8)*Xu-R(2);
256%     A21=R(7)*Yu-R(4);
257%     A22=R(8)*Yu-R(5);
258%     B1=(R(3)-R(9)*Xu)*Zphys-Tz*Xu+Tx;
259%     B2=(R(6)-R(9)*Yu)*Zphys-Tz*Yu+Ty;
260%     deter=A12.*A21-A11.*A22;
261%     Xphys=(A21.*B1-A11.*B2)./deter;
262%     Yphys=(-A22.*B1+A12.*B2)./deter;
263else
264    Xphys=-Calib.Tx_Ty_Tz(1)+X/Calib.fx_fy(1);
265    Yphys=-Calib.Tx_Ty_Tz(2)+Y/Calib.fx_fy(2);
266end
[40]267%%%%%%%%%%%%%%%%%%%%
268function [A_out,Rangx,Rangy]=phys_Ima(A,CalibIn,ZIndex,origin_xy,radius_offset,angle_offset,angle_scale)
269xcorner=[];
270ycorner=[];
271npx=[];
272npy=[];
273
274for icell=1:length(A)
275    siz=size(A{icell});
276    npx=[npx siz(2)];
277    npy=[npy siz(1)];
278    zphys=0; %default
279    if isfield(CalibIn{icell},'SliceCoord') %.Z= index of plane
280       SliceCoord=CalibIn{icell}.SliceCoord(ZIndex,:);
281       zphys=SliceCoord(3); %to generalize for non-parallel planes
282    end
283    xima=[0.5 siz(2)-0.5 0.5 siz(2)-0.5];%image coordiantes of corners
284    yima=[0.5 0.5 siz(1)-0.5 siz(1)-0.5];
285    [xcorner_new,ycorner_new]=phys_XYZ(CalibIn{icell},xima,yima,ZIndex);%corresponding physical coordinates
286    %transform the corner coordinates into polar ones   
287    xcorner_new=xcorner_new-origin_xy(1);%shift to the origin of the polar coordinates
288    ycorner_new=ycorner_new-origin_xy(2);%shift to the origin of the polar coordinates       
289    [theta,xcorner_new] = cart2pol(xcorner_new,ycorner_new);%theta  and X are the polar coordinates angle and radius
290    if (max(theta)-min(theta))>pi   %if the polar origin is inside the image
291        xcorner_new=[0 max(xcorner_new)];
292        theta=[-pi pi];
293    end
294          %shift and renormalize the polar coordinates
295    xcorner_new=xcorner_new-radius_offset;%
296    ycorner_new=theta*angle_scale-angle_offset;% normalized angle: distance along reference radius
297    xcorner=[xcorner xcorner_new];
298    ycorner=[ycorner ycorner_new];
299end
300Rangx(1)=min(xcorner);
301Rangx(2)=max(xcorner);
302Rangy(2)=min(ycorner);
303Rangy(1)=max(ycorner);
304% test_multi=(max(npx)~=min(npx)) | (max(npy)~=min(npy));
305npx=max(npx);
306npy=max(npy);
307x=linspace(Rangx(1),Rangx(2),npx);
308y=linspace(Rangy(1),Rangy(2),npy);
309[X,Y]=meshgrid(x,y);%grid in physical coordinates
310%transform X, Y in cartesian
311X=X+radius_offset;%
312Y=(Y+angle_offset)/angle_scale;% normalized angle: distance along reference radius
313[X,Y] = pol2cart(Y,X);
314X=X+origin_xy(1);%shift to the origin of the polar coordinates
315Y=Y+origin_xy(2);%shift to the origin of the polar coordinates
316for icell=1:length(A)
317    [XIMA,YIMA]=px_XYZ(CalibIn{icell},X,Y,zphys);%corresponding image indices for each point in the real space grid
318    XIMA=reshape(round(XIMA),1,npx*npy);%indices reorganized in 'line'
319    YIMA=reshape(round(YIMA),1,npx*npy);
320    flagin=XIMA>=1 & XIMA<=npx & YIMA >=1 & YIMA<=npy;%flagin=1 inside the original image
321    vec_A=reshape(A{icell}(:,:,1),1,npx*npy);%put the original image in line
322    ind_in=find(flagin);
323    ind_out=find(~flagin);
324    ICOMB=((XIMA-1)*npy+(npy+1-YIMA));
325    ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A
326    vec_B(ind_in)=vec_A(ICOMB);
327    vec_B(ind_out)=zeros(size(ind_out));
328    A_out{icell}=reshape(vec_B,npy,npx);%new image in real coordinates
329end
330%Rangx=Rangx-radius_offset;
331
332
333
[157]334%
335% function [X,Y]=px_XYZ(Calib,Xphys,Yphys,Zphys)
336% X=[];%default
337% Y=[];
338% % if exist('Z','var')& isequal(Z,round(Z))& Z>0 & isfield(Calib,'PlanePos')&length(Calib.PlanePos)>=Z
339% %     Zindex=Z;
340% %     planepos=Calib.PlanePos{Zindex};
341% %     zphys=planepos(3);%A GENERALISER CAS AVEC ANGLE
342% % else
343% %     zphys=0;
344% % end
345% if ~exist('Zphys','var')
346%     Zphys=0;
[40]347% end
[157]348%
349% %%%%%%%%%%%%%
350% if isfield(Calib,'R')
351%     R=(Calib.R)';
352%     xc=R(1)*Xphys+R(2)*Yphys+R(3)*Zphys+Calib.Tx;
353%     yc=R(4)*Xphys+R(5)*Yphys+R(6)*Zphys+Calib.Ty;
354%     zc=R(7)*Xphys+R(8)*Yphys+R(9)*Zphys+Calib.Tz;
355% %undistorted image coordinates
356%     Xu=Calib.f*xc./zc;
357%     Yu=Calib.f*yc./zc;
358% %distorted image coordinates
359%     distortion=(Calib.kappa1)*(Xu.*Xu+Yu.*Yu)+1; %A REVOIR
360% % distortion=1;
361%     Xd=Xu./distortion;
362%     Yd=Yu./distortion;
363% %pixel coordinates
364%     X=Xd*Calib.sx/Calib.dpx+Calib.Cx;
365%     Y=Yd/Calib.dpy+Calib.Cy;
366%
367% elseif isfield(Calib,'Pxcmx')&isfield(Calib,'Pxcmy')%old calib 
368%         X=Xphys*Calib.Pxcmx;
369%         Y=Yphys*Calib.Pxcmy;
370% end
371%
[40]372
Note: See TracBrowser for help on using the repository browser.