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

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

various updates

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