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

Last change on this file since 1184 was 1184, checked in by sommeria, 4 weeks ago

bed-scan updated and many updates

File size: 12.8 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;
103j_series=Param.IndexRange.first_j:Param.IndexRange.incr_j:Param.IndexRange.last_j;
104nbfield_i=numel(i_series);
105nbfield_j=numel(j_series);
106
107%% directory for output files
108DirOut=fullfile(RootPath,[Param.OutputSubDir Param.OutputDirExt]);
109
110%% get the set of input file names (cell array filecell), and file indices
111%[filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
112% filecell{iview,fileindex}: cell array representing the list of file names
113%        iview: line in the table corresponding to a given file series
114%        fileindex: file index within  the file series,
115% i1_series(iview,ref_j,ref_i)... are the corresponding arrays of indices i1,i2,j1,j2, depending on the input line iview and the two reference indices ref_i,ref_j
116% i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices
117%nbfield_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices)
118% nbfield_i=size(i1_series{1},2); %nb of fields for the i index
119%
120% nbfield_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices)
121CheckVirtual=false;
122if isfield(Param,'FileSeries')% virtual file indexing used (e.g. multitif images)
123    CheckVirtual=true;
124end
125
126%%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%fullfile(
127% EDIT FROM HERE
128%% load the initial scan
129[RootRoot,CamName]=fileparts(RootPath);
130RootRoot=fileparts(RootRoot);
131CalibFolder=fullfile(RootRoot,'EXP_INIT',CamName);
132File_init=fullfile(CalibFolder,'images.png.bed','Z_init.nc');
133Data_init=nc2struct(File_init);
134%% set of y positions
135
136nb_scan=400;% nbre of planes for a scan
137if nbfield_j<400
138    nb_scan=nbfield_j;
139end
140%ycalib=[-51 -1 49];% calibration planes
141y_scan=-51+0.25*(1:nb_scan);% transverse position given by the translating system: first view at y=-51, view 400 at y=+49
142coord_x=0.25:0.25:450;%% coord x in phys coordinates for final projection
143
144Mfiltre=ones(2,10)/20;%filter matrix for imnages
145
146%% calibration data and timing: read the ImaDoc files
147
148XmlData_A=xml2struct(fullfile(CalibFolder,'planeA.xml'));
149XmlData_B=xml2struct(fullfile(CalibFolder,'planeB.xml'));
150XmlData_C=xml2struct(fullfile(CalibFolder,'planeC.xml'));
151ycalib=[-51 -1 49];% the three y positions for calibration=
152fx(1)=XmlData_C.GeometryCalib.fx_fy(1);
153fx(2)=XmlData_B.GeometryCalib.fx_fy(1);
154fx(3)=XmlData_A.GeometryCalib.fx_fy(1);
155fy(1)=XmlData_C.GeometryCalib.fx_fy(2);
156fy(2)=XmlData_B.GeometryCalib.fx_fy(2);
157fy(3)=XmlData_A.GeometryCalib.fx_fy(2);
158Tx(1)=XmlData_C.GeometryCalib.Tx_Ty_Tz(1);
159Tx(2)=XmlData_B.GeometryCalib.Tx_Ty_Tz(1);
160Tx(3)=XmlData_A.GeometryCalib.Tx_Ty_Tz(1);
161Ty(1)=XmlData_C.GeometryCalib.Tx_Ty_Tz(2);
162Ty(2)=XmlData_B.GeometryCalib.Tx_Ty_Tz(2);
163Ty(3)=XmlData_A.GeometryCalib.Tx_Ty_Tz(2);
164R11(1)=XmlData_C.GeometryCalib.R(1,1);
165R11(2)=XmlData_B.GeometryCalib.R(1,1);
166R11(3)=XmlData_A.GeometryCalib.R(1,1);
167R12(1)=XmlData_C.GeometryCalib.R(1,2);
168R12(2)=XmlData_B.GeometryCalib.R(1,2);
169R12(3)=XmlData_A.GeometryCalib.R(1,2);
170R21(1)=XmlData_C.GeometryCalib.R(2,1);
171R21(2)=XmlData_B.GeometryCalib.R(2,1);
172R21(3)=XmlData_A.GeometryCalib.R(2,1);
173R22(1)=XmlData_C.GeometryCalib.R(2,2);
174R22(2)=XmlData_B.GeometryCalib.R(2,2);
175R22(3)=XmlData_A.GeometryCalib.R(2,2);
176pfx=polyfit(ycalib,fx,1);%get thfield_ie linear interpolation of each parameter of the three calibrations
177pfy=polyfit(ycalib,fy,1);
178pTx=polyfit(ycalib,Tx,1);
179pTy=polyfit(ycalib,Ty,1);
180p11=polyfit(ycalib,R11,1);
181p12=polyfit(ycalib,R12,1);
182p21=polyfit(ycalib,R21,1);
183p22=polyfit(ycalib,R22,1);
184%get the calibration parameters at each position y by interpolation of the 3 calibration parameters
185for img=1:nb_scan
186    Calib(img).fx_fy(1)=pfx(1)*y_scan(img)+pfx(2);
187    Calib(img).fx_fy(2)=pfy(1)*y_scan(img)+pfy(2);
188    Calib(img).Tx_Ty_Tz(1)=pTx(1)*y_scan(img)+pTx(2);
189    Calib(img).Tx_Ty_Tz(2)=pTy(1)*y_scan(img)+pTy(2);
190    Calib(img).Tx_Ty_Tz(3)=1;
191    Calib(img).R=zeros(3,3);
192    Calib(img).R(3,3)=-1;
193    Calib(img).R(1,2)=p12(1)*y_scan(img)+p12(2);
194    Calib(img).R(1,1)=p11(1)*y_scan(img)+p11(2);
195    Calib(img).R(1,2)=p12(1)*y_scan(img)+p12(2);
196    Calib(img).R(2,1)=p21(1)*y_scan(img)+p21(2);
197    Calib(img).R(2,2)=p22(1)*y_scan(img)+p22(2);
198end
199
200
201
202
203%% Load the init bed scan
204tic
205%filecell=reshape(filecell,nbfield_j,nbfield_i)
206% main loop
207for ifield=1:nbfield_i
208    for img=1:nbfield_j% loop on positions
209        if CheckVirtual
210            [FileName,FrameIndex]=index2filename(Param.FileSeries,i_series(ifield),j_series(img),nbfield_j);
211            InputFile=fullfile(RootPath,SubDir,FileName);
212        else
213            InputFile=fullfile_uvmat(RootPath,SubDir,RootFile,FileExt,NomType,FileIndex,ifield,[],img);
214            FrameIndex=1;
215        end
216        a=flipud(read_image(InputFile,'multimage',[],FrameIndex));
217        %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,:))
218        if img==1
219            [nby,nbx]=size(a);
220            x_ima=1:nbx;%image absissa in pixel coordinates
221            X_phys=zeros(nb_scan,nbx);
222            Z_phys=zeros(nb_scan,nbx);
223        end
224        % filtering
225            a=filter2(Mfiltre,a);%smoothed image
226        amean=mean(a,2);
227     [~,ind_max]=max(amean);% get the max of the image averaged along x, to restrict the search region
228    ind_range=max(1,ind_max-30):min(nby,ind_max+30);% search band to find the line
229    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
230
231 %       [~,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
232        z_ima=smooth(z_ima,20,'rloess');%smooth Z, the image index of max luminosity (dependning on x)
233        [X_phys(img,:),Z_phys(img,:)]=phys_XYZ(Calib(img),[],x_ima,z_ima');
234    end
235    disp(['last file of ' num2str(ifield)])
236     disp(FileName)
237     disp(FrameIndex)
238
239    %% interpolate on a regular grid
240    %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)
241    [X_m,Y_m]=meshgrid(coord_x,y_scan);
242    Y=y_scan'*ones(1,nbx);%initialisation of X, Y final topography map
243
244    Data.Z=griddata(X_phys,Y,Z_phys,X_m,Y_m);% dZ interpolated on the regular ph1ys grid X_m,Y_m
245    size(Data.Z)
246    size(Data_init.Z_init)
247    Data.dZ=Data.Z-Data_init.Z_init;
248
249    toc
250
251    % save netcdf
252    Data.ListVarName={'coord_x','y_scan','Z','dZ'};
253    Data.VarDimName={'coord_x','y_scan',{'y_scan','coord_x'},{'y_scan','coord_x'}};
254    Data.VarAttribute{1}.Role='coord_x';
255    Data.VarAttribute{1}.unit='cm';
256    Data.VarAttribute{2}.Role='coord_y';
257    Data.VarAttribute{2}.unit='cm';
258    Data.VarAttribute{3}.Role='scalar';
259    Data.VarAttribute{3}.unit='cm';
260        Data.VarAttribute{4}.Role='scalar';
261    Data.VarAttribute{4}.unit='cm';
262    Data.coord_x=coord_x;
263    Data.y_scan=y_scan;
264    struct2nc(fullfile(DirOut,['dZ_' num2str(ifield) '.nc']),Data)
265end
266
267%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
268function iy=get_max(a)% get the max with sub picel resolution
269[a_max,iy]=max(a);
270[Nby,Nbx]=size(a);
271for ind_x=1:Nbx
272    if iy(ind_x)>1 && iy(ind_x)<Nby
273        a_plus=a(iy(ind_x)+1,ind_x);
274        a_min=a(iy(ind_x)-1,ind_x);
275        denom=2*a_max(ind_x)-a_plus-a_min;
276        if denom >0
277            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
278        end
279    end
280end
Note: See TracBrowser for help on using the repository browser.