source: trunk/src/geometry_calib.m @ 121

Last change on this file since 121 was 121, checked in by sommeria, 13 years ago

geometry_calib: adapt all the calibration options to the new format for calibration parameters. Suppress the definition of Z for 2D calibrations
msbox_uvmat: suppress a warning by a test on the input string
uvmat: improve the dispaly of dt for pairs

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