source: trunk/src/geometry_calib.m @ 697

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

bug corrected in geometry_calib: more precise determination ofcalib points
xml2struct corrected to work in background

File size: 55.8 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 29-Oct-2013 06:46:10
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
63   [pp,ff]=fileparts(which(varargin{1})); % name of the input file
64   if strcmp(ff,mfilename)% if we are activating a sub-function of geometry_calib
65   % ~isempty(regexp(varargin{1},'_Callback','once'))
66    gui_State.gui_Callback = str2func(varargin{1});
67   end
68end
69
70if nargout
71    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
72else
73    gui_mainfcn(gui_State, varargin{:});
74end
75% End initialization code - DO NOT edit
76
77
78% --- Executes just before geometry_calib is made visible.
79%INPUT:
80%handles: handles of the geometry_calib interface elements
81% PlotHandles: set of handles of the elements contolling the plotting
82% parameters on the uvmat interface (obtained by 'get_plot_handle.m')
83%------------------------------------------------------------------------
84function geometry_calib_OpeningFcn(hObject, eventdata, handles,inputfile)
85%------------------------------------------------------------------------
86% Choose default command line output for geometry_calib
87
88handles.output = hObject;
89
90% Update handles structure
91guidata(hObject, handles);
92set(hObject,'DeleteFcn',{@closefcn})%
93%set(hObject,'WindowButtonDownFcn',{'mouse_alt_gui',handles}) % allows mouse action with right button (zoom for uicontrol display)
94
95%% position
96set(0,'Unit','pixels')
97ScreenSize=get(0,'ScreenSize');% get the size of the screen, to put the fig on the upper right
98Left=ScreenSize(3)- 460; %right edge close to the right, with margin=40 (GUI width=420 px)
99if ScreenSize(4)>920
100    Height=840;%default height of the GUI
101    Bottom=ScreenSize(4)-Height-40; %put fig at top right
102else
103    Height=ScreenSize(4)-80;
104    Bottom=40; % GUI lies o the screen bottom (with margin =40)
105end
106set(handles.calib_type,'Position',[1 Height-40 194 30])%  rank 1
107set(handles.APPLY,'Position',[197 Height-40 110 30])%  rank 1
108set(handles.REPLICATE,'Position',[309 Height-40 110 30])%  rank 1
109set(handles.Intrinsic,'Position',[1 Height-40-2-92 418 92])%  rank 2
110set(handles.Extrinsic,'Position',[1 Height-40-4-92-75 418 75])%  rank 3
111set(handles.PointLists,'Position',[1 Height-40-6-92-75-117 418 117]) %  rank 4
112set(handles.CheckEnableMouse,'Position',[3 Height-40-8-92-75-117-30 203 30])%  rank 5
113set(handles.PLOT,'Position',[3 Height-394 120 30])%  rank 6
114set(handles.Copy,'Position',[151 Height-394 120 30])%  rank 6
115set(handles.CLEAR_PTS,'Position',[297 Height-394 120 30])%  rank 6
116set(handles.ClearLine,'Position',[297 Height-364 120 30])%  rank 6
117set(handles.phys_title,'Position',[38 Height-426 125 20])%  rank 7
118set(handles.CoordUnit,'Position',[151 Height-426 120 30])%  rank 7
119set(handles.px_title,'Position',[272 Height-426 125 20])%  rank 7
120set(handles.ListCoord,'Position',[1 20 418 Height-446])% rank 8
121set(handles.geometry_calib,'Position',[Left Bottom 420 Height])
122
123%set menu of calibration options
124set(handles.calib_type,'String',{'rescale';'linear';'3D_linear';'3D_quadr';'3D_extrinsic'})
125if exist('inputfile','var')&& ~isempty(inputfile)
126    struct.XmlInputFile=inputfile;
127    [RootPath,SubDir,RootFile,tild,tild,tild,tild,FileExt]=fileparts_uvmat(inputfile);
128    if ~strcmp(FileExt,'.xml')
129        inputfile=fullfile(RootPath,[SubDir '.xml']);%xml file corresponding to the input file
130        if ~exist(inputfile,'file')% case of civ files , removes the extension for subdir
131            inputfile=fullfile(RootPath,[regexprep(SubDir,'\..+$','') '.xml']);
132            if ~exist(inputfile,'file')
133                inputfile=[fullfile(RootPath,SubDir,RootFile) '.xml'];%old convention
134                if ~exist(inputfile,'file')
135                    inputfile='';
136                end
137            end
138        end
139    end
140    set(handles.ListCoord,'Data',[])
141    if exist(inputfile,'file')
142        Heading=loadfile(handles,inputfile);% load data from the xml file
143        if isfield(Heading,'Campaign')&& ischar(Heading.Campaign)
144            struct.Campaign=Heading.Campaign;
145        end
146    end   
147    set(hObject,'UserData',struct)
148end
149
150%------------------------------------------------------------------------
151% --- Outputs from this function are returned to the command line.
152function varargout = geometry_calib_OutputFcn(~, eventdata, handles)
153%------------------------------------------------------------------------
154% Get default command line output from handles structure
155varargout{1} = handles.output;
156varargout{2}=handles;
157%
158%------------------------------------------------------------------------
159% executed when closing: set the parent interface button to value 0
160function closefcn(gcbo,eventdata)
161%------------------------------------------------------------------------
162huvmat=findobj(allchild(0),'Name','uvmat');
163if ~isempty(huvmat)
164    handles=guidata(huvmat);
165    set(handles.MenuCalib,'Checked','off')
166    hobject=findobj(handles.PlotAxes,'tag','calib_points');
167    if ~isempty(hobject)
168        delete(hobject)
169    end
170    hobject=findobj(handles.PlotAxes,'tag','calib_marker');
171    if ~isempty(hobject)
172        delete(hobject)
173    end   
174end
175
176%------------------------------------------------------------------------
177% --- Executes on button press APPLY (used to launch the calibration).
178function APPLY_Callback(hObject, eventdata, handles)
179%------------------------------------------------------------------------
180%% look for the GUI uvmat and check for an image as input
181set(handles.APPLY,'BackgroundColor',[1 1 0])
182huvmat=findobj(allchild(0),'Name','uvmat');
183hhuvmat=guidata(huvmat);%handles of elements in the GUI uvmat
184
185RootPath='';
186if ~isempty(hhuvmat.RootPath)&& ~isempty(hhuvmat.RootFile)
187    RootPath=get(hhuvmat.RootPath,'String');
188    SubDirBase=regexprep(get(hhuvmat.SubDir,'String'),'\..+$','');
189    outputfile=[fullfile(RootPath,SubDirBase) '.xml'];%xml file associated with the currently displayed image
190else
191    question={'save the calibration data and point coordinates in'};
192    def={fullfile(RootPath,'ObjectCalib.xml')};
193    options.Resize='on';
194    answer=inputdlg(question,'',1,def,options);
195    outputfile=answer{1};
196end
197[GeometryCalib,index]=calibrate(handles,hhuvmat);% apply calibration
198
199if isempty(GeometryCalib) % if calibration cancelled
200    set(handles.APPLY,'BackgroundColor',[1 0 1])
201else   % if calibration confirmed
202   
203    %% copy the xml file from the old location if appropriate, then update with the calibration parameters
204    if ~exist(outputfile,'file') && ~isempty(SubDirBase)
205        oldxml=[fullfile(RootPath,SubDirBase,get(hhuvmat.RootFile,'String')) '.xml'];
206        if exist(oldxml,'file')
207            [success,message]=copyfile(oldxml,outputfile);%copy the old xml file to a new one with the new convention
208        end
209    end
210    errormsg=update_imadoc(GeometryCalib,outputfile,'GeometryCalib');% introduce the calibration data in the xml file
211    if ~strcmp(errormsg,'')
212        msgbox_uvmat('ERROR',errormsg);
213    end
214   
215    %% display image with new calibration in the currently opened uvmat interface
216    hhh=findobj(hhuvmat.PlotAxes,'Tag','calib_marker');% delete calib points and markers
217    if ~isempty(hhh)
218        delete(hhh);
219    end
220    hhh=findobj(hhuvmat.PlotAxes,'Tag','calib_points');
221    if ~isempty(hhh)
222        delete(hhh);
223    end
224    set(hhuvmat.CheckFixLimits,'Value',0)% put FixedLimits option to 'off'
225    set(hhuvmat.CheckFixLimits,'BackgroundColor',[0.7 0.7 0.7])
226    UserData=get(handles.geometry_calib,'UserData');
227    UserData.XmlInputFile=outputfile;%save the current xml file name
228    set(handles.geometry_calib,'UserData',UserData)
229    uvmat('RootPath_Callback',hObject,eventdata,hhuvmat); %file input with xml reading  in uvmat, show the image in phys coordinates
230    PLOT_Callback(hObject, eventdata, handles)
231    Data=get(handles.ListCoord,'Data');
232    Data(:,6)=zeros(size(Data,1),1);
233    Data(index,6)=-1;% indicate in the list the point with max deviation (possible mistake)
234    set(handles.ListCoord,'Data',Data)% indicate in the list the point with max deviation (possible mistake)
235    figure(handles.geometry_calib)
236    set(handles.APPLY,'BackgroundColor',[1 0 0])
237end
238
239%------------------------------------------------------------------------
240% --- Executes on button press in REPLICATE
241function REPLICATE_Callback(hObject, eventdata, handles)
242%------------------------------------------------------------------------
243
244%% look for the GUI uvmat and check for an image as input
245huvmat=findobj(allchild(0),'Name','uvmat');
246hhuvmat=guidata(huvmat);%handles of elements in the GUI uvmat
247GeometryCalib=calibrate(handles,hhuvmat);% apply calibration
248
249%% open the GUI browse_data
250CalibData=get(handles.geometry_calib,'UserData');%read the calibration image source on the interface userdata
251if isfield(CalibData,'XmlInputFile')
252    InputDir=fileparts(fileparts(CalibData.XmlInputFile));
253end
254answer=msgbox_uvmat('INPUT_TXT','Campaign to calibrate?',InputDir);
255if strcmp(answer,'Cancel')
256    return
257end
258OutPut=browse_data(answer);
259nbcalib=0;
260for ilist=1:numel(OutPut.Experiment)
261    SubDirBase=regexprep(OutPut.Device{1},'\..+$','');
262    XmlName=fullfile(OutPut.Campaign,OutPut.Experiment{ilist},[SubDirBase '.xml']);
263    % copy the xml file from the old location if appropriate, then update with the calibration parameters
264    if ~exist(XmlName,'file') && ~isempty(SubDirBase)
265        oldxml=fullfile(OutPut.Campaign,OutPut.Experiment{ilist},SubDirBase,[get(hhuvmat.RootFile,'String') '.xml']);
266        if exist(oldxml,'file')
267            [success,message]=copyfile(oldxml,XmlName);%copy the old xml file to a new one with the new convention
268        end
269    end
270    errormsg=update_imadoc(GeometryCalib,XmlName,'GeometryCalib');% introduce the calibration data in the xml file
271    if ~strcmp(errormsg,'')
272        msgbox_uvmat('ERROR',errormsg);
273    else
274        display([XmlName ' updated with calibration parameters'])
275        nbcalib=nbcalib+1;
276    end
277end
278msgbox_uvmat('CONFIMATION',[SubDirBase ' calibrated for ' num2str(nbcalib) ' experiments']);
279
280%------------------------------------------------------------------------
281% --- activate calibration and store parameters in ouputfile .
282function [GeometryCalib,index]=calibrate(handles,hhuvmat)
283%------------------------------------------------------------------------
284%% read the current calibration points
285Coord=get(handles.ListCoord,'Data');
286Coord(:,6)=[];
287% apply the calibration, whose type is selected in  handles.calib_type
288if ~isempty(Coord)
289    calib_cell=get(handles.calib_type,'String');
290    val=get(handles.calib_type,'Value');
291    GeometryCalib=feval(['calib_' calib_cell{val}],Coord,handles);
292else
293    msgbox_uvmat('ERROR','No calibration points, abort')
294    return
295end
296Z_plane=[];
297if ~isempty(Coord)
298    %check error
299    X=Coord(:,1);
300    Y=Coord(:,2);
301    Z=Coord(:,3);
302    x_ima=Coord(:,4);
303    y_ima=Coord(:,5);
304    [Xpoints,Ypoints]=px_XYZ(GeometryCalib,X,Y,Z);
305    GeometryCalib.ErrorRms(1)=sqrt(mean((Xpoints-x_ima).*(Xpoints-x_ima)));
306    [GeometryCalib.ErrorMax(1),index(1)]=max(abs(Xpoints-x_ima));
307    GeometryCalib.ErrorRms(2)=sqrt(mean((Ypoints-y_ima).*(Ypoints-y_ima)));
308    [GeometryCalib.ErrorMax(2),index(2)]=max(abs(Ypoints-y_ima));
309    [tild,ind_dim]=max(GeometryCalib.ErrorMax);
310    index=index(ind_dim);
311    %set the Z position of the reference plane used for calibration
312    if isequal(max(Z),min(Z))%Z constant
313        Z_plane=Z(1);
314        GeometryCalib.NbSlice=1;
315        GeometryCalib.SliceCoord=[0 0 Z_plane];
316    end
317end
318%set the coordinate unit
319unitlist=get(handles.CoordUnit,'String');
320unit=unitlist{get(handles.CoordUnit,'value')};
321GeometryCalib.CoordUnit=unit;
322%record the points
323GeometryCalib.SourceCalib.PointCoord=Coord;
324display_intrinsic(GeometryCalib,handles)%display calibration intrinsic parameters
325
326% Display extrinsinc parameters (rotation and translation of camera with  respect to the phys coordiantes)
327set(handles.Tx,'String',num2str(GeometryCalib.Tx_Ty_Tz(1),4))
328set(handles.Ty,'String',num2str(GeometryCalib.Tx_Ty_Tz(2),4))
329set(handles.Tz,'String',num2str(GeometryCalib.Tx_Ty_Tz(3),4))
330set(handles.Phi,'String',num2str(GeometryCalib.omc(1),4))
331set(handles.Theta,'String',num2str(GeometryCalib.omc(2),4))
332set(handles.Psi,'String',num2str(GeometryCalib.omc(3),4))
333
334%% store the calibration data, by default in the xml file of the currently displayed image
335UvData=get(hhuvmat.uvmat,'UserData');
336NbSlice_j=1;%default
337ZStart=Z_plane;
338ZEnd=Z_plane;
339volume_scan='n';
340if isfield(UvData,'XmlData')
341    if isfield(UvData.XmlData,'TranslationMotor')
342        NbSlice_j=UvData.XmlData.TranslationMotor.Nbslice;
343        ZStart=UvData.XmlData.TranslationMotor.ZStart/10;
344        ZEnd=UvData.XmlData.TranslationMotor.ZEnd/10;
345        volume_scan='y';
346    end
347end
348
349answer=msgbox_uvmat('INPUT_Y-N',{'store calibration data';...
350    ['Error rms (along x,y)=' num2str(GeometryCalib.ErrorRms) ' pixels'];...
351    ['Error max (along x,y)=' num2str(GeometryCalib.ErrorMax) ' pixels']});
352
353%% get plane position(s)
354if ~strcmp(answer,'Yes')
355    GeometryCalib=[];
356    index=1;
357    return
358end
359if strcmp(calib_cell{val}(1:2),'3D')%set the plane position for 3D (projection) calibration
360    input_key={'Z (first position)','Z (last position)','Z (water surface)', 'refractive index','NbSlice','volume scan (y/n)','tilt angle y axis','tilt angle x axis'};
361    input_val=[{num2str(ZEnd)} {num2str(ZStart)} {num2str(ZStart)} {'1.333'} num2str(NbSlice_j) {volume_scan} {'0'} {'0'}];
362    answer=inputdlg(input_key,'slice position(s)',ones(1,8), input_val,'on');
363    GeometryCalib.NbSlice=str2double(answer{5});
364    GeometryCalib.VolumeScan=answer{6};
365    if isempty(answer)
366        Z_plane=0; %default
367    else
368        Z_plane=linspace(str2double(answer{1}),str2double(answer{2}),GeometryCalib.NbSlice);
369    end
370    GeometryCalib.SliceCoord=Z_plane'*[0 0 1];
371    GeometryCalib.SliceAngle(:,3)=0;
372    GeometryCalib.SliceAngle(:,2)=str2double(answer{7})*ones(GeometryCalib.NbSlice,1);%rotation around y axis (to generalise)
373    GeometryCalib.SliceAngle(:,1)=str2double(answer{8})*ones(GeometryCalib.NbSlice,1);%rotation around x axis (to generalise)
374    GeometryCalib.InterfaceCoord=[0 0 str2double(answer{3})];
375    GeometryCalib.RefractionIndex=str2double(answer{4});
376end
377
378
379
380%------------------------------------------------------------------------
381% determine the parameters for a calibration by an affine function (rescaling and offset, no rotation)
382function GeometryCalib=calib_rescale(Coord,handles)
383%------------------------------------------------------------------------
384X=Coord(:,1);
385Y=Coord(:,2);% Z not used
386x_ima=Coord(:,4);
387y_ima=Coord(:,5);
388[px]=polyfit(X,x_ima,1);
389[py]=polyfit(Y,y_ima,1);
390% T_x=px(2);
391% T_y=py(2);
392GeometryCalib.CalibrationType='rescale';
393GeometryCalib.fx_fy=[px(1) py(1)];%.fx_fy corresponds to pxcm along x and y
394GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
395GeometryCalib.Tx_Ty_Tz=[px(2)/px(1) py(2)/py(1) 1];
396GeometryCalib.omc=[0 0 0];
397
398%------------------------------------------------------------------------
399% determine the parameters for a calibration by a linear transform matrix (rescale and rotation)
400
401
402function GeometryCalib=calib_linear(Coord,handles)
403%------------------------------------------------------------------------
404X=Coord(:,1);
405Y=Coord(:,2);% Z not used
406x_ima=Coord(:,4);
407y_ima=Coord(:,5);
408XY_mat=[ones(size(X)) X Y];
409a_X1=XY_mat\x_ima; %transformation matrix for X
410a_Y1=XY_mat\y_ima;%transformation matrix for X
411R=[a_X1(2),a_X1(3);a_Y1(2),a_Y1(3)];
412epsilon=sign(det(R));
413norm=abs(det(R));
414GeometryCalib.CalibrationType='linear';
415if (a_X1(2)/a_Y1(3))>0
416    GeometryCalib.fx_fy(1)=sqrt((a_X1(2)/a_Y1(3))*norm);
417else
418    GeometryCalib.fx_fy(1)=-sqrt(-(a_X1(2)/a_Y1(3))*norm);
419end
420GeometryCalib.fx_fy(2)=(a_Y1(3)/a_X1(2))*GeometryCalib.fx_fy(1);
421GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
422GeometryCalib.Tx_Ty_Tz=[a_X1(1)/GeometryCalib.fx_fy(1) a_Y1(1)/GeometryCalib.fx_fy(2) 1];
423R(1,:)=R(1,:)/GeometryCalib.fx_fy(1);
424R(2,:)=R(2,:)/GeometryCalib.fx_fy(2);
425R=[R;[0 0]];
426GeometryCalib.R=[R [0;0;-epsilon]];
427GeometryCalib.omc=(180/pi)*[acos(GeometryCalib.R(1,1)) 0 0];
428
429%------------------------------------------------------------------------
430% determine the tsai parameters for a view normal to the grid plane
431% NOT USED
432function GeometryCalib=calib_normal(Coord,handles)
433%------------------------------------------------------------------------
434Calib.f1=str2num(get(handles.fx,'String'));
435Calib.f2=str2num(get(handles.fy,'String'));
436Calib.k=str2num(get(handles.kc,'String'));
437Calib.Cx=str2num(get(handles.Cx,'String'));
438Calib.Cy=str2num(get(handles.Cy,'String'));
439%default
440if isempty(Calib.f1)
441    Calib.f1=25/0.012;
442end
443if isempty(Calib.f2)
444    Calib.f2=25/0.012;
445end
446if isempty(Calib.k)
447    Calib.k=0;
448end
449if isempty(Calib.Cx)||isempty(Calib.Cy)
450    huvmat=findobj(allchild(0),'Tag','uvmat');
451    hhuvmat=guidata(huvmat);
452    Calib.Cx=str2num(get(hhuvmat.num_Npx,'String'))/2;
453    Calib.Cx=str2num(get(hhuvmat.num_Npy,'String'))/2;
454end   
455%tsai parameters
456Calib.dpx=0.012;%arbitrary
457Calib.dpy=0.012;
458Calib.sx=Calib.f1*Calib.dpx/(Calib.f2*Calib.dpy);
459Calib.f=Calib.f2*Calib.dpy;
460Calib.kappa1=Calib.k/(Calib.f*Calib.f);
461
462%initial guess
463X=Coord(:,1);
464Y=Coord(:,2);
465Zmean=mean(Coord(:,3));
466x_ima=Coord(:,4)-Calib.Cx;
467y_ima=Coord(:,5)-Calib.Cy;
468XY_mat=[ones(size(X)) X Y];
469a_X1=XY_mat\x_ima; %transformation matrix for X
470a_Y1=XY_mat\y_ima;%transformation matrix for Y
471R=[a_X1(2),a_X1(3),0;a_Y1(2),a_Y1(3),0;0,0,-1];% rotation+ z axis reversal (upward)
472norm=sqrt(det(-R));
473calib_param(1)=0;% quadratic distortion
474calib_param(2)=a_X1(1);
475calib_param(3)=a_Y1(1);
476calib_param(4)=Calib.f/(norm*Calib.dpx)-R(3,3)*Zmean;
477calib_param(5)=angle(a_X1(2)+1i*a_X1(3));
478display(['initial guess=' num2str(calib_param)])
479
480%optimise the parameters: minimisation of error
481calib_param = fminsearch(@(calib_param) error_calib(calib_param,Calib,Coord),calib_param);
482
483GeometryCalib.CalibrationType='tsai_normal';
484GeometryCalib.focal=Calib.f;
485GeometryCalib.dpx_dpy=[Calib.dpx Calib.dpy];
486GeometryCalib.Cx_Cy=[Calib.Cx Calib.Cy];
487GeometryCalib.sx=Calib.sx;
488GeometryCalib.kappa1=calib_param(1);
489GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
490GeometryCalib.Tx_Ty_Tz=[calib_param(2) calib_param(3) calib_param(4)];
491alpha=calib_param(5);
492GeometryCalib.R=[cos(alpha) sin(alpha) 0;-sin(alpha) cos(alpha) 0;0 0 -1];
493
494%------------------------------------------------------------------------
495function GeometryCalib=calib_3D_linear(Coord,handles)
496%------------------------------------------------------------------
497path_uvmat=which('uvmat');% check the path detected for source file uvmat
498path_UVMAT=fileparts(path_uvmat); %path to UVMAT
499huvmat=findobj(allchild(0),'Tag','uvmat');
500hhuvmat=guidata(huvmat);
501coord_files=get(handles.ListCoordFiles,'String');
502if ischar(coord_files)
503    coord_files={coord_files};
504end
505if isempty(coord_files{1}) || isequal(coord_files,{''})
506    coord_files={};
507end
508%retrieve the calibration points stored in the files listed in the popup list ListCoordFiles
509x_1=Coord(:,4:5)';%px coordinates of the ref points
510nx=str2num(get(hhuvmat.num_Npx,'String'));
511ny=str2num(get(hhuvmat.num_Npy,'String'));
512x_1(2,:)=ny-x_1(2,:);%reverse the y image coordinates
513X_1=Coord(:,1:3)';%phys coordinates of the ref points
514n_ima=numel(coord_files)+1;
515if ~isempty(coord_files)
516    msgbox_uvmat('CONFIRMATION',['The xy coordinates of the calibration points in ' num2str(n_ima) ' planes will be used'])
517    for ifile=1:numel(coord_files)
518    t=xmltree(coord_files{ifile});
519    s=convert(t);%convert to matlab structure
520        if isfield(s,'GeometryCalib')
521            if isfield(s.GeometryCalib,'SourceCalib')
522                if isfield(s.GeometryCalib.SourceCalib,'PointCoord')
523                PointCoord=s.GeometryCalib.SourceCalib.PointCoord;
524                Coord_file=zeros(length(PointCoord),5);%default
525                for i=1:length(PointCoord)
526                    line=str2num(PointCoord{i});
527                    Coord_file(i,4:5)=line(4:5);%px x
528                    Coord_file(i,1:3)=line(1:3);%phys x
529                end
530                eval(['x_' num2str(ifile+1) '=Coord_file(:,4:5)'';']);
531                eval(['x_' num2str(ifile+1) '(2,:)=ny-x_' num2str(ifile+1) '(2,:);' ]);
532                eval(['X_' num2str(ifile+1) '=Coord_file(:,1:3)'';']);
533                end
534            end
535        end
536    end
537end
538n_ima=numel(coord_files)+1;
539est_dist=[0;0;0;0;0];
540est_aspect_ratio=0;
541est_fc=[1;1];
542center_optim=0;
543run(fullfile(path_UVMAT,'toolbox_calib','go_calib_optim'));% apply fct 'toolbox_calib/go_calib_optim'
544GeometryCalib.CalibrationType='3D_linear';
545GeometryCalib.fx_fy=fc';
546GeometryCalib.Cx_Cy=cc';
547GeometryCalib.kc=kc(1);
548GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
549GeometryCalib.Tx_Ty_Tz=Tc_1';
550GeometryCalib.R=Rc_1;
551GeometryCalib.R(2,1:3)=-GeometryCalib.R(2,1:3);%inversion of the y image coordinate
552GeometryCalib.Tx_Ty_Tz(2)=-GeometryCalib.Tx_Ty_Tz(2);%inversion of the y image coordinate
553GeometryCalib.Cx_Cy(2)=ny-GeometryCalib.Cx_Cy(2);%inversion of the y image coordinate
554GeometryCalib.omc=(180/pi)*omc_1;%angles in degrees
555GeometryCalib.ErrorRMS=[];
556GeometryCalib.ErrorMax=[];
557
558%------------------------------------------------------------------------
559function GeometryCalib=calib_3D_quadr(Coord,handles)
560%------------------------------------------------------------------
561
562path_uvmat=which('uvmat');% check the path detected for source file uvmat
563path_UVMAT=fileparts(path_uvmat); %path to UVMAT
564huvmat=findobj(allchild(0),'Tag','uvmat');
565hhuvmat=guidata(huvmat);
566% check_cond=0;
567coord_files=get(handles.ListCoordFiles,'String');
568if ischar(coord_files)
569    coord_files={coord_files};
570end
571if isempty(coord_files{1}) || isequal(coord_files,{''})
572    coord_files={};
573end
574
575%retrieve the calibration points stored in the files listed in the popup list ListCoordFiles
576x_1=Coord(:,4:5)';%px coordinates of the ref points
577nx=str2num(get(hhuvmat.num_Npx,'String'));
578ny=str2num(get(hhuvmat.num_Npy,'String'));
579x_1(2,:)=ny-x_1(2,:);%reverse the y image coordinates
580X_1=Coord(:,1:3)';%phys coordinates of the ref points
581n_ima=numel(coord_files)+1;
582if ~isempty(coord_files)
583    msgbox_uvmat('CONFIRMATION',['The xy coordinates of the calibration points in ' num2str(n_ima) ' planes will be used'])
584    for ifile=1:numel(coord_files)
585    t=xmltree(coord_files{ifile});
586    s=convert(t);%convert to matlab structure
587        if isfield(s,'GeometryCalib')
588            if isfield(s.GeometryCalib,'SourceCalib')
589                if isfield(s.GeometryCalib.SourceCalib,'PointCoord')
590                PointCoord=s.GeometryCalib.SourceCalib.PointCoord;
591                Coord_file=zeros(length(PointCoord),5);%default
592                for i=1:length(PointCoord)
593                    line=str2num(PointCoord{i});
594                    Coord_file(i,4:5)=line(4:5);%px x
595                    Coord_file(i,1:3)=line(1:3);%phys x
596                end
597                eval(['x_' num2str(ifile+1) '=Coord_file(:,4:5)'';']);
598                eval(['x_' num2str(ifile+1) '(2,:)=ny-x_' num2str(ifile+1) '(2,:);' ]);
599                eval(['X_' num2str(ifile+1) '=Coord_file(:,1:3)'';']);
600                end
601            end
602        end
603    end
604end
605n_ima=numel(coord_files)+1;
606est_dist=[1;0;0;0;0];
607est_aspect_ratio=1;
608center_optim=0;
609run(fullfile(path_UVMAT,'toolbox_calib','go_calib_optim'));% apply fct 'toolbox_calib/go_calib_optim'
610
611GeometryCalib.CalibrationType='3D_quadr';
612GeometryCalib.fx_fy=fc';
613GeometryCalib.Cx_Cy=cc';
614GeometryCalib.kc=kc(1);
615GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
616GeometryCalib.Tx_Ty_Tz=Tc_1';
617if ~exist('Rc_1','var')
618    msgbox_uvmat('ERROR',['calibration function ' fullfile('toolbox_calib','go_calib_optim') ' did not converge: use multiple views or option 3D_extrinsic'])
619    return
620end
621GeometryCalib.R=Rc_1;
622GeometryCalib.R(2,1:3)=-GeometryCalib.R(2,1:3);%inversion of the y image coordinate
623GeometryCalib.Tx_Ty_Tz(2)=-GeometryCalib.Tx_Ty_Tz(2);%inversion of the y image coordinate
624GeometryCalib.Cx_Cy(2)=ny-GeometryCalib.Cx_Cy(2);%inversion of the y image coordinate
625GeometryCalib.omc=(180/pi)*omc_1;%angles in degrees
626GeometryCalib.ErrorRMS=[];
627GeometryCalib.ErrorMax=[];
628
629
630%------------------------------------------------------------------------
631function GeometryCalib=calib_3D_extrinsic(Coord,handles)
632%------------------------------------------------------------------
633path_uvmat=which('geometry_calib');% check the path detected for source file uvmat
634path_UVMAT=fileparts(path_uvmat); %path to UVMAT
635x_1=double(Coord(:,4:5)');%image coordiantes
636X_1=double(Coord(:,1:3)');% phys coordinates
637huvmat=findobj(allchild(0),'Tag','uvmat');
638hhuvmat=guidata(huvmat);
639ny=str2double(get(hhuvmat.num_Npy,'String'));
640x_1(2,:)=ny-x_1(2,:);%reverse the y image coordinates
641n_ima=1;
642GeometryCalib.CalibrationType='3D_extrinsic';
643GeometryCalib.fx_fy(1)=str2num(get(handles.fx,'String'));
644GeometryCalib.fx_fy(2)=str2num(get(handles.fy,'String'));
645GeometryCalib.Cx_Cy(1)=str2num(get(handles.Cx,'String'));
646GeometryCalib.Cx_Cy(2)=str2num(get(handles.Cy,'String'));
647GeometryCalib.kc=str2num(get(handles.kc,'String'));
648fct_path=fullfile(path_UVMAT,'toolbox_calib');
649addpath(fct_path)
650GeometryCalib.Cx_Cy(2)=ny-GeometryCalib.Cx_Cy(2);%reverse Cx_Cy(2) for calibration (inversion of px ordinate)
651[omc,Tc1,Rc1,H,x,ex,JJ] = compute_extrinsic(x_1,X_1,...
652   (GeometryCalib.fx_fy)',GeometryCalib.Cx_Cy',[GeometryCalib.kc 0 0 0 0]);
653rmpath(fct_path);
654GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
655GeometryCalib.Tx_Ty_Tz=Tc1';
656%inversion of z axis
657GeometryCalib.R=Rc1;
658GeometryCalib.R(2,1:3)=-GeometryCalib.R(2,1:3);%inversion of the y image coordinate
659GeometryCalib.Tx_Ty_Tz(2)=-GeometryCalib.Tx_Ty_Tz(2);%inversion of the y image coordinate
660GeometryCalib.Cx_Cy(2)=ny-GeometryCalib.Cx_Cy(2);%inversion of the y image coordinate
661GeometryCalib.omc=(180/pi)*omc';
662%GeometryCalib.R(3,1:3)=-GeometryCalib.R(3,1:3);%inversion for z upward
663
664
665
666%------------------------------------------------------------------------
667%function GeometryCalib=calib_tsai_heikkila(Coord)
668% TEST: NOT IMPLEMENTED
669%------------------------------------------------------------------
670% path_uvmat=which('uvmat');% check the path detected for source file uvmat
671% path_UVMAT=fileparts(path_uvmat); %path to UVMAT
672% path_calib=fullfile(path_UVMAT,'toolbox_calib_heikkila');
673% addpath(path_calib)
674% npoints=size(Coord,1);
675% Coord(:,1:3)=10*Coord(:,1:3);
676% Coord=[Coord zeros(npoints,2) -ones(npoints,1)];
677% [par,pos,iter,res,er,C]=cacal('dalsa',Coord);
678% GeometryCalib.CalibrationType='tsai';
679% GeometryCalib.focal=par(2);
680
681
682%------------------------------------------------------------------------
683% --- determine the rms of calibration error
684function ErrorRms=error_calib(calib_param,Calib,Coord)
685%calib_param: vector of free calibration parameters (to optimise)
686%Calib: structure of the given calibration parameters
687%Coord: list of phys coordinates (columns 1-3, and pixel coordinates (columns 4-5)
688Calib.f=25;
689Calib.dpx=0.012;
690Calib.dpy=0.012;
691Calib.sx=1;
692Calib.Cx=512;
693Calib.Cy=512;
694Calib.kappa1=calib_param(1);
695Calib.Tx=calib_param(2);
696Calib.Ty=calib_param(3);
697Calib.Tz=calib_param(4);
698alpha=calib_param(5);
699Calib.R=[cos(alpha) sin(alpha) 0;-sin(alpha) cos(alpha) 0;0 0 -1];
700
701X=Coord(:,1);
702Y=Coord(:,2);
703Z=Coord(:,3);
704x_ima=Coord(:,4);
705y_ima=Coord(:,5);
706[Xpoints,Ypoints]=px_XYZ(Calib,X,Y,Z);
707ErrorRms(1)=sqrt(mean((Xpoints-x_ima).*(Xpoints-x_ima)));
708ErrorRms(2)=sqrt(mean((Ypoints-y_ima).*(Ypoints-y_ima)));
709ErrorRms=mean(ErrorRms);
710
711% %------------------------------------------------------------------------
712% function XImage_Callback(hObject, eventdata, handles)
713% %------------------------------------------------------------------------
714% update_list(hObject, eventdata,handles)
715%
716% %------------------------------------------------------------------------
717% function YImage_Callback(hObject, eventdata, handles)
718% %------------------------------------------------------------------------
719% update_list(hObject, eventdata,handles)
720
721%------------------------------------------------------------------------
722% --- Executes on button press in STORE.
723function STORE_Callback(hObject, eventdata, handles)
724Coord=get(handles.ListCoord,'Data');
725%Object=read_geometry_calib(Coord_cell);
726unitlist=get(handles.CoordUnit,'String');
727unit=unitlist{get(handles.CoordUnit,'value')};
728GeometryCalib.CoordUnit=unit;
729GeometryCalib.SourceCalib.PointCoord=Coord(:,1:5);
730huvmat=findobj(allchild(0),'Name','uvmat');
731hhuvmat=guidata(huvmat);%handles of elements in the GUI uvmat
732% RootPath='';
733% RootFile='';
734if ~isempty(hhuvmat.RootPath)&& ~isempty(hhuvmat.RootFile)
735%     testhandle=1;
736    RootPath=get(hhuvmat.RootPath,'String');
737    RootFile=get(hhuvmat.RootFile,'String');
738    filebase=fullfile(RootPath,RootFile);
739    while exist([filebase '.xml'],'file')
740        filebase=[filebase '~'];
741    end
742    outputfile=[filebase '.xml'];
743    errormsg=update_imadoc(GeometryCalib,outputfile,'GeometryCalib');
744    if ~strcmp(errormsg,'')
745        msgbox_uvmat('ERROR',errormsg);
746    end
747    listfile=get(handles.ListCoordFiles,'string');
748    if isequal(listfile,{''})
749        listfile={outputfile};
750    else
751        listfile=[listfile;{outputfile}];%update the list of coord files
752    end
753    set(handles.ListCoordFiles,'string',listfile);
754end
755set(handles.ListCoord,'Data',[])
756
757% --------------------------------------------------------------------
758% --- Executes on button press in CLEAR_PTS: clear the list of calibration points
759function CLEAR_PTS_Callback(hObject, eventdata, handles)
760% --------------------------------------------------------------------
761set(handles.ListCoord,'Data',[])
762PLOT_Callback(hObject, eventdata, handles)
763
764%------------------------------------------------------------------------
765% --- Executes on button press in CLEAR.
766function CLEAR_Callback(hObject, eventdata, handles)
767%------------------------------------------------------------------------
768set(handles.ListCoordFiles,'Value',1)
769set(handles.ListCoordFiles,'String',{''})
770
771%------------------------------------------------------------------------
772% --- Executes on selection change in CheckEnableMouse.
773function CheckEnableMouse_Callback(hObject, eventdata, handles)
774%------------------------------------------------------------------------
775choice=get(handles.CheckEnableMouse,'Value');
776if choice
777    set(handles.CheckEnableMouse,'BackgroundColor',[1 1 0])
778    huvmat=findobj(allchild(0),'tag','uvmat');
779    if ishandle(huvmat)
780        hhuvmat=guidata(huvmat);
781        if get(hhuvmat.CheckEditObject,'Value')
782        set(hhuvmat.CheckEditObject,'Value',0)
783        uvmat('CheckEditObject_Callback',hhuvmat.CheckEditObject,[],hhuvmat)
784        end
785    end
786else
787    set(handles.CheckEnableMouse,'BackgroundColor',[0.7 0.7 0.7])
788end
789
790
791% --------------------------------------------------------------------
792function MenuHelp_Callback(hObject, eventdata, handles)
793path_to_uvmat=which('uvmat');% check the path of uvmat
794pathelp=fileparts(path_to_uvmat);
795helpfile=fullfile(pathelp,'uvmat_doc','uvmat_doc.html');
796if isempty(dir(helpfile)), msgbox_uvmat('ERROR','Please put the help file uvmat_doc.html in the sub-directory /uvmat_doc of the UVMAT package')
797else
798   addpath (fullfile(pathelp,'uvmat_doc'))
799   web([helpfile '#geometry_calib'])
800end
801
802% --------------------------------------------------------------------
803function MenuSetScale_Callback(hObject, eventdata, handles)
804
805 answer=msgbox_uvmat('INPUT_TXT','scale pixel/cm?','');
806 %create test points
807 huvmat=findobj(allchild(0),'Name','uvmat');%find the current uvmat interface handle
808UvData=get(huvmat,'UserData');%Data associated to the current uvmat interface
809npy=size(UvData.Field.A,1);
810npx=size(UvData.Field.A,2);
811Xima=[0.25*npx 0.75*npx 0.75*npx 0.25*npx]';
812Yima=[0.25*npy 0.25*npy 0.75*npy 0.75*npy]';
813x=Xima/str2num(answer);
814y=Yima/str2num(answer);
815Coord=[x y zeros(4,1) Xima Yima zeros(4,1)];
816set(handles.ListCoord,'Data',Coord)
817set(handles.APPLY,'BackgroundColor',[1 0 1])
818
819%------------------------------------------------------------------------
820function MenuCreateGrid_Callback(hObject, eventdata, handles)
821%------------------------------------------------------------------------
822CalibData=get(handles.geometry_calib,'UserData');
823Tinput=[];%default
824if isfield(CalibData,'grid')
825    Tinput=CalibData.grid;
826end
827[T,CalibData.grid]=create_grid(Tinput);%display the GUI create_grid
828set(handles.geometry_calib,'UserData',CalibData)
829
830%grid in phys space
831Coord=get(handles.ListCoord,'Data');
832Coord(1:size(T,1),1:3)=T;%update the existing list of phys coordinates from the GUI create_grid
833set(handles.ListCoord,'Data',Coord)
834set(handles.APPLY,'BackgroundColor',[1 0 1])
835
836% -----------------------------------------------------------------------
837% --- automatic grid dectection from local maxima of the images
838function MenuDetectGrid_Callback(hObject, eventdata, handles)
839%------------------------------------------------------------------------
840%% read the four last point coordinates in pixels
841Coord=get(handles.ListCoord,'Data');%read list of coordinates on geometry_calib
842nbpoints=size(Coord,1); %nbre of calibration points
843if nbpoints~=4
844    msgbox_uvmat('ERROR','four points must have be selected by the mouse to delimitate the phys grid area; the Ox axis will be defined by the two first points')
845    return
846end
847corners_X=(Coord(end:-1:end-3,4)); %pixel absissa of the four corners
848corners_Y=(Coord(end:-1:end-3,5));
849
850%%%%%%
851%   corners_X=1000*[1.5415  1.7557 1.7539 1.5415]';
852%   corners_Y=1000*[1.1515 1.1509 1.3645  1.3639]';
853
854%reorder the last two points (the two first in the list) if needed
855angles=angle((corners_X-corners_X(1))+1i*(corners_Y-corners_Y(1)));
856if abs(angles(4)-angles(2))>abs(angles(3)-angles(2))
857      X_end=corners_X(4);
858      Y_end=corners_Y(4);
859      corners_X(4)=corners_X(3);
860      corners_Y(4)=corners_Y(3);
861      corners_X(3)=X_end;
862      corners_Y(3)=Y_end;
863end
864
865%% initiate the grid in phys coordinates
866CalibData=get(handles.geometry_calib,'UserData');%get information stored on the GUI geometry_calib
867grid_input=[];%default
868if isfield(CalibData,'grid')
869    grid_input=CalibData.grid;%retrieve the previously used grid
870end
871[T,CalibData.grid,CalibData.grid.CheckWhite]=create_grid(grid_input,'detect_grid');%display the GUI create_grid, read the set of phys coordinates T
872set(handles.geometry_calib,'UserData',CalibData)%store the phys grid parameters for later use
873X=[CalibData.grid.x_0 CalibData.grid.x_1 CalibData.grid.x_0 CalibData.grid.x_1]';%corner absissa in the phys coordinates (cm)
874Y=[CalibData.grid.y_0 CalibData.grid.y_0 CalibData.grid.y_1 CalibData.grid.y_1]';%corner ordinates in the phys coordinates (cm)
875
876%% read the current image, displayed in the GUI uvmat
877huvmat=findobj(allchild(0),'Name','uvmat');
878UvData=get(huvmat,'UserData');
879A=UvData.Field.A;%currently displayed image
880npxy=size(A);
881
882%% calculate transform matrices for plane projection: rectangle assumed to be viewed in perspective
883% reference: http://alumni.media.mit.edu/~cwren/interpolator/ by Christopher R. Wren
884B = [ X Y ones(size(X)) zeros(4,3)        -X.*corners_X -Y.*corners_X ...
885      zeros(4,3)        X Y ones(size(X)) -X.*corners_Y -Y.*corners_Y ];
886B = reshape (B', 8 , 8 )';
887D = [ corners_X , corners_Y ];
888D = reshape (D', 8 , 1 );
889l = (B' * B)\B' * D;
890Amat = reshape([l(1:6)' 0 0 1 ],3,3)';
891C = [l(7:8)' 1];
892
893%% transform grid image into 'phys' coordinates
894GeometryCalib.CalibrationType='3D_linear';
895GeometryCalib.fx_fy=[1 1];
896GeometryCalib.Tx_Ty_Tz=[Amat(1,3) Amat(2,3) 1];
897GeometryCalib.R=[Amat(1,1),Amat(1,2),0;Amat(2,1),Amat(2,2),0;C(1),C(2),0];
898GeometryCalib.CoordUnit='cm';
899path_uvmat=which('uvmat');% check the path detected for source file uvmat
900path_UVMAT=fileparts(path_uvmat); %path to UVMAT
901addpath(fullfile(path_UVMAT,'transform_field'))
902Data.ListVarName={'AY','AX','A'};
903Data.VarDimName={'AY','AX',{'AY','AX'}};
904if ndims(A)==3
905    A=mean(A,3);
906end
907Data.A=A-min(min(A));
908Data.AY=[npxy(1)-0.5 0.5];
909Data.AX=[0.5 npxy(2)];
910Data.CoordUnit='pixel';
911Calib.GeometryCalib=GeometryCalib;
912DataOut=phys(Data,Calib);
913rmpath(fullfile(path_UVMAT,'transform_field'))
914Amod=DataOut.A;% current image expressed in 'phys' coord
915Rangx=DataOut.AX;% x coordinates of first and last pixel centres in phys
916Rangy=DataOut.AY;% y coordinates of first and last pixel centres in phys
917if CalibData.grid.CheckWhite
918    Amod=double(Amod);%case of white grid markers: will look for image maxima
919else
920    Amod=-double(Amod);%case of black grid markers: will look for image minima
921end
922
923%% detection of local image extrema in each direction
924Dx=(Rangx(2)-Rangx(1))/(npxy(2)-1); %x mesh in real space
925Dy=(Rangy(2)-Rangy(1))/(npxy(1)-1); %y mesh in real space
926ind_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
927ind_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
928nbpoints=size(T,1);
929TIndex=ones(size(T));% image indices corresponding to point coordinates
930%look for image maxima around each expected grid point
931for ipoint=1:nbpoints
932    i0=1+round((T(ipoint,1)-Rangx(1))/Dx);% x index of the expected point in the phys image Amod
933    j0=1+round((T(ipoint,2)-Rangy(1))/Dy);% y index of the expected point in the phys image Amod
934    j0min=max(j0-ind_range_y,1);% min y index selected for the subimage (cut at the edge to avoid index <1)
935    j0max=min(j0+ind_range_y,size(Amod,1));% max y index selected for the subimage (cut at the edge to avoid index > size)
936    i0min=max(i0-ind_range_x,1);% min x index selected for the subimage (cut at the edge to avoid index <1)
937    i0max=min(i0+ind_range_x,size(Amod,2));% max x index selected for the subimage (cut at the edge to avoid index > size)
938    Asub=Amod(j0min:j0max,i0min:i0max); %subimage used to find brigthness extremum
939    x_profile=sum(Asub,1);%profile of subimage summed over y
940    y_profile=sum(Asub,2);%profile of subimage summed over x
941
942    [tild,ind_x_max]=max(x_profile);% index of max for the x profile
943    [tild,ind_y_max]=max(y_profile);% index of max for the y profile
944    %sub-pixel improvement using moments
945    x_shift=0;
946    y_shift=0;
947    if ind_x_max+2<=numel(x_profile) && ind_x_max-2>=1
948        Atop=x_profile(ind_x_max-2:ind_x_max+2);% extract x profile around the max
949        x_shift=sum(Atop.*[-2 -1 0 1 2])/sum(Atop);
950    end
951    if ind_y_max+2<=numel(y_profile) && ind_y_max-2>=1
952        Atop=y_profile(ind_y_max-2:ind_y_max+2);% extract y profile around the max
953        y_shift=sum(Atop.*[-2 -1 0 1 2]')/sum(Atop);
954    end
955        %%%%
956%     if ipoint==9
957%                 figure(11)
958%   imagesc(Asub)
959%     figure(12)
960%     plot(x_profile,'r')
961%     hold on
962%     plot(y_profile,'b')
963%     grid on
964%     end
965    %%%%
966    TIndex(ipoint,1)=(i0min+ind_x_max-1+x_shift);% x position of the maximum (in index of Amod)
967    TIndex(ipoint,2)=(j0min+ind_y_max-1+y_shift);% y position of the maximum (in index of Amod)
968end
969Tmod(:,1)=(TIndex(:,1)-1)*Dx+Rangx(1);
970Tmod(:,2)=(TIndex(:,2)-1)*Dy+Rangy(1);
971%Tmod=T(:,(1:2))+Delta;% 'phys' coordinates of the detected points
972[Xpx,Ypx]=px_XYZ(GeometryCalib,Tmod(:,1),Tmod(:,2));% image coordinates of the detected points
973Coord=[T Xpx Ypx zeros(size(T,1),1)];
974set(handles.ListCoord,'Data',Coord)
975PLOT_Callback(hObject, eventdata, handles)
976set(handles.APPLY,'BackgroundColor',[1 0 1])
977
978% figure(10)
979% hold off
980% imagesc(Rangx,Rangy,Amod)
981% hold on
982% plot(Tmod(:,1),Tmod(:,2),'+')
983
984
985%-----------------------------------------------------------------------
986function MenuTranslatePoints_Callback(hObject, eventdata, handles)
987%-----------------------------------------------------------------------
988%hcalib=get(handles.calib_type,'parent');%handles of the GUI geometry_calib
989CalibData=get(handles.geometry_calib,'UserData');
990Tinput=[];%default
991if isfield(CalibData,'translate')
992    Tinput=CalibData.translate;
993end
994T=translate_points(Tinput);%display translate_points GUI and get shift parameters
995CalibData.translate=T;
996set(handles.geometry_calib,'UserData',CalibData)
997%translation
998Coord=get(handles.ListCoord,'Data');
999Coord(:,1)=T(1)+Coord(:,1);
1000Coord(:,2)=T(2)+Coord(:,2);
1001Coord(:,3)=T(3)+Coord(:,3);
1002set(handles.ListCoord,'Data',Coord);
1003set(handles.APPLY,'BackgroundColor',[1 0 1])
1004
1005% --------------------------------------------------------------------
1006function MenuRotatePoints_Callback(hObject, eventdata, handles)
1007%hcalib=get(handles.calib_type,'parent');%handles of the GUI geometry_calib
1008CalibData=get(handles.geometry_calib,'UserData');
1009Tinput=[];%default
1010if isfield(CalibData,'rotate')
1011    Tinput=CalibData.rotate;
1012end
1013T=rotate_points(Tinput);%display rotate_points GUI to introduce rotation parameters
1014CalibData.rotate=T;
1015set(handles.geometry_calib,'UserData',CalibData)
1016%-----------------------------------------------------
1017%rotation
1018Phi=T(1);
1019O_x=0;%default
1020O_y=0;%default
1021if numel(T)>=2
1022    O_x=T(2);%default
1023end
1024if numel(T)>=3
1025    O_y=T(3);%default
1026end
1027Coord=get(handles.ListCoord,'Data');
1028r1=cos(pi*Phi/180);
1029r2=-sin(pi*Phi/180);
1030r3=sin(pi*Phi/180);
1031r4=cos(pi*Phi/180);
1032x=Coord(:,1)-O_x;
1033y=Coord(:,2)-O_y;
1034Coord(:,1)=r1*x+r2*y;
1035Coord(:,2)=r3*x+r4*y;
1036set(handles.ListCoord,'Data',Coord)
1037set(handles.APPLY,'BackgroundColor',[1 0 1])
1038
1039% --------------------------------------------------------------------
1040function MenuImportPoints_Callback(hObject, eventdata, handles)
1041fileinput=browse_xml(hObject, eventdata, handles);
1042if isempty(fileinput)
1043    return
1044end
1045[s,errormsg]=imadoc2struct(fileinput,'GeometryCalib');
1046if ~isfield(s,'GeometryCalib')
1047    msgbox_uvmat('ERROR','invalid input file: no geometry_calib data')
1048    return
1049end
1050GeometryCalib=s.GeometryCalib;
1051if ~(isfield(GeometryCalib,'SourceCalib')&&isfield(GeometryCalib.SourceCalib,'PointCoord'))
1052        msgbox_uvmat('ERROR','invalid input file: no calibration points')
1053    return
1054end
1055Coord=GeometryCalib.SourceCalib.PointCoord;
1056Coord=[Coord zeros(size(Coord,1),1)];
1057set(handles.ListCoord,'Data',Coord)
1058PLOT_Callback(handles.geometry_calib, [], handles)
1059set(handles.APPLY,'BackgroundColor',[1 0 1])
1060
1061% -----------------------------------------------------------------------
1062function MenuImportIntrinsic_Callback(hObject, eventdata, handles)
1063%------------------------------------------------------------------------
1064fileinput=browse_xml(hObject, eventdata, handles);
1065if isempty(fileinput)
1066    return
1067end
1068[s,errormsg]=imadoc2struct(fileinput,'GeometryCalib');
1069GeometryCalib=s.GeometryCalib;
1070display_intrinsic(GeometryCalib,handles)
1071
1072% -----------------------------------------------------------------------
1073function MenuImportAll_Callback(hObject, eventdata, handles)
1074%------------------------------------------------------------------------
1075fileinput=browse_xml(hObject, eventdata, handles);
1076if ~isempty(fileinput)
1077    loadfile(handles,fileinput)
1078end
1079
1080% -----------------------------------------------------------------------
1081% --- Executes on menubar option Import/Grid file: introduce previous grid files
1082function MenuGridFile_Callback(hObject, eventdata, handles)
1083% -----------------------------------------------------------------------
1084inputfile=browse_xml(hObject, eventdata, handles);
1085listfile=get(handles.ListCoordFiles,'String');
1086if isequal(listfile,{''})
1087    listfile={inputfile};
1088else
1089    listfile=[listfile;{inputfile}];%update the list of coord files
1090end
1091set(handles.ListCoordFiles,'string',listfile);
1092
1093
1094%------------------------------------------------------------------------
1095function fileinput=browse_xml(hObject, eventdata, handles)
1096%------------------------------------------------------------------------
1097fileinput=[];%default
1098oldfile=''; %default
1099UserData=get(handles.geometry_calib,'UserData');
1100if isfield(UserData,'XmlInputFile')
1101    oldfile=UserData.XmlInputFile;
1102end
1103[FileName, PathName, filterindex] = uigetfile( ...
1104       {'*.xml;*.mat', ' (*.xml,*.mat)';
1105       '*.xml',  '.xml files '; ...
1106        '*.mat',  '.mat matlab files '}, ...
1107        'Pick a file',oldfile);
1108fileinput=[PathName FileName];%complete file name
1109testblank=findstr(fileinput,' ');%look for blanks
1110if ~isempty(testblank)
1111    msgbox_uvmat('ERROR','forbidden input file name or path: no blank character allowed')
1112    return
1113end
1114sizf=size(fileinput);
1115if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end
1116UserData.XmlInputFile=fileinput;
1117set(handles.geometry_calib,'UserData',UserData)%record current file foer further use of browser
1118
1119% -----------------------------------------------------------------------
1120function Heading=loadfile(handles,fileinput)
1121%------------------------------------------------------------------------
1122Heading=[];%default
1123[s,errormsg]=imadoc2struct(fileinput,'Heading','GeometryCalib');
1124if ~isempty(errormsg)
1125    msgbox_uvmat('ERROR',errormsg)
1126    return
1127end
1128if ~isempty(s.Heading)
1129    Heading=s.Heading;
1130end
1131
1132GeometryCalib=s.GeometryCalib;
1133fx=1;fy=1;Cx=0;Cy=0;kc=0; %default
1134CoordCell={};
1135Tabchar={};%default
1136val_cal=1;%default
1137if ~isempty(GeometryCalib)
1138    % choose the calibration option
1139    if isfield(GeometryCalib,'CalibrationType')
1140        calib_list=get(handles.calib_type,'String');
1141        for ilist=1:numel(calib_list)
1142            if strcmp(calib_list{ilist},GeometryCalib.CalibrationType)
1143                val_cal=ilist;
1144                break
1145            end
1146        end
1147    end
1148    display_intrinsic(GeometryCalib,handles)%intrinsic param
1149    %extrinsic param
1150    if isfield(GeometryCalib,'Tx_Ty_Tz')
1151        Tx_Ty_Tz=GeometryCalib.Tx_Ty_Tz;
1152        set(handles.Tx,'String',num2str(GeometryCalib.Tx_Ty_Tz(1),4))
1153        set(handles.Ty,'String',num2str(GeometryCalib.Tx_Ty_Tz(2),4))
1154        set(handles.Tz,'String',num2str(GeometryCalib.Tx_Ty_Tz(3),4))
1155    end
1156    if isfield(GeometryCalib,'omc')
1157        set(handles.Phi,'String',num2str(GeometryCalib.omc(1),4))
1158        set(handles.Theta,'String',num2str(GeometryCalib.omc(2),4))
1159        set(handles.Psi,'String',num2str(GeometryCalib.omc(3),4))
1160    end
1161    if isfield(GeometryCalib,'SourceCalib')
1162        calib=GeometryCalib.SourceCalib.PointCoord;
1163        Coord=[calib zeros(size(calib,1),1)];
1164        set(handles.ListCoord,'Data',Coord)
1165    end
1166    PLOT_Callback(handles.geometry_calib, [], handles)
1167    set(handles.APPLY,'BackgroundColor',[1 0 1])
1168end
1169set(handles.calib_type,'Value',val_cal)
1170
1171if isempty(CoordCell)% allow mouse action by default in the absence of input points
1172    set(handles.CheckEnableMouse,'Value',1)
1173    set(handles.CheckEnableMouse,'BackgroundColor',[1 1 0])
1174else % does not allow mouse action by default in the presence of input points
1175    set(handles.CheckEnableMouse,'Value',0)
1176    set(handles.CheckEnableMouse,'BackgroundColor',[0.7 0.7 0.7])
1177end
1178
1179%------------------------------------------------------------------------
1180%---display calibration intrinsic parameters
1181function display_intrinsic(GeometryCalib,handles)
1182%------------------------------------------------------------------------
1183fx=[];
1184fy=[];
1185if isfield(GeometryCalib,'fx_fy')
1186    fx=GeometryCalib.fx_fy(1);
1187    fy=GeometryCalib.fx_fy(2);
1188end
1189Cx_Cy=[0 0];%default
1190if isfield(GeometryCalib,'Cx_Cy')
1191    Cx_Cy=GeometryCalib.Cx_Cy;
1192end
1193kc=0;
1194if isfield(GeometryCalib,'kc')
1195    kc=GeometryCalib.kc; %* GeometryCalib.focal*GeometryCalib.focal;
1196end
1197set(handles.fx,'String',num2str(fx,5))
1198set(handles.fy,'String',num2str(fy,5))
1199set(handles.Cx,'String',num2str(Cx_Cy(1),'%1.1f'))
1200set(handles.Cy,'String',num2str(Cx_Cy(2),'%1.1f'))
1201set(handles.kc,'String',num2str(kc,'%1.4f'))
1202
1203
1204% --- Executes when user attempts to close geometry_calib.
1205function geometry_calib_CloseRequestFcn(hObject, eventdata, handles)
1206
1207delete(hObject); % closes the figure
1208
1209%------------------------------------------------------------------------
1210% --- Executes on button press in PLOT.
1211%------------------------------------------------------------------------
1212function PLOT_Callback(hObject, eventdata, handles)
1213huvmat=findobj(allchild(0),'Name','uvmat');%find the current uvmat interface handle
1214hhuvmat=guidata(huvmat); %handles of GUI elements in uvmat
1215h_menu_coord=findobj(huvmat,'Tag','TransformName');
1216menu=get(h_menu_coord,'String');
1217choice=get(h_menu_coord,'Value');
1218if iscell(menu)
1219    option=menu{choice};
1220else
1221    option='px'; %default
1222end
1223Coord=get(handles.ListCoord,'Data');
1224if ~isempty(Coord)
1225    if isequal(option,'phys')
1226        Coord_plot=Coord(:,1:3);
1227    elseif isequal(option,'px')||isequal(option,'')
1228        Coord_plot=Coord(:,4:5);
1229    else
1230        msgbox_uvmat('ERROR','the choice in menu_coord of uvmat must be blank, px or phys ')
1231    end
1232end
1233
1234set(0,'CurrentFigure',huvmat)
1235set(huvmat,'CurrentAxes',hhuvmat.PlotAxes)
1236hh=findobj('Tag','calib_points');
1237if  ~isempty(Coord) && isempty(hh)
1238    hh=line(Coord_plot(:,1),Coord_plot(:,2),'Color','m','Tag','calib_points','LineStyle','.','Marker','+');
1239elseif isempty(Coord)%empty list of points, suppress the plot
1240    delete(hh)
1241else
1242    set(hh,'XData',Coord_plot(:,1))
1243    set(hh,'YData',Coord_plot(:,2))
1244end
1245pause(.1)
1246figure(handles.geometry_calib)
1247
1248%------------------------------------------------------------------------
1249% --- Executes on button press in Copy: display Coord on the Matlab work space
1250%------------------------------------------------------------------------
1251function Copy_Callback(hObject, eventdata, handles)
1252global Coord
1253evalin('base','global Coord')%make CurData global in the workspace
1254Coord=get(handles.ListCoord,'Data');
1255display('coordinates of calibration points (phys,px,marker) :')
1256evalin('base','Coord') %display CurData in the workspace
1257commandwindow; %brings the Matlab command window to the front
1258
1259%------------------------------------------------------------------------
1260% --- Executes when selected cell(s) is changed in ListCoord.
1261%------------------------------------------------------------------------
1262function ListCoord_CellSelectionCallback(hObject, eventdata, handles)
1263if ~isempty(eventdata.Indices)
1264    iline=eventdata.Indices(1);% selected line number
1265    Data=get(handles.ListCoord,'Data');
1266    Data(:,6)=zeros(size(Data,1),1);
1267    Data(iline,6)=-1;% mark the selected line
1268    set(handles.ListCoord,'Data',Data)
1269    update_calib_marker(Data(iline,:))
1270end
1271
1272%------------------------------------------------------------------------
1273% --- Executes when entered data in editable cell(s) in ListCoord.
1274%------------------------------------------------------------------------
1275function ListCoord_CellEditCallback(hObject, eventdata, handles)
1276
1277Input=str2num(eventdata.EditData);%pasted input
1278Coord=get(handles.ListCoord,'Data');
1279iline=eventdata.Indices(1);% selected line number
1280if size(Coord,1)<iline+numel(Input)
1281    Coord=[Coord ; zeros(iline+numel(Input)-size(Coord,1),6)];% append zeros to fit the new column
1282end
1283Coord(iline:iline+numel(Input)-1,eventdata.Indices(2))=Input';
1284set(handles.ListCoord,'Data',Coord)
1285PLOT_Callback(hObject, eventdata, handles)
1286
1287%------------------------------------------------------------------------
1288% --- 'key_press_fcn:' function activated when a key is pressed on the keyboard
1289%------------------------------------------------------------------------
1290function ListCoord_KeyPressFcn(hObject, eventdata, handles)
1291xx=double(get(handles.geometry_calib,'CurrentCharacter'));%get the keyboard character
1292if ismember(xx,[30 31 127])% arrow upward, downward, or delete
1293    Coord=get(handles.ListCoord,'Data');
1294    ind=find(Coord(:,6));%find the marker '+' for line selection
1295    Coord(:,6)=zeros(size(Coord,1),1);% desactivate the current line mark
1296    switch xx
1297        case 30 % arrow upward
1298            Coord(ind-1,6)=1;
1299        case 31% arrow downward
1300            Coord(ind+1,6)=1;
1301        case 127% remove line
1302            Coord(ind,:)=[];
1303            PLOT_Callback(hObject,eventdata,handles)
1304            set(handles.APPLY,'BackgroundColor',[1 0 1])
1305        otherwise
1306    end
1307    set(handles.ListCoord,'Data',Coord);
1308else
1309    set(handles.APPLY,'BackgroundColor',[1 0 1])
1310end
1311
1312
1313%------------------------------------------------------------------------
1314% --- update the plot of calibration points
1315%------------------------------------------------------------------------
1316function update_calib_marker(Coord)
1317%% update the plot on uvmat
1318huvmat=findobj(allchild(0),'Name','uvmat');%find the current uvmat interface handle
1319hplot=findobj(huvmat,'Tag','PlotAxes');%main plotting axis of uvmat
1320hhh=findobj(hplot,'Tag','calib_marker');
1321
1322h_menu_coord=findobj(huvmat,'Tag','TransformName');
1323menu=get(h_menu_coord,'String');
1324choice=get(h_menu_coord,'Value');
1325if iscell(menu)
1326    option=menu{choice};
1327else
1328    option='px'; %default
1329end
1330if isequal(option,'phys')
1331    XCoord=Coord(1);
1332    YCoord=Coord(2);
1333elseif isequal(option,'px')|| isequal(option,'')
1334    XCoord=Coord(4);
1335    YCoord=Coord(5);
1336else
1337    msgbox_uvmat('ERROR','the choice in menu_coord of uvmat must be blank, px or phys ')
1338end
1339if isempty(XCoord)||isempty(YCoord)
1340     if ~isempty(hhh)
1341        delete(hhh)%delete the circle marker
1342    end
1343    return
1344end
1345xlim=get(hplot,'XLim');
1346ylim=get(hplot,'YLim');
1347ind_range=max(abs(xlim(2)-xlim(1)),abs(ylim(end)-ylim(1)))/20;%defines the size of the circle marker
1348if isempty(hhh)
1349    set(0,'CurrentFig',huvmat)
1350    set(huvmat,'CurrentAxes',hplot)
1351    rectangle('Curvature',[1 1],...
1352              'Position',[XCoord-ind_range/2 YCoord-ind_range/2 ind_range ind_range],'EdgeColor','m',...
1353              'LineStyle','-','Tag','calib_marker');
1354else
1355    set(hhh,'Position',[XCoord-ind_range/2 YCoord-ind_range/2 ind_range ind_range])
1356end
1357
1358%------------------------------------------------------------------------
1359% --- Executes on button press in ClearLine: remove the selected line in the table Coord
1360%------------------------------------------------------------------------
1361function ClearLine_Callback(hObject, eventdata, handles)
1362
1363Coord=get(handles.ListCoord,'Data');
1364ind=find(Coord(:,6));%find the marker '-' for line selection
1365if isempty(ind)
1366    msgbox_uvmat('WARNING','no line suppressed, select a line in the table')
1367else
1368    answer=msgbox_uvmat('INPUT_Y-N',['suppress line ' num2str(ind) '?']);
1369    if isequal(answer,'Yes')
1370Coord(:,6)=zeros(size(Coord,1),1);% desactivate the current line mark
1371Coord(ind,:)=[];
1372PLOT_Callback(hObject,eventdata,handles)
1373set(handles.APPLY,'BackgroundColor',[1 0 1])
1374set(handles.ListCoord,'Data',Coord);
1375    end
1376end
Note: See TracBrowser for help on using the repository browser.