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

Last change on this file since 1099 was 1094, checked in by sommeria, 4 years ago

phys_polar debugged

File size: 26.8 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% Data:   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%[Data,Data_1]=phys_polar(varargin)
17%
18% OUTPUT:
19% Data: structure of modified data field: .X=radius, .Y=azimuth angle, .U, .V are radial and azimuthal velocity components
20% Data_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-2021, 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 Data=phys_polar(DataIn,XmlData,DataIn_1,XmlData_1)
47%------------------------------------------------------------------------
48
49%% request input parameters
50if isfield(DataIn,'Action') && isfield(DataIn.Action,'RUN') && isequal(DataIn.Action.RUN,0)
51    prompt = {'origin [x y] of polar coordinates';'reference radius';'reference angle(degrees)';'angle direction and switch x y(+/-)'};
52    dlg_title = 'set the parameters for the polar coordinates';
53    num_lines= 2;
54    def     = { '[0 0]';'';'0';'+'};
55    if isfield(XmlData,'TransformInput')
56        if isfield(XmlData.TransformInput,'PolarCentre')
57            def{1}=num2str(XmlData.TransformInput.PolarCentre);
58        end
59        if isfield(XmlData.TransformInput,'PolarReferenceRadius')
60            def{2}=num2str(XmlData.TransformInput.PolarReferenceRadius);
61        end
62        if isfield(XmlData.TransformInput,'PolarReferenceAngle')
63            def{3}=num2str(XmlData.TransformInput.PolarReferenceAngle);
64        end
65        if isfield(XmlData.TransformInput,'PolarAngleDirection')
66            def{4}=XmlData.TransformInput.PolarAngleDirection;
67        end
68    end
69    answer = inputdlg(prompt,dlg_title,num_lines,def);
70    Data.TransformInput.PolarCentre=str2num(answer{1});
71    Data.TransformInput.PolarReferenceRadius=str2num(answer{2});
72    Data.TransformInput.PolarReferenceAngle=str2num(answer{3});
73    Data.TransformInput.PolarAngleDirection=answer{4};
74    return
75end
76
77%% default outputs
78Data=DataIn; %default output
79if isfield(Data,'CoordUnit')
80Data=rmfield(Data,'CoordUnit');
81end
82Data.ListVarName = {};
83Data.VarDimName={};
84Data.VarAttribute={};
85DataCell{1}=DataIn;
86Calib{1}=[];
87DataCell{2}=[];%default
88checkpixel(1)=0;
89if isfield(DataCell{1},'CoordUnit')&& strcmp(DataCell{1}.CoordUnit,'pixel')
90    checkpixel(1)=1;
91end
92if nargin==2||nargin==4
93    if isfield(XmlData,'GeometryCalib') && ~isempty(XmlData.GeometryCalib)&& checkpixel(1)
94        Calib{1}=XmlData.GeometryCalib;
95    end
96    Calib{2}=Calib{1};
97else
98    Data.Txt='wrong input: need two or four structures';
99end
100nbinput=1;
101if nargin==4% case of two input fields
102    checkpixel(2)=0;
103if isfield(DataCell{2},'CoordUnit')&& strcmp(DataCell{2}.CoordUnit,'pixel')
104    checkpixel(2)=1;
105end
106    DataCell{2}=DataIn_1;%default
107    if isfield(XmlData_1,'GeometryCalib')&& ~isempty(XmlData_1.GeometryCalib) && checkpixel(2)
108        Calib{2}=XmlData_1.GeometryCalib;
109    end
110    nbinput=2;
111end
112
113%% parameters for polar coordinates (taken from the calibration data of the first field)
114%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115origin_xy=[0 0];%center for the polar coordinates in the original x,y coordinates
116radius_offset=0;%reference radius used to offset the radial coordinate r
117angle_offset=0; %reference angle used as new origin of the polar angle (= axis Ox by default)
118angle_scale=180/pi;
119check_degree=1;%angle expressed in degrees by default
120if isfield(XmlData,'TransformInput')
121    if isfield(XmlData.TransformInput,'PolarCentre') && isnumeric(XmlData.TransformInput.PolarCentre)
122        if isequal(length(XmlData.TransformInput.PolarCentre),2)
123            origin_xy= XmlData.TransformInput.PolarCentre;
124        end
125    end
126    if isfield(XmlData.TransformInput,'PolarReferenceRadius') && ~isempty(XmlData.TransformInput.PolarReferenceRadius)
127        radius_offset=XmlData.TransformInput.PolarReferenceRadius;
128    end
129    if radius_offset > 0
130        angle_scale=radius_offset; %the azimuth is rescale in terms of the length along the reference radius
131        check_degree=0; %the output has the same unit as the input
132    else
133        angle_scale=180/pi; %polar angle in degrees
134        check_degree=1;%angle expressed in degrees
135    end
136    if isfield(XmlData.TransformInput,'PolarReferenceAngle') && isnumeric(XmlData.TransformInput.PolarReferenceAngle)
137        angle_offset=(pi/180)*XmlData.TransformInput.PolarReferenceAngle; %offset angle (in unit of the final angle, degrees or arc length along the reference radius))
138    end
139    check_reverse=isfield(XmlData.TransformInput,'PolarAngleDirection')&& strcmp(XmlData.TransformInput.PolarAngleDirection,'-');
140end
141
142%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143%% get fields
144
145nbvar=0;%counter for the number of output variables
146nbcoord=0;%counter for the number of variablecheck_degrees for radial coordiantes (case of multiple field inputs)
147nbgrid=0;%counter for the number of gridded fields (all linearly interpolated on the same output polar grid)
148nbscattered=0;%counter of scattered fields
149radius_name='radius';
150theta_name='theta';
151U_r_name='U_r';
152U_theta_name='U_theta';
153for ifield=1:nbinput %1 or 2 input fields
154    [CellInfo,NbDim,errormsg]=find_field_cells(DataCell{ifield});
155    if ~isempty(errormsg)
156        Data.Txt=['bad input to phys_polar: ' errormsg];
157        return
158    end
159    %transform of X,Y coordinates for vector fields
160    if isfield(DataCell{ifield},'ZIndex')&& ~isempty(DataCell{ifield}.ZIndex)
161        ZIndex=DataCell{ifield}.ZIndex;
162    else
163        ZIndex=0;
164    end
165    check_scalar=zeros(1,numel(CellInfo));
166    check_vector=zeros(1,numel(CellInfo));
167    for icell=1:numel(CellInfo)
168        if NbDim(icell)==2
169            % case of input field with scattered coordinates
170            if strcmp(CellInfo{icell}.CoordType,'scattered')
171                nbscattered=nbscattered+1;
172                nbcoord=nbcoord+1;
173                radius_name = rename_indexing(radius_name,Data.ListVarName);
174                theta_name = rename_indexing(theta_name,Data.ListVarName);
175                Data.ListVarName = [Data.ListVarName {radius_name} {theta_name}];
176                dim_name = rename_indexing('nb_point',Data.VarDimName);
177                Data.VarDimName=[Data.VarDimName {dim_name} {dim_name}];
178                nbvar=nbvar+2;
179                Data.VarAttribute{nbvar-1}.Role='coord_x';
180                check_unit=1;
181                %unit of output field
182                if isfield(XmlData,'GeometryCalib')&& isfield(XmlData.GeometryCalib,'CoordUnit')
183                    radius_unit=XmlData.GeometryCalib.CoordUnit;% states that the output is in unit defined by GeometryCalib, then erased all projection objects with different units
184                elseif isfield(DataCell{ifield},'CoordUnit')
185                    radius_unit=DataCell{ifield}.CoordUnit;
186                else
187                    radius_unit='';
188                end
189                Data.VarAttribute{nbvar-1}.units=radius_unit;
190                if check_degree
191                     Data.VarAttribute{nbvar}.units='degree';
192                else %case of a reference radius
193                    Data.VarAttribute{nbvar}.units=radius_unit;
194                    Data.CoordUnit=radius_unit;
195                end
196%                 if isfield(DataCell{ifield},'CoordUnit')
197%                     Data=rmfield(Data,'CoordUnit');
198%                     Data.VarAttribute{nbvar-1}.unit=DataCell{ifield}.CoordUnit;
199%                 elseif isfield(XmlData,'GeometryCalib')&& isfield(XmlData.GeometryCalib,'CoordUnit')
200%                     Data.VarAttribute{nbvar-1}.unit=XmlData.GeometryCalib.CoordUnit;% states that the output is in unit defined by GeometryCalib, then erased all projection objects with different units
201%                 else
202%                     check_unit=0;
203%                 end
204                Data.VarAttribute{nbvar}.Role='coord_y';
205%                 if check_degree
206%                 Data.VarAttribute{nbvar}.units='degree';
207%                 elseif check_unit
208%                     Data.VarAttribute{nbvar}.units=Data.VarAttribute{nbvar-1}.units;
209%                 end
210 
211                %transform u,v into polar coordinates
212                X=DataCell{ifield}.(CellInfo{icell}.XName);
213                Y=DataCell{ifield}.(CellInfo{icell}.YName);
214                if isfield(CellInfo{icell},'VarIndex_vector_x')&& isfield(CellInfo{icell},'VarIndex_vector_y')
215                    UName=DataCell{ifield}.ListVarName{CellInfo{icell}.VarIndex_vector_x};
216                    VName=DataCell{ifield}.ListVarName{CellInfo{icell}.VarIndex_vector_y};
217                    if ~isempty(Calib{ifield})
218                        [X,Y,Z,DataCell{ifield}.(UName),DataCell{ifield}.(VName)]=...
219                            phys_XYUV(DataCell{ifield},Calib{ifield},ZIndex);
220                    end
221                end
222                [Theta,Radius] = cart2pol(X-origin_xy(1),Y-origin_xy(2));
223                Data.(radius_name)=Radius-radius_offset;
224                Data.(theta_name)=Theta*angle_scale-angle_offset;
225                if Z~=0
226                    Data.Z=Z;
227                    nbvar=nbvar+1;
228                    Data.ListVarName = [Data.ListVarName {'Z'}];
229                    Data.VarDimName=[Data.VarDimName {dim_name}];
230                    Data.VarAttribute{nbvar}.Role='coord_z';
231                end
232                if isfield(CellInfo{icell},'VarIndex_scalar')
233                    ScalarName=DataCell{ifield}.ListVarName{CellInfo{icell}.VarIndex_scalar};
234                    ScalarName=rename_indexing(ScalarName,Data.ListVarName);
235                    Data.(ScalarName)=DataCell{ifield}.(ScalarName);
236                    nbvar=nbvar+1;
237                    Data.ListVarName = [Data.ListVarName {ScalarName}];
238                    Data.VarDimName=[Data.VarDimName {dim_name}];
239                    Data.VarAttribute{nbvar}.Role='scalar';
240                end
241                if isfield(CellInfo{icell},'VarIndex_vector_x')&& isfield(CellInfo{icell},'VarIndex_vector_y')
242                    U_r_name= rename_indexing(U_r_name,Data.ListVarName);
243                    U_theta_name= rename_indexing(U_theta_name,Data.ListVarName);
244                    Data.(U_r_name)=DataCell{ifield}.(UName).*cos(Theta)+DataCell{ifield}.(VName).*sin(Theta);%radial velocity
245                    Data.(U_theta_name)=(-DataCell{ifield}.(UName).*sin(Theta)+DataCell{ifield}.(VName).*cos(Theta));%./(Data.X)%+radius_ref);% azimuthal velocity component
246                    Data.ListVarName = [Data.ListVarName {U_r_name} {U_theta_name}];
247                    Data.VarDimName=[Data.VarDimName {dim_name} {dim_name}];
248                    Data.VarAttribute{nbvar+1}.Role='vector_x';
249                    Data.VarAttribute{nbvar+2}.Role='vector_y';
250                    nbvar=nbvar+2;
251                end
252                if isfield(CellInfo{icell},'VarIndex_errorflag')
253                    error_flag_name=DataCell{ifield}.ListVarName{CellInfo{icell}.VarIndex_errorflag};
254                    error_flag_newname= rename_indexing(error_flag_name,Data.ListVarName);
255                    Data.(error_flag_newname)=DataCell{ifield}.(error_flag_name);
256                    Data.ListVarName = [Data.ListVarName {error_flag_newname}];
257                    Data.VarDimName=[Data.VarDimName {dim_name}];
258                    nbvar=nbvar+1;
259                    Data.VarAttribute{nbvar}.Role='errorflag';
260                end
261               
262           %caseof input fields on gridded coordinates (matrix)
263            elseif strcmp(CellInfo{icell}.CoordType,'grid')
264                if nbgrid==0% no gridded data yet, introduce the coordinate variables common to all gridded data
265                    nbcoord=nbcoord+1;%add new radial coordinates for the first gridded field
266                    radius_name = rename_indexing(radius_name,Data.ListVarName);% add an index to the name, or increment an existing index,
267                    theta_name = rename_indexing(theta_name,Data.ListVarName);% if the proposed Name already exists in the list
268                    Data.ListVarName = [Data.ListVarName {radius_name} {theta_name}];%add polar coordinates to the list of variables
269                    Data.VarDimName=[Data.VarDimName {radius_name} {theta_name}];
270                    nbvar=nbvar+2;
271                    if check_reverse
272                        Data.VarAttribute{nbvar-1}.Role='coord_y';
273                        Data.VarAttribute{nbvar}.Role='coord_x';
274                    else
275                        Data.VarAttribute{nbvar-1}.Role='coord_x';
276                        Data.VarAttribute{nbvar}.Role='coord_y';
277                    end
278                    check_unit=1;
279
280                    if isfield(XmlData,'GeometryCalib')&& isfield(XmlData.GeometryCalib,'CoordUnit')
281                        Data.VarAttribute{nbvar-1}.units=XmlData.GeometryCalib.CoordUnit;% states that the output is in unit defined by GeometryCalib, then erased all projection objects with different units
282                    elseif isfield(DataCell{ifield},'CoordUnit')
283                        Data.VarAttribute{nbvar-1}.units=DataCell{ifield}.CoordUnit;%radius in coord units
284                    else
285                        check_unit=0;
286                    end
287                    if check_degree
288                        Data.VarAttribute{nbvar}.units='degree';%angle in degree
289                    elseif check_unit
290                        Data.VarAttribute{nbvar}.units=Data.VarAttribute{nbvar-1}.units;% angle in coord unit (normalised by reference radiuss)
291                    end
292                end
293                if isfield(CellInfo{icell},'VarIndex_scalar')
294                    nbgrid=nbgrid+1;
295                    nbvar=nbvar+1;
296                    Data.VarAttribute{nbvar}.Role='scalar';
297                    FieldName{nbgrid}=DataCell{ifield}.ListVarName{CellInfo{icell}.VarIndex_scalar};
298                    A{nbgrid}=DataCell{ifield}.(FieldName{nbgrid});
299                    nbpoint(nbgrid)=numel(A{nbgrid});
300                    check_scalar(nbgrid)=1;
301                    coord_x{nbgrid}=DataCell{ifield}.(DataCell{ifield}.ListVarName{CellInfo{icell}.XIndex});
302                    coord_y{nbgrid}=DataCell{ifield}.(DataCell{ifield}.ListVarName{CellInfo{icell}.YIndex});
303                    ZInd(nbgrid)=ZIndex;
304                    Calib_new{nbgrid}=Calib{ifield};
305                end
306                if isfield(CellInfo{icell},'VarIndex_vector_x')&& isfield(CellInfo{icell},'VarIndex_vector_y')
307                    FieldName{nbgrid+1}=DataCell{ifield}.ListVarName{CellInfo{icell}.VarIndex_vector_x};
308                    FieldName{nbgrid+2}=DataCell{ifield}.ListVarName{CellInfo{icell}.VarIndex_vector_y};
309                    A{nbgrid+1}=DataCell{ifield}.(FieldName{nbgrid+1});
310                    A{nbgrid+2}=DataCell{ifield}.(FieldName{nbgrid+2});
311                    % Data.ListVarName=[Data.ListVarName {'U_r','U_theta'}];
312                    %Data.VarDimName=[Data.VarDimName {{theta_name,radius_name}} {{theta_name,radius_name}}];
313                    Data.VarAttribute{nbvar+1}.Role='vector_x';
314                    Data.VarAttribute{nbvar+2}.Role='vector_y';
315                    nbpoint([nbgrid+1 nbgrid+2])=numel(A{nbgrid+1});
316                    check_vector(nbgrid+1)=1;
317                    check_vector(nbgrid+2)=1;
318                    coord_x{nbgrid+1}=DataCell{ifield}.(DataCell{ifield}.ListVarName{CellInfo{icell}.XIndex});
319                    coord_y{nbgrid+1}=DataCell{ifield}.(DataCell{ifield}.ListVarName{CellInfo{icell}.YIndex});
320                    coord_x{nbgrid+2}=DataCell{ifield}.(DataCell{ifield}.ListVarName{CellInfo{icell}.XIndex});
321                    coord_y{nbgrid+2}=DataCell{ifield}.(DataCell{ifield}.ListVarName{CellInfo{icell}.YIndex});
322                    ZInd(nbgrid+1)=ZIndex;
323                    ZInd(nbgrid+2)=ZIndex;
324                    Calib_new{nbgrid+1}=Calib{ifield};
325                    Calib_new{nbgrid+2}=Calib{ifield};
326                    nbgrid=nbgrid+2;
327                    nbvar=nbvar+2;
328                end
329            end
330        end
331    end
332end
333
334%% tranform cartesian to polar coordinates for gridded data
335if nbgrid~=0
336    [A,Data.radius,Data.theta]=phys_Ima_polar(A,coord_x,coord_y,Calib_new,ZInd,origin_xy,radius_offset,angle_offset,angle_scale);
337    for icell=1:numel(A)
338        if icell<=numel(A)-1 && check_vector(icell)==1 && check_vector(icell+1)==1   %transform u,v into polar coordinates
339            theta=Data.theta/angle_scale-angle_offset;
340            [~,Theta]=meshgrid(Data.radius,theta);%grid in physical coordinates
341            U_r_name= rename_indexing(U_r_name,Data.ListVarName);
342            U_theta_name= rename_indexing(U_theta_name,Data.ListVarName);       
343                Data.(U_r_name)=A{icell}.*cos(Theta)+A{icell+1}.*sin(Theta);%radial velocity
344                Data.(U_theta_name)=(-A{icell}.*sin(Theta)+A{icell+1}.*cos(Theta));% azimuthal velocity component
345            if check_reverse
346                Data.(U_theta_name)=(Data.(U_theta_name))';
347                Data.(U_r_name)=Data.(U_r_name)';
348                Data.ListVarName=[Data.ListVarName {U_theta_name,U_r_name}];
349                Data.VarDimName=[Data.VarDimName {{radius_name,theta_name}} {{radius_name,theta_name}}];
350            else
351                Data.ListVarName=[Data.ListVarName {U_r_name,U_theta_name}];
352                Data.VarDimName=[Data.VarDimName {{theta_name,radius_name}} {{theta_name,radius_name}}];
353            end
354        elseif ~check_vector(icell)% for scalar fields
355            FieldName{icell}= rename_indexing(FieldName{icell},Data.ListVarName);
356            Data.ListVarName=[Data.ListVarName FieldName(icell)];       
357            if check_reverse
358                Data.(FieldName{icell})=A{icell}';
359                Data.VarDimName=[Data.VarDimName {{radius_name,theta_name}}];
360            else
361                Data.VarDimName=[Data.VarDimName {{theta_name,radius_name}}];
362                Data.(FieldName{icell})=A{icell};
363            end
364        end
365    end
366end
367if check_reverse
368    Data.(theta_name)=-Data.(theta_name);
369end
370
371
372%------------------------------------------------
373%--- transform a single field into phys coordiantes
374function [X,Y,Z,U,V]=phys_XYUV(Data,Calib,ZIndex)
375%------------------------------------------------
376%% set default output
377%DataOut=Data;%default
378%DataOut.CoordUnit=Calib.CoordUnit;% the output coord unit is set by the calibration parameters
379X=[];%default output
380Y=[];
381Z=0;
382U=[];
383V=[];
384%% transform  X,Y coordinates for velocity fields (transform of an image or scalar done in phys_ima)
385if isfield(Data,'X') &&isfield(Data,'Y')&&~isempty(Data.X) && ~isempty(Data.Y)
386    [X,Y,Z]=phys_XYZ(Calib,Data.X,Data.Y,ZIndex);
387    Dt=1; %default
388    if isfield(Data,'dt')&&~isempty(Data.dt)
389        Dt=Data.dt;
390    end
391    if isfield(Data,'Dt')&&~isempty(Data.Dt)
392        Dt=Data.Dt;
393    end
394    if isfield(Data,'U')&&isfield(Data,'V')&&~isempty(Data.U) && ~isempty(Data.V)
395        [XOut_1,YOut_1]=phys_XYZ(Calib,Data.X-Data.U/2,Data.Y-Data.V/2,ZIndex);
396        [XOut_2,YOut_2]=phys_XYZ(Calib,Data.X+Data.U/2,Data.Y+Data.V/2,ZIndex);
397        U=(XOut_2-XOut_1)/Dt;
398        V=(YOut_2-YOut_1)/Dt;
399    end
400end
401
402%%%%%%%%%%%%%%%%%%%%
403% tranform gridded field into polar coordiantes on a regular polar grid,
404% transform to phys coordiantes if requested by calibration input
405function [A_out,radius,theta]=phys_Ima_polar(A,coord_x,coord_y,CalibIn,ZIndex,origin_xy,radius_offset,angle_offset,angle_scale)
406rcorner=[];
407thetacorner=[];
408npx=[];
409npy=[];
410for icell=1:length(A)
411    siz=size(A{icell});
412    npx(icell)=siz(2);
413    npy(icell)=siz(1);
414    x_edge=[linspace(coord_x{icell}(1),coord_x{icell}(end),npx(icell)) coord_x{icell}(end)*ones(1,npy(icell))...
415        linspace(coord_x{icell}(end),coord_x{icell}(1),npx(icell)) coord_x{icell}(1)*ones(1,npy(icell))];%x coordinates of the image edge(four sides)
416    y_edge=[coord_y{icell}(1)*ones(1,npx(icell)) linspace(coord_y{icell}(1),coord_y{icell}(end),npy(icell))...
417        coord_y{icell}(end)*ones(1,npx(icell)) linspace(coord_y{icell}(end),coord_y{icell}(1),npy(icell))];%y coordinates of the image edge(four sides)
418   
419    % transform edges into phys coordinates if requested
420    if ~isempty(CalibIn{icell})
421        [x_edge,y_edge]=phys_XYZ(CalibIn{icell},x_edge,y_edge,ZIndex(icell));% physical coordinates of the image edge
422    end
423   
424    %transform the corner coordinates into polar ones
425    x_edge=x_edge-origin_xy(1);%shift to the origin of the polar coordinates
426    y_edge=y_edge-origin_xy(2);%shift to the origin of the polar coordinates
427    [theta_edge,r_edge] = cart2pol(x_edge,y_edge);%theta  and X are the polar coordinates angle and radius
428    if (max(theta_edge)-min(theta_edge))>pi   %if the polar origin is inside the image
429        r_edge=[0 max(r_edge)];
430        theta_edge=[-pi pi];
431    end
432    rcorner=[rcorner r_edge];
433    thetacorner=[thetacorner theta_edge];
434end
435nbpoint=max(npx.*npy);
436Min_r=min(rcorner);
437Max_r=max(rcorner);
438Min_theta=min(thetacorner)*angle_scale;
439Max_theta=max(thetacorner)*angle_scale;
440Dr=round_uvmat((Max_r-Min_r)/sqrt(nbpoint));
441Dtheta=round_uvmat((Max_theta-Min_theta)/sqrt(nbpoint));% get a simple mesh for the rescaled angle
442radius=Min_r:Dr:Max_r;% polar coordinates for projections
443theta=Min_theta:Dtheta:Max_theta;
444%theta=Max_theta:-Dtheta:Min_theta;
445[Radius,Theta]=meshgrid(radius,theta/angle_scale);%grid in polar coordinates (angles in radians)
446%transform X, Y in cartesian
447[X,Y] = pol2cart(Theta,Radius);% cartesian coordinates associated to the grid in polar coordinates
448X=X+origin_xy(1);%shift to the origin of the polar coordinates
449Y=Y+origin_xy(2);%shift to the origin of the polar coordinates
450radius=radius-radius_offset;
451theta=theta-angle_offset*angle_scale;
452[np_theta,np_r]=size(Radius);
453
454for icell=1:length(A)
455    XIMA=X;
456    YIMA=Y;
457    if ~isempty(CalibIn{icell})%transform back to pixel if calibration parameters are introduced
458        Z=0; %default
459        if isfield(CalibIn{icell},'SliceCoord') %.Z= index of plane
460            if ZIndex(icell)==0
461                ZIndex(icell)=1;
462            end
463            SliceCoord=CalibIn{icell}.SliceCoord(ZIndex(icell),:);
464            Z=SliceCoord(3); %to generalize for non-parallel planes
465            if isfield(CalibIn{icell},'SliceAngle')
466            norm_plane=angle2normal(CalibIn{icell}.SliceAngle);
467            Z=Z-(norm_plane(1)*(X-SliceCoord(1))+norm_plane(2)*(Y-SliceCoord(2)))/norm_plane(3);
468            end
469        end
470        [XIMA,YIMA]=px_XYZ(CalibIn{icell},X,Y,Z);%corresponding image indices for each point in the real space grid
471    end
472    Dx=(coord_x{icell}(end)-coord_x{icell}(1))/(npx(icell)-1);
473    Dy=(coord_y{icell}(end)-coord_y{icell}(1))/(npy(icell)-1);
474    indx_ima=1+round((XIMA-coord_x{icell}(1))/Dx);%indices of the initial matrix close to the points of the new grid
475    %indy_ima=1+round((YIMA-coord_y{icell}(1))/Dy);
476    indy_ima=1+round((coord_y{icell}(end)-YIMA)/Dy);
477     Delta_x=1+(XIMA-coord_x{icell}(1))/Dx-indx_ima;%error in the index discretisation
478     Delta_y=1+(coord_y{icell}(end)-YIMA)/Dy-indy_ima;
479    XIMA=reshape(indx_ima,1,[]);%indices reorganized in 'line'
480    YIMA=reshape(indy_ima,1,[]);%indices reorganized in 'line'
481    flagin=XIMA>=1 & XIMA<=npx(icell) & YIMA >=1 & YIMA<=npy(icell);%flagin=1 inside the original image
482    siz=size(A{icell});
483    checkuint8=isa(A{icell},'uint8');%check for image input with 8 bits
484    checkuint16=isa(A{icell},'uint16');%check for image input with 16 bits
485    A{icell}=double(A{icell});
486    if numel(siz)==2 %(B/W images)
487        vec_A=reshape(A{icell}(:,:,1),1,[]);%put the original image in line
488        ind_in=find(flagin);
489        ind_out=find(~flagin);
490        ICOMB=((XIMA-1)*npy(icell)+(npy(icell)+1-YIMA));% indices in vec_A
491        ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A
492        vec_B(ind_in)=vec_A(ICOMB);
493        vec_B(ind_out)=zeros(size(ind_out));
494        A_out{icell}=reshape(vec_B,np_theta,np_r);%new image in real coordinates
495        DA_y=circshift(A_out{icell},-1,1)-A_out{icell};% derivative
496        DA_y(end,:)=0;
497        DA_x=circshift(A_out{icell},-1,2)-A_out{icell};
498        DA_x(:,end)=0;
499        A_out{icell}=A_out{icell}+Delta_x.*DA_x+Delta_y.*DA_y;%linear interpolation
500    else
501        for icolor=1:siz(3)
502            vec_A=reshape(A{icell}(:,:,icolor),1,[]);%put the original image in line
503            ind_in=find(flagin);
504            ind_out=find(~flagin);
505            ICOMB=((XIMA-1)*npy(icell)+(npy(icell)+1-YIMA));
506            ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A
507            vec_B(ind_in)=vec_A(ICOMB);
508            vec_B(ind_out)=zeros(size(ind_out));
509            A_out{icell}(:,:,icolor)=reshape(vec_B,np_theta,np_r);%new image in real coordinates
510            DA_y=circshift(A_out{icell}(:,:,icolor),-1,1)-A_out{icell}(:,:,icolor);
511            DA_y(end,:)=0;
512            DA_x=circshift(A_out{icell}(:,:,icolor),-1,2)-A_out{icell}(:,:,icolor);
513            DA_x(:,end)=0;
514            A_out{icell}(:,:,icolor)=A_out{icell}(:,:,icolor)+Delta_x.*DA_x+Delta_y.*DA_y;%linear interpolation
515        end
516    end
517    if checkuint8
518        A_out{icell}=uint8(A_out{icell});
519    elseif checkuint16
520        A_out{icell}=uint16(A_out{icell});
521    end
522end
523
Note: See TracBrowser for help on using the repository browser.