source: trunk/src/transform_field/phys_polar.m @ 933

Last change on this file since 933 was 933, checked in by sommeria, 8 years ago

physpolar corrected

File size: 14.3 KB
Line 
1%'phys_polar': transforms image (Unit='pixel') to polar (phys) coordinates using geometric calibration parameters
2%------------------------------------------------------------------------
3%%%%  Use the general syntax for transform fields %%%%
4% OUTPUT:
5% DataOut:   output field structure
6%      .X=radius, .Y=azimuth angle, .U, .V are radial and azimuthal velocity components
7%
8%INPUT:
9% DataIn:  first input field structure
10% XmlData: first input parameter structure,
11%        .GeometryCalib: substructure of the calibration parameters
12% DataIn_1: optional second input field structure
13% XmlData_1: optional second input parameter structure
14%         .GeometryCalib: substructure of the calibration parameters
15% transform image coordinates (px) to polar physical coordinates
16%[DataOut,DataOut_1]=phys_polar(varargin)
17%
18% OUTPUT:
19% DataOut: structure of modified data field: .X=radius, .Y=azimuth angle, .U, .V are radial and azimuthal velocity components
20% DataOut_1:  second data field (if two fields are in input)
21%
22%INPUT:
23% Data:  structure of input data (like UvData)
24% XmlData= structure containing the field .GeometryCalib with calibration parameters
25% Data_1:  second input field (not mandatory)
26% XmlData_1= calibration parameters for the second field
27
28%=======================================================================
29% Copyright 2008-2016, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
30%   http://www.legi.grenoble-inp.fr
31%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
32%
33%     This file is part of the toolbox UVMAT.
34%
35%     UVMAT is free software; you can redistribute it and/or modify
36%     it under the terms of the GNU General Public License as published
37%     by the Free Software Foundation; either version 2 of the license,
38%     or (at your option) any later version.
39%
40%     UVMAT is distributed in the hope that it will be useful,
41%     but WITHOUT ANY WARRANTY; without even the implied warranty of
42%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
43%     GNU General Public License (see LICENSE.txt) for more details.
44%=======================================================================
45
46function DataOut=phys_polar(DataIn,XmlData,DataIn_1,XmlData_1)
47%------------------------------------------------------------------------
48%% request input parameters
49if isfield(DataIn,'Action') && isfield(DataIn.Action,'RUN') && isequal(DataIn.Action.RUN,0)
50    prompt = {'origin [x y] of polar coordinates';'reference radius';'reference angle(degrees)'};
51    dlg_title = 'set the parameters for the polar coordinates';
52    num_lines= 2;
53    def     = { '[0 0]';'0';'0'};
54    if isfield(XmlData,'TransformInput')
55        if isfield(XmlData.TransformInput,'PolarCentre')
56            def{1}=num2str(XmlData.TransformInput.PolarCentre);
57        end
58        if isfield(XmlData.TransformInput,'PolarReferenceRadius')
59            def{2}=num2str(XmlData.TransformInput.PolarReferenceRadius);
60        end
61        if isfield(XmlData.TransformInput,'PolarReferenceAngle')
62            def{3}=num2str(XmlData.TransformInput.PolarReferenceAngle);
63        end
64    end
65    answer = inputdlg(prompt,dlg_title,num_lines,def);
66    DataOut.TransformInput.PolarCentre=str2num(answer{1});
67    DataOut.TransformInput.PolarReferenceRadius=str2num(answer{2});
68    DataOut.TransformInput.PolarReferenceAngle=str2num(answer{3});
69    return
70end
71
72Calib{1}=[];
73if nargin==2||nargin==4
74    DataOut=DataIn;%default
75    DataOut_1=[];%default
76    if isfield(XmlData,'GeometryCalib')
77        Calib{1}=XmlData.GeometryCalib;
78    end
79    Calib{2}=Calib{1};
80else
81    DataOut.Txt='wrong input: need two or four structures';
82end
83test_1=0;
84if nargin==4% case of two input fields
85    test_1=1;
86    DataOut_1=DataIn_1;%default
87    if isfield(XmlData_1,'GeometryCalib')
88        Calib{2}=XmlData_1.GeometryCalib;
89    end
90end
91
92%parameters for polar coordinates (taken from the calibration data of the first field)
93%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
94XmlData.PolarReferenceRadius=450;
95XmlData.PolarReferenceAngle=450*pi/2;
96origin_xy=[0 0];%center for the polar coordinates in the original x,y coordinates
97radius_offset=0;%reference radius used to offset the radial coordinate r
98angle_offset=0; %reference angle used as new origin of the polar angle (= axis Ox by default)
99if isfield(XmlData,'TransformInput')
100    if isfield(XmlData.TransformInput,'PolarCentre') && isnumeric(XmlData.TransformInput.PolarCentre)
101        if isequal(length(XmlData.TransformInput.PolarCentre),2);
102            origin_xy= XmlData.TransformInput.PolarCentre;
103        end
104    end
105    if isfield(XmlData.TransformInput,'PolarReferenceRadius') && isnumeric(XmlData.TransformInput.PolarReferenceRadius)
106        radius_offset=XmlData.TransformInput.PolarReferenceRadius;
107    end
108    if radius_offset > 0
109        angle_scale=radius_offset; %the azimuth is rescale in terms of the length along the reference radius
110    else
111        angle_scale=180/pi; %polar angle in degrees
112    end
113    if isfield(XmlData.TransformInput,'PolarReferenceAngle') && isnumeric(XmlData.TransformInput.PolarReferenceAngle)
114        angle_offset=(pi/180)*XmlData.TransformInput.PolarReferenceAngle; %offset angle (in unit of the final angle, degrees or arc length along the reference radius))
115    end
116end
117
118%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119
120iscalar=0;
121%transform first field to cartesian phys coordiantes
122if  ~isempty(Calib{1})
123    DataOut=phys_1(DataIn,Calib{1},origin_xy,radius_offset,angle_offset,angle_scale);
124    %case of images or scalar
125    if isfield(DataIn,'A')&isfield(DataIn,'Coord_x')&~isempty(DataIn.Coord_x) & isfield(DataIn,'Coord_y')&...
126                                           ~isempty(DataIn.Coord_y)&length(DataIn.A)>1
127        iscalar=1;
128        A{1}=DataIn.A;
129    end
130    %transform of X,Y coordinates for vector fields
131    if isfield(DataIn,'ZIndex')&~isempty(DataIn.ZIndex)
132        ZIndex=DataIn.ZIndex;
133    else
134        ZIndex=0;
135    end
136end
137
138%transform second field (if exists) to cartesian phys coordiantes
139if test_1
140    DataOut_1=phys_1(Data_1,Calib{2},origin_xy,radius_offset,angle_offset,angle_scale);
141    if isfield(Data_1,'A')&isfield(Data_1,'Coord_x')&~isempty(Data_1.Coord_x) & isfield(Data_1,'Coord_y')&...
142                                       ~isempty(Data_1.Coord_y)&length(Data_1.A)>1
143          iscalar=iscalar+1;
144          Calib{iscalar}=Calib{2};
145          A{iscalar}=Data_1.A;
146          if isfield(Data_1,'ZIndex')&~isequal(Data_1.ZIndex,ZIndex)
147              DataOut.Txt='inconsistent plane indexes in the two input fields';
148          end
149          if iscalar==1% case for which only the second field is a scalar
150               [A,Coord_x,Coord_y]=phys_Ima_polar(A,Calib,ZIndex,origin_xy,radius_offset,angle_offset,angle_scale);
151               DataOut_1.A=A{1};
152               DataOut_1.Coord_x=Coord_x;
153               DataOut_1.Coord_y=Coord_y;
154               return
155          end
156    end
157end
158if iscalar~=0
159    [A,Coord_x,Coord_y]=phys_Ima_polar(A,Calib,ZIndex,origin_xy,radius_offset,angle_offset,angle_scale);%
160    DataOut.A=A{1};
161    DataOut.Coord_x=Coord_x;
162    DataOut.Coord_y=Coord_y;
163    if iscalar==2
164        DataOut_1.A=A{2};
165        DataOut_1.Coord_x=Coord_x;
166        DataOut_1.Coord_y=Coord_y;
167    end
168end
169
170
171
172
173%------------------------------------------------
174function DataOut=phys_1(Data,Calib,origin_xy,radius_offset,angle_offset,angle_scale)
175
176DataOut=Data;
177% DataOut.CoordUnit=Calib.CoordUnit; %put flag for physical coordinates
178if isfield(Calib,'SliceCoord')
179    DataOut.PlaneCoord=Calib.SliceCoord;%to generalise for any plane
180end
181
182if isfield(Data,'CoordUnit')%&& isequal(Data.CoordType,'px')&& ~isempty(Calib)
183    if isfield(Calib,'CoordUnit')
184        DataOut.CoordUnit=Calib.CoordUnit;
185    else
186        DataOut.CoordUnit='cm'; %default
187    end
188    DataOut.TimeUnit='s';
189    %transform of X,Y coordinates for vector fields
190    if isfield(Data,'ZIndex') && ~isempty(Data.ZIndex)&&~isnan(Data.ZIndex)
191        Z=Data.ZIndex;
192    else
193        Z=0;
194    end
195    if isfield(Data,'X') &isfield(Data,'Y')&~isempty(Data.X) & ~isempty(Data.Y)
196        [DataOut.X,DataOut.Y,DataOut.Z]=phys_XYZ(Calib,Data.X,Data.Y,Z); %transform from pixels to physical
197        DataOut.X=DataOut.X-origin_xy(1);%origin of coordinates at the tank center
198        DataOut.Y=DataOut.Y-origin_xy(2);%origin of coordinates at the tank center
199        [theta,DataOut.X] = cart2pol(DataOut.X,DataOut.Y);%theta  and X are the polar coordinates angle and radius
200          %shift and renormalize the polar coordinates
201        DataOut.X=DataOut.X-radius_offset;%shift the origin of radius, taken as the new X coordinate
202        DataOut.Y=(theta-angle_offset)*angle_scale;% normalized angle: distance along reference radius,taken as the new Y coordinate
203        %transform velocity field if exists
204        if isfield(Data,'U') & isfield(Data,'V') & ~isempty(Data.U) & ~isempty(Data.V)& isfield(Data,'Dt')
205            if ~isempty(Data.Dt)
206            [XOut_1,YOut_1]=phys_XYZ(Calib,Data.X-Data.U/2,Data.Y-Data.V/2,Z);% X,Y positions of the vector origin in phys
207            [XOut_2,YOut_2]=phys_XYZ(Calib,Data.X+Data.U/2,Data.Y+Data.V/2,Z);% X,Y positions of the vector end in phys
208            UX=(XOut_2-XOut_1)/Data.Dt;% phys velocity u component
209            VY=(YOut_2-YOut_1)/Data.Dt; % phys velocity v component     
210            %transform u,v into polar coordiantes
211            DataOut.U=UX.*cos(theta)+VY.*sin(theta);%radial velocity
212            DataOut.V=(-UX.*sin(theta)+VY.*cos(theta));%./(DataOut.X)%+radius_ref);% azimuthal velocity component
213            %shift and renormalize the angular velocity
214            end
215        end
216        %transform of spatial derivatives
217        if isfield(Data,'X') && ~isempty(Data.X) && isfield(Data,'DjUi') && ~isempty(Data.DjUi)...
218                && isfield(Data,'Dt')
219            if ~isempty(Data.Dt)
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),Z);
223                    [Xm1,Ym1]=phys_XYZ(Calib,Data.X(ip)-0.5,Data.Y(ip),Z);
224                    [Xp2,Yp2]=phys_XYZ(Calib,Data.X(ip),Data.Y(ip)+0.5,Z);
225                    [Xm2,Ym2]=phys_XYZ(Calib,Data.X(ip),Data.Y(ip)-0.5,Z);
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/Data.Dt;   %     min(Data.DjUi(:,1,1))=DUDX
236            end
237        end
238    end
239end
240
241
242%%%%%%%%%%%%%%%%%%%%
243function [A_out,Rangx,Rangy]=phys_Ima_polar(A,CalibIn,ZIndex,origin_xy,radius_offset,angle_offset,angle_scale)
244xcorner=[];
245ycorner=[];
246npx=[];
247npy=[];
248for icell=1:length(A)
249    siz=size(A{icell});
250    npx=[npx siz(2)];
251    npy=[npy siz(1)];
252    zphys=0; %default
253    if isfield(CalibIn{icell},'SliceCoord') %.Z= index of plane
254       SliceCoord=CalibIn{icell}.SliceCoord(ZIndex,:);
255       zphys=SliceCoord(3); %to generalize for non-parallel planes
256    end
257    xima=[0.5 siz(2)-0.5 0.5 siz(2)-0.5];%image coordiantes of corners
258    yima=[0.5 0.5 siz(1)-0.5 siz(1)-0.5];
259    [xcorner_new,ycorner_new]=phys_XYZ(CalibIn{icell},xima,yima,ZIndex);%corresponding physical coordinates
260    %transform the corner coordinates into polar ones   
261    xcorner_new=xcorner_new-origin_xy(1);%shift to the origin of the polar coordinates
262    ycorner_new=ycorner_new-origin_xy(2);%shift to the origin of the polar coordinates       
263    [theta,xcorner_new] = cart2pol(xcorner_new,ycorner_new);%theta  and X are the polar coordinates angle and radius
264    if (max(theta)-min(theta))>pi   %if the polar origin is inside the image
265        xcorner_new=[0 max(xcorner_new)];
266        theta=[-pi pi];
267    end
268          %shift and renormalize the polar coordinates
269    xcorner_new=xcorner_new-radius_offset;%
270    ycorner_new=theta*angle_scale-angle_offset;% normalized angle: distance along reference radius
271    xcorner=[xcorner xcorner_new];
272    ycorner=[ycorner ycorner_new];
273end
274Rangx(1)=min(xcorner);
275Rangx(2)=max(xcorner);
276Rangy(2)=min(ycorner);
277Rangy(1)=max(ycorner);
278% test_multi=(max(npx)~=min(npx)) | (max(npy)~=min(npy));
279npx=max(npx);
280npy=max(npy);
281x=linspace(Rangx(1),Rangx(2),npx);
282y=linspace(Rangy(1),Rangy(2),npy);
283[X,Y]=meshgrid(x,y);%grid in physical coordinates
284%transform X, Y in cartesian
285X=X+radius_offset;%
286Y=(Y+angle_offset)/angle_scale;% normalized angle: distance along reference radius
287[X,Y] = pol2cart(Y,X);
288X=X+origin_xy(1);%shift to the origin of the polar coordinates
289Y=Y+origin_xy(2);%shift to the origin of the polar coordinates
290for icell=1:length(A)
291    siz=size(A{icell});
292    [XIMA,YIMA]=px_XYZ(CalibIn{icell},X,Y,zphys);%corresponding image indices for each point in the real space grid
293    XIMA=reshape(round(XIMA),1,npx*npy);%indices reorganized in 'line'
294    YIMA=reshape(round(YIMA),1,npx*npy);
295    flagin=XIMA>=1 & XIMA<=npx & YIMA >=1 & YIMA<=npy;%flagin=1 inside the original image
296    if numel(siz)==2 %(B/W images)
297        vec_A=reshape(A{icell}(:,:,1),1,npx*npy);%put the original image in line
298        ind_in=find(flagin);
299        ind_out=find(~flagin);
300        ICOMB=((XIMA-1)*npy+(npy+1-YIMA));
301        ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A
302        vec_B(ind_in)=vec_A(ICOMB);
303        vec_B(ind_out)=zeros(size(ind_out));
304        A_out{icell}=reshape(vec_B,npy,npx);%new image in real coordinates
305    else
306        for icolor=1:siz(3)
307                vec_A=reshape(A{icell}(:,:,icolor),1,npx*npy);%put the original image in line
308                ind_in=find(flagin);
309                ind_out=find(~flagin);
310                ICOMB=((XIMA-1)*npy+(npy+1-YIMA));
311                ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A
312                vec_B(ind_in)=vec_A(ICOMB);
313                vec_B(ind_out)=zeros(size(ind_out));
314                A_out{icell}(:,:,icolor)=reshape(vec_B,npy,npx);%new image in real coordinates
315        end
316    end
317end
318
Note: See TracBrowser for help on using the repository browser.