source: trunk/src/series/bed_scan.m @ 1185

Last change on this file since 1185 was 1185, checked in by sommeria, 5 weeks ago

bed_scan repaired

File size: 12.4 KB
Line 
1%'bed_scan': get the bed shape from laser impact
2% firts line input files = active images
3% second line, reference images for the initial bed
4
5%------------------------------------------------------------------------
6% function GUI_input=bed_scan(Param)
7%
8%------------------------------------------------------------------------
9%%%%%%%%%%% GENERAL TO ALL SERIES ACTION FCTS %%%%%%%%%%%%%%%%%%%%%%%%%%%
10%
11%OUTPUT
12% ParamOut: sets options in the GUI series.fig needed for the function
13%
14%INPUT:
15% In run mode, the input parameters are given as a Matlab structure Param copied from the GUI series.
16% In batch mode, Param is the name of the corresponding xml file containing the same information
17% when Param.Action.RUN=0 (as activated when the current Action is selected
18% in series), the function ouput paramOut set the activation of the needed GUI elements
19%
20% Param contains the elements:(use the menu bar command 'export/GUI config' in series to
21% see the current structure Param)
22%    .InputTable: cell of input file names, (several lines for multiple input)
23%                      each line decomposed as {RootPath,SubDir,Rootfile,NomType,Extension}
24%    .OutputSubDir: name of the subdirectory for data outputs
25%    .OutputDirExt: directory extension for data outputs
26%    .Action: .ActionName: name of the current activated function
27%             .ActionPath:   path of the current activated function
28%             .ActionExt: fct extension ('.m', Matlab fct, '.sh', compiled   Matlab fct
29%             .RUN =0 for GUI input, =1 for function activation
30%             .RunMode='local','background', 'cluster': type of function  use
31%             
32%    .IndexRange: set the file or frame indices on which the action must be performed
33%    .FieldTransform: .TransformName: name of the selected transform function
34%                     .TransformPath:   path  of the selected transform function
35%    .InputFields: sub structure describing the input fields withfields
36%              .FieldName: name(s) of the field
37%              .VelType: velocity type
38%              .FieldName_1: name of the second field in case of two input series
39%              .VelType_1: velocity type of the second field in case of two input series
40%              .Coord_y: name of y coordinate variable
41%              .Coord_x: name of x coordinate variable
42%    .ProjObject: %sub structure describing a projection object (read from ancillary GUI set_object)
43%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
44
45%=======================================================================
46% Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
47%   http://www.legi.grenoble-inp.fr
48%   Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.fr
49%
50%     This file is part of the toolbox UVMAT.
51%
52%     UVMAT is free software; you can redistribute it and/or modify
53%     it under the terms of the GNU General Public License as published
54%     by the Free Software Foundation; either version 2 of the license,
55%     or (at your option) any later version.
56%
57%     UVMAT is distributed in the hope that it will be useful,
58%     but WITHOUT ANY WARRANTY; without even the implied warranty of
59%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
60%     GNU General Public License (see LICENSE.txt) for more details.
61%=======================================================================
62
63function ParamOut=bed_scan(Param)
64
65%% set the input elements needed on the GUI series when the action is selected in the menu ActionName or InputTable refreshed
66if isstruct(Param) && isequal(Param.Action.RUN,0)
67    ParamOut.NbViewMax=1;% max nbre of input file series (default , no limitation)
68    ParamOut.AllowInputSort='off';% allow alphabetic sorting of the list of input file SubDir (options 'off'/'on', 'off' by default)
69    ParamOut.WholeIndexRange='on';% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default)
70    ParamOut.NbSlice='off'; %nbre of slices ('off' by default)
71    ParamOut.VelType='off';% menu for selecting the velocity type (options 'off'/'one'/'two',  'off' by default)
72    ParamOut.FieldName='one';% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default)
73    ParamOut.FieldTransform = 'off';%can use a transform function
74    ParamOut.ProjObject='off';%can use projection object(option 'off'/'on',
75    ParamOut.Mask='off';%can use mask option   (option 'off'/'on', 'off' by default)
76    ParamOut.OutputDirExt='.bed';%set the output dir extension
77    ParamOut.OutputFileMode='NbInput_i';% ='=NbInput': 1 output file per input file index, '=NbInput_i': 1 file per input file index i, '=NbSlice': 1 file per slice   
78    return
79end
80
81%%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%%
82%% read input parameters from an xml file if input is a file name (batch mode)
83ParamOut=[];
84RUNHandle=[];
85WaitbarHandle=[];
86checkrun=1;
87if ischar(Param)% case of batch: Param is the name of the xml file containing the input parameters
88    Param=xml2struct(Param);% read Param as input file (batch case)
89    checkrun=0;
90else% interactive mode in Matlab
91    hseries=findobj(allchild(0),'Tag','series');
92    RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series
93    WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series
94end
95
96%% root input file names and nomenclature type (cell arrays with one element)
97RootPath=Param.InputTable{1,1};
98SubDir=Param.InputTable{1,2};
99RootFile=Param.InputTable{1,3};
100NomType=Param.InputTable{1,4};
101FileExt=Param.InputTable{1,5};
102i_series=Param.IndexRange.first_i:Param.IndexRange.incr_i:Param.IndexRange.last_i;
103nbfield_i=numel(i_series);
104nb_scan=400;% nbre of planes for a scan
105
106%% directory for output files
107DirOut=fullfile(RootPath,[Param.OutputSubDir Param.OutputDirExt]);
108
109%% get the set of input file names and frame indices
110CheckVirtual=false;
111if isfield(Param,'FileSeries')% virtual file indexing used (e.g. multitif images)
112    CheckVirtual=true;
113end
114
115
116%%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%fullfile(
117% EDIT FROM HERE
118%% load the initial scan
119[RootRoot,CamName]=fileparts(RootPath);
120RootRoot=fileparts(RootRoot);
121CalibFolder=fullfile(RootRoot,'EXP_INIT',CamName);
122File_init=fullfile(CalibFolder,'images.png.bed','Z_init.nc');
123[Data_init,~,~,errormsg]=nc2struct(File_init);
124if isempty(errormsg)
125    disp([File_init ' loaded'])
126else
127    disp(errormsg)
128    return
129end
130%% get the time from the ImaDoc xml file
131XmlFileName=fullfile(RootPath,[SubDir '.xml']);
132[XmlData,warnmsg]=imadoc2struct(XmlFileName);
133if isempty(warnmsg)
134    Time=mean(XmlData.Time(i_series+1,2:nb_scan+1),2);% time averaged on the j index (laser scan)
135else
136    disp(warnmsg)
137    Time=zeros(size(i_series));% time not defined
138end
139
140
141%% set of y positions
142
143
144%ycalib=[-51 -1 49];% calibration planes
145y_scan=-51+0.25*(1:nb_scan);% transverse position given by the translating system: first view at y=-51, view 400 at y=+49
146coord_x=0.25:0.25:450;%% coord x in phys coordinates for final projection
147
148Mfiltre=ones(2,10)/20;%filter matrix for imnages
149
150%% calibration data and timing: read the ImaDoc files
151
152XmlData_A=xml2struct(fullfile(CalibFolder,'planeA.xml'));
153XmlData_B=xml2struct(fullfile(CalibFolder,'planeB.xml'));
154XmlData_C=xml2struct(fullfile(CalibFolder,'planeC.xml'));
155ycalib=[-51 -1 49];% the three y positions for calibration=
156fx(1)=XmlData_C.GeometryCalib.fx_fy(1);
157fx(2)=XmlData_B.GeometryCalib.fx_fy(1);
158fx(3)=XmlData_A.GeometryCalib.fx_fy(1);
159fy(1)=XmlData_C.GeometryCalib.fx_fy(2);
160fy(2)=XmlData_B.GeometryCalib.fx_fy(2);
161fy(3)=XmlData_A.GeometryCalib.fx_fy(2);
162Tx(1)=XmlData_C.GeometryCalib.Tx_Ty_Tz(1);
163Tx(2)=XmlData_B.GeometryCalib.Tx_Ty_Tz(1);
164Tx(3)=XmlData_A.GeometryCalib.Tx_Ty_Tz(1);
165Ty(1)=XmlData_C.GeometryCalib.Tx_Ty_Tz(2);
166Ty(2)=XmlData_B.GeometryCalib.Tx_Ty_Tz(2);
167Ty(3)=XmlData_A.GeometryCalib.Tx_Ty_Tz(2);
168R11(1)=XmlData_C.GeometryCalib.R(1,1);
169R11(2)=XmlData_B.GeometryCalib.R(1,1);
170R11(3)=XmlData_A.GeometryCalib.R(1,1);
171R12(1)=XmlData_C.GeometryCalib.R(1,2);
172R12(2)=XmlData_B.GeometryCalib.R(1,2);
173R12(3)=XmlData_A.GeometryCalib.R(1,2);
174R21(1)=XmlData_C.GeometryCalib.R(2,1);
175R21(2)=XmlData_B.GeometryCalib.R(2,1);
176R21(3)=XmlData_A.GeometryCalib.R(2,1);
177R22(1)=XmlData_C.GeometryCalib.R(2,2);
178R22(2)=XmlData_B.GeometryCalib.R(2,2);
179R22(3)=XmlData_A.GeometryCalib.R(2,2);
180pfx=polyfit(ycalib,fx,1);%get thfield_ie linear interpolation of each parameter of the three calibrations
181pfy=polyfit(ycalib,fy,1);
182pTx=polyfit(ycalib,Tx,1);
183pTy=polyfit(ycalib,Ty,1);
184p11=polyfit(ycalib,R11,1);
185p12=polyfit(ycalib,R12,1);
186p21=polyfit(ycalib,R21,1);
187p22=polyfit(ycalib,R22,1);
188%get the calibration parameters at each position y by interpolation of the 3 calibration parameters
189for img=1:nb_scan
190    Calib(img).fx_fy(1)=pfx(1)*y_scan(img)+pfx(2);
191    Calib(img).fx_fy(2)=pfy(1)*y_scan(img)+pfy(2);
192    Calib(img).Tx_Ty_Tz(1)=pTx(1)*y_scan(img)+pTx(2);
193    Calib(img).Tx_Ty_Tz(2)=pTy(1)*y_scan(img)+pTy(2);
194    Calib(img).Tx_Ty_Tz(3)=1;
195    Calib(img).R=zeros(3,3);
196    Calib(img).R(3,3)=-1;
197    Calib(img).R(1,2)=p12(1)*y_scan(img)+p12(2);
198    Calib(img).R(1,1)=p11(1)*y_scan(img)+p11(2);
199    Calib(img).R(1,2)=p12(1)*y_scan(img)+p12(2);
200    Calib(img).R(2,1)=p21(1)*y_scan(img)+p21(2);
201    Calib(img).R(2,2)=p22(1)*y_scan(img)+p22(2);
202end
203
204
205
206
207%% Load the init bed scan
208tic
209%filecell=reshape(filecell,nbfield_j,nbfield_i)
210% main loop
211for ifield=1:nbfield_i
212    ifield
213    for img=1:nb_scan % loop on y positions
214        if CheckVirtual
215            [FileName,FrameIndex]=index2filename(Param.FileSeries,i_series(ifield),img,nb_scan);
216            InputFile=fullfile(RootPath,SubDir,FileName);
217        else
218            InputFile=fullfile_uvmat(RootPath,SubDir,RootFile,FileExt,NomType,FileIndex,i_series(ifield),[],img);
219            FrameIndex=1;
220        end
221        a=flipud(read_image(InputFile,'multimage',[],FrameIndex));
222        %a=flipud(imread(InputFile));%image of the initial bed  [X_b_new(img,:),Z_b_new(img,:)]=phys_XYZ(Calib(img),[],x,Z_sb(img,:))
223        if img==1
224            [nby,nbx]=size(a);
225            x_ima=1:nbx;%image absissa in pixel coordinates
226            X_phys=zeros(nb_scan,nbx);
227            Z_phys=zeros(nb_scan,nbx);
228        end
229        % filtering
230            a=filter2(Mfiltre,a);%smoothed image
231        amean=mean(a,2);
232     [~,ind_max]=max(amean);% get the max of the image averaged along x, to restrict the search region
233    ind_range=max(1,ind_max-30):min(nby,ind_max+30);% search band to find the line
234    z_ima=get_max(a(ind_range,:))+ind_range(1)-1;% get the max in the search band and shift to express it in indices of the original image
235
236 %       [~,iy]=max(a);% find the max along the first coordinate     Z_s_new=zeros(nb_scan,size(Z_s,2)); y, max values imax and the corresponding  y index iy along the first coordinate y
237        z_ima=smooth(z_ima,20,'rloess');%smooth Z, the image index of max luminosity (dependning on x)
238        [X_phys(img,:),Z_phys(img,:)]=phys_XYZ(Calib(img),[],x_ima,z_ima');
239    end
240    disp(['last file of ' num2str(ifield)])
241     disp(FileName)
242     disp(FrameIndex)
243
244    %% interpolate on a regular grid
245    %coord_x=X_phys(end,1):0.1:X_phys(end,end);%% coord x in phys coordinates based in the last view plane (the last)
246    [X_m,Y_m]=meshgrid(coord_x,y_scan);
247    Y=y_scan'*ones(1,nbx);%initialisation of X, Y final topography map
248
249    Data.Z=griddata(X_phys,Y,Z_phys,X_m,Y_m);% dZ interpolated on the regular ph1ys grid X_m,Y_m
250    size(Data.Z)
251    size(Data_init.Z_init)
252    Data.dZ=Data.Z-Data_init.Z_init;
253
254    toc
255
256    % save netcdf
257    Data.ListVarName={'coord_x','y_scan','Z','dZ'};
258    Data.VarDimName={'coord_x','y_scan',{'y_scan','coord_x'},{'y_scan','coord_x'}};
259    Data.ListGlobalAttribute={'Time'};
260     Data.Time=Time(ifield);
261    Data.VarAttribute{1}.Role='coord_x';
262    Data.VarAttribute{1}.unit='cm';
263    Data.VarAttribute{2}.Role='coord_y';
264    Data.VarAttribute{2}.unit='cm';
265    Data.VarAttribute{3}.Role='scalar';
266    Data.VarAttribute{3}.unit='cm';
267        Data.VarAttribute{4}.Role='scalar';
268    Data.VarAttribute{4}.unit='cm';
269    Data.coord_x=coord_x;
270    Data.y_scan=y_scan;
271    struct2nc(fullfile(DirOut,['dZ_' num2str(i_series(ifield)) '.nc']),Data)
272end
273
274%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275function iy=get_max(a)% get the max with sub picel resolution
276[a_max,iy]=max(a);
277[Nby,Nbx]=size(a);
278for ind_x=1:Nbx
279    if iy(ind_x)>1 && iy(ind_x)<Nby
280        a_plus=a(iy(ind_x)+1,ind_x);
281        a_min=a(iy(ind_x)-1,ind_x);
282        denom=2*a_max(ind_x)-a_plus-a_min;
283        if denom >0
284            iy(ind_x)=iy(ind_x)+0.5*(a_plus-a_min)/denom;%adjust the position of the max with a quadratic fit of the three points around the max
285        end
286    end
287end
Note: See TracBrowser for help on using the repository browser.