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

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

bed_scan added + bug repairs

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='off';% 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    %check the type of the existence and type of the first input file:
79    Param.IndexRange.last_i=Param.IndexRange.first_i;%keep only the first index in the series
80    if isfield(Param.IndexRange,'first_j')
81    Param.IndexRange.last_j=Param.IndexRange.first_j;
82    end
83    filecell=get_file_series(Param);
84    if ~exist(filecell{1,1},'file')
85        msgbox_uvmat('WARNING','the first input file does not exist')
86    else
87        FileInfo=get_file_info(filecell{1,1});
88        FileType=FileInfo.FileType;
89        if isempty(find(strcmp(FileType,{'image','multimage','mmreader','video'})))% =1 for images
90            msgbox_uvmat('ERROR',['bad input file type for ' mfilename ': an image is needed'])
91        end
92    end
93return
94end
95
96%%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%%
97%% read input parameters from an xml file if input is a file name (batch mode)
98ParamOut=[];
99RUNHandle=[];
100WaitbarHandle=[];
101checkrun=1;
102if ischar(Param)% case of batch: Param is the name of the xml file containing the input parameters
103    Param=xml2struct(Param);% read Param as input file (batch case)
104    checkrun=0;
105else% interactive mode in Matlab
106    hseries=findobj(allchild(0),'Tag','series');
107    RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series
108    WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series
109end
110
111%% root input file names and nomenclature type (cell arrays with one element)
112RootPath=Param.InputTable{1,1};
113
114
115%% directory for output files
116DirOut=fullfile(RootPath,[Param.OutputSubDir Param.OutputDirExt]);
117
118%% get the set of input file names (cell array filecell), and file indices
119[filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
120% filecell{iview,fileindex}: cell array representing the list of file names
121%        iview: line in the table corresponding to a given file series
122%        fileindex: file index within  the file series,
123% 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
124% i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices
125nbfield_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices)
126nbfield_i=size(i1_series{1},2); %nb of fields for the i index
127
128%% set of y positions 
129ycalib=[-51 -1 49];% calibration planes
130y_scan=-51+(100/400)*(i1_series{1}-1);% transverse position given by the translating system: first view at y=-51, view 400 at y=+49
131Mfiltre=ones(2,10)/20;%filter matrix for imnages
132
133%% calibration data and timing: read the ImaDoc files
134XmlData_A=xml2struct(fullfile(RootPath,'planeA.xml'));
135XmlData_B=xml2struct(fullfile(RootPath,'planeB.xml'));
136XmlData_C=xml2struct(fullfile(RootPath,'planeC.xml'));
137ycalib=[-51 -1 49];% the three y positions for calibration
138fx(1)=XmlData_A.GeometryCalib.fx_fy(1);
139fx(2)=XmlData_B.GeometryCalib.fx_fy(1);
140fx(3)=XmlData_C.GeometryCalib.fx_fy(1);
141fy(1)=XmlData_A.GeometryCalib.fx_fy(2);
142fy(2)=XmlData_B.GeometryCalib.fx_fy(2);
143fy(3)=XmlData_C.GeometryCalib.fx_fy(2);
144Tx(1)=XmlData_A.GeometryCalib.Tx_Ty_Tz(1);
145Tx(2)=XmlData_B.GeometryCalib.Tx_Ty_Tz(1);
146Tx(3)=XmlData_C.GeometryCalib.Tx_Ty_Tz(1);
147Ty(1)=XmlData_A.GeometryCalib.Tx_Ty_Tz(2);
148Ty(2)=XmlData_B.GeometryCalib.Tx_Ty_Tz(2);
149Ty(3)=XmlData_C.GeometryCalib.Tx_Ty_Tz(2);
150R11(1)=XmlData_A.GeometryCalib.R(1,1);
151R11(2)=XmlData_B.GeometryCalib.R(1,1);
152R11(3)=XmlData_C.GeometryCalib.R(1,1);
153R12(1)=XmlData_A.GeometryCalib.R(1,2);
154R12(2)=XmlData_B.GeometryCalib.R(1,2);
155R12(3)=XmlData_C.GeometryCalib.R(1,2);
156R21(1)=XmlData_A.GeometryCalib.R(2,1);
157R21(2)=XmlData_B.GeometryCalib.R(2,1);
158R21(3)=XmlData_C.GeometryCalib.R(2,1);
159R22(1)=XmlData_A.GeometryCalib.R(2,2);
160R22(2)=XmlData_B.GeometryCalib.R(2,2);
161R22(3)=XmlData_C.GeometryCalib.R(2,2);
162pfx=polyfit(ycalib,fx,1);%get the linear interpolation of each parameter of the three calibrations
163pfy=polyfit(ycalib,fy,1);
164pTx=polyfit(ycalib,Tx,1);
165pTy=polyfit(ycalib,Ty,1);
166p11=polyfit(ycalib,R11,1);
167p12=polyfit(ycalib,R12,1);
168p21=polyfit(ycalib,R21,1);
169p22=polyfit(ycalib,R22,1);
170%get the calibration parameters at each position y by interpolation of the 3 calibration parameters
171for img=1:nbfield_i
172    Calib(img).fx_fy(1)=pfx(1)*y_scan(img)+pfx(2);
173    Calib(img).fx_fy(2)=pfy(1)*y_scan(img)+pfy(2);
174    Calib(img).Tx_Ty_Tz(1)=pTx(1)*y_scan(img)+pTx(2);
175    Calib(img).Tx_Ty_Tz(2)=pTy(1)*y_scan(img)+pTy(2);
176    Calib(img).Tx_Ty_Tz(3)=1;
177    Calib(img).R=zeros(3,3);
178    Calib(img).R(3,3)=-1;
179    Calib(img).R(1,2)=p12(1)*y_scan(img)+p12(2);
180    Calib(img).R(1,1)=p11(1)*y_scan(img)+p11(2);
181    Calib(img).R(1,2)=p12(1)*y_scan(img)+p12(2);
182    Calib(img).R(2,1)=p21(1)*y_scan(img)+p21(2);
183    Calib(img).R(2,2)=p22(1)*y_scan(img)+p22(2);
184end
185
186%% check coincdence in time for several input file series
187%not relevant for this function
188
189%% coordinate transform or other user defined transform
190%not relevant for this function
191
192%%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
193 % EDIT FROM HERE
194
195%% Load the init bed scan
196tic
197nb_scan=10;
198for img=1:nb_scan
199     img
200    a=flipud(imread(filecell{1,img}));%image of the initial bed
201    if img==1
202        x=1:size(a,2);%image absissa in pixel coordinates
203    end
204    % filtering
205    a=filter2(Mfiltre,a);%smoothed image
206    [imax,iy]=max(a);% find the max along the first coordinate y, max values imax and the corresponding  y index iy along the first coordinate y
207    Z_s(img,:)=smooth(iy,40,'rloess');%smooth Z, the image index of max luminosity (dependning on x)
208    Yima=y_scan(img)*ones(size(x));%positions Y transformed into a vector
209    X_new(img,:)=phys_XYZ(Calib(img),x,Yima,1);
210    %X_new(:,img)=phys_scan(x,y(img));
211end
212
213toc
214
215[X,Y]=meshgrid(x,y_scan);
216
217
218%% Load the current bed scan
219for img=1:nb_scan
220   b=flipud(imread(filecell{2,img}));%image of the current bed
221    b=filter2(Mfiltre,b); % filtering
222    [imaxb,iyb]=max(b);
223    Z_sb(img,:)=smooth(iyb,20,'rloess');
224end
225
226%% bed change
227dZ=Z_s-Z_sb;% displacement between current position and initial
228dZ_new=zeros(nb_scan,size(dZ,2));
229for img=1:nb_scan 
230    Yima=y_scan(img)*ones(1,size(dZ,2));
231    [~,dZ_new(img,:)]=phys_XYZ(Calib(img),dZ(img,:),Yima,1);
232   % dZ_new(:,img)=phys_scanz(dZ(:,img),y(img));
233end
234
235
236%% PLOTS
237coord_x=X_new(end,1):0.1:X_new(end,end);
238[Y_m,X_m]=meshgrid(y_scan,coord_x);
239%Y_new=Y';
240dZ_mesh=griddata(X_new,Y,dZ_new,X_m,Y_m);
241
242if checkrun
243    figure(1)
244    hold on
245    plot(x,Z_s+700)
246    % xlim([0 4096])
247    % ylim([0 3000])
248   
249    figure(2)
250    hold on
251    plot(x,Z_sb+700)
252    xlim([0 4096])
253    ylim([0 3000])
254   
255    figure(3)
256    surfc(X_m,Y_m,dZ_mesh)
257    shading interp;
258    colorbar;
259    caxis([0 3]);
260   
261    figure
262    pcolor(X_m,Y_m,dZ_mesh);
263    colormap;
264    set(gca,'Xdir','reverse');
265    caxis([0 3]);
266    shading flat
267    hold on
268    colorbar
269    title('Dz')
270end
271
272%save(fullfile(DirOut,'18OS_f.mat'),'dZ','dZ_new','X','Y','Z_s','Z_sb','y')
273
274% save netcdf
275Data.ListVarName={'coord_x','coord_y','dZ'};
276Data.VarDimName={'coord_x','coord_y',{'coord_y','coord_x'}};
277Data.VarAttribute{1}.Role='coord_x';
278Data.VarAttribute{1}.unit='cm';
279Data.VarAttribute{2}.Role='coord_y';
280Data.VarAttribute{2}.unit='cm';
281Data.VarAttribute{3}.Role='scalar';
282Data.VarAttribute{3}.unit='cm';
283Data.coord_x=[coord_x(1) coord_x(end)];
284Data.coord_y=[y(1) y(end)];
285Data.dZ=dZ_mesh';
286struct2nc(fullfile(DirOut,'dZ.nc'),Data)
287
288%% gives the physical position x from the image position X and the physical position y of the laser plane
289function F=phys_scan(X,y)
290% linear fct of X whose coefficient depend on y in a quadratic way
291F=(9.4*10^(-7)*y.^2-3.09*10^(-4)*y+0.07).*X +(-0.001023*y.^2+0.469*y+186.9);
292
293%% gives the physical position z from the image position Z and the physical position y of the laser plane
294function Fz=phys_scanz(Z,y)
295% scale factor applied to Z depending on the physical position y of the laser plane
296Fz=(-1.4587*10^(-5)*y.^2 + 0.001072*y+0.0833).*Z; %+(-2.1*10^(-6)*x.^2+5.1*10^(-4)*x+0.0735).*Z;
297
298 
Note: See TracBrowser for help on using the repository browser.