source: trunk/src/geometry_calib.m @ 207

Last change on this file since 207 was 207, checked in by sommeria, 10 years ago

bug fixes to deal with volumes

File size: 63.4 KB
Line 
1%'geometry_calib': associated to the GUI geometry_calib to perform geometric calibration from a set of reference points
2%------------------------------------------------------------------------
3% function hgeometry_calib = geometry_calib(inputfile,pos)
4%
5%OUTPUT:
6% hgeometry_calib=current handles of the GUI geometry_calib.fig
7%
8%INPUT:
9% inputfile: (optional) name of an xml file containing coordinates of reference points
10% pos: (optional) 4 element vector setting the 'Position' of the GUI
11%
12%A%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
13%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
14%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
15%     This file is part of the toolbox UVMAT.
16%
17%     UVMAT is free software; you can redistribute it and/or modify
18%     it under the terms of the GNU General Public License as published by
19%     the Free Software Foundation; either version 2 of the License, or
20%     (at your option) any later version.
21%
22%     UVMAT is distributed in the hope that it will be useful,
23%     but WITHOUT ANY WARRANTY; without even the implied warranty of
24%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25%     GNU General Public License (file UVMAT/COPYING.txt) for more details.
26%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
27
28function varargout = geometry_calib(varargin)
29% GEOMETRY_CALIB M-file for geometry_calib.fig
30%      GEOMETRY_CALIB, by itself, creates a MenuCoord GEOMETRY_CALIB or raises the existing
31%      singleton*.
32%
33%      H = GEOMETRY_CALIB returns the handle to a MenuCoord GEOMETRY_CALIB or the handle to
34%      the existing singleton*.
35%
36%      GEOMETRY_CALIB('CALLBACK',hObject,eventData,handles,...) calls the local
37%      function named CALLBACK in GEOMETRY_CALIB.M with the given input arguments.
38%
39%      GEOMETRY_CALIB('Property','Value',...) creates a MenuCoord GEOMETRY_CALIB or raises the
40%      existing singleton*.  Starting from the left, property value pairs are
41%      applied to the GUI before geometry_calib_OpeningFunction gets called.  An
42%      unrecognized property name or invalid value makes property application
43%      stop.  All inputs are passed to geometry_calib_OpeningFcn via varargin.
44%
45%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
46%      instance to run (singleton)".
47%
48% See also: GUIDE, GUIDATA, GUIHANDLES
49
50% Edit the above text to modify the response to help geometry_calib
51
52% Last Modified by GUIDE v2.5 05-Oct-2010 13:47:00
53
54% Begin initialization code - DO NOT edit
55gui_Singleton = 1;
56gui_State = struct('gui_Name',       mfilename, ...
57                   'gui_Singleton',  gui_Singleton, ...
58                   'gui_OpeningFcn', @geometry_calib_OpeningFcn, ...
59                   'gui_OutputFcn',  @geometry_calib_OutputFcn, ...
60                   'gui_LayoutFcn',  [] , ...
61                   'gui_Callback',   []);
62if nargin && ischar(varargin{1}) && ~isempty(regexp(varargin{1},'_Callback','once'))
63    gui_State.gui_Callback = str2func(varargin{1});
64end
65
66if nargout
67    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
68else
69    gui_mainfcn(gui_State, varargin{:});
70end
71% End initialization code - DO NOT edit
72
73
74% --- Executes just before geometry_calib is made visible.
75%INPUT:
76%handles: handles of the geometry_calib interface elements
77% PlotHandles: set of handles of the elements contolling the plotting
78% parameters on the uvmat interface (obtained by 'get_plot_handle.m')
79%------------------------------------------------------------------------
80function geometry_calib_OpeningFcn(hObject, eventdata, handles,inputfile,pos)
81%------------------------------------------------------------------------
82% Choose default command line output for geometry_calib
83
84handles.output = hObject;
85
86% Update handles structure
87guidata(hObject, handles);
88set(hObject,'DeleteFcn',{@closefcn})%
89
90%set the position of the interface
91if exist('pos','var')&& length(pos)>=4
92%     %pos_gui=get(hObject,'Position');
93%     pos_gui(1)=pos(1);
94%     pos_gui(2)=pos(2);
95    set(hObject,'Position',pos);
96end
97
98%set menu of calibration options
99set(handles.calib_type,'String',{'rescale';'linear';'3D_linear';'3D_quadr';'3D_extrinsic'})
100inputxml='';
101if exist('inputfile','var')&& ~isempty(inputfile)
102    struct.XmlInputFile=inputfile;
103    [Pathsub,RootFile,field_count,str2,str_a,str_b,ext]=name2display(inputfile);
104    if ~strcmp(ext,'.xml')
105        inputfile=[fullfile(Pathsub,RootFile) '.xml'];%xml file corresponding to the input file
106    end
107    set(handles.ListCoord,'String',{'......'})
108    if exist(inputfile,'file')
109        Heading=loadfile(handles,inputfile);% load the point coordiantes existing in the xml file
110        if isfield(Heading,'Campaign')&& ischar(Heading.Campaign)
111            struct.Campaign=Heading.Campaign;
112        end
113    end   
114    set(hObject,'UserData',struct)
115end
116
117set(handles.ListCoord,'KeyPressFcn',{@key_press_fcn,handles})%set keyboard action function
118
119
120%------------------------------------------------------------------------
121% --- Outputs from this function are returned to the command line.
122function varargout = geometry_calib_OutputFcn(hObject, eventdata, handles)
123%------------------------------------------------------------------------
124% Get default command line output from handles structure
125varargout{1} = handles.output;
126varargout{2}=handles;
127%
128%------------------------------------------------------------------------
129% executed when closing: set the parent interface button to value 0
130function closefcn(gcbo,eventdata)
131%------------------------------------------------------------------------
132huvmat=findobj(allchild(0),'Name','uvmat');
133if ~isempty(huvmat)
134    handles=guidata(huvmat);
135%     set(handles.MenuMask,'enable','on')
136%     set(handles.MenuGrid,'enable','on')
137%     set(handles.MenuObject,'enable','on')
138%     set(handles.MenuEdit,'enable','on')
139%     set(handles.edit,'enable','on')
140    hobject=findobj(handles.axes3,'tag','calib_points');
141    if ~isempty(hobject)
142        delete(hobject)
143    end
144    hobject=findobj(handles.axes3,'tag','calib_marker');
145    if ~isempty(hobject)
146        delete(hobject)
147    end   
148end
149
150%------------------------------------------------------------------------
151% --- Executes on button press in calibrate_lin.
152function APPLY_Callback(hObject, eventdata, handles)
153%------------------------------------------------------------------------
154%read the current calibration points
155Coord_cell=get(handles.ListCoord,'String');
156Object=read_geometry_calib(Coord_cell);
157Coord=Object.Coord;
158% apply the calibration, whose type is selected in  handles.calib_type
159if ~isempty(Coord)
160    calib_cell=get(handles.calib_type,'String');
161    val=get(handles.calib_type,'Value');
162    GeometryCalib=feval(['calib_' calib_cell{val}],Coord,handles);
163else
164    msgbox_uvmat('ERROR','No calibration points, abort')
165    return
166end
167Z_plane=[];
168if ~isempty(Coord)
169    %check error
170    X=Coord(:,1);
171    Y=Coord(:,2);
172    Z=Coord(:,3);
173    x_ima=Coord(:,4);
174    y_ima=Coord(:,5);
175    [Xpoints,Ypoints]=px_XYZ(GeometryCalib,X,Y,Z);
176    GeometryCalib.ErrorRms(1)=sqrt(mean((Xpoints-x_ima).*(Xpoints-x_ima)));
177    [GeometryCalib.ErrorMax(1),index(1)]=max(abs(Xpoints-x_ima));
178    GeometryCalib.ErrorRms(2)=sqrt(mean((Ypoints-y_ima).*(Ypoints-y_ima)));
179    [GeometryCalib.ErrorMax(2),index(2)]=max(abs(Ypoints-y_ima));
180    [EM,ind_dim]=max(GeometryCalib.ErrorMax);
181    index=index(ind_dim);
182    %set the Z position of the reference plane used for calibration
183    if isequal(max(Z),min(Z))%Z constant
184        Z_plane=Z(1);
185        GeometryCalib.NbSlice=1;
186        GeometryCalib.SliceCoord=[0 0 Z_plane];
187    end
188end
189%set the coordinate unit
190unitlist=get(handles.CoordUnit,'String');
191unit=unitlist{get(handles.CoordUnit,'value')};
192GeometryCalib.CoordUnit=unit;
193%record the points
194GeometryCalib.SourceCalib.PointCoord=Coord;
195display_intrinsic(GeometryCalib,handles)%display calibration intrinsic parameters
196
197% Display extrinsinc parameters (rotation and translation of camera with  respect to the phys coordiantes)
198set(handles.Tx,'String',num2str(GeometryCalib.Tx_Ty_Tz(1),4))
199set(handles.Ty,'String',num2str(GeometryCalib.Tx_Ty_Tz(2),4))
200set(handles.Tz,'String',num2str(GeometryCalib.Tx_Ty_Tz(3),4))
201set(handles.Phi,'String',num2str(GeometryCalib.omc(1),4))
202set(handles.Theta,'String',num2str(GeometryCalib.omc(2),4))
203set(handles.Psi,'String',num2str(GeometryCalib.omc(3),4))
204
205% store the calibration data, by default in the xml file of the currently displayed image
206huvmat=findobj(allchild(0),'Name','uvmat');
207UvData=get(huvmat,'UserData');
208NbSlice_j=1;%default
209ZStart=Z_plane;
210ZEnd=Z_plane;
211volume_scan='n';
212if isfield(UvData,'XmlData')
213    UvData.XmlData
214    if isfield(UvData.XmlData,'TranslationMotor')
215        NbSlice_j=UvData.XmlData.TranslationMotor.Nbslice;
216        ZStart=UvData.XmlData.TranslationMotor.ZStart/10;
217        ZEnd=UvData.XmlData.TranslationMotor.ZEnd/10;
218        volume_scan='y';
219    end
220end
221hhuvmat=guidata(huvmat);%handles of elements in the GUI uvmat
222RootPath='';
223RootFile='';
224if ~isempty(hhuvmat.RootPath)&& ~isempty(hhuvmat.RootFile)
225    testhandle=1;
226    RootPath=get(hhuvmat.RootPath,'String');
227    RootFile=get(hhuvmat.RootFile,'String');
228    filebase=fullfile(RootPath,RootFile);
229    outputfile=[filebase '.xml'];%xml file associated with the currently displayed image
230else
231    question={'save the calibration data and point coordinates in'};
232    def={fullfile(RootPath,'ObjectCalib.xml')};
233    options.Resize='on';
234    answer=inputdlg(question,'save average in a new file',1,def,options);
235    outputfile=answer{1};
236end
237answer=msgbox_uvmat('INPUT_Y-N',{[outputfile ' updated with calibration data'];...
238    ['Error rms (along x,y)=' num2str(GeometryCalib.ErrorRms) ' pixels'];...
239    ['Error max (along x,y)=' num2str(GeometryCalib.ErrorMax) ' pixels']});
240
241%% record the calibration parameters and display the current image of uvmat in the new phys coordinates
242if strcmp(answer,'Yes')
243    if strcmp(calib_cell{val}(1:2),'3D')%set the plane position for 3D (projection) calibration
244       input_key={'Z (first position)','Z (last position)','Z (water surface)', 'refractive index','NbSlice','volume scan (y/n)','tilt angle'};
245       input_val=[{num2str(ZEnd)} {num2str(ZStart)} {num2str(ZStart)} {'1.33'} num2str(NbSlice_j) {volume_scan} {'0'}];
246        answer=inputdlg(input_key,'slice position(s)',ones(1,7), input_val,'on');
247        %answer_1=msgbox_uvmat('INPUT_TXT',' Z= ',num2str(Z_plane));
248        GeometryCalib.NbSlice=str2double(answer{5});
249        GeometryCalib.VolumeScan=answer{6};
250        if isempty(answer)
251            Z_plane=0; %default
252        else
253            Z_plane=linspace(str2double(answer{1}),str2double(answer{2}),GeometryCalib.NbSlice);
254        end     
255        GeometryCalib.SliceCoord=Z_plane'*[0 0 1];
256        GeometryCalib.SliceAngle=answer{7}*ones(GeometryCalib.NbSlice,1)*[0 1 0];%rotation around y axis (to generalise)
257        GeometryCalib.InterfaceCoord=[0 0 str2double(answer{3})];
258        GeometryCalib.RefractionIndex=str2double(answer{4});     
259    end
260    errormsg=update_imadoc(GeometryCalib,outputfile);% introduce the calibration data in the xml file
261    if ~strcmp(errormsg,'')
262        msgbox_uvmat('ERROR',errormsg);
263    end
264   
265    %display image with new calibration in the currently opened uvmat interface
266    hhh=findobj(hhuvmat.axes3,'Tag','calib_marker');% delete calib points and markers
267    if ~isempty(hhh)
268        delete(hhh);     
269    end
270    hhh=findobj(hhuvmat.axes3,'Tag','calib_points');
271    if ~isempty(hhh)
272        delete(hhh);
273    end
274    set(hhuvmat.FixLimits,'Value',0)% put FixedLimits option to 'off'
275    set(hhuvmat.FixLimits,'BackgroundColor',[0.7 0.7 0.7])
276    UserData=get(handles.geometry_calib,'UserData');
277    UserData.XmlInputFile=outputfile;%save the current xml file name
278    set(handles.geometry_calib,'UserData',UserData)
279    uvmat('RootPath_Callback',hObject,eventdata,hhuvmat); %file input with xml reading  in uvmat, show the image in phys coordinates
280    MenuPlot_Callback(hObject, eventdata, handles)
281    set(handles.ListCoord,'Value',index)% indicate in the list the point with max deviation (possible mistake)
282    ListCoord_Callback(hObject, eventdata, handles)
283    figure(handles.geometry_calib)
284end
285
286%------------------------------------------------------------------
287% --- Executes on button press in calibrate_lin.
288
289function REPLICATE_Callback(hObject, eventdata, handles)
290%------------------------------------------------------------------------
291
292%% Apply calibration
293calib_cell=get(handles.calib_type,'String'); %#ok<NASGU>
294val=get(handles.calib_type,'Value'); %#ok<NASGU>
295
296%read the current calibration points
297Coord_cell=get(handles.ListCoord,'String');
298Object=read_geometry_calib(Coord_cell);
299Coord=Object.Coord;
300
301% apply the calibration, whose type is selected in  handles.calib_type
302if ~isempty(Coord)
303    calib_cell=get(handles.calib_type,'String');
304    val=get(handles.calib_type,'Value');
305    GeometryCalib=feval(['calib_' calib_cell{val}],Coord,handles);
306else
307    msgbox_uvmat('ERROR','No calibration points, abort')
308    return
309end
310
311if ~isempty(Coord)
312    %check error
313    X=Coord(:,1);
314    Y=Coord(:,2);
315    Z=Coord(:,3);
316    x_ima=Coord(:,4);
317    y_ima=Coord(:,5);
318    [Xpoints,Ypoints]=px_XYZ(GeometryCalib,X,Y,Z);
319    GeometryCalib.ErrorRms(1)=sqrt(mean((Xpoints-x_ima).*(Xpoints-x_ima)));
320    [GeometryCalib.ErrorMax(1),index(1)]=max(abs(Xpoints-x_ima));
321    GeometryCalib.ErrorRms(2)=sqrt(mean((Ypoints-y_ima).*(Ypoints-y_ima)));
322    [GeometryCalib.ErrorMax(2),index(2)]=max(abs(Ypoints-y_ima));
323    [EM,ind_dim]=max(GeometryCalib.ErrorMax);
324%     index=index(ind_dim);
325    %set the Z position of the reference plane used for calibration
326    Z_plane=[];
327    if isequal(max(Z),min(Z))
328        Z_plane=Z(1);
329    end
330    answer_1=msgbox_uvmat('INPUT_TXT',' Z= ',num2str(Z_plane));
331    Z_plane=str2double(answer_1);
332    GeometryCalib.NbSlice=1;
333    GeometryCalib.SliceCoord=[0 0 Z_plane];
334    %set the coordinate unit
335    unitlist=get(handles.CoordUnit,'String');
336    unit=unitlist{get(handles.CoordUnit,'value')};
337    GeometryCalib.CoordUnit=unit;
338    %record the points
339    GeometryCalib.SourceCalib.PointCoord=Coord;
340end
341
342%% display calibration paprameters
343display_intrinsic(GeometryCalib,handles)%display calibration intrinsic parameters
344
345% Display extrinsinc parameters (rotation and translation of camera with  respect to the phys coordiantes)
346set(handles.Tx,'String',num2str(GeometryCalib.Tx_Ty_Tz(1),4))
347set(handles.Ty,'String',num2str(GeometryCalib.Tx_Ty_Tz(2),4))
348set(handles.Tz,'String',num2str(GeometryCalib.Tx_Ty_Tz(3),4))
349set(handles.Phi,'String',num2str(GeometryCalib.omc(1),4))
350set(handles.Theta,'String',num2str(GeometryCalib.omc(2),4))
351set(handles.Psi,'String',num2str(GeometryCalib.omc(3),4))
352
353%% open the GUI dataview
354h_dataview=findobj(allchild(0),'name','dataview');
355if ~isempty(h_dataview)
356    delete(h_dataview)
357end
358CalibData=get(handles.geometry_calib,'UserData');%read the calibration image source on the interface userdata
359InputFile='';
360if isfield(CalibData,'XmlInputFile')
361    InputDir=fileparts(CalibData.XmlInputFile);
362    [InputDir,DirName]=fileparts(InputDir);
363end
364SubCampaignTest='n'; %default
365testup=0;
366if isfield(CalibData,'SubCampaign')
367    SubCampaignTest='y';
368    dir_ref=CalibData.SubCampaign;
369    testup=1;
370elseif isfield(CalibData,'Campaign')
371    dir_ref=CalibData.Campaign;
372    testup=1;
373end
374while testup
375    [InputDir,DirName]=fileparts(InputDir);
376    if strcmp(DirName,dir_ref)
377        break
378    end
379end
380InputDir=fullfile(InputDir,DirName);
381answer=msgbox_uvmat('INPUT_TXT','Campaign ?',InputDir);
382if strcmp(answer,'Cancel')
383    return
384end
385
386dataview(answer,SubCampaignTest,GeometryCalib);
387       
388%     if isfield(Heading,'Device') && isequal([filename ext],Heading.Device)
389%         [XmlInput,filename,ext]=fileparts(XmlInput);
390%         Device=Heading.Device;
391%     end
392%     if isfield(Heading,'Experiment') && isequal([filename ext],Heading.Experiment)
393%         [PP,filename,ext]=fileparts(XmlInput);
394%     end
395%     testinput=0;
396%     if isfield(Heading,'SubCampaign') && isequal([filename ext],Heading.SubCampaign)
397%         SubCampaignTest='y';
398%         testinput=1;
399%     elseif isfield(Heading,'Campaign') && isequal([filename ext],Heading.Campaign)
400%         testinput=1;
401% %     end
402% end
403% if ~testinput
404%     filename='PROJETS';%default
405%     if isfield(CalibData,'XmlInputFile')
406%          [pp,filename]=fileparts(CalibData.XmlInputFile);
407%     end
408%     while ~isequal(filename,'PROJETS') && numel(filename)>1
409%         filename_1=filename;
410%         pp_1=pp;
411%         [pp,filename]=fileparts(pp);
412%     end
413%     XmlInput=fullfile(pp_1,filename_1);
414%     testinput=1;
415% end
416% if testinput
417%     outcome=dataview(XmlInput,SubCampaignTest,GeometryCalib);
418% end
419
420%------------------------------------------------------------------------
421% determine the parameters for a calibration by an affine function (rescaling and offset, no rotation)
422function GeometryCalib=calib_rescale(Coord,handles)
423%------------------------------------------------------------------------
424X=Coord(:,1);
425Y=Coord(:,2);% Z not used
426x_ima=Coord(:,4);
427y_ima=Coord(:,5);
428[px,sx]=polyfit(X,x_ima,1);
429[py,sy]=polyfit(Y,y_ima,1);
430T_x=px(2);
431T_y=py(2);
432GeometryCalib.CalibrationType='rescale';
433GeometryCalib.fx_fy=[px(1) py(1)];%.fx_fy corresponds to pxcm along x and y
434GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
435GeometryCalib.Tx_Ty_Tz=[px(2)/px(1) py(2)/py(1) 1];
436%GeometryCalib.R=[1,0,0;0,1,0;0,0,0];
437GeometryCalib.omc=[0 0 0];
438
439%------------------------------------------------------------------------
440% determine the parameters for a calibration by a linear transform matrix (rescale and rotation)
441function GeometryCalib=calib_linear(Coord,handles) %TO UPDATE
442%------------------------------------------------------------------------
443X=Coord(:,1);
444Y=Coord(:,2);% Z not used
445x_ima=Coord(:,4);
446y_ima=Coord(:,5);
447XY_mat=[ones(size(X)) X Y];
448a_X1=XY_mat\x_ima; %transformation matrix for X
449% x1=XY_mat*a_X1;%reconstruction
450% err_X1=max(abs(x1-x_ima));%error
451a_Y1=XY_mat\y_ima;%transformation matrix for X
452% y1=XY_mat*a_Y1;
453% err_Y1=max(abs(y1-y_ima));%error
454% R=[a_X1(2),a_X1(3),0;a_Y1(2),a_Y1(3),0;0,0,1];
455R=[a_X1(2),a_X1(3);a_Y1(2),a_Y1(3)];
456norm=abs(det(R));
457GeometryCalib.CalibrationType='linear';
458GeometryCalib.fx_fy(1)=sqrt((a_X1(2)/a_Y1(3))*norm);
459GeometryCalib.fx_fy(2)=(a_Y1(3)/a_X1(2))*GeometryCalib.fx_fy(1);
460GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
461GeometryCalib.Tx_Ty_Tz=[a_X1(1) a_Y1(1) 1];
462R(1,:)=R(1,:)/GeometryCalib.fx_fy(1);
463R(2,:)=R(2,:)/GeometryCalib.fx_fy(2);
464R=[R;[0 0]];
465GeometryCalib.R=[R [0;0;1]];
466GeometryCalib.omc=(180/pi)*[acos(GeometryCalib.R(1,1)) 0 0];
467%------------------------------------------------------------------------
468% determine the tsai parameters for a view normal to the grid plane
469% NOT USED
470function GeometryCalib=calib_normal(Coord,handles)
471%------------------------------------------------------------------------
472Calib.f1=str2num(get(handles.fx,'String'));
473Calib.f2=str2num(get(handles.fy,'String'));
474Calib.k=str2num(get(handles.kc,'String'));
475Calib.Cx=str2num(get(handles.Cx,'String'));
476Calib.Cy=str2num(get(handles.Cy,'String'));
477%default
478if isempty(Calib.f1)
479    Calib.f1=25/0.012;
480end
481if isempty(Calib.f2)
482    Calib.f2=25/0.012;
483end
484if isempty(Calib.k)
485    Calib.k=0;
486end
487if isempty(Calib.Cx)||isempty(Calib.Cy)
488    huvmat=findobj(allchild(0),'Tag','uvmat');
489    hhuvmat=guidata(huvmat);
490    Calib.Cx=str2num(get(hhuvmat.npx,'String'))/2;
491    Calib.Cx=str2num(get(hhuvmat.npy,'String'))/2;
492end   
493%tsai parameters
494Calib.dpx=0.012;%arbitrary
495Calib.dpy=0.012;
496Calib.sx=Calib.f1*Calib.dpx/(Calib.f2*Calib.dpy);
497Calib.f=Calib.f2*Calib.dpy;
498Calib.kappa1=Calib.k/(Calib.f*Calib.f);
499
500%initial guess
501X=Coord(:,1);
502Y=Coord(:,2);
503Zmean=mean(Coord(:,3));
504x_ima=Coord(:,4)-Calib.Cx;
505y_ima=Coord(:,5)-Calib.Cy;
506XY_mat=[ones(size(X)) X Y];
507a_X1=XY_mat\x_ima; %transformation matrix for X
508a_Y1=XY_mat\y_ima;%transformation matrix for Y
509R=[a_X1(2),a_X1(3),0;a_Y1(2),a_Y1(3),0;0,0,-1];% rotation+ z axis reversal (upward)
510norm=sqrt(det(-R));
511calib_param(1)=0;% quadratic distortion
512calib_param(2)=a_X1(1);
513calib_param(3)=a_Y1(1);
514calib_param(4)=Calib.f/(norm*Calib.dpx)-R(3,3)*Zmean;
515calib_param(5)=angle(a_X1(2)+1i*a_X1(3));
516display(['initial guess=' num2str(calib_param)])
517
518%optimise the parameters: minimisation of error
519calib_param = fminsearch(@(calib_param) error_calib(calib_param,Calib,Coord),calib_param);
520
521GeometryCalib.CalibrationType='tsai_normal';
522GeometryCalib.focal=Calib.f;
523GeometryCalib.dpx_dpy=[Calib.dpx Calib.dpy];
524GeometryCalib.Cx_Cy=[Calib.Cx Calib.Cy];
525GeometryCalib.sx=Calib.sx;
526GeometryCalib.kappa1=calib_param(1);
527GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
528GeometryCalib.Tx_Ty_Tz=[calib_param(2) calib_param(3) calib_param(4)];
529alpha=calib_param(5);
530GeometryCalib.R=[cos(alpha) sin(alpha) 0;-sin(alpha) cos(alpha) 0;0 0 -1];
531
532%------------------------------------------------------------------------
533function GeometryCalib=calib_3D_linear(Coord,handles)
534%------------------------------------------------------------------
535path_uvmat=which('uvmat');% check the path detected for source file uvmat
536path_UVMAT=fileparts(path_uvmat); %path to UVMAT
537huvmat=findobj(allchild(0),'Tag','uvmat');
538hhuvmat=guidata(huvmat);
539coord_files=get(handles.coord_files,'String');
540if ischar(coord_files)
541    coord_files={coord_files};
542end
543if isempty(coord_files{1}) || isequal(coord_files,{''})
544    coord_files={};
545end
546%retrieve the calibration points stored in the files listed in the popup list coord_files
547x_1=Coord(:,4:5)';%px coordinates of the ref points
548nx=str2num(get(hhuvmat.npx,'String'));
549ny=str2num(get(hhuvmat.npy,'String'));
550x_1(2,:)=ny-x_1(2,:);%reverse the y image coordinates
551X_1=Coord(:,1:3)';%phys coordinates of the ref points
552n_ima=numel(coord_files)+1;
553if ~isempty(coord_files)
554    msgbox_uvmat('CONFIRMATION',['The xy coordinates of the calibration points in ' num2str(n_ima) ' planes will be used'])
555    for ifile=1:numel(coord_files)
556    t=xmltree(coord_files{ifile});
557    s=convert(t);%convert to matlab structure
558        if isfield(s,'GeometryCalib')
559            if isfield(s.GeometryCalib,'SourceCalib')
560                if isfield(s.GeometryCalib.SourceCalib,'PointCoord')
561                PointCoord=s.GeometryCalib.SourceCalib.PointCoord;
562                Coord_file=zeros(length(PointCoord),5);%default
563                for i=1:length(PointCoord)
564                    line=str2num(PointCoord{i});
565                    Coord_file(i,4:5)=line(4:5);%px x
566                    Coord_file(i,1:3)=line(1:3);%phys x
567                end
568                eval(['x_' num2str(ifile+1) '=Coord_file(:,4:5)'';']);
569                eval(['x_' num2str(ifile+1) '(2,:)=ny-x_' num2str(ifile+1) '(2,:);' ]);
570                eval(['X_' num2str(ifile+1) '=Coord_file(:,1:3)'';']);
571                end
572            end
573        end
574    end
575end
576n_ima=numel(coord_files)+1;
577est_dist=[0;0;0;0;0];
578est_aspect_ratio=0;
579est_fc=[1;1];
580%fc=[25;25]/0.012;
581center_optim=0;
582run(fullfile(path_UVMAT,'toolbox_calib','go_calib_optim'));
583GeometryCalib.CalibrationType='3D_linear';
584GeometryCalib.fx_fy=fc';
585%GeometryCalib.focal=fc(2);
586%GeometryCalib.dpx_dpy=[1 1];
587GeometryCalib.Cx_Cy=cc';
588%GeometryCalib.sx=fc(1)/fc(2);
589GeometryCalib.kc=kc(1);
590%GeometryCalib.kappa1=-kc(1)/fc(2)^2;
591GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
592GeometryCalib.Tx_Ty_Tz=Tc_1';
593GeometryCalib.R=Rc_1;
594GeometryCalib.R(2,1:3)=-GeometryCalib.R(2,1:3);%inversion of the y image coordinate
595GeometryCalib.Tx_Ty_Tz(2)=-GeometryCalib.Tx_Ty_Tz(2);%inversion of the y image coordinate
596GeometryCalib.Cx_Cy(2)=ny-GeometryCalib.Cx_Cy(2);%inversion of the y image coordinate
597GeometryCalib.omc=(180/pi)*omc_1;%angles in degrees
598GeometryCalib.ErrorRMS=[];
599GeometryCalib.ErrorMax=[];
600
601%------------------------------------------------------------------------
602function GeometryCalib=calib_3D_quadr(Coord,handles)
603%------------------------------------------------------------------
604
605path_uvmat=which('uvmat');% check the path detected for source file uvmat
606path_UVMAT=fileparts(path_uvmat); %path to UVMAT
607huvmat=findobj(allchild(0),'Tag','uvmat');
608hhuvmat=guidata(huvmat);
609% check_cond=0;
610coord_files=get(handles.coord_files,'String');
611if ischar(coord_files)
612    coord_files={coord_files};
613end
614if isempty(coord_files{1}) || isequal(coord_files,{''})
615    coord_files={};
616end
617
618%retrieve the calibration points stored in the files listed in the popup list coord_files
619x_1=Coord(:,4:5)';%px coordinates of the ref points
620nx=str2num(get(hhuvmat.npx,'String'));
621ny=str2num(get(hhuvmat.npy,'String'));
622x_1(2,:)=ny-x_1(2,:);%reverse the y image coordinates
623X_1=Coord(:,1:3)';%phys coordinates of the ref points
624n_ima=numel(coord_files)+1;
625if ~isempty(coord_files)
626    msgbox_uvmat('CONFIRMATION',['The xy coordinates of the calibration points in ' num2str(n_ima) ' planes will be used'])
627    for ifile=1:numel(coord_files)
628    t=xmltree(coord_files{ifile});
629    s=convert(t);%convert to matlab structure
630        if isfield(s,'GeometryCalib')
631            if isfield(s.GeometryCalib,'SourceCalib')
632                if isfield(s.GeometryCalib.SourceCalib,'PointCoord')
633                PointCoord=s.GeometryCalib.SourceCalib.PointCoord;
634                Coord_file=zeros(length(PointCoord),5);%default
635                for i=1:length(PointCoord)
636                    line=str2num(PointCoord{i});
637                    Coord_file(i,4:5)=line(4:5);%px x
638                    Coord_file(i,1:3)=line(1:3);%phys x
639                end
640                eval(['x_' num2str(ifile+1) '=Coord_file(:,4:5)'';']);
641                eval(['x_' num2str(ifile+1) '(2,:)=ny-x_' num2str(ifile+1) '(2,:);' ]);
642                eval(['X_' num2str(ifile+1) '=Coord_file(:,1:3)'';']);
643                end
644            end
645        end
646    end
647end
648n_ima=numel(coord_files)+1;
649est_dist=[1;0;0;0;0];
650est_aspect_ratio=1;
651%est_fc=[0;0];
652%fc=[25;25]/0.012;
653center_optim=0;
654run(fullfile(path_UVMAT,'toolbox_calib','go_calib_optim'));
655
656GeometryCalib.CalibrationType='3D_quadr';
657GeometryCalib.fx_fy=fc';
658%GeometryCalib.focal=fc(2);
659%GeometryCalib.dpx_dpy=[1 1];
660GeometryCalib.Cx_Cy=cc';
661%GeometryCalib.sx=fc(1)/fc(2);
662GeometryCalib.kc=kc(1);
663%GeometryCalib.kappa1=-kc(1)/fc(2)^2;
664GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
665GeometryCalib.Tx_Ty_Tz=Tc_1';
666GeometryCalib.R=Rc_1;
667GeometryCalib.R(2,1:3)=-GeometryCalib.R(2,1:3);%inversion of the y image coordinate
668GeometryCalib.Tx_Ty_Tz(2)=-GeometryCalib.Tx_Ty_Tz(2);%inversion of the y image coordinate
669GeometryCalib.Cx_Cy(2)=ny-GeometryCalib.Cx_Cy(2);%inversion of the y image coordinate
670GeometryCalib.omc=(180/pi)*omc_1;%angles in degrees
671GeometryCalib.ErrorRMS=[];
672GeometryCalib.ErrorMax=[];
673
674
675%------------------------------------------------------------------------
676function GeometryCalib=calib_3D_extrinsic(Coord,handles)
677%------------------------------------------------------------------
678path_uvmat=which('geometry_calib');% check the path detected for source file uvmat
679path_UVMAT=fileparts(path_uvmat); %path to UVMAT
680x_1=double(Coord(:,4:5)');%image coordiantes
681X_1=double(Coord(:,1:3)');% phys coordinates
682huvmat=findobj(allchild(0),'Tag','uvmat');
683hhuvmat=guidata(huvmat);
684ny=str2double(get(hhuvmat.npy,'String'));
685x_1(2,:)=ny-x_1(2,:);%reverse the y image coordinates
686n_ima=1;
687GeometryCalib.CalibrationType='3D_extrinsic';
688GeometryCalib.fx_fy(1)=str2num(get(handles.fx,'String'));
689GeometryCalib.fx_fy(2)=str2num(get(handles.fy,'String'));
690GeometryCalib.Cx_Cy(1)=str2num(get(handles.Cx,'String'));
691GeometryCalib.Cx_Cy(2)=str2num(get(handles.Cy,'String'));
692GeometryCalib.kc=str2num(get(handles.kc,'String'));
693fct_path=fullfile(path_UVMAT,'toolbox_calib');
694addpath(fct_path)
695GeometryCalib.Cx_Cy(2)=ny-GeometryCalib.Cx_Cy(2);%reverse Cx_Cy(2) for calibration (inversion of px ordinate)
696% [omc1,Tc1,Rc1,H,x,ex,JJ] = compute_extrinsic(x_1,X_1,...
697%     [Calib.f Calib.f*Calib.sx]',...
698%     [Calib.Cx Calib.Cy]',...
699%     [-Calib.kappa1*Calib.f^2 0 0 0 0]);
700[omc,Tc1,Rc1,H,x,ex,JJ] = compute_extrinsic(x_1,X_1,...
701    (GeometryCalib.fx_fy)',GeometryCalib.Cx_Cy',[GeometryCalib.kc 0 0 0 0]);
702rmpath(fct_path);
703GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
704GeometryCalib.Tx_Ty_Tz=Tc1';
705%inversion of z axis
706GeometryCalib.R=Rc1;
707GeometryCalib.R(2,1:3)=-GeometryCalib.R(2,1:3);%inversion of the y image coordinate
708GeometryCalib.Tx_Ty_Tz(2)=-GeometryCalib.Tx_Ty_Tz(2);%inversion of the y image coordinate
709GeometryCalib.Cx_Cy(2)=ny-GeometryCalib.Cx_Cy(2);%inversion of the y image coordinate
710GeometryCalib.omc=(180/pi)*omc';
711%GeometryCalib.R(3,1:3)=-GeometryCalib.R(3,1:3);%inversion for z upward
712
713
714
715%------------------------------------------------------------------------
716%function GeometryCalib=calib_tsai_heikkila(Coord)
717% TEST: NOT IMPLEMENTED
718%------------------------------------------------------------------
719% path_uvmat=which('uvmat');% check the path detected for source file uvmat
720% path_UVMAT=fileparts(path_uvmat); %path to UVMAT
721% path_calib=fullfile(path_UVMAT,'toolbox_calib_heikkila');
722% addpath(path_calib)
723% npoints=size(Coord,1);
724% Coord(:,1:3)=10*Coord(:,1:3);
725% Coord=[Coord zeros(npoints,2) -ones(npoints,1)];
726% [par,pos,iter,res,er,C]=cacal('dalsa',Coord);
727% GeometryCalib.CalibrationType='tsai';
728% GeometryCalib.focal=par(2);
729
730
731%--------------------------------------------------------------------------
732function GeometryCalib=calib_tsai(Coord,handles)% OBSOLETE: old version using gauthier's bianry ccal_fo
733% NOT USED
734%------------------------------------------------------------------------
735%TSAI
736path_uvmat=which('uvmat');% check the path detected for source file uvmat
737path_UVMAT=fileparts(path_uvmat); %path to UVMAT
738xmlfile=fullfile(path_UVMAT,'PARAM.xml');%name of the file containing names of binary executables
739if exist(xmlfile,'file')
740    t=xmltree(xmlfile);% read the (xml) file containing names of binary executables
741    sparam=convert(t);% convert to matlab structure
742end
743if ~isfield(sparam,'GeometryCalibBin')
744    msgbox_uvmat('ERROR',['calibration program <GeometryCalibBin> undefined in parameter file ' xmlfile])
745    return
746end
747Tsai_exe=sparam.GeometryCalibBin;
748if ~exist(Tsai_exe,'file')%the binary is defined in /bin, default setting
749     Tsai_exe=fullfile(path_UVMAT,Tsai_exe);
750end
751if ~exist(Tsai_exe,'file')
752    msgbox_uvmat('ERROR',['calibration program ' sparam.GeometryCalibBin ' defined in PARAM.xml does not exist'])
753    return
754end
755
756textcoord=num2str(Coord,4);
757dlmwrite('t.txt',textcoord,''); 
758% ['!' Tsai_exe ' -fx 0 -fy t.txt']
759eval(['!' Tsai_exe ' -f t.txt > tsaicalib.log']);
760if ~exist('calib.dat','file')
761    msgbox_uvmat('ERROR','no output from calibration program Tsai_exe: possibly too few points')
762end
763calibdat=dlmread('calib.dat');
764delete('calib.dat')
765%delete('t.txt')
766GeometryCalib.CalibrationType='tsai';
767GeometryCalib.focal=calibdat(10);
768GeometryCalib.dpx_dpy=[calibdat(5) calibdat(6)];
769GeometryCalib.Cx_Cy=[calibdat(7) calibdat(8)];
770GeometryCalib.sx=calibdat(9);
771GeometryCalib.kappa1=calibdat(11);
772GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
773GeometryCalib.Tx_Ty_Tz=[calibdat(12) calibdat(13) calibdat(14)];
774Rx_Ry_Rz=calibdat(15:17);
775sa = sin(Rx_Ry_Rz(1)) ;
776ca=cos(Rx_Ry_Rz(1));
777sb=sin(Rx_Ry_Rz(2));
778cb =cos(Rx_Ry_Rz(2));
779sg =sin(Rx_Ry_Rz(3));
780cg =cos(Rx_Ry_Rz(3));
781r1 = cb * cg;
782r2 = cg * sa * sb - ca * sg;
783r3 = sa * sg + ca * cg * sb;
784r4 = cb * sg;
785r5 = sa * sb * sg + ca * cg;
786r6 = ca * sb * sg - cg * sa;
787r7 = -sb;
788r8 = cb * sa;
789r9 = ca * cb;
790%EN DEDUIRE MATRICE R ??
791GeometryCalib.R=[r1,r2,r3;r4,r5,r6;r7,r8,r9];
792
793%------------------------------------------------------------------------
794% --- determine the rms of calibration error
795function ErrorRms=error_calib(calib_param,Calib,Coord)
796%calib_param: vector of free calibration parameters (to optimise)
797%Calib: structure of the given calibration parameters
798%Coord: list of phys coordinates (columns 1-3, and pixel coordinates (columns 4-5)
799Calib.f=25;
800Calib.dpx=0.012;
801Calib.dpy=0.012;
802Calib.sx=1;
803Calib.Cx=512;
804Calib.Cy=512;
805Calib.kappa1=calib_param(1);
806Calib.Tx=calib_param(2);
807Calib.Ty=calib_param(3);
808Calib.Tz=calib_param(4);
809alpha=calib_param(5);
810Calib.R=[cos(alpha) sin(alpha) 0;-sin(alpha) cos(alpha) 0;0 0 -1];
811
812X=Coord(:,1);
813Y=Coord(:,2);
814Z=Coord(:,3);
815x_ima=Coord(:,4);
816y_ima=Coord(:,5);
817[Xpoints,Ypoints]=px_XYZ(Calib,X,Y,Z);
818ErrorRms(1)=sqrt(mean((Xpoints-x_ima).*(Xpoints-x_ima)));
819ErrorRms(2)=sqrt(mean((Ypoints-y_ima).*(Ypoints-y_ima)));
820ErrorRms=mean(ErrorRms);
821
822%------------------------------------------------------------------------
823function XImage_Callback(hObject, eventdata, handles)
824%------------------------------------------------------------------------
825update_list(hObject, eventdata,handles)
826
827%------------------------------------------------------------------------
828function YImage_Callback(hObject, eventdata, handles)
829%------------------------------------------------------------------------
830update_list(hObject, eventdata,handles)
831
832%------------------------------------------------------------------------
833% --- Executes on button press in STORE.
834function STORE_Callback(hObject, eventdata, handles)
835Coord_cell=get(handles.ListCoord,'String');
836Object=read_geometry_calib(Coord_cell);
837unitlist=get(handles.CoordUnit,'String');
838unit=unitlist{get(handles.CoordUnit,'value')};
839GeometryCalib.CoordUnit=unit;
840GeometryCalib.SourceCalib.PointCoord=Object.Coord;
841huvmat=findobj(allchild(0),'Name','uvmat');
842hhuvmat=guidata(huvmat);%handles of elements in the GUI uvmat
843% RootPath='';
844% RootFile='';
845if ~isempty(hhuvmat.RootPath)&& ~isempty(hhuvmat.RootFile)
846    testhandle=1;
847    RootPath=get(hhuvmat.RootPath,'String');
848    RootFile=get(hhuvmat.RootFile,'String');
849    filebase=fullfile(RootPath,RootFile);
850    while exist([filebase '.xml'],'file')
851        filebase=[filebase '~'];
852    end
853    outputfile=[filebase '.xml'];
854    errormsg=update_imadoc(GeometryCalib,outputfile);
855    if ~strcmp(errormsg,'')
856        msgbox_uvmat('ERROR',errormsg);
857    end
858    listfile=get(handles.coord_files,'string');
859    if isequal(listfile,{''})
860        listfile={outputfile};
861    else
862        listfile=[listfile;{outputfile}];%update the list of coord files
863    end
864    set(handles.coord_files,'string',listfile);
865end
866set(handles.ListCoord,'Value',1)% refresh the display of coordinates
867set(handles.ListCoord,'String',{'......'})
868
869% --------------------------------------------------------------------
870% --- Executes on button press in CLEAR_PTS: clear the list of calibration points
871function CLEAR_PTS_Callback(hObject, eventdata, handles)
872% --------------------------------------------------------------------
873set(handles.ListCoord,'Value',1)% refresh the display of coordinates
874set(handles.ListCoord,'String',{'......'})
875MenuPlot_Callback(hObject, eventdata, handles)
876
877%------------------------------------------------------------------------
878% --- Executes on button press in CLEAR.
879function CLEAR_Callback(hObject, eventdata, handles)
880%------------------------------------------------------------------------
881set(handles.coord_files,'Value',1)
882set(handles.coord_files,'String',{''})
883
884%------------------------------------------------------------------------
885function XObject_Callback(hObject, eventdata, handles)
886%------------------------------------------------------------------------
887update_list(hObject, eventdata,handles)
888
889%------------------------------------------------------------------------
890function YObject_Callback(hObject, eventdata, handles)
891%------------------------------------------------------------------------
892update_list(hObject, eventdata,handles)
893
894%------------------------------------------------------------------------
895function ZObject_Callback(hObject, eventdata, handles)
896%------------------------------------------------------------------------
897update_list(hObject, eventdata,handles)
898
899%------------------------------------------------------------------------
900function update_list(hObject, eventdata, handles)
901%------------------------------------------------------------------------
902newval(4)=str2double(get(handles.XImage,'String'));
903newval(5)=str2double(get(handles.YImage,'String'));
904newval(1)=str2double(get(handles.XObject,'String'));
905newval(2)=str2double(get(handles.YObject,'String'));
906newval(3)=str2double(get(handles.ZObject,'String'));
907if isnan(newval(3))
908    newval(3)=0;%put z to 0 by default
909end
910Coord=get(handles.ListCoord,'String');
911Coord(end)=[]; %remove last string '.....'
912val=get(handles.ListCoord,'Value');
913data=read_geometry_calib(Coord);
914data.Coord(val,:)=newval;
915for i=1:size(data.Coord,1)
916    for j=1:5
917          Coord_cell{i,j}=num2str(data.Coord(i,j),4);%display coordiantes with 4 digits
918    end
919end
920
921Tabchar=cell2tab(Coord_cell,' | ');
922Tabchar=[Tabchar ;{'......'}];
923set(handles.ListCoord,'String',Tabchar)
924
925%update the plot
926ListCoord_Callback(hObject, eventdata, handles)
927MenuPlot_Callback(hObject, eventdata, handles)
928
929%------------------------------------------------------------------------
930% --- Executes on selection change in ListCoord.
931function ListCoord_Callback(hObject, eventdata, handles)
932%------------------------------------------------------------------------
933huvmat=findobj(allchild(0),'Name','uvmat');%find the current uvmat interface handle
934hplot=findobj(huvmat,'Tag','axes3');%main plotting axis of uvmat
935hhh=findobj(hplot,'Tag','calib_marker');
936Coord_cell=get(handles.ListCoord,'String');
937val=get(handles.ListCoord,'Value');
938if numel(val)>1
939    return %no action if several lines have been selected
940end
941coord_str=Coord_cell{val};
942k=findstr(' | ',coord_str);
943if isempty(k)%last line '.....' selected
944    if ~isempty(hhh)
945        delete(hhh)%delete the circle marker
946    end
947    return
948end
949%fill the edit boxex
950set(handles.XObject,'String',coord_str(1:k(1)-1))
951set(handles.YObject,'String',coord_str(k(1)+3:k(2)-1))
952set(handles.ZObject,'String',coord_str(k(2)+3:k(3)-1))
953set(handles.XImage,'String',coord_str(k(3)+3:k(4)-1))
954set(handles.YImage,'String',coord_str(k(4)+3:end))
955h_menu_coord=findobj(huvmat,'Tag','transform_fct');
956menu=get(h_menu_coord,'String');
957choice=get(h_menu_coord,'Value');
958if iscell(menu)
959    option=menu{choice};
960else
961    option='px'; %default
962end
963if isequal(option,'phys')
964    XCoord=str2double(coord_str(1:k(1)-1));
965    YCoord=str2double(coord_str(k(1)+3:k(2)-1));
966elseif isequal(option,'px')|| isequal(option,'')
967    XCoord=str2double(coord_str(k(3)+3:k(4)-1));
968    YCoord=str2double(coord_str(k(4)+3:end));
969else
970    msgbox_uvmat('ERROR','the choice in menu_coord of uvmat must be px or phys ')
971end
972if isempty(XCoord)||isempty(YCoord)
973     if ~isempty(hhh)
974        delete(hhh)%delete the circle marker
975    end
976    return
977end
978xlim=get(hplot,'XLim');
979ylim=get(hplot,'YLim');
980ind_range=max(abs(xlim(2)-xlim(1)),abs(ylim(end)-ylim(1)))/20;%defines the size of the circle marker
981if isempty(hhh)
982    set(0,'CurrentFig',huvmat)
983    set(huvmat,'CurrentAxes',hplot)
984    rectangle('Curvature',[1 1],...
985              'Position',[XCoord-ind_range/2 YCoord-ind_range/2 ind_range ind_range],'EdgeColor','m',...
986              'LineStyle','-','Tag','calib_marker');
987else
988    set(hhh,'Position',[XCoord-ind_range/2 YCoord-ind_range/2 ind_range ind_range])
989end
990
991%------------------------------------------------------------------------
992% --- Executes on selection change in edit_append.
993function edit_append_Callback(hObject, eventdata, handles)
994%------------------------------------------------------------------------
995choice=get(handles.edit_append,'Value');
996if choice
997    set(handles.edit_append,'BackgroundColor',[1 1 0])
998    huvmat=findobj(allchild(0),'tag','uvmat');
999    if ishandle(huvmat)
1000        hhuvmat=guidata(huvmat);
1001        set(hhuvmat.edit_object,'Value',0)
1002        set(hhuvmat.edit_object,'BackgroundColor',[0.7 0.7 0.7])
1003    end
1004else
1005    set(handles.edit_append,'BackgroundColor',[0.7 0.7 0.7])
1006end
1007   
1008function NEW_Callback(hObject, eventdata, handles)
1009%A METTRE SOUS UN BOUTON
1010huvmat=findobj(allchild(0),'Name','uvmat');
1011hchild=get(huvmat,'children');
1012hcoord=findobj(hchild,'Tag','menu_coord');
1013coordtype=get(hcoord,'Value');
1014haxes=findobj(hchild,'Tag','axes3');
1015AxeData=get(haxes,'UserData');
1016if ~isequal(hcoord,2)
1017    set(hcoord,'Value',2)
1018    huvmat=uvmat(AxeData);
1019    'relancer uvmat';
1020end
1021if ~isfield(AxeData,'ZoomAxes')
1022    msgbox_uvmat('ERROR','first draw a window around a grid marker')
1023    return
1024end
1025XLim=get(AxeData.ZoomAxes,'XLim');
1026YLim=get(AxeData.ZoomAxes,'YLim');
1027np=size(AxeData.A);
1028ind_sub_x=round(XLim);
1029ind_sub_y=np(1)-round(YLim);
1030Mfiltre=AxeData.A([ind_sub_y(2):ind_sub_y(1)] ,ind_sub_x,:);
1031Mfiltre_norm=double(Mfiltre);
1032Mfiltre_norm=Mfiltre_norm/sum(sum(Mfiltre_norm));
1033Mfiltre_norm=100*(Mfiltre_norm-mean(mean(Mfiltre_norm)));
1034Atype=class(AxeData.A);
1035Data.NbDim=2;
1036Data.A=filter2(Mfiltre_norm,double(AxeData.A));
1037Data.A=feval(Atype,Data.A);
1038Data.AName='image';
1039Data.AX=AxeData.AX;
1040Data.AY=AxeData.AY;
1041Data.CoordType='px';
1042plot_field(Data)
1043
1044%------------------------------------------------------------------------
1045function MenuPlot_Callback(hObject, eventdata, handles)
1046%------------------------------------------------------------------------
1047huvmat=findobj(allchild(0),'Name','uvmat');%find the current uvmat interface handle
1048%UvData=get(huvmat,'UserData');%Data associated to the current uvmat interface
1049hhuvmat=guidata(huvmat); %handles of GUI elements in uvmat
1050%hplot=findobj(huvmat,'Tag','axes3');%main plotting axis of uvmat
1051h_menu_coord=findobj(huvmat,'Tag','transform_fct');
1052menu=get(h_menu_coord,'String');
1053choice=get(h_menu_coord,'Value');
1054if iscell(menu)
1055    option=menu{choice};
1056else
1057    option='px'; %default
1058end
1059Coord_cell=get(handles.ListCoord,'String');
1060ObjectData=read_geometry_calib(Coord_cell);
1061%ObjectData=read_geometry_calib(handles);%read the interface input parameters defining the object
1062if ~isempty(ObjectData.Coord)
1063    if isequal(option,'phys')
1064        ObjectData.Coord=ObjectData.Coord(:,[1:3]);
1065    elseif isequal(option,'px')||isequal(option,'')
1066        ObjectData.Coord=ObjectData.Coord(:,[4:5]);
1067    else
1068        msgbox_uvmat('ERROR','the choice in menu_coord of uvmat must be '''', px or phys ')
1069    end
1070end
1071axes(hhuvmat.axes3)
1072hh=findobj('Tag','calib_points');
1073if  ~isempty(ObjectData.Coord) && isempty(hh)
1074    hh=line(ObjectData.Coord(:,1),ObjectData.Coord(:,2),'Color','m','Tag','calib_points','LineStyle','.','Marker','+');
1075elseif isempty(ObjectData.Coord)%empty list of points, suppress the plot
1076    delete(hh)
1077else
1078    set(hh,'XData',ObjectData.Coord(:,1))
1079    set(hh,'YData',ObjectData.Coord(:,2))
1080end
1081pause(.1)
1082figure(handles.geometry_calib)
1083
1084% --------------------------------------------------------------------
1085function MenuHelp_Callback(hObject, eventdata, handles)
1086path_to_uvmat=which('uvmat');% check the path of uvmat
1087pathelp=fileparts(path_to_uvmat);
1088helpfile=fullfile(pathelp,'uvmat_doc','uvmat_doc.html');
1089if isempty(dir(helpfile)), msgbox_uvmat('ERROR','Please put the help file uvmat_doc.html in the sub-directory /uvmat_doc of the UVMAT package')
1090else
1091   addpath (fullfile(pathelp,'uvmat_doc'))
1092   web([helpfile '#geometry_calib'])
1093end
1094
1095%------------------------------------------------------------------------
1096function MenuCreateGrid_Callback(hObject, eventdata, handles)
1097%------------------------------------------------------------------------
1098%hcalib=get(handles.calib_type,'parent');%handles of the GUI geometry_calib
1099CalibData=get(handles.geometry_calib,'UserData');
1100Tinput=[];%default
1101if isfield(CalibData,'grid')
1102    Tinput=CalibData.grid;
1103end
1104[T,CalibData.grid]=create_grid(Tinput);%display the GUI create_grid
1105set(handles.geometry_calib,'UserData',CalibData)
1106
1107%grid in phys space
1108Coord=get(handles.ListCoord,'String');
1109val=get(handles.ListCoord,'Value');
1110data=read_geometry_calib(Coord);
1111%nbpoints=size(data.Coord,1); %nbre of calibration points
1112data.Coord(val:val+size(T,1)-1,1:3)=T(end:-1:1,:);%update the existing list of phys coordinates from the GUI create_grid
1113% for i=1:nbpoints
1114%    for j=1:5
1115%           Coord{i,j}=num2str(data.Coord(i,j),4);%display coordiantes with 4 digits
1116%    end
1117% end
1118%update the phys coordinates starting from the selected point (down in the
1119Coord(end,:)=[]; %remove last string '.....'
1120for i=1:size(data.Coord,1)
1121    for j=1:5
1122          Coord{i,j}=num2str(data.Coord(i,j),4);%display coordiantes with 4 digits
1123    end
1124end
1125
1126%size(data.Coord,1)
1127Tabchar=cell2tab(Coord,' | ');
1128Tabchar=[Tabchar ;{'......'}];
1129set(handles.ListCoord,'String',Tabchar)
1130
1131% -----------------------------------------------------------------------
1132% --- automatic grid dectection from local maxima of the images
1133function MenuDetectGrid_Callback(hObject, eventdata, handles)
1134%------------------------------------------------------------------------
1135%% initiate the grid
1136CalibData=get(handles.geometry_calib,'UserData');%get information stored on the GUI geometry_calib
1137grid_input=[];%default
1138if isfield(CalibData,'grid')
1139    grid_input=CalibData.grid;%retrieve the previously used grid
1140end
1141[T,CalibData.grid,white_test]=create_grid(grid_input,'detect_grid');%display the GUI create_grid, read the set of phys coordinates T
1142set(handles.geometry_calib,'UserData',CalibData)%store the phys grid parameters for later use
1143
1144%% read the four last point coordinates in pixels
1145Coord_cell=get(handles.ListCoord,'String');%read list of coordinates on geometry_calib
1146data=read_geometry_calib(Coord_cell);
1147nbpoints=size(data.Coord,1); %nbre of calibration points
1148if nbpoints~=4
1149    msgbox_uvmat('ERROR','four points must have be selected by the mouse, beginning by the new x axis, to delimitate the phys grid area')
1150    return
1151end
1152corners_X=(data.Coord(end:-1:end-3,4)); %pixel absissa of the four corners
1153corners_Y=(data.Coord(end:-1:end-3,5));
1154
1155%reorder the last two points (the two first in the list) if needed
1156angles=angle((corners_X-corners_X(1))+1i*(corners_Y-corners_Y(1)));
1157if abs(angles(4)-angles(2))>abs(angles(3)-angles(2))
1158      X_end=corners_X(4);
1159      Y_end=corners_Y(4);
1160      corners_X(4)=corners_X(3);
1161      corners_Y(4)=corners_Y(3);
1162      corners_X(3)=X_end;
1163      corners_Y(3)=Y_end;
1164end
1165
1166%% read the current image, displayed in the GUI uvmat
1167huvmat=findobj(allchild(0),'Name','uvmat');
1168UvData=get(huvmat,'UserData');
1169A=UvData.Field.A;
1170npxy=size(A);
1171X=[CalibData.grid.x_0 CalibData.grid.x_1 CalibData.grid.x_0 CalibData.grid.x_1]';%corner absissa in the phys coordinates (cm)
1172Y=[CalibData.grid.y_0 CalibData.grid.y_0 CalibData.grid.y_1 CalibData.grid.y_1]';%corner ordinates in the phys coordinates (cm)
1173
1174%calculate transform matrices for plane projection
1175% reference: http://alumni.media.mit.edu/~cwren/interpolator/ by Christopher R. Wren
1176B = [ X Y ones(size(X)) zeros(4,3)        -X.*corners_X -Y.*corners_X ...
1177      zeros(4,3)        X Y ones(size(X)) -X.*corners_Y -Y.*corners_Y ];
1178B = reshape (B', 8 , 8 )';
1179D = [ corners_X , corners_Y ];
1180D = reshape (D', 8 , 1 );
1181l = (B' * B)\B' * D;
1182Amat = reshape([l(1:6)' 0 0 1 ],3,3)';
1183C = [l(7:8)' 1];
1184
1185% transform grid image into 'phys' coordinates
1186GeometryCalib.fx_fy=[1 1];
1187GeometryCalib.Tx_Ty_Tz=[Amat(1,3) Amat(2,3) 1];
1188GeometryCalib.R=[Amat(1,1),Amat(1,2),0;Amat(2,1),Amat(2,2),0;C(1),C(2),0];
1189GeometryCalib.CoordUnit='cm';
1190path_uvmat=which('uvmat');% check the path detected for source file uvmat
1191path_UVMAT=fileparts(path_uvmat); %path to UVMAT
1192addpath(fullfile(path_UVMAT,'transform_field'))
1193Data.ListVarName={'AY','AX','A'};
1194Data.VarDimName={'AY','AX',{'AY','AX'}};
1195if ndims(A)==3
1196    A=mean(A,3);
1197end
1198Data.A=A-min(min(A));
1199Data.AY=[npxy(1)-0.5 0.5];
1200Data.AX=[0.5 npxy(2)];
1201Data.CoordUnit='pixel';
1202Calib.GeometryCalib=GeometryCalib;
1203DataOut=phys(Data,Calib);
1204rmpath(fullfile(path_UVMAT,'transform_field'))
1205Amod=DataOut.A;
1206Rangx=DataOut.AX;
1207Rangy=DataOut.AY;
1208if white_test
1209    Amod=double(Amod);%case of white grid markers: will look for image maxima
1210else
1211    Amod=-double(Amod);%case of black grid markers: will look for image minima
1212end
1213% figure(12) %display corrected image
1214% Amax=max(max(Amod));
1215% image(Rangx,Rangy,uint8(255*Amod/Amax))
1216
1217%% detection of local image extrema in each direction
1218Dx=(Rangx(2)-Rangx(1))/(npxy(2)-1); %x mesh in real space
1219Dy=(Rangy(2)-Rangy(1))/(npxy(1)-1); %y mesh in real space
1220ind_range_x=ceil(abs(GeometryCalib.R(1,1)*CalibData.grid.Dx/3));% range of search of image ma around each point obtained by linear interpolation from the marked points
1221ind_range_y=ceil(abs(GeometryCalib.R(2,2)*CalibData.grid.Dy/3));% range of search of image ma around each point obtained by linear interpolation from the marked points
1222nbpoints=size(T,1);
1223for ipoint=1:nbpoints
1224    i0=1+round((T(ipoint,1)-Rangx(1))/Dx);%round(Xpx(ipoint));
1225    j0=1+round((T(ipoint,2)-Rangy(1))/Dy);%round(Xpx(ipoint));
1226    j0min=max(j0-ind_range_y,1);
1227    j0max=min(j0+ind_range_y,size(Amod,1));
1228    i0min=max(i0-ind_range_x,1);
1229    i0max=min(i0+ind_range_x,size(Amod,2));
1230    Asub=Amod(j0min:j0max,i0min:i0max);
1231    x_profile=sum(Asub,1);
1232    y_profile=sum(Asub,2);
1233    [Amax,ind_x_max]=max(x_profile);
1234    [Amax,ind_y_max]=max(y_profile);
1235    %sub-pixel improvement using moments
1236    x_shift=0;
1237    y_shift=0;
1238    if ind_x_max+2<=numel(x_profile) && ind_x_max-2>=1
1239        Atop=x_profile(ind_x_max-2:ind_x_max+2);
1240        x_shift=sum(Atop.*[-2 -1 0 1 2])/sum(Atop);
1241    end
1242    if ind_y_max+2<=numel(y_profile) && ind_y_max-2>=1
1243        Atop=y_profile(ind_y_max-2:ind_y_max+2);
1244        y_shift=sum(Atop.*[-2 -1 0 1 2]')/sum(Atop);
1245    end
1246    Delta(ipoint,1)=(x_shift+ind_x_max+i0min-i0-1)*Dx;%shift from the initial guess
1247    Delta(ipoint,2)=(y_shift+ind_y_max+j0min-j0-1)*Dy;
1248end
1249Tmod=T(:,(1:2))+Delta;
1250[Xpx,Ypx]=px_XYZ(GeometryCalib,Tmod(:,1),Tmod(:,2));
1251for ipoint=1:nbpoints
1252     Coord{ipoint,1}=num2str(T(ipoint,1),4);%display coordiantes with 4 digits
1253     Coord{ipoint,2}=num2str(T(ipoint,2),4);%display coordiantes with 4 digits
1254     Coord{ipoint,3}=num2str(T(ipoint,3),4);%display coordiantes with 4 digits;
1255     Coord{ipoint,4}=num2str(Xpx(ipoint),4);%display coordiantes with 4 digits
1256     Coord{ipoint,5}=num2str(Ypx(ipoint),4);%display coordiantes with 4 digits
1257end
1258Tabchar=cell2tab(Coord(end:-1:1,:),' | ');
1259Tabchar=[Tabchar ;{'......'}];
1260set(handles.ListCoord,'Value',1)
1261set(handles.ListCoord,'String',Tabchar)
1262MenuPlot_Callback(hObject, eventdata, handles)
1263
1264%-----------------------------------------------------------------------
1265function MenuTranslatePoints_Callback(hObject, eventdata, handles)
1266%-----------------------------------------------------------------------
1267%hcalib=get(handles.calib_type,'parent');%handles of the GUI geometry_calib
1268CalibData=get(handles.geometry_calib,'UserData');
1269Tinput=[];%default
1270if isfield(CalibData,'translate')
1271    Tinput=CalibData.translate;
1272end
1273T=translate_points(Tinput);%display translate_points GUI and get shift parameters
1274CalibData.translate=T;
1275set(handles.geometry_calib,'UserData',CalibData)
1276%translation
1277Coord_cell=get(handles.ListCoord,'String');
1278data=read_geometry_calib(Coord_cell);
1279data.Coord(:,1)=T(1)+data.Coord(:,1);
1280data.Coord(:,2)=T(2)+data.Coord(:,2);
1281data.Coord(:,3)=T(3)+data.Coord(:,3);
1282data.Coord(:,[4 5])=data.Coord(:,[4 5]);
1283for i=1:size(data.Coord,1)
1284    for j=1:5
1285          Coord{i,j}=num2str(data.Coord(i,j),4);%phys x,y,z
1286   end
1287end
1288Tabchar=cell2tab(Coord,' | ');
1289Tabchar=[Tabchar; {'.....'}];
1290%set(handles.ListCoord,'Value',1)
1291set(handles.ListCoord,'String',Tabchar)
1292
1293
1294% --------------------------------------------------------------------
1295function MenuRotatePoints_Callback(hObject, eventdata, handles)
1296%hcalib=get(handles.calib_type,'parent');%handles of the GUI geometry_calib
1297CalibData=get(handles.geometry_calib,'UserData');
1298Tinput=[];%default
1299if isfield(CalibData,'rotate')
1300    Tinput=CalibData.rotate;
1301end
1302T=rotate_points(Tinput);%display translate_points GUI and get shift parameters
1303CalibData.rotate=T;
1304set(handles.geometry_calib,'UserData',CalibData)
1305%-----------------------------------------------------
1306%rotation
1307Phi=T(1);
1308O_x=0;%default
1309O_y=0;%default
1310if numel(T)>=2
1311    O_x=T(2);%default
1312end
1313if numel(T)>=3
1314    O_y=T(3);%default
1315end
1316Coord_cell=get(handles.ListCoord,'String');
1317data=read_geometry_calib(Coord_cell);
1318r1=cos(pi*Phi/180);
1319r2=-sin(pi*Phi/180);
1320r3=sin(pi*Phi/180);
1321r4=cos(pi*Phi/180);
1322x=data.Coord(:,1)-O_x;
1323y=data.Coord(:,2)-O_y;
1324data.Coord(:,1)=r1*x+r2*y;
1325data.Coord(:,2)=r3*x+r4*y;
1326% data.Coord(:,[4 5])=data.Coord(:,[4 5]);
1327for i=1:size(data.Coord,1)
1328    for j=1:5
1329          Coord{i,j}=num2str(data.Coord(i,j),4);%phys x,y,z
1330   end
1331end
1332Tabchar=cell2tab(Coord,' | ');
1333Tabchar=[Tabchar;{'......'}];
1334set(handles.ListCoord,'Value',1)
1335set(handles.ListCoord,'String',Tabchar)
1336
1337
1338% %------------------------------------------------------------------------
1339% % --- Executes on button press in rotation.
1340% function rotation_Callback(hObject, eventdata, handles)
1341% %------------------------------------------------------------------------
1342% angle_rot=(pi/180)*str2num(get(handles.Phi,'String'));
1343% Coord_cell=get(handles.ListCoord,'String');
1344% data=read_geometry_calib(Coord_cell);
1345% data.Coord(:,1)=cos(angle_rot)*data.Coord(:,1)+sin(angle_rot)*data.Coord(:,2);
1346% data.Coord(:,1)=-sin(angle_rot)*data.Coord(:,1)+cos(angle_rot)*data.Coord(:,2);
1347% set(handles.XObject,'String',num2str(data.Coord(:,1),4));
1348% set(handles.YObject,'String',num2str(data.Coord(:,2),4));
1349
1350
1351%------------------------------------------------------------------------
1352% image transform from px to phys
1353%INPUT:
1354%Zindex: index of plane
1355% function [A_out,Rangx,Rangy]=phys_Ima(A,Calib,ZIndex)
1356% %------------------------------------------------------------------------
1357% xcorner=[];
1358% ycorner=[];
1359% npx=[];
1360% npy=[];
1361% siz=size(A)
1362% npx=[npx siz(2)];
1363% npy=[npy siz(1)]
1364% xima=[0.5 siz(2)-0.5 0.5 siz(2)-0.5];%image coordinates of corners
1365% yima=[0.5 0.5 siz(1)-0.5 siz(1)-0.5];
1366% [xcorner,ycorner]=phys_XYZ(Calib,xima,yima,ZIndex);%corresponding physical coordinates
1367% Rangx(1)=min(xcorner);
1368% Rangx(2)=max(xcorner);
1369% Rangy(2)=min(ycorner);
1370% Rangy(1)=max(ycorner);
1371% test_multi=(max(npx)~=min(npx)) | (max(npy)~=min(npy));
1372% npx=max(npx);
1373% npy=max(npy);
1374% x=linspace(Rangx(1),Rangx(2),npx);
1375% y=linspace(Rangy(1),Rangy(2),npy);
1376% [X,Y]=meshgrid(x,y);%grid in physical coordiantes
1377% vec_B=[];
1378%
1379% zphys=0; %default
1380% if isfield(Calib,'SliceCoord') %.Z= index of plane
1381%    SliceCoord=Calib.SliceCoord(ZIndex,:);
1382%    zphys=SliceCoord(3); %to generalize for non-parallel planes
1383% end
1384% [XIMA,YIMA]=px_XYZ(Calib,X,Y,zphys);%corresponding image indices for each point in the real space grid
1385% XIMA=reshape(round(XIMA),1,npx*npy);%indices reorganized in 'line'
1386% YIMA=reshape(round(YIMA),1,npx*npy);
1387% flagin=XIMA>=1 & XIMA<=npx & YIMA >=1 & YIMA<=npy;%flagin=1 inside the original image
1388% testuint8=isa(A,'uint8');
1389% testuint16=isa(A,'uint16');
1390% if numel(siz)==2 %(B/W images)
1391%     vec_A=reshape(A,1,npx*npy);%put the original image in line
1392%     ind_in=find(flagin);
1393%     ind_out=find(~flagin);
1394%     ICOMB=((XIMA-1)*npy+(npy+1-YIMA));
1395%     ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A
1396%     vec_B(ind_in)=vec_A(ICOMB);
1397%     vec_B(ind_out)=zeros(size(ind_out));
1398%     A_out=reshape(vec_B,npy,npx);%new image in real coordinates
1399% elseif numel(siz)==3     
1400%     for icolor=1:siz(3)
1401%         vec_A=reshape(A{icell}(:,:,icolor),1,npx*npy);%put the original image in line
1402%         ind_in=find(flagin);
1403%         ind_out=find(~flagin);
1404%         ICOMB=((XIMA-1)*npy+(npy+1-YIMA));
1405%         ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A
1406%         vec_B(ind_in)=vec_A(ICOMB);
1407%         vec_B(ind_out)=zeros(size(ind_out));
1408%         A_out(:,:,icolor)=reshape(vec_B,npy,npx);%new image in real coordinates
1409%     end
1410% end
1411% if testuint8
1412%     A_out=uint8(A_out);
1413% end
1414% if testuint16
1415%     A_out=uint16(A_out);
1416% end
1417
1418%------------------------------------------------------------------------
1419% pointwise transform from px to phys
1420%INPUT:
1421%Z: index of plane
1422% function [Xphys,Yphys,Zphys]=phys_XYZ(Calib,X,Y,Z)
1423% %------------------------------------------------------------------------
1424% if exist('Z','var')& isequal(Z,round(Z))& Z>0 & isfield(Calib,'SliceCoord')&length(Calib.SliceCoord)>=Z
1425%     Zindex=Z;
1426%     Zphys=Calib.SliceCoord(Zindex,3);%GENERALISER AUX CAS AVEC ANGLE
1427% else
1428%     Zphys=0;
1429% end
1430% if ~exist('X','var')||~exist('Y','var')
1431%     Xphys=[];
1432%     Yphys=[];%default
1433%     return
1434% end
1435% Xphys=X;%default
1436% Yphys=Y;
1437% %image transform
1438% if isfield(Calib,'R')
1439%     R=(Calib.R)';
1440%     Dx=R(5)*R(7)-R(4)*R(8);
1441%     Dy=R(1)*R(8)-R(2)*R(7);
1442%     D0=Calib.f*(R(2)*R(4)-R(1)*R(5));
1443%     Z11=R(6)*R(8)-R(5)*R(9);
1444%     Z12=R(2)*R(9)-R(3)*R(8); 
1445%     Z21=R(4)*R(9)-R(6)*R(7);
1446%     Z22=R(3)*R(7)-R(1)*R(9);
1447%     Zx0=R(3)*R(5)-R(2)*R(6);
1448%     Zy0=R(1)*R(6)-R(3)*R(4);
1449%     A11=R(8)*Calib.Ty-R(5)*Calib.Tz+Z11*Zphys;
1450%     A12=R(2)*Calib.Tz-R(8)*Calib.Tx+Z12*Zphys;
1451%     A21=-R(7)*Calib.Ty+R(4)*Calib.Tz+Z21*Zphys;
1452%     A22=-R(1)*Calib.Tz+R(7)*Calib.Tx+Z11*Zphys;
1453%     X0=Calib.f*(R(5)*Calib.Tx-R(2)*Calib.Ty+Zx0*Zphys);
1454%     Y0=Calib.f*(-R(4)*Calib.Tx+R(1)*Calib.Ty+Zy0*Zphys);
1455%         %px to camera:
1456%     Xd=(Calib.dpx/Calib.sx)*(X-Calib.Cx); % sensor coordinates
1457%     Yd=Calib.dpy*(Y-Calib.Cy);
1458%     dist_fact=1+Calib.kappa1*(Xd.*Xd+Yd.*Yd); %distortion factor
1459%     Xu=dist_fact.*Xd;%undistorted sensor coordinates
1460%     Yu=dist_fact.*Yd;
1461%     denom=Dx*Xu+Dy*Yu+D0;
1462%     % denom2=denom.*denom;
1463%     Xphys=(A11.*Xu+A12.*Yu+X0)./denom;%world coordinates
1464%     Yphys=(A21.*Xu+A22.*Yu+Y0)./denom;
1465% end
1466
1467
1468% --------------------------------------------------------------------
1469function MenuImportPoints_Callback(hObject, eventdata, handles)
1470fileinput=browse_xml(hObject, eventdata, handles);
1471if isempty(fileinput)
1472    return
1473end
1474[s,errormsg]=imadoc2struct(fileinput,'GeometryCalib');
1475GeometryCalib=s.GeometryCalib;
1476%GeometryCalib=load_calib(hObject, eventdata, handles)
1477calib=reshape(GeometryCalib.PointCoord,[],1);
1478for ilist=1:numel(calib)
1479    CoordCell{ilist}=num2str(calib(ilist));
1480end
1481CoordCell=reshape(CoordCell,[],5);
1482Tabchar=cell2tab(CoordCell,' | ');%transform cells into table ready for display
1483Tabchar=[Tabchar;{'......'}];
1484set(handles.ListCoord,'Value',1)
1485set(handles.ListCoord,'String',Tabchar)
1486MenuPlot_Callback(handles.geometry_calib, [], handles)
1487
1488% -----------------------------------------------------------------------
1489function MenuImportIntrinsic_Callback(hObject, eventdata, handles)
1490%------------------------------------------------------------------------
1491fileinput=browse_xml(hObject, eventdata, handles);
1492if isempty(fileinput)
1493    return
1494end
1495[s,errormsg]=imadoc2struct(fileinput,'GeometryCalib');
1496GeometryCalib=s.GeometryCalib;
1497display_intrinsic(GeometryCalib,handles)
1498
1499% -----------------------------------------------------------------------
1500function MenuImportAll_Callback(hObject, eventdata, handles)
1501%------------------------------------------------------------------------
1502fileinput=browse_xml(hObject, eventdata, handles);
1503if ~isempty(fileinput)
1504    loadfile(handles,fileinput)
1505end
1506
1507% -----------------------------------------------------------------------
1508% --- Executes on menubar option Import/Grid file: introduce previous grid files
1509function MenuGridFile_Callback(hObject, eventdata, handles)
1510% -----------------------------------------------------------------------
1511inputfile=browse_xml(hObject, eventdata, handles);
1512listfile=get(handles.coord_files,'string');
1513if isequal(listfile,{''})
1514    listfile={inputfile};
1515else
1516    listfile=[listfile;{inputfile}];%update the list of coord files
1517end
1518set(handles.coord_files,'string',listfile);
1519
1520%------------------------------------------------------------------------
1521% --- 'key_press_fcn:' function activated when a key is pressed on the keyboard
1522function key_press_fcn(hObject,eventdata,handles)
1523%------------------------------------------------------------------------
1524xx=double(get(handles.geometry_calib,'CurrentCharacter')); %get the keyboard character
1525if ismember(xx,[8 127])%backspace or delete
1526    Coord_cell=get(handles.ListCoord,'String');
1527    val=get(handles.ListCoord,'Value');
1528     if max(val)<numel(Coord_cell) % the last element '...' has not been selected
1529        Coord_cell(val)=[];%remove the selected line
1530        set(handles.ListCoord,'Value',min(val))
1531        set(handles.ListCoord,'String',Coord_cell)         
1532        ListCoord_Callback(hObject, eventdata, handles)
1533        MenuPlot_Callback(hObject,eventdata,handles)
1534     end
1535end
1536
1537%------------------------------------------------------------------------
1538function fileinput=browse_xml(hObject, eventdata, handles)
1539%------------------------------------------------------------------------
1540fileinput=[];%default
1541oldfile=''; %default
1542UserData=get(handles.geometry_calib,'UserData');
1543if isfield(UserData,'XmlInputFile')
1544    oldfile=UserData.XmlInputFile;
1545end
1546[FileName, PathName, filterindex] = uigetfile( ...
1547       {'*.xml;*.mat', ' (*.xml,*.mat)';
1548       '*.xml',  '.xml files '; ...
1549        '*.mat',  '.mat matlab files '}, ...
1550        'Pick a file',oldfile);
1551fileinput=[PathName FileName];%complete file name
1552testblank=findstr(fileinput,' ');%look for blanks
1553if ~isempty(testblank)
1554    msgbox_uvmat('ERROR','forbidden input file name or path: no blank character allowed')
1555    return
1556end
1557sizf=size(fileinput);
1558if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end
1559UserData.XmlInputFile=fileinput;
1560set(handles.geometry_calib,'UserData',UserData)%record current file foer further use of browser
1561
1562% -----------------------------------------------------------------------
1563function Heading=loadfile(handles,fileinput)
1564%------------------------------------------------------------------------
1565Heading=[];%default
1566[s,errormsg]=imadoc2struct(fileinput,'GeometryCalib');
1567if ~isempty(errormsg)
1568    msgbox_uvmat('ERROR',['Error for reading ' fileinput ': '  errormsg])
1569    return
1570end
1571if ~isempty(s.Heading)
1572    Heading=s.Heading;
1573end
1574   
1575GeometryCalib=s.GeometryCalib;
1576fx=1;fy=1;Cx=0;Cy=0;kc=0; %default
1577%     Tabchar={};
1578CoordCell={};
1579%     kc=0;%default
1580%     f1=1000;
1581%     f2=1000;
1582%     hhuvmat=guidata(findobj(allchild(0),'Name','uvmat'));
1583%     Cx=str2num(get(hhuvmat.npx,'String'))/2;
1584%     Cy=str2num(get(hhuvmat.npy,'String'))/2;
1585Tabchar={};%default
1586val_cal=1;%default
1587if ~isempty(GeometryCalib)
1588    % choose the calibration option
1589    if isfield(GeometryCalib,'CalibrationType')
1590       calib_list=get(handles.calib_type,'String');
1591       for ilist=1:numel(calib_list)
1592           if strcmp(calib_list{ilist},GeometryCalib.CalibrationType)
1593               val_cal=ilist;
1594               break
1595           end
1596       end
1597    end
1598    display_intrinsic(GeometryCalib,handles)%intrinsic param
1599    %extrinsic param
1600    if isfield(GeometryCalib,'Tx_Ty_Tz')
1601        Tx_Ty_Tz=GeometryCalib.Tx_Ty_Tz;
1602        set(handles.Tx,'String',num2str(GeometryCalib.Tx_Ty_Tz(1),4))
1603        set(handles.Ty,'String',num2str(GeometryCalib.Tx_Ty_Tz(2),4))
1604        set(handles.Tz,'String',num2str(GeometryCalib.Tx_Ty_Tz(3),4))
1605    end
1606    if isfield(GeometryCalib,'omc')
1607        set(handles.Phi,'String',num2str(GeometryCalib.omc(1),4))
1608        set(handles.Theta,'String',num2str(GeometryCalib.omc(2),4))
1609        set(handles.Psi,'String',num2str(GeometryCalib.omc(3),4))
1610    end
1611    calib=reshape(GeometryCalib.PointCoord,[],1);
1612    for ilist=1:numel(calib)
1613        CoordCell{ilist}=num2str(calib(ilist));
1614    end
1615    CoordCell=reshape(CoordCell,[],5);
1616    Tabchar=cell2tab(CoordCell,' | ');%transform cells into table ready for display
1617    MenuPlot_Callback(handles.geometry_calib, [], handles)
1618end
1619set(handles.calib_type,'Value',val_cal)
1620Tabchar=[Tabchar;{'......'}];
1621set(handles.ListCoord,'Value',1)
1622set(handles.ListCoord,'String',Tabchar)
1623
1624if isempty(CoordCell)% allow mouse action by default in the absence of input points
1625    set(handles.edit_append,'Value',1)
1626    set(handles.edit_append,'BackgroundColor',[1 1 0])
1627else % does not allow mouse action by default in the presence of input points
1628    set(handles.edit_append,'Value',0)
1629    set(handles.edit_append,'BackgroundColor',[0.7 0.7 0.7])
1630end
1631
1632%------------------------------------------------------------------------
1633%---display calibration intrinsic parameters
1634function display_intrinsic(GeometryCalib,handles)
1635%------------------------------------------------------------------------
1636fx=[];
1637fy=[];
1638if isfield(GeometryCalib,'fx_fy')
1639    fx=GeometryCalib.fx_fy(1);
1640    fy=GeometryCalib.fx_fy(2);
1641end
1642Cx_Cy=[0 0];%default
1643if isfield(GeometryCalib,'Cx_Cy')
1644    Cx_Cy=GeometryCalib.Cx_Cy;
1645end
1646kc=0;
1647if isfield(GeometryCalib,'kc')
1648    kc=GeometryCalib.kc; %* GeometryCalib.focal*GeometryCalib.focal;
1649end
1650set(handles.fx,'String',num2str(fx,5))
1651set(handles.fy,'String',num2str(fy,5))
1652set(handles.Cx,'String',num2str(Cx_Cy(1),'%1.1f'))
1653set(handles.Cy,'String',num2str(Cx_Cy(2),'%1.1f'))
1654set(handles.kc,'String',num2str(kc,'%1.4f'))
1655
1656
Note: See TracBrowser for help on using the repository browser.