| 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 | |
|---|
| 63 | function ParamOut=bed_scan_init (Param) |
|---|
| 64 | |
|---|
| 65 | %% set the input elements needed on the GUI series when the action is selected in the menu ActionName or InputTable refreshed |
|---|
| 66 | if 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=1; %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='NbSlice';% ='=NbInput': 1 output file per input file index, '=NbInput_i': 1 file per input file index i, '=NbSlice': 1 file per slice |
|---|
| 78 | return |
|---|
| 79 | end |
|---|
| 80 | |
|---|
| 81 | %%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%% |
|---|
| 82 | %% read input parameters from an xml file if input is a file name (batch mode) |
|---|
| 83 | ParamOut=[]; |
|---|
| 84 | RUNHandle=[]; |
|---|
| 85 | WaitbarHandle=[]; |
|---|
| 86 | checkrun=1; |
|---|
| 87 | if 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; |
|---|
| 90 | else% 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 |
|---|
| 94 | end |
|---|
| 95 | |
|---|
| 96 | %% root input file names and nomenclature type (cell arrays with one element) |
|---|
| 97 | RootPath=Param.InputTable{1,1}; |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | %% directory for output files |
|---|
| 101 | DirOut=fullfile(RootPath,[Param.OutputSubDir Param.OutputDirExt]); |
|---|
| 102 | |
|---|
| 103 | %% get the set of input file names (cell array filecell), and file indices |
|---|
| 104 | [filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param); |
|---|
| 105 | % filecell{iview,fileindex}: cell array representing the list of file names |
|---|
| 106 | % iview: line in the table corresponding to a given file series |
|---|
| 107 | % fileindex: file index within the file series, |
|---|
| 108 | % 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 |
|---|
| 109 | % i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices |
|---|
| 110 | %nbfield_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices) |
|---|
| 111 | nbfield_i=size(i1_series{1},2); %nb of fields for the i index |
|---|
| 112 | |
|---|
| 113 | %%%%%%%%%%%% END STANDARD PART %%%%%%%%%%%%fullfile( |
|---|
| 114 | % EDIT FROM HERE |
|---|
| 115 | |
|---|
| 116 | %% set of y positions |
|---|
| 117 | |
|---|
| 118 | nb_scan=400;% nbre of planes for a scan |
|---|
| 119 | if nbfield_i<400 |
|---|
| 120 | nb_scan=nbfield_i; |
|---|
| 121 | end |
|---|
| 122 | %ycalib=[-51 -1 49];% calibration planes |
|---|
| 123 | y_scan=-51+0.25*(1:nb_scan);% transverse position given by the translating system: first view at y=-51, view 400 at y=+49 |
|---|
| 124 | coord_x=0.25:0.25:450;%% coord x in phys coordinates for final projection |
|---|
| 125 | %%%%%%%% A GENERALISER pour plusieurs scans |
|---|
| 126 | Mfiltre=ones(2,10)/20;%filter matrix for imnages |
|---|
| 127 | |
|---|
| 128 | %% calibration data and timing: read the ImaDoc files |
|---|
| 129 | [RootRoot,CamName]=fileparts(RootPath); |
|---|
| 130 | RootRoot=fileparts(RootRoot); |
|---|
| 131 | CalibFolder=fullfile(RootRoot,'EXP_INIT',CamName); |
|---|
| 132 | XmlData_A=xml2struct(fullfile(CalibFolder,'planeA.xml')); |
|---|
| 133 | XmlData_B=xml2struct(fullfile(CalibFolder,'planeB.xml')); |
|---|
| 134 | XmlData_C=xml2struct(fullfile(CalibFolder,'planeC.xml')); |
|---|
| 135 | ycalib=[-51 -1 49];% the three y positions for calibration= |
|---|
| 136 | fx(1)=XmlData_C.GeometryCalib.fx_fy(1); |
|---|
| 137 | fx(2)=XmlData_B.GeometryCalib.fx_fy(1); |
|---|
| 138 | fx(3)=XmlData_A.GeometryCalib.fx_fy(1); |
|---|
| 139 | fy(1)=XmlData_C.GeometryCalib.fx_fy(2); |
|---|
| 140 | fy(2)=XmlData_B.GeometryCalib.fx_fy(2); |
|---|
| 141 | fy(3)=XmlData_A.GeometryCalib.fx_fy(2); |
|---|
| 142 | Tx(1)=XmlData_C.GeometryCalib.Tx_Ty_Tz(1); |
|---|
| 143 | Tx(2)=XmlData_B.GeometryCalib.Tx_Ty_Tz(1); |
|---|
| 144 | Tx(3)=XmlData_A.GeometryCalib.Tx_Ty_Tz(1); |
|---|
| 145 | Ty(1)=XmlData_C.GeometryCalib.Tx_Ty_Tz(2); |
|---|
| 146 | Ty(2)=XmlData_B.GeometryCalib.Tx_Ty_Tz(2); |
|---|
| 147 | Ty(3)=XmlData_A.GeometryCalib.Tx_Ty_Tz(2); |
|---|
| 148 | R11(1)=XmlData_C.GeometryCalib.R(1,1); |
|---|
| 149 | R11(2)=XmlData_B.GeometryCalib.R(1,1); |
|---|
| 150 | R11(3)=XmlData_A.GeometryCalib.R(1,1); |
|---|
| 151 | R12(1)=XmlData_C.GeometryCalib.R(1,2); |
|---|
| 152 | R12(2)=XmlData_B.GeometryCalib.R(1,2); |
|---|
| 153 | R12(3)=XmlData_A.GeometryCalib.R(1,2); |
|---|
| 154 | R21(1)=XmlData_C.GeometryCalib.R(2,1); |
|---|
| 155 | R21(2)=XmlData_B.GeometryCalib.R(2,1); |
|---|
| 156 | R21(3)=XmlData_A.GeometryCalib.R(2,1); |
|---|
| 157 | R22(1)=XmlData_C.GeometryCalib.R(2,2); |
|---|
| 158 | R22(2)=XmlData_B.GeometryCalib.R(2,2); |
|---|
| 159 | R22(3)=XmlData_A.GeometryCalib.R(2,2); |
|---|
| 160 | pfx=polyfit(ycalib,fx,1);%get thfield_ie linear interpolation of each parameter of the three calibrations |
|---|
| 161 | pfy=polyfit(ycalib,fy,1); |
|---|
| 162 | pTx=polyfit(ycalib,Tx,1); |
|---|
| 163 | pTy=polyfit(ycalib,Ty,1); |
|---|
| 164 | p11=polyfit(ycalib,R11,1); |
|---|
| 165 | p12=polyfit(ycalib,R12,1); |
|---|
| 166 | p21=polyfit(ycalib,R21,1); |
|---|
| 167 | p22=polyfit(ycalib,R22,1); |
|---|
| 168 | %get the calibration parameters at each position y by interpolation of the 3 calibration parameters |
|---|
| 169 | for img=1:nb_scan |
|---|
| 170 | Calib(img).fx_fy(1)=pfx(1)*y_scan(img)+pfx(2); |
|---|
| 171 | Calib(img).fx_fy(2)=pfy(1)*y_scan(img)+pfy(2); |
|---|
| 172 | Calib(img).Tx_Ty_Tz(1)=pTx(1)*y_scan(img)+pTx(2); |
|---|
| 173 | Calib(img).Tx_Ty_Tz(2)=pTy(1)*y_scan(img)+pTy(2); |
|---|
| 174 | Calib(img).Tx_Ty_Tz(3)=1; |
|---|
| 175 | Calib(img).R=zeros(3,3); |
|---|
| 176 | Calib(img).R(3,3)=-1; |
|---|
| 177 | Calib(img).R(1,2)=p12(1)*y_scan(img)+p12(2); |
|---|
| 178 | Calib(img).R(1,1)=p11(1)*y_scan(img)+p11(2); |
|---|
| 179 | Calib(img).R(1,2)=p12(1)*y_scan(img)+p12(2); |
|---|
| 180 | Calib(img).R(2,1)=p21(1)*y_scan(img)+p21(2); |
|---|
| 181 | Calib(img).R(2,2)=p22(1)*y_scan(img)+p22(2); |
|---|
| 182 | end |
|---|
| 183 | |
|---|
| 184 | %% check coincdence in time for several input file series |
|---|
| 185 | %not relevant for this function |
|---|
| 186 | |
|---|
| 187 | %% coordinate transform or other user defined transform |
|---|
| 188 | %not relevant for this function |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | %% Load the init bed scan |
|---|
| 193 | tic |
|---|
| 194 | |
|---|
| 195 | %for img=1:nbfield_i |
|---|
| 196 | for img=1:nb_scan%nbfield_i |
|---|
| 197 | img |
|---|
| 198 | a=flipud(imread(filecell{1,img+400}));%image of the initial bed [X_b_new(img,:),Z_b_new(img,:)]=phys_XYZ(Calib(img),[],x,Z_sb(img,:)) |
|---|
| 199 | if img==1 |
|---|
| 200 | [nby,nbx]=size(a); |
|---|
| 201 | x_ima=1:nbx;%image absissa in pixel coordinates |
|---|
| 202 | % X_new=zeros(nb_scan,size(a,2));% initialise the x po400sitions in phys coordinates |
|---|
| 203 | % Z_s=zeros(nb_scan,size(a,2));% initialise the image index of max luminosity (dependning on x) for the reference scan |
|---|
| 204 | |
|---|
| 205 | X_phys=zeros(nb_scan,nbx); |
|---|
| 206 | Z_phys=zeros(nb_scan,nbx); |
|---|
| 207 | end |
|---|
| 208 | % filtering |
|---|
| 209 | amean=mean(a,2); |
|---|
| 210 | [~,ind_max]=max(amean);% get the max of the image averaged along x, to restrict the search region |
|---|
| 211 | ind_range=max(1,ind_max-30):min(nby,ind_max+30);% search band to find the line |
|---|
| 212 | a=filter2(Mfiltre,a);%smoothed image |
|---|
| 213 | % [~,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 |
|---|
| 214 | |
|---|
| 215 | 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 |
|---|
| 216 | % z_ima=iy'; |
|---|
| 217 | z_ima=smooth(z_ima,20,'rloess');%smooth Z, the image index of max luminosity (dependning on x) |
|---|
| 218 | [X_phys(img,:),Z_phys(img,:)]=phys_XYZ(Calib(mod(img-1,nb_scan)+1),[],x_ima,z_ima'); |
|---|
| 219 | end |
|---|
| 220 | |
|---|
| 221 | %% interpolate on a regular grid |
|---|
| 222 | %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) |
|---|
| 223 | %y_scan_2=y_scan(1:400); |
|---|
| 224 | [X_m,Y_m]=meshgrid(coord_x,y_scan); |
|---|
| 225 | Y=y_scan'*ones(1,nbx);%initialisation of X, Y final topography map |
|---|
| 226 | Data.Z_init=griddata(X_phys,Y,Z_phys,X_m,Y_m);% dZ interpolated on the regular ph1ys grid X_m,Y_m |
|---|
| 227 | |
|---|
| 228 | %Data.Z_init=0.5*(Data.Z_init(1:nb_scan,:)+Data.Z_init(nb_scan+1:2*nb_scan,:)); |
|---|
| 229 | toc |
|---|
| 230 | |
|---|
| 231 | % save netcdf |
|---|
| 232 | Data.ListVarName={'coord_x','y_scan','Z_init'}; |
|---|
| 233 | Data.VarDimName={'coord_x','y_scan',{'y_scan','coord_x'}}; |
|---|
| 234 | Data.VarAttribute{1}.Role='coord_x'; |
|---|
| 235 | Data.VarAttribute{1}.unit='cm'; |
|---|
| 236 | Data.VarAttribute{2}.Role='coord_y'; |
|---|
| 237 | Data.VarAttribute{2}.unit='cm'; |
|---|
| 238 | Data.VarAttribute{3}.Role='scalar'; |
|---|
| 239 | Data.VarAttribute{3}.unit='cm'; |
|---|
| 240 | Data.coord_x=coord_x; |
|---|
| 241 | Data.y_scan=y_scan; |
|---|
| 242 | struct2nc(fullfile(DirOut,'Z_init.nc'),Data) |
|---|
| 243 | |
|---|
| 244 | %%%%% |
|---|
| 245 | function iy=get_max(a)% get the max with sub picel resolution |
|---|
| 246 | [a_max,iy]=max(a); |
|---|
| 247 | [Nby,Nbx]=size(a); |
|---|
| 248 | for ind_x=1:Nbx |
|---|
| 249 | if iy(ind_x)>1 && iy(ind_x)<Nby |
|---|
| 250 | a_plus=a(iy(ind_x)+1,ind_x); |
|---|
| 251 | a_min=a(iy(ind_x)-1,ind_x); |
|---|
| 252 | denom=2*a_max(ind_x)-a_plus-a_min; |
|---|
| 253 | if denom >0 |
|---|
| 254 | 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 |
|---|
| 255 | end |
|---|
| 256 | end |
|---|
| 257 | end |
|---|