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

Last change on this file since 1107 was 1107, checked in by g7moreau, 2 years ago

Update Copyright to 2022

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