source: trunk/src/geometry_calib.m @ 1009

Last change on this file since 1009 was 1009, checked in by sommeria, 7 years ago

various bugs fixed

File size: 65.0 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%=======================================================================
13% Copyright 2008-2017, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
14%   http://www.legi.grenoble-inp.fr
15%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
16%
17%     This file is part of the toolbox UVMAT.
18%
19%     UVMAT is free software; you can redistribute it and/or modify
20%     it under the terms of the GNU General Public License as published
21%     by the Free Software Foundation; either version 2 of the license,
22%     or (at your option) any later version.
23%
24%     UVMAT is distributed in the hope that it will be useful,
25%     but WITHOUT ANY WARRANTY; without even the implied warranty of
26%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27%     GNU General Public License (see LICENSE.txt) for more details.
28%=======================================================================
29
30function varargout = geometry_calib(varargin)
31% GEOMETRY_CALIB M-file for geometry_calib.fig
32%      GEOMETRY_CALIB, by itself, creates a MenuCoord GEOMETRY_CALIB or raises the existing
33%      singleton*.
34%
35%      H = GEOMETRY_CALIB returns the handle to a MenuCoord GEOMETRY_CALIB or the handle to
36%      the existing singleton*.
37%
38%      GEOMETRY_CALIB('CALLBACK',hObject,eventData,handles,...) calls the local
39%      function named CALLBACK in GEOMETRY_CALIB.M with the given input arguments.
40%
41%      GEOMETRY_CALIB('Property','Value',...) creates a MenuCoord GEOMETRY_CALIB or raises the
42%      existing singleton*.  Starting from the left, property value pairs are
43%      applied to the GUI before geometry_calib_OpeningFunction gets called.  An
44%      unrecognized property name or invalid value makes property application
45%      stop.  All inputs are passed to geometry_calib_OpeningFcn via varargin.
46%
47%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
48%      instance to run (singleton)".
49%
50% See also: GUIDE, GUIDATA, GUIHANDLES
51
52% Edit the above text to modify the response to help geometry_calib
53
54% Last Modified by GUIDE v2.5 16-Apr-2015 17:29:02
55
56% Begin initialization code - DO NOT edit
57gui_Singleton = 1;
58gui_State = struct('gui_Name',       mfilename, ...
59                   'gui_Singleton',  gui_Singleton, ...
60                   'gui_OpeningFcn', @geometry_calib_OpeningFcn, ...
61                   'gui_OutputFcn',  @geometry_calib_OutputFcn, ...
62                   'gui_LayoutFcn',  [] , ...
63                   'gui_Callback',   []);
64if nargin
65   [pp,ff]=fileparts(which(varargin{1})); % name of the input file
66   if strcmp(ff,mfilename)% if we are activating a sub-function of geometry_calib
67   % ~isempty(regexp(varargin{1},'_Callback','once'))
68    gui_State.gui_Callback = str2func(varargin{1});
69   end
70end
71
72if nargout
73    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
74else
75    gui_mainfcn(gui_State, varargin{:});
76end
77% End initialization code - DO NOT edit
78
79
80% --- Executes just before geometry_calib is made visible.
81%INPUT:
82%handles: handles of the geometry_calib interface elements
83% PlotHandles: set of handles of the elements contolling the plotting
84% parameters on the uvmat interface (obtained by 'get_plot_handle.m')
85%------------------------------------------------------------------------
86function geometry_calib_OpeningFcn(hObject, eventdata, handles,inputfile)
87%------------------------------------------------------------------------
88% Choose default command line output for geometry_calib
89
90handles.output = hObject;
91
92% Update handles structure
93guidata(hObject, handles);
94set(hObject,'DeleteFcn',{@closefcn})%
95%set(hObject,'WindowButtonDownFcn',{'mouse_alt_gui',handles}) % allows mouse action with right button (zoom for uicontrol display)
96
97%% position
98set(0,'Unit','pixels')
99ScreenSize=get(0,'ScreenSize');% get the size of the screen, to put the fig on the upper right
100Left=ScreenSize(3)- 460; %right edge close to the right, with margin=40 (GUI width=420 px)
101if ScreenSize(4)>920
102    Height=840;%default height of the GUI
103    Bottom=ScreenSize(4)-Height-40; %put fig at top right
104else
105    Height=ScreenSize(4)-80;
106    Bottom=40; % GUI lies o the screen bottom (with margin =40)
107end
108set(handles.calib_type,'Position',[1 Height-40 194 30])%  rank 1
109set(handles.APPLY,'Position',[197 Height-40 110 30])%  rank 1
110set(handles.REPLICATE,'Position',[309 Height-40 110 30])%  rank 1
111set(handles.Intrinsic,'Position',[1 Height-40-2-92 418 92])%  rank 2
112set(handles.Extrinsic,'Position',[1 Height-40-4-92-75 418 75])%  rank 3
113set(handles.PointLists,'Position',[1 Height-40-6-92-75-117 418 117]) %  rank 4
114set(handles.CheckEnableMouse,'Position',[3 Height-362 208 30])%  rank 5
115set(handles.PLOT,'Position',[3 Height-394 120 30])%  rank 6
116set(handles.Copy,'Position',[151 Height-394 120 30])%  rank 6
117set(handles.ClearAll,'Position',[297 Height-394 120 30])%  rank 6
118set(handles.ClearPoint,'Position',[297 Height-362 120 30])%  rank 6
119set(handles.CoordLine,'Position',[211 Height-362 86 30])%  rank 6
120set(handles.phys_title,'Position',[38 Height-426 125 20])%  rank 7
121set(handles.CoordUnit,'Position',[151 Height-426 120 30])%  rank 7
122set(handles.px_title,'Position',[272 Height-426 125 20])%  rank 7
123set(handles.ListCoord,'Position',[1 20 418 Height-446])% rank 8
124set(handles.geometry_calib,'Position',[Left Bottom 420 Height])
125
126%set menu of calibration options
127set(handles.calib_type,'String',{'rescale';'linear';'3D_linear';'3D_quadr';'3D_extrinsic'})
128if exist('inputfile','var')&& ~isempty(inputfile)
129    [RootPath,SubDir,RootFile,tild,tild,tild,tild,FileExt]=fileparts_uvmat(inputfile);
130    struct.XmlInputFile=find_imadoc(RootPath,SubDir,RootFile,FileExt);
131    set(handles.ListCoord,'Data',[])
132    if exist(struct.XmlInputFile,'file')
133        Heading=loadfile(handles,struct.XmlInputFile);% load data from the xml file and fill the GUI
134        if isfield(Heading,'Campaign')&& ischar(Heading.Campaign)
135            struct.Campaign=Heading.Campaign;
136        end
137    end   
138    set(hObject,'UserData',struct)
139end
140
141%------------------------------------------------------------------------
142% --- Outputs from this function are returned to the command line.
143function varargout = geometry_calib_OutputFcn(~, eventdata, handles)
144%------------------------------------------------------------------------
145% Get default command line output from handles structure
146varargout{1} = handles.output;
147varargout{2}=handles;
148%
149%------------------------------------------------------------------------
150% executed when closing: set the parent interface button to value 0
151function closefcn(gcbo,eventdata)
152%------------------------------------------------------------------------
153huvmat=findobj(allchild(0),'Name','uvmat');
154if ~isempty(huvmat)
155    handles=guidata(huvmat);
156    set(handles.MenuCalib,'Checked','off')
157    hobject=findobj(handles.PlotAxes,'tag','calib_points');
158    if ~isempty(hobject)
159        delete(hobject)
160    end
161    hobject=findobj(handles.PlotAxes,'tag','calib_marker');
162    if ~isempty(hobject)
163        delete(hobject)
164    end   
165end
166
167%------------------------------------------------------------------------
168% --- Executes on button press APPLY (used to launch the calibration).
169function APPLY_Callback(hObject, eventdata, handles)
170set(handles.CheckEnableMouse,'Value',0)% desactivate mouse (to avoid spurious creation of new points)
171
172%------------------------------------------------------------------------
173%% look for the GUI uvmat and check for an image as input
174set(handles.APPLY,'BackgroundColor',[1 1 0])% paint APPLY button in yellow to show activation
175huvmat=findobj(allchild(0),'Name','uvmat');% look for the GUI uvmat
176hhuvmat=guidata(huvmat);%handles of elements in the GUI uvmat
177if ~strcmp(get(hhuvmat.Scalar,'Visible'),'on')
178    msgbox_uvmat('ERROR','An image needs to be opened in uvmat for calibration')
179  return
180end
181
182RootPath='';
183if ~isempty(hhuvmat.RootPath)&& ~isempty(hhuvmat.RootFile)
184    RootPath=get(hhuvmat.RootPath,'String');% path to the currently displayed image
185    SubDirBase=regexprep(get(hhuvmat.SubDir,'String'),'\..+$','');
186    outputfile=[fullfile(RootPath,SubDirBase) '.xml'];%xml file associated with the currently displayed image
187else
188    question={'save the calibration data and point coordinates in'};
189    def={fullfile(RootPath,'ObjectCalib.xml')};
190    options.Resize='on';
191    answer=inputdlg(question,'',1,def,options);
192    outputfile=answer{1};
193end
194
195%% read coordinates of the calibration poinnts: Coord(:,1-3) in phys, Coord(:,4-5) image
196Coord=get(handles.ListCoord,'Data');
197
198 
199%% read the type of calibration
200calib_cell=get(handles.calib_type,'String');
201val=get(handles.calib_type,'Value');
202CalibFcn=['calib_' calib_cell{val}];
203
204%% read the intrinsic parameters
205Intrinsic.Npx=str2num(get(hhuvmat.num_Npx,'String'));
206Intrinsic.Npy=str2num(get(hhuvmat.num_Npy,'String'));
207Intrinsic.coord_files=get(handles.ListCoordFiles,'String');
208Intrinsic.fx=str2num(get(handles.fx,'String'));
209Intrinsic.fy=str2num(get(handles.fy,'String'));
210Intrinsic.kc=str2num(get(handles.kc,'String'));
211Intrinsic.Cx=str2num(get(handles.Cx,'String'));
212Intrinsic.Cy=str2num(get(handles.Cy,'String'));
213if isempty(Intrinsic.kc)
214    Intrinsic.kc=0;
215end
216if isempty(Intrinsic.Cx)||isempty(Intrinsic.Cy)
217    Intrinsic.Cx=Intrinsic.Npx/2;
218    Intrinsic.Cy=Intrinsic.Npy/2;
219end 
220
221%% Apply calibration
222[GeometryCalib,index,ind_removed,Z_plane]=calibrate(Coord,CalibFcn,Intrinsic);% apply calibration
223
224%% record the coordinate unit
225unitlist=get(handles.CoordUnit,'String');
226unit=unitlist{get(handles.CoordUnit,'value')};
227GeometryCalib.CoordUnit=unit;
228
229%% record the coordinates of the calibration points
230GeometryCalib.SourceCalib.PointCoord=Coord;
231
232%% display calibration results on the GUI geometry_calib
233display_intrinsic(GeometryCalib,handles)%display calibration intrinsic parameters
234display_extrinsic(GeometryCalib,handles)%display calibration extrinsic parameters
235%     (rotation and translation of camera with  respect to the phys coordinates)
236
237%% store the calibration data, by default in the xml file of the currently displayed image
238answer=msgbox_uvmat('INPUT_Y-N',{'store calibration data';...
239    ['Error rms (along x,y)=' num2str(GeometryCalib.ErrorRms) ' pixels'];...
240    ['Error max (along x,y)=' num2str(GeometryCalib.ErrorMax) ' pixels'];
241    [num2str(numel(ind_removed)) ' points removed']});
242if strcmp(answer,'Yes') %store the calibration data
243    if strcmp(calib_cell{val}(1:2),'3D')%set the plane position for 3D (projection) calibration
244        msgbox_uvmat('CONFIRMATION',{['The current image series is assumed by default in the plane of the calib points z=' num2str(Z_plane) ] ; 'can be modified by MenuSetSlice in the upper bar menu of uvmat'})
245        GeometryCalib.SliceCoord=Z_plane'*[0 0 1];
246    end   
247else
248    GeometryCalib=[];
249    index=1;
250end
251
252if isempty(GeometryCalib) % if calibration cancelled
253    set(handles.APPLY,'BackgroundColor',[1 0 1])
254else   % if calibration confirmed
255   
256    %% copy the xml file from the old location if appropriate, then update with the calibration parameters
257    if ~exist(outputfile,'file') && ~isempty(SubDirBase)
258        oldxml=[fullfile(RootPath,SubDirBase,get(hhuvmat.RootFile,'String')) '.xml'];
259        if exist(oldxml,'file')
260            [success,message]=copyfile(oldxml,outputfile);%copy the old xml file to a new one with the new convention
261        end
262    end
263    errormsg=update_imadoc(GeometryCalib,outputfile,'GeometryCalib');% introduce the calibration data in the xml file
264    if ~strcmp(errormsg,'')
265        msgbox_uvmat('ERROR',errormsg);
266    end
267   
268    %% display image with new calibration in the currently opened uvmat interface
269    FieldList=get(hhuvmat.FieldName,'String');
270    val=get(hhuvmat.FieldName,'Value');
271    if strcmp(FieldList{val},'image')
272        set(hhuvmat.CheckFixLimits,'Value',0)% put FixedLimits option to 'off' to plot the whole image
273        UserData=get(handles.geometry_calib,'UserData');
274        UserData.XmlInputFile=outputfile;%save the current xml file name
275        set(handles.geometry_calib,'UserData',UserData)
276        uvmat('InputFileREFRESH_Callback',hObject,eventdata,hhuvmat); %file input with xml reading  in uvmat, show the image in phys coordinates
277        PLOT_Callback(hObject, eventdata, handles)
278        set(handles.CoordLine,'string',num2str(index))
279        Coord=get(handles.ListCoord,'Data');
280        update_calib_marker(Coord(index,:)); %indicate the point with max deviations from phys coord to calibration
281        figure(handles.geometry_calib)% put the GUI geometry_calib in front
282        set(handles.APPLY,'BackgroundColor',[1 0 0]) % set APPLY button to red color
283    else
284        msgbox_uvmat('WARNING','open the image to see the effect of the new calibration')
285    end
286end
287
288%------------------------------------------------------------------------
289% --- Executes on button press in REPLICATE
290function REPLICATE_Callback(hObject, eventdata, handles)
291%------------------------------------------------------------------------
292set(handles.CheckEnableMouse,'Value',0)% desactivate mouse (to avoid spurious creation of new points)
293
294%% look for the GUI uvmat and check for an image as input
295huvmat=findobj(allchild(0),'Name','uvmat');
296hhuvmat=guidata(huvmat);%handles of elements in the GUI uvmat
297
298%% read coordinates of the calibration poinnts: Coord(:,1-3) in phys, Coord(:,4-5) image
299Coord=get(handles.ListCoord,'Data');
300 
301%% read the type of calibration
302calib_cell=get(handles.calib_type,'String');
303val=get(handles.calib_type,'Value');
304CalibFcn=['calib_' calib_cell{val}];
305
306%% read the intrinsic parameters
307Intrinsic.Npx=str2num(get(hhuvmat.num_Npx,'String'));
308Intrinsic.Npy=str2num(get(hhuvmat.num_Npy,'String'));
309Intrinsic.coord_files=get(handles.ListCoordFiles,'String');
310Intrinsic.fx=str2num(get(handles.fx,'String'));
311Intrinsic.fy=str2num(get(handles.fy,'String'));
312Intrinsic.kc=str2num(get(handles.kc,'String'));
313Intrinsic.Cx=str2num(get(handles.Cx,'String'));
314Intrinsic.Cy=str2num(get(handles.Cy,'String'));
315if isempty(Intrinsic.kc)
316    Intrinsic.kc=0;
317end
318if isempty(Intrinsic.Cx)||isempty(Intrinsic.Cy)
319    Intrinsic.Cx=Intrinsic.Npx/2;
320    Intrinsic.Cy=Intrinsic.Npy/2;
321end
322
323%% apply to cropped images if requested
324answer=msgbox_uvmat('INPUT_Y-N','apply to cropped images?');
325if strcmp(answer,'Yes')
326    prompt = {'npy_lower'};
327    dlg_title = 'remove image the npy_lower image lines (removal of the upper linedoes not change calibration)';
328    num_lines= 1;
329    def     = {'0'};
330    answer = inputdlg(prompt,dlg_title,num_lines,def);
331    npy_crop=str2num(answer{1});
332    Intrinsic.Npy=Intrinsic.Npy-npy_crop; %size of the filtering window
333    Coord(:,5)=Coord(:,5)-npy_crop;% shift the image ordinates of the calibration points by removing the lower band
334end
335
336%% Apply calibration
337[GeometryCalib,index,ind_removed,Z_plane]=calibrate(Coord,CalibFcn,Intrinsic);% apply calibration
338
339
340%% record the coordinate unit
341unitlist=get(handles.CoordUnit,'String');
342unit=unitlist{get(handles.CoordUnit,'value')};
343GeometryCalib.CoordUnit=unit;
344
345%% record the coordinates of the calibration points
346GeometryCalib.SourceCalib.PointCoord=Coord;
347
348%% display calibration results on the GUI geometry_calib
349display_intrinsic(GeometryCalib,handles)%display calibration intrinsic parameters
350display_extrinsic(GeometryCalib,handles)%display calibration extrinsic parameters
351%     (rotation and translation of camera with  respect to the phys coordinates)
352
353%% store the calibration data, by default in the xml file of the currently displayed image
354answer=msgbox_uvmat('INPUT_Y-N',{'store calibration data';...
355    ['Error rms (along x,y)=' num2str(GeometryCalib.ErrorRms) ' pixels'];...
356    ['Error max (along x,y)=' num2str(GeometryCalib.ErrorMax) ' pixels'];...
357    [num2str(numel(ind_removed)) ' points removed']});
358if strcmp(answer,'Yes') %store the calibration data
359    if strcmp(calib_cell{val}(1:2),'3D')%set the plane position for 3D (projection) calibration
360        msgbox_uvmat('CONFIRMATION',{['The current image series is assumed by default in the plane of the calib points z=' num2str(Z_plane) ] ; 'can be modified by MenuSetSlice in the upper bar menu of uvmat'})
361        GeometryCalib.SliceCoord=Z_plane'*[0 0 1];
362    end   
363else
364    GeometryCalib=[];
365    index=1;
366end
367
368%% open the GUI browse_data
369CalibData=get(handles.geometry_calib,'UserData');%read the calibration image source on the interface userdata
370if isfield(CalibData,'XmlInputFile')
371    InputDir=fileparts(fileparts(CalibData.XmlInputFile));
372end
373DataSeries=uigetfile_uvmat('open a folder of images to calibrate',InputDir,'uigetdir');
374%SubProject=uigetfile_uvmat('open folder of subproject to calibrate',InputDir,'uigetdir');
375OutPut=browse_data(DataSeries);
376nbcalib=0;
377for ilist=1:numel(OutPut.Experiment)
378    SubDirBase=regexprep(OutPut.DataSeries{1},'\..+$','');
379    XmlName=fullfile(OutPut.Campaign,OutPut.Experiment{ilist},[SubDirBase '.xml']);
380    % copy the xml file from the old location if appropriate, then update with the calibration parameters
381    if ~exist(XmlName,'file') && ~isempty(SubDirBase)
382        oldxml=fullfile(OutPut.Campaign,OutPut.Experiment{ilist},SubDirBase,[get(hhuvmat.RootFile,'String') '.xml']);
383        if exist(oldxml,'file')
384            [success,message]=copyfile(oldxml,XmlName);%copy the old xml file to a new one with the new convention
385        end
386    end
387    errormsg=update_imadoc(GeometryCalib,XmlName,'GeometryCalib');% introduce the calibration data in the xml file
388    if ~strcmp(errormsg,'')
389        msgbox_uvmat('ERROR',errormsg);
390    else
391        display([XmlName ' updated with calibration parameters'])
392        nbcalib=nbcalib+1;
393    end
394end
395msgbox_uvmat('CONFIMATION',[SubDirBase ' calibrated for ' num2str(nbcalib) ' experiments']);
396
397%------------------------------------------------------------------------
398% --- activate calibration and store parameters in ouputfile .
399function [GeometryCalib,ind_max,ind_removed,Z_plane]=calibrate(Coord,CalibFcn,Intrinsic)
400%------------------------------------------------------------------------
401
402index=[];
403GeometryCalib=[];
404% apply the calibration, whose type is selected in  handles.calib_type
405if ~isempty(Coord)
406    GeometryCalib=feval(CalibFcn,Coord,Intrinsic);
407else
408    msgbox_uvmat('ERROR','No calibration points, abort')
409end
410if isempty(GeometryCalib)
411    return
412end
413Z_plane=[];
414
415% estimate calibration error rms and max
416X=Coord(:,1);
417Y=Coord(:,2);
418Z=Coord(:,3);
419x_ima=Coord(:,4);
420y_ima=Coord(:,5);
421[Xpoints,Ypoints]=px_XYZ(GeometryCalib,X,Y,Z);
422GeometryCalib.ErrorRms(1)=sqrt(mean((Xpoints-x_ima).*(Xpoints-x_ima)));
423GeometryCalib.ErrorRms(2)=sqrt(mean((Ypoints-y_ima).*(Ypoints-y_ima)));
424[ErrorMax(1),index(1)]=max(abs(Xpoints-x_ima));
425[ErrorMax(2),index(2)]=max(abs(Ypoints-y_ima));
426[tild,ind_dim]=max(ErrorMax);
427ind_max=index(ind_dim); % mark the index with maximum deviation
428
429% detect bad calibration points, marked by indices ind_bad, if the
430% difference of actual image coordinates and those given by calibration is
431% greater than 2 pixels and greater than 4 rms.
432check_x=abs(Xpoints-x_ima)>max(2,3*GeometryCalib.ErrorRms(1));
433check_y=abs(Ypoints-y_ima)>max(2,3*GeometryCalib.ErrorRms(2));
434ind_removed=find(check_x | check_y)
435% repeat calibration without the excluded points:
436if ~isempty(ind_removed)
437    Coord(ind_removed,:)=[];
438    GeometryCalib=feval(CalibFcn,Coord,Intrinsic);
439    X=Coord(:,1);
440    Y=Coord(:,2);
441    Z=Coord(:,3);
442    x_ima=Coord(:,4);
443    y_ima=Coord(:,5);
444    [Xpoints,Ypoints]=px_XYZ(GeometryCalib,X,Y,Z);
445    GeometryCalib.ErrorRms(1)=sqrt(mean((Xpoints-x_ima).*(Xpoints-x_ima)));
446    GeometryCalib.ErrorRms(2)=sqrt(mean((Ypoints-y_ima).*(Ypoints-y_ima)));
447end
448GeometryCalib.ErrorMax=ErrorMax;
449%set the Z position of the reference plane used for calibration
450if isequal(max(Z),min(Z))%Z constant
451    Z_plane=Z(1);
452    GeometryCalib.NbSlice=1;
453    GeometryCalib.SliceCoord=[0 0 Z_plane];
454end
455
456
457%------------------------------------------------------------------------
458% --- determine the parameters for a calibration by an affine function (rescaling and offset, no rotation)
459function GeometryCalib=calib_rescale(Coord,Intrinsic)
460%------------------------------------------------------------------------
461X=Coord(:,1);
462Y=Coord(:,2);% Z not used
463x_ima=Coord(:,4);
464y_ima=Coord(:,5);
465[px]=polyfit(X,x_ima,1);
466[py]=polyfit(Y,y_ima,1);
467% T_x=px(2);
468% T_y=py(2);
469GeometryCalib.CalibrationType='rescale';
470GeometryCalib.fx_fy=[px(1) py(1)];%.fx_fy corresponds to pxcm along x and y
471GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
472GeometryCalib.Tx_Ty_Tz=[px(2)/px(1) py(2)/py(1) 1];
473GeometryCalib.omc=[0 0 0];
474
475%------------------------------------------------------------------------
476% --- determine the parameters for a calibration by a linear transform matrix (rescale and rotation)
477function GeometryCalib=calib_linear(Coord,Intrinsic)
478%------------------------------------------------------------------------
479X=Coord(:,1);
480Y=Coord(:,2);% Z not used
481x_ima=Coord(:,4);
482y_ima=Coord(:,5);
483XY_mat=[ones(size(X)) X Y];
484a_X1=XY_mat\x_ima; %transformation matrix for X
485a_Y1=XY_mat\y_ima;%transformation matrix for X
486R=[a_X1(2),a_X1(3);a_Y1(2),a_Y1(3)];
487epsilon=sign(det(R));
488norm=abs(det(R));
489GeometryCalib.CalibrationType='linear';
490if (a_X1(2)/a_Y1(3))>0
491    GeometryCalib.fx_fy(1)=sqrt((a_X1(2)/a_Y1(3))*norm);
492else
493    GeometryCalib.fx_fy(1)=-sqrt(-(a_X1(2)/a_Y1(3))*norm);
494end
495GeometryCalib.fx_fy(2)=(a_Y1(3)/a_X1(2))*GeometryCalib.fx_fy(1);
496GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
497GeometryCalib.Tx_Ty_Tz=[a_X1(1)/GeometryCalib.fx_fy(1) a_Y1(1)/GeometryCalib.fx_fy(2) 1];
498R(1,:)=R(1,:)/GeometryCalib.fx_fy(1);
499R(2,:)=R(2,:)/GeometryCalib.fx_fy(2);
500R=[R;[0 0]];
501GeometryCalib.R=[R [0;0;-epsilon]];
502GeometryCalib.omc=(180/pi)*[acos(GeometryCalib.R(1,1)) 0 0];
503
504%------------------------------------------------------------------------
505% --- determine the tsai parameters for a view normal to the grid plane
506% NOT USED
507function GeometryCalib=calib_normal(Coord,Intrinsic)
508%------------------------------------------------------------------------
509Calib.fx=str2num(get(handles.fx,'String'));
510Calib.fy=str2num(get(handles.fy,'String'));
511Calib.kc=str2num(get(handles.kc,'String'));
512Calib.Cx=str2num(get(handles.Cx,'String'));
513Calib.Cy=str2num(get(handles.Cy,'String'));
514%default
515if isempty(Calib.fx)
516    Calib.fx=25/0.012;
517end
518if isempty(Calib.fy)
519    Calib.fy=25/0.012;
520end
521if isempty(Calib.kc)
522    Calib.kc=0;
523end
524if isempty(Calib.Cx)||isempty(Calib.Cy)
525    huvmat=findobj(allchild(0),'Tag','uvmat');
526    hhuvmat=guidata(huvmat);
527    Calib.Cx=str2num(get(hhuvmat.num_Npx,'String'))/2;
528    Calib.Cx=str2num(get(hhuvmat.num_Npy,'String'))/2;
529end   
530%tsai parameters
531Calib.dpx=0.012;%arbitrary
532Calib.dpy=0.012;
533Calib.sx=Calib.fx*Calib.dpx/(Calib.fy*Calib.dpy);
534Calib.f=Calib.fy*Calib.dpy;
535Calib.kappa1=Calib.kc/(Calib.f*Calib.f);
536
537%initial guess
538X=Coord(:,1);
539Y=Coord(:,2);
540Zmean=mean(Coord(:,3));
541x_ima=Coord(:,4)-Calib.Cx;
542y_ima=Coord(:,5)-Calib.Cy;
543XY_mat=[ones(size(X)) X Y];
544a_X1=XY_mat\x_ima; %transformation matrix for X
545a_Y1=XY_mat\y_ima;%transformation matrix for Y
546R=[a_X1(2),a_X1(3),0;a_Y1(2),a_Y1(3),0;0,0,-1];% rotation+ z axis reversal (upward)
547norm=sqrt(det(-R));
548calib_param(1)=0;% quadratic distortion
549calib_param(2)=a_X1(1);
550calib_param(3)=a_Y1(1);
551calib_param(4)=Calib.f/(norm*Calib.dpx)-R(3,3)*Zmean;
552calib_param(5)=angle(a_X1(2)+1i*a_X1(3));
553display(['initial guess=' num2str(calib_param)])
554
555%optimise the parameters: minimisation of error
556calib_param = fminsearch(@(calib_param) error_calib(calib_param,Calib,Coord),calib_param);
557
558GeometryCalib.CalibrationType='tsai_normal';
559GeometryCalib.focal=Calib.f;
560GeometryCalib.dpx_dpy=[Calib.dpx Calib.dpy];
561GeometryCalib.Cx_Cy=[Calib.Cx Calib.Cy];
562GeometryCalib.sx=Calib.sx;
563GeometryCalib.kappa1=calib_param(1);
564GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
565GeometryCalib.Tx_Ty_Tz=[calib_param(2) calib_param(3) calib_param(4)];
566alpha=calib_param(5);
567GeometryCalib.R=[cos(alpha) sin(alpha) 0;-sin(alpha) cos(alpha) 0;0 0 -1];
568
569%------------------------------------------------------------------------
570function GeometryCalib=calib_3D_linear(Coord,Intrinsic)
571%------------------------------------------------------------------------
572coord_files=Intrinsic.coord_files;
573if ischar(Intrinsic.coord_files)
574    coord_files={coord_files};
575end
576if isempty(coord_files{1}) || isequal(coord_files,{''})
577    coord_files={};
578end
579%retrieve the calibration points stored in the files listed in the popup list ListCoordFiles
580x_1=Coord(:,4:5)';%px coordinates of the ref points
581
582nx=Intrinsic.Npx;
583ny=Intrinsic.Npy;
584x_1(2,:)=ny-x_1(2,:);%reverse the y image coordinates
585X_1=Coord(:,1:3)';%phys coordinates of the ref points
586n_ima=numel(coord_files)+1;
587if ~isempty(coord_files)
588    msgbox_uvmat('CONFIRMATION',['The xy coordinates of the calibration points in ' num2str(n_ima) ' planes will be used'])
589    for ifile=1:numel(coord_files)
590    t=xmltree(coord_files{ifile});
591    s=convert(t);%convert to matlab structure
592        if isfield(s,'GeometryCalib')
593            if isfield(s.GeometryCalib,'SourceCalib')
594                if isfield(s.GeometryCalib.SourceCalib,'PointCoord')
595                PointCoord=s.GeometryCalib.SourceCalib.PointCoord;
596                Coord_file=zeros(length(PointCoord),5);%default
597                for i=1:length(PointCoord)
598                    line=str2num(PointCoord{i});
599                    Coord_file(i,4:5)=line(4:5);%px x
600                    Coord_file(i,1:3)=line(1:3);%phys x
601                end
602                eval(['x_' num2str(ifile+1) '=Coord_file(:,4:5)'';']);
603                eval(['x_' num2str(ifile+1) '(2,:)=ny-x_' num2str(ifile+1) '(2,:);' ]);
604                eval(['X_' num2str(ifile+1) '=Coord_file(:,1:3)'';']);
605                end
606            end
607        end
608    end
609end
610n_ima=numel(coord_files)+1;
611est_dist=[0;0;0;0;0];
612est_aspect_ratio=0;
613est_fc=[1;1];
614center_optim=0;
615path_uvmat=which('uvmat');% check the path detected for source file uvmat
616path_UVMAT=fileparts(path_uvmat); %path to UVMAT
617run(fullfile(path_UVMAT,'toolbox_calib','go_calib_optim'));% apply fct 'toolbox_calib/go_calib_optim'
618if exist('Rc_1','var')
619    GeometryCalib.CalibrationType='3D_linear';
620    GeometryCalib.fx_fy=fc';
621    GeometryCalib.Cx_Cy=cc';
622    GeometryCalib.kc=kc(1);
623    GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
624    GeometryCalib.Tx_Ty_Tz=Tc_1';
625    GeometryCalib.R=Rc_1;
626    GeometryCalib.R(2,1:3)=-GeometryCalib.R(2,1:3);%inversion of the y image coordinate
627    GeometryCalib.Tx_Ty_Tz(2)=-GeometryCalib.Tx_Ty_Tz(2);%inversion of the y image coordinate
628    GeometryCalib.Cx_Cy(2)=ny-GeometryCalib.Cx_Cy(2);%inversion of the y image coordinate
629    GeometryCalib.omc=(180/pi)*omc_1;%angles in degrees
630    GeometryCalib.ErrorRMS=[];
631    GeometryCalib.ErrorMax=[];
632else
633    msgbox_uvmat('ERROR',['calibration function ' fullfile('toolbox_calib','go_calib_optim') ' did not converge: use multiple views or option 3D_extrinsic'])
634    GeometryCalib=[];
635end
636
637%------------------------------------------------------------------------
638function GeometryCalib=calib_3D_quadr(Coord,Intrinsic)
639%------------------------------------------------------------------
640
641path_uvmat=which('uvmat');% check the path detected for source file uvmat
642path_UVMAT=fileparts(path_uvmat); %path to UVMAT
643huvmat=findobj(allchild(0),'Tag','uvmat');
644hhuvmat=guidata(huvmat);
645if ~strcmp(get(hhuvmat.Scalar,'Visible'),'on')
646    msgbox_uvmat('ERROR','An image needs to be opened in uvmat for calibration')
647  return
648end
649% check_cond=0;
650
651coord_files=Intrinsic.coord_files;
652
653if ischar(coord_files)
654    coord_files={coord_files};
655end
656if isempty(coord_files{1}) || isequal(coord_files,{''})
657    coord_files={};
658end
659
660%retrieve the calibration points stored in the files listed in the popup list ListCoordFiles
661x_1=Coord(:,4:5)';%px coordinates of the ref points
662nx=str2num(get(hhuvmat.num_Npx,'String'));
663ny=str2num(get(hhuvmat.num_Npy,'String'));
664x_1(2,:)=ny-x_1(2,:);%reverse the y image coordinates
665X_1=Coord(:,1:3)';%phys coordinates of the ref points
666n_ima=numel(coord_files)+1;
667if ~isempty(coord_files)
668    msgbox_uvmat('CONFIRMATION',['The xy coordinates of the calibration points in ' num2str(n_ima) ' planes will be used'])
669    for ifile=1:numel(coord_files)
670    t=xmltree(coord_files{ifile});
671    s=convert(t);%convert to matlab structure
672        if isfield(s,'GeometryCalib')
673            if isfield(s.GeometryCalib,'SourceCalib')
674                if isfield(s.GeometryCalib.SourceCalib,'PointCoord')
675                PointCoord=s.GeometryCalib.SourceCalib.PointCoord;
676                Coord_file=zeros(length(PointCoord),5);%default
677                for i=1:length(PointCoord)
678                    line=str2num(PointCoord{i});
679                    Coord_file(i,4:5)=line(4:5);%px x
680                    Coord_file(i,1:3)=line(1:3);%phys x
681                end
682                eval(['x_' num2str(ifile+1) '=Coord_file(:,4:5)'';']);
683                eval(['x_' num2str(ifile+1) '(2,:)=ny-x_' num2str(ifile+1) '(2,:);' ]);
684                eval(['X_' num2str(ifile+1) '=Coord_file(:,1:3)'';']);
685                end
686            end
687        end
688    end
689end
690n_ima=numel(coord_files)+1;
691est_dist=[1;0;0;0;0];
692est_aspect_ratio=1;
693center_optim=0;
694run(fullfile(path_UVMAT,'toolbox_calib','go_calib_optim'));% apply fct 'toolbox_calib/go_calib_optim'
695
696if exist('Rc_1','var')
697    GeometryCalib.CalibrationType='3D_quadr';
698    GeometryCalib.fx_fy=fc';
699    GeometryCalib.Cx_Cy=cc';
700    GeometryCalib.kc=kc(1);
701    GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
702    GeometryCalib.Tx_Ty_Tz=Tc_1';
703    GeometryCalib.R=Rc_1;
704    GeometryCalib.R(2,1:3)=-GeometryCalib.R(2,1:3);%inversion of the y image coordinate
705    GeometryCalib.Tx_Ty_Tz(2)=-GeometryCalib.Tx_Ty_Tz(2);%inversion of the y image coordinate
706    GeometryCalib.Cx_Cy(2)=ny-GeometryCalib.Cx_Cy(2);%inversion of the y image coordinate
707    GeometryCalib.omc=(180/pi)*omc_1;%angles in degrees
708    GeometryCalib.ErrorRMS=[];
709    GeometryCalib.ErrorMax=[];
710else
711    msgbox_uvmat('ERROR',['calibration function ' fullfile('toolbox_calib','go_calib_optim') ' did not converge: use multiple views or option 3D_extrinsic'])
712    GeometryCalib=[];
713end
714
715%------------------------------------------------------------------------
716function GeometryCalib=calib_3D_extrinsic(Coord, Intrinsic)
717%------------------------------------------------------------------
718path_uvmat=which('geometry_calib');% check the path detected for source file uvmat
719path_UVMAT=fileparts(path_uvmat); %path to UVMAT
720x_1=double(Coord(:,4:5)');%image coordinates
721X_1=double(Coord(:,1:3)');% phys coordinates
722huvmat=findobj(allchild(0),'Tag','uvmat');
723hhuvmat=guidata(huvmat);
724if ~strcmp(get(hhuvmat.Scalar,'Visible'),'on')
725    msgbox_uvmat('ERROR','An image needs to be opened in uvmat for calibration')
726  return
727end
728ny=str2double(get(hhuvmat.num_Npy,'String'));
729x_1(2,:)=ny-x_1(2,:);%reverse the y image coordinates
730n_ima=1;
731GeometryCalib.CalibrationType='3D_extrinsic';
732fx=Intrinsic.fx;
733fy=Intrinsic.fy;
734Cx=Intrinsic.Cx;
735Cy=Intrinsic.Cy;
736kc=Intrinsic.kc;
737errormsg='';
738if isempty(fx)
739    errormsg='focal length fx needs to be introduced';
740elseif isempty(fy)
741    errormsg='focal length fy needs to be introduced';
742elseif isempty(Cx)
743    errormsg='shift Cx to image centre needs to be introduced';
744elseif isempty(Cy)
745    errormsg='shift Cy to image centre needs to be introduced';
746end
747if ~isempty(errormsg)
748    GeometryCalib=[];
749    msgbox_uvmat('ERROR',errormsg)
750    return
751end
752GeometryCalib.fx_fy(1)=fx;
753GeometryCalib.fx_fy(2)=fy;
754GeometryCalib.Cx_Cy(1)=Cx;
755GeometryCalib.Cx_Cy(2)=Cy;
756GeometryCalib.kc=kc;
757fct_path=fullfile(path_UVMAT,'toolbox_calib');
758addpath(fct_path)
759GeometryCalib.Cx_Cy(2)=ny-GeometryCalib.Cx_Cy(2);%reverse Cx_Cy(2) for calibration (inversion of px ordinate)
760[omc,Tc1,Rc1,H,x,ex,JJ] = compute_extrinsic(x_1,X_1,...
761   (GeometryCalib.fx_fy)',GeometryCalib.Cx_Cy',[GeometryCalib.kc 0 0 0 0]);
762rmpath(fct_path);
763GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
764GeometryCalib.Tx_Ty_Tz=Tc1';
765%inversion of z axis
766GeometryCalib.R=Rc1;
767GeometryCalib.R(2,1:3)=-GeometryCalib.R(2,1:3);%inversion of the y image coordinate
768GeometryCalib.Tx_Ty_Tz(2)=-GeometryCalib.Tx_Ty_Tz(2);%inversion of the y image coordinate
769GeometryCalib.Cx_Cy(2)=ny-GeometryCalib.Cx_Cy(2);%inversion of the y image coordinate
770GeometryCalib.omc=(180/pi)*omc';
771
772%------------------------------------------------------------------------
773%function GeometryCalib=calib_tsai_heikkila(Coord)
774% TEST: NOT IMPLEMENTED
775%------------------------------------------------------------------
776% path_uvmat=which('uvmat');% check the path detected for source file uvmat
777% path_UVMAT=fileparts(path_uvmat); %path to UVMAT
778% path_calib=fullfile(path_UVMAT,'toolbox_calib_heikkila');
779% addpath(path_calib)
780% npoints=size(Coord,1);
781% Coord(:,1:3)=10*Coord(:,1:3);
782% Coord=[Coord zeros(npoints,2) -ones(npoints,1)];
783% [par,pos,iter,res,er,C]=cacal('dalsa',Coord);
784% GeometryCalib.CalibrationType='tsai';
785% GeometryCalib.focal=par(2);
786
787
788%------------------------------------------------------------------------
789% --- determine the rms of calibration error
790function ErrorRms=error_calib(calib_param,Calib,Coord)
791%------------------------------------------------------------------------
792%calib_param: vector of free calibration parameters (to optimise)
793%Calib: structure of the given calibration parameters
794%Coord: list of phys coordinates (columns 1-3, and pixel coordinates (columns 4-5)
795Calib.f=25;
796Calib.dpx=0.012;
797Calib.dpy=0.012;
798Calib.sx=1;
799Calib.Cx=512;
800Calib.Cy=512;
801Calib.kappa1=calib_param(1);
802Calib.Tx=calib_param(2);
803Calib.Ty=calib_param(3);
804Calib.Tz=calib_param(4);
805alpha=calib_param(5);
806Calib.R=[cos(alpha) sin(alpha) 0;-sin(alpha) cos(alpha) 0;0 0 -1];
807
808X=Coord(:,1);
809Y=Coord(:,2);
810Z=Coord(:,3);
811x_ima=Coord(:,4);
812y_ima=Coord(:,5);
813[Xpoints,Ypoints]=px_XYZ(Calib,X,Y,Z);
814ErrorRms(1)=sqrt(mean((Xpoints-x_ima).*(Xpoints-x_ima)));
815ErrorRms(2)=sqrt(mean((Ypoints-y_ima).*(Ypoints-y_ima)));
816ErrorRms=mean(ErrorRms);
817
818
819%------------------------------------------------------------------------
820% --- Executes on button press in STORE.
821function STORE_Callback(hObject, eventdata, handles)
822%------------------------------------------------------------------------
823Coord=get(handles.ListCoord,'Data');
824%Object=read_geometry_calib(Coord_cell);
825unitlist=get(handles.CoordUnit,'String');
826unit=unitlist{get(handles.CoordUnit,'value')};
827GeometryCalib.CoordUnit=unit;
828GeometryCalib.SourceCalib.PointCoord=Coord(:,1:5);
829huvmat=findobj(allchild(0),'Name','uvmat');
830hhuvmat=guidata(huvmat);%handles of elements in the GUI uvmat
831% RootPath='';
832% RootFile='';
833if ~isempty(hhuvmat.RootPath)&& ~isempty(hhuvmat.RootFile)
834%     testhandle=1;
835    RootPath=get(hhuvmat.RootPath,'String');
836    RootFile=get(hhuvmat.RootFile,'String');
837    filebase=[fullfile(RootPath,RootFile) '~'];
838    while exist([filebase '.xml'],'file')
839        filebase=[filebase '~'];
840    end
841    outputfile=[filebase '.xml'];
842    errormsg=update_imadoc(GeometryCalib,outputfile,'GeometryCalib');
843    if ~strcmp(errormsg,'')
844        msgbox_uvmat('ERROR',errormsg);
845    end
846    listfile=get(handles.ListCoordFiles,'string');
847    if isequal(listfile,{''})
848        listfile={outputfile};
849    else
850        listfile=[listfile;{outputfile}];%update the list of coord files
851    end
852    set(handles.ListCoordFiles,'string',listfile);
853end
854ClearAll_Callback(hObject, eventdata, handles)% clear the current list and point plots
855
856% --------------------------------------------------------------------
857% --- Executes on button press in ClearAll: clear the list of calibration points
858function ClearAll_Callback(hObject, eventdata, handles)
859% --------------------------------------------------------------------
860set(handles.ListCoord,'Data',[])
861PLOT_Callback(hObject, eventdata, handles)
862update_calib_marker([]);%remove circle marker
863set(handles.APPLY,'backgroundColor',[1 0 0])% set APPLY button to red color: no calibration wanted without points
864set(handles.CheckEnableMouse,'value',1); %activate mouse to pick new points
865
866%------------------------------------------------------------------------
867% --- Executes on button press in CLEAR LIST
868function CLEAR_Callback(hObject, eventdata, handles)
869%------------------------------------------------------------------------
870set(handles.ListCoordFiles,'Value',1)
871set(handles.ListCoordFiles,'String',{''})
872
873%------------------------------------------------------------------------
874% --- Executes on selection change in CheckEnableMouse.
875function CheckEnableMouse_Callback(hObject, eventdata, handles)
876%------------------------------------------------------------------------
877choice=get(handles.CheckEnableMouse,'Value');
878if choice
879    huvmat=findobj(allchild(0),'tag','uvmat');
880    if ishandle(huvmat)
881        hhuvmat=guidata(huvmat);
882        set(hhuvmat.MenuRuler,'checked','off')%desactivate ruler
883        if get(hhuvmat.CheckEditObject,'Value')
884            set(hhuvmat.CheckEditObject,'Value',0)
885            uvmat('CheckEditObject_Callback',hhuvmat.CheckEditObject,[],hhuvmat)
886        end
887        set(hhuvmat.MenuRuler,'checked','off')%desactivate ruler
888    end
889end
890
891% --------------------------------------------------------------------
892function MenuHelp_Callback(hObject, eventdata, handles)
893web('http://servforge.legi.grenoble-inp.fr/projects/soft-uvmat/wiki/UvmatHelp#GeometryCalib')
894
895% --------------------------------------------------------------------
896function MenuSetScale_Callback(hObject, eventdata, handles)
897
898answer=msgbox_uvmat('INPUT_TXT','scale pixel/cm?','');
899%create test points
900huvmat=findobj(allchild(0),'Name','uvmat');%find the current uvmat interface handle
901hhuvmat=guidata(huvmat);
902if ~strcmp(get(hhuvmat.Scalar,'Visible'),'on')
903    msgbox_uvmat('ERROR','An image needs to be opened in uvmat for calibration')
904    return
905end
906set(handles.calib_type,'Value',1)% set calib option to 'rescale'
907npx=str2num(get(hhuvmat.num_Npx,'String'))/2;
908npy=str2num(get(hhuvmat.num_Npy,'String'))/2;
909Xima=[0.25*npx 0.75*npx 0.75*npx 0.25*npx]';
910Yima=[0.25*npy 0.25*npy 0.75*npy 0.75*npy]';
911x=Xima/str2num(answer);
912y=Yima/str2num(answer);
913Coord=[x y zeros(4,1) Xima Yima zeros(4,1)];
914set(handles.ListCoord,'Data',Coord)
915set(handles.APPLY,'BackgroundColor',[1 0 1])
916set(handles.CheckEnableMouse,'value',0); %desactivate mouse to avoid spurious points
917
918%------------------------------------------------------------------------
919function MenuCreateGrid_Callback(hObject, eventdata, handles)
920%------------------------------------------------------------------------
921CalibData=get(handles.geometry_calib,'UserData');
922Tinput=[];%default
923if isfield(CalibData,'grid')
924    Tinput=CalibData.grid;
925end
926[T,CalibData.grid]=create_grid(Tinput);%display the GUI create_grid
927set(handles.geometry_calib,'UserData',CalibData)
928
929%grid in phys space
930Coord=get(handles.ListCoord,'Data');
931Coord(1:size(T,1),1:3)=T;%update the existing list of phys coordinates from the GUI create_grid
932set(handles.ListCoord,'Data',Coord)
933set(handles.APPLY,'BackgroundColor',[1 0 1])
934set(handles.CheckEnableMouse,'value',0); %desactivate mouse to avoid spurious points
935
936% -----------------------------------------------------------------------
937% --- automatic grid dectection from local maxima of the images
938function MenuDetectGrid_Callback(hObject, eventdata, handles)
939%------------------------------------------------------------------------
940%% read the four last point coordinates in pixels
941Coord=get(handles.ListCoord,'Data');%read list of coordinates on geometry_calib
942nbpoints=size(Coord,1); %nbre of calibration points
943if nbpoints~=4
944    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')
945    return
946end
947% corners_X=(Coord(end:-1:end-3,4)); %pixel absissa of the four corners
948% corners_Y=(Coord(end:-1:end-3,5));
949corners_X=(Coord(:,4)); %pixel absissa of the four corners
950corners_Y=(Coord(:,5));
951
952%%%%%%
953%   corners_X=1000*[1.5415  1.7557 1.7539 1.5415]';
954%   corners_Y=1000*[1.1515 1.1509 1.3645  1.3639]';
955
956%reorder the last two points (the two first in the list) if needed
957angles=angle((corners_X-corners_X(1))+1i*(corners_Y-corners_Y(1)));
958if abs(angles(4)-angles(2))>abs(angles(3)-angles(2))
959      X_end=corners_X(4);
960      Y_end=corners_Y(4);
961      corners_X(4)=corners_X(3);
962      corners_Y(4)=corners_Y(3);
963      corners_X(3)=X_end;
964      corners_Y(3)=Y_end;
965end
966
967%% initiate the grid in phys coordinates
968CalibData=get(handles.geometry_calib,'UserData');%get information stored on the GUI geometry_calib
969grid_input=[];%default
970if isfield(CalibData,'grid')
971    grid_input=CalibData.grid;%retrieve the previously used grid
972end
973[T,CalibData.grid,CalibData.grid.CheckWhite,CalibData.grid.FilterWindow]=create_grid(grid_input,'detect_grid');%display the GUI create_grid, read the set of phys coordinates T
974set(handles.geometry_calib,'UserData',CalibData)%store the phys grid parameters for later use
975X=[CalibData.grid.x_0 CalibData.grid.x_1 CalibData.grid.x_0 CalibData.grid.x_1]';%corner absissa in the phys coordinates (cm)
976Y=[CalibData.grid.y_0 CalibData.grid.y_0 CalibData.grid.y_1 CalibData.grid.y_1]';%corner ordinates in the phys coordinates (cm)
977
978%% read the current image, displayed in the GUI uvmat
979huvmat=findobj(allchild(0),'Name','uvmat');
980UvData=get(huvmat,'UserData');
981A=UvData.Field.A;%currently displayed image
982npxy=size(A);
983
984%% calculate transform matrices for plane projection: rectangle assumed to be viewed in perspective
985% reference: http://alumni.media.mit.edu/~cwren/interpolator/ by Christopher R. Wren
986B = [ X Y ones(size(X)) zeros(4,3)        -X.*corners_X -Y.*corners_X ...
987      zeros(4,3)        X Y ones(size(X)) -X.*corners_Y -Y.*corners_Y ];
988B = reshape (B', 8 , 8 )';
989D = [ corners_X , corners_Y ];
990D = reshape (D', 8 , 1 );
991l = (B' * B)\B' * D;
992Amat = reshape([l(1:6)' 0 0 1 ],3,3)';
993C = [l(7:8)' 1];
994
995%% transform grid image into 'phys' coordinates
996GeometryCalib.CalibrationType='3D_linear';
997GeometryCalib.fx_fy=[1 1];
998GeometryCalib.Tx_Ty_Tz=[Amat(1,3) Amat(2,3) 1];
999GeometryCalib.R=[Amat(1,1),Amat(1,2),0;Amat(2,1),Amat(2,2),0;C(1),C(2),0];
1000GeometryCalib.CoordUnit='cm';
1001path_uvmat=which('uvmat');% check the path detected for source file uvmat
1002path_UVMAT=fileparts(path_uvmat); %path to UVMAT
1003addpath(fullfile(path_UVMAT,'transform_field'))
1004Data.ListVarName={'Coord_y','Coord_x','A'};
1005Data.VarDimName={'Coord_y','Coord_x',{'Coord_y','Coord_x'}};
1006if ndims(A)==3
1007    A=mean(A,3);
1008end
1009Data.A=A-min(min(A));
1010Data.Coord_y=[npxy(1)-0.5 0.5];
1011Data.Coord_x=[0.5 npxy(2)];
1012Data.CoordUnit='pixel';
1013Calib.GeometryCalib=GeometryCalib;
1014DataOut=phys(Data,Calib);
1015rmpath(fullfile(path_UVMAT,'transform_field'))
1016Amod=DataOut.A;% current image expressed in 'phys' coord
1017Rangx=DataOut.Coord_x;% x coordinates of first and last pixel centres in phys
1018Rangy=DataOut.Coord_y;% y coordinates of first and last pixel centres in phys
1019if CalibData.grid.CheckWhite
1020    Amod=double(Amod);%case of white grid markers: will look for image maxima
1021else
1022    Amod=-double(Amod);%case of black grid markers: will look for image minima
1023end
1024
1025%%%%%%filterfor i;proved detection of dots
1026if ~isequal(CalibData.grid.FilterWindow,0)
1027    %definition of the cos shape matrix filter
1028    FilterBoxSize_x=CalibData.grid.FilterWindow;
1029    FilterBoxSize_y=CalibData.grid.FilterWindow;
1030    ix=1/2-FilterBoxSize_x/2:-1/2+FilterBoxSize_x/2;%
1031    iy=1/2-FilterBoxSize_y/2:-1/2+FilterBoxSize_y/2;%
1032    %del=np/3;
1033    %fct=exp(-(ix/del).^2);
1034    fct2_x=cos(ix/((FilterBoxSize_x-1)/2)*pi/2);
1035    fct2_y=cos(iy/((FilterBoxSize_y-1)/2)*pi/2);
1036    %Mfiltre=(ones(5,5)/5^2);
1037    Mfiltre=fct2_y'*fct2_x;
1038    Mfiltre=Mfiltre/(sum(sum(Mfiltre)));%normalize filter
1039   
1040    if ndims(Amod)==3
1041        Amod=filter2(Mfiltre,sum(Amod,3));%filter the input image, after summation on the color component (for color images)
1042    else
1043        Amod=filter2(Mfiltre,Amod);
1044    end
1045end
1046%%%%%%%%%%%%%%
1047
1048
1049%% detection of local image extrema in each direction
1050Dx=(Rangx(2)-Rangx(1))/(npxy(2)-1); %x mesh in real space
1051Dy=(Rangy(2)-Rangy(1))/(npxy(1)-1); %y mesh in real space
1052ind_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
1053ind_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
1054nbpoints=size(T,1);
1055TIndex=ones(size(T));% image indices corresponding to point coordinates
1056%look for image maxima around each expected grid point
1057for ipoint=1:nbpoints
1058    i0=1+round((T(ipoint,1)-Rangx(1))/Dx);% x index of the expected point in the phys image Amod
1059    j0=1+round((T(ipoint,2)-Rangy(1))/Dy);% y index of the expected point in the phys image Amod
1060    j0min=max(j0-ind_range_y,1);% min y index selected for the subimage (cut at the edge to avoid index <1)
1061    j0max=min(j0+ind_range_y,size(Amod,1));% max y index selected for the subimage (cut at the edge to avoid index > size)
1062    i0min=max(i0-ind_range_x,1);% min x index selected for the subimage (cut at the edge to avoid index <1)
1063    i0max=min(i0+ind_range_x,size(Amod,2));% max x index selected for the subimage (cut at the edge to avoid index > size)
1064    Asub=Amod(j0min:j0max,i0min:i0max); %subimage used to find brigthness extremum
1065    x_profile=sum(Asub,1);%profile of subimage summed over y
1066    y_profile=sum(Asub,2);%profile of subimage summed over x
1067
1068    [tild,ind_x_max]=max(x_profile);% index of max for the x profile
1069    [tild,ind_y_max]=max(y_profile);% index of max for the y profile
1070    %sub-pixel improvement using moments
1071    x_shift=0;
1072    y_shift=0;
1073    if ind_x_max+2<=numel(x_profile) && ind_x_max-2>=1
1074        Atop=x_profile(ind_x_max-2:ind_x_max+2);% extract x profile around the max
1075        x_shift=sum(Atop.*[-2 -1 0 1 2])/sum(Atop);
1076    end
1077    if ind_y_max+2<=numel(y_profile) && ind_y_max-2>=1
1078        Atop=y_profile(ind_y_max-2:ind_y_max+2);% extract y profile around the max
1079        y_shift=sum(Atop.*[-2 -1 0 1 2]')/sum(Atop);
1080    end
1081        %%%%
1082%     if ipoint==9
1083%                 figure(11)
1084%   imagesc(Asub)
1085%     figure(12)
1086%     plot(x_profile,'r')
1087%     hold on
1088%     plot(y_profile,'b')
1089%     grid on
1090%     end
1091    %%%%
1092    TIndex(ipoint,1)=(i0min+ind_x_max-1+x_shift);% x position of the maximum (in index of Amod)
1093    TIndex(ipoint,2)=(j0min+ind_y_max-1+y_shift);% y position of the maximum (in index of Amod)
1094end
1095Tmod(:,1)=(TIndex(:,1)-1)*Dx+Rangx(1);
1096Tmod(:,2)=(TIndex(:,2)-1)*Dy+Rangy(1);
1097%Tmod=T(:,(1:2))+Delta;% 'phys' coordinates of the detected points
1098[Xpx,Ypx]=px_XYZ(GeometryCalib,Tmod(:,1),Tmod(:,2));% image coordinates of the detected points
1099Coord=[T Xpx Ypx zeros(size(T,1),1)];
1100set(handles.ListCoord,'Data',Coord)
1101PLOT_Callback(hObject, eventdata, handles)
1102set(handles.APPLY,'BackgroundColor',[1 0 1])
1103set(handles.CheckEnableMouse,'value',0); %desactivate mouse to avoid spurious points
1104
1105
1106%-----------------------------------------------------------------------
1107function MenuTranslatePoints_Callback(hObject, eventdata, handles)
1108%-----------------------------------------------------------------------
1109%hcalib=get(handles.calib_type,'parent');%handles of the GUI geometry_calib
1110CalibData=get(handles.geometry_calib,'UserData');
1111Tinput=[];%default
1112if isfield(CalibData,'translate')
1113    Tinput=CalibData.translate;
1114end
1115T=translate_points(Tinput);%display translate_points GUI and get shift parameters
1116CalibData.translate=T;
1117set(handles.geometry_calib,'UserData',CalibData)
1118%translation
1119Coord=get(handles.ListCoord,'Data');
1120Coord(:,1)=T(1)+Coord(:,1);
1121Coord(:,2)=T(2)+Coord(:,2);
1122Coord(:,3)=T(3)+Coord(:,3);
1123set(handles.ListCoord,'Data',Coord);
1124set(handles.APPLY,'BackgroundColor',[1 0 1])
1125
1126% --------------------------------------------------------------------
1127function MenuRotatePoints_Callback(hObject, eventdata, handles)
1128%hcalib=get(handles.calib_type,'parent');%handles of the GUI geometry_calib
1129CalibData=get(handles.geometry_calib,'UserData');
1130Tinput=[];%default
1131if isfield(CalibData,'rotate')
1132    Tinput=CalibData.rotate;
1133end
1134T=rotate_points(Tinput);%display rotate_points GUI to introduce rotation parameters
1135CalibData.rotate=T;
1136set(handles.geometry_calib,'UserData',CalibData)
1137%-----------------------------------------------------
1138%rotation
1139Phi=T(1);
1140O_x=0;%default
1141O_y=0;%default
1142if numel(T)>=2
1143    O_x=T(2);%default
1144end
1145if numel(T)>=3
1146    O_y=T(3);%default
1147end
1148Coord=get(handles.ListCoord,'Data');
1149r1=cos(pi*Phi/180);
1150r2=-sin(pi*Phi/180);
1151r3=sin(pi*Phi/180);
1152r4=cos(pi*Phi/180);
1153x=Coord(:,1)-O_x;
1154y=Coord(:,2)-O_y;
1155Coord(:,1)=r1*x+r2*y;
1156Coord(:,2)=r3*x+r4*y;
1157set(handles.ListCoord,'Data',Coord)
1158set(handles.APPLY,'BackgroundColor',[1 0 1])
1159
1160% --------------------------------------------------------------------
1161function MenuFlip_x_Callback(hObject, eventdata, handles)
1162Coord=get(handles.ListCoord,'Data');
1163Coord(:,1)=-Coord(:,1);
1164set(handles.ListCoord,'Data',Coord)
1165
1166% --------------------------------------------------------------------
1167function MenuFlip_y_Callback(hObject, eventdata, handles)
1168Coord=get(handles.ListCoord,'Data');
1169Coord(:,2)=-Coord(:,2);
1170set(handles.ListCoord,'Data',Coord)
1171
1172% --------------------------------------------------------------------
1173function MenuImportPoints_Callback(hObject, eventdata, handles)
1174fileinput=browse_xml(hObject, eventdata, handles);
1175if isempty(fileinput)
1176    return
1177end
1178[s,errormsg]=imadoc2struct(fileinput,'GeometryCalib');
1179if ~isfield(s,'GeometryCalib')
1180    msgbox_uvmat('ERROR','invalid input file: no geometry_calib data')
1181    return
1182end
1183GeometryCalib=s.GeometryCalib;
1184if ~(isfield(GeometryCalib,'SourceCalib')&&isfield(GeometryCalib.SourceCalib,'PointCoord'))
1185        msgbox_uvmat('ERROR','invalid input file: no calibration points')
1186    return
1187end
1188Coord=GeometryCalib.SourceCalib.PointCoord;
1189Coord=[Coord zeros(size(Coord,1),1)];
1190set(handles.ListCoord,'Data',Coord)
1191PLOT_Callback(handles.geometry_calib, [], handles)
1192set(handles.APPLY,'BackgroundColor',[1 0 1])
1193set(handles.CheckEnableMouse,'value',0); %desactivate mouse to avoid modifications by default
1194
1195% -----------------------------------------------------------------------
1196function MenuImportIntrinsic_Callback(hObject, eventdata, handles)
1197%------------------------------------------------------------------------
1198fileinput=browse_xml(hObject, eventdata, handles);
1199if isempty(fileinput)
1200    return
1201end
1202[s,errormsg]=imadoc2struct(fileinput,'GeometryCalib');
1203GeometryCalib=s.GeometryCalib;
1204display_intrinsic(GeometryCalib,handles)
1205
1206% -----------------------------------------------------------------------
1207function MenuImportAll_Callback(hObject, eventdata, handles)
1208%------------------------------------------------------------------------
1209fileinput=browse_xml(hObject, eventdata, handles);
1210if ~isempty(fileinput)
1211    loadfile(handles,fileinput)
1212end
1213set(handles.CheckEnableMouse,'value',0); %desactivate mouse to avoid modifications by default
1214
1215% -----------------------------------------------------------------------
1216% --- Executes on menubar option Import/Grid file: introduce previous grid files
1217function MenuGridFile_Callback(hObject, eventdata, handles)
1218% -----------------------------------------------------------------------
1219inputfile=browse_xml(hObject, eventdata, handles);
1220listfile=get(handles.ListCoordFiles,'String');
1221if isequal(listfile,{''})
1222    listfile={inputfile};
1223else
1224    listfile=[listfile;{inputfile}];%update the list of coord files
1225end
1226set(handles.ListCoordFiles,'String',listfile);
1227
1228
1229%------------------------------------------------------------------------
1230function fileinput=browse_xml(hObject, eventdata, handles)
1231%------------------------------------------------------------------------
1232fileinput=[];%default
1233oldfile=''; %default
1234UserData=get(handles.geometry_calib,'UserData');
1235if isfield(UserData,'XmlInputFile')
1236    oldfile=UserData.XmlInputFile;
1237end
1238[FileName, PathName, filterindex] = uigetfile( ...
1239       {'*.xml;*.mat', ' (*.xml,*.mat)';
1240       '*.xml',  '.xml files '; ...
1241        '*.mat',  '.mat matlab files '}, ...
1242        'Pick a file',oldfile);
1243fileinput=[PathName FileName];%complete file name
1244testblank=findstr(fileinput,' ');%look for blanks
1245if ~isempty(testblank)
1246    msgbox_uvmat('ERROR','forbidden input file name or path: no blank character allowed')
1247    return
1248end
1249sizf=size(fileinput);
1250if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end
1251UserData.XmlInputFile=fileinput;
1252set(handles.geometry_calib,'UserData',UserData)%record current file foer further use of browser
1253
1254% -----------------------------------------------------------------------
1255function Heading=loadfile(handles,fileinput)
1256%------------------------------------------------------------------------
1257Heading=[];%default
1258[s,errormsg]=imadoc2struct(fileinput,'Heading','GeometryCalib');
1259if ~isempty(errormsg)
1260    msgbox_uvmat('ERROR',errormsg)
1261    return
1262end
1263if ~isempty(s.Heading)
1264    Heading=s.Heading;
1265end
1266
1267GeometryCalib=s.GeometryCalib;
1268fx=1;fy=1;Cx=0;Cy=0;kc=0; %default
1269CoordCell={};
1270Tabchar={};%default
1271val_cal=1;%default
1272if ~isempty(GeometryCalib)
1273    % choose the calibration option
1274    if isfield(GeometryCalib,'CalibrationType')
1275        calib_list=get(handles.calib_type,'String');
1276        for ilist=1:numel(calib_list)
1277            if strcmp(calib_list{ilist},GeometryCalib.CalibrationType)
1278                val_cal=ilist;
1279                break
1280            end
1281        end
1282    end
1283    display_intrinsic(GeometryCalib,handles)%intrinsic param
1284    %extrinsic param
1285    if isfield(GeometryCalib,'Tx_Ty_Tz')
1286        Tx_Ty_Tz=GeometryCalib.Tx_Ty_Tz;
1287        set(handles.Tx,'String',num2str(GeometryCalib.Tx_Ty_Tz(1),4))
1288        set(handles.Ty,'String',num2str(GeometryCalib.Tx_Ty_Tz(2),4))
1289        set(handles.Tz,'String',num2str(GeometryCalib.Tx_Ty_Tz(3),4))
1290    end
1291    if isfield(GeometryCalib,'omc')
1292        set(handles.Phi,'String',num2str(GeometryCalib.omc(1),4))
1293        set(handles.Theta,'String',num2str(GeometryCalib.omc(2),4))
1294        set(handles.Psi,'String',num2str(GeometryCalib.omc(3),4))
1295    end
1296    if isfield(GeometryCalib,'SourceCalib')&& isfield(GeometryCalib.SourceCalib,'PointCoord')
1297        calib=GeometryCalib.SourceCalib.PointCoord;
1298        Coord=[calib zeros(size(calib,1),1)];
1299        set(handles.ListCoord,'Data',Coord)
1300    end
1301    PLOT_Callback(handles.geometry_calib, [], handles)
1302    set(handles.APPLY,'BackgroundColor',[1 0 1])
1303end
1304set(handles.calib_type,'Value',val_cal)
1305
1306if isempty(CoordCell)% allow mouse action by default in the absence of input points
1307    set(handles.CheckEnableMouse,'Value',1)
1308    set(handles.CheckEnableMouse,'BackgroundColor',[1 1 0])
1309else % does not allow mouse action by default in the presence of input points
1310    set(handles.CheckEnableMouse,'Value',0)
1311    set(handles.CheckEnableMouse,'BackgroundColor',[0.7 0.7 0.7])
1312end
1313
1314%------------------------------------------------------------------------
1315%---display calibration intrinsic parameters
1316function display_intrinsic(GeometryCalib,handles)
1317%------------------------------------------------------------------------
1318fx=[];
1319fy=[];
1320if isfield(GeometryCalib,'fx_fy')
1321    fx=GeometryCalib.fx_fy(1);
1322    fy=GeometryCalib.fx_fy(2);
1323end
1324Cx_Cy=[0 0];%default
1325if isfield(GeometryCalib,'Cx_Cy')
1326    Cx_Cy=GeometryCalib.Cx_Cy;
1327end
1328kc=0;
1329if isfield(GeometryCalib,'kc')
1330    kc=GeometryCalib.kc; %* GeometryCalib.focal*GeometryCalib.focal;
1331end
1332set(handles.fx,'String',num2str(fx,5))
1333set(handles.fy,'String',num2str(fy,5))
1334set(handles.Cx,'String',num2str(Cx_Cy(1),'%1.1f'))
1335set(handles.Cy,'String',num2str(Cx_Cy(2),'%1.1f'))
1336set(handles.kc,'String',num2str(kc,'%1.4f'))
1337
1338%------------------------------------------------------------------------
1339% ---display calibration extrinsic parameters
1340function display_extrinsic(GeometryCalib,handles)
1341%------------------------------------------------------------------------
1342set(handles.Tx,'String',num2str(GeometryCalib.Tx_Ty_Tz(1),4))
1343set(handles.Ty,'String',num2str(GeometryCalib.Tx_Ty_Tz(2),4))
1344set(handles.Tz,'String',num2str(GeometryCalib.Tx_Ty_Tz(3),4))
1345set(handles.Phi,'String',num2str(GeometryCalib.omc(1),4))
1346set(handles.Theta,'String',num2str(GeometryCalib.omc(2),4))
1347set(handles.Psi,'String',num2str(GeometryCalib.omc(3),4))
1348
1349%------------------------------------------------------------------------
1350% --- executes when user attempts to close geometry_calib.
1351function geometry_calib_CloseRequestFcn(hObject, eventdata, handles)
1352%------------------------------------------------------------------------
1353delete(hObject); % closes the figure
1354
1355%------------------------------------------------------------------------
1356% --- executes on button press in PLOT.
1357%------------------------------------------------------------------------
1358function PLOT_Callback(hObject, eventdata, handles)
1359huvmat=findobj(allchild(0),'Name','uvmat');%find the current uvmat interface handle
1360hhuvmat=guidata(huvmat); %handles of GUI elements in uvmat
1361h_menu_coord=findobj(huvmat,'Tag','TransformName');
1362menu=get(h_menu_coord,'String');
1363choice=get(h_menu_coord,'Value');
1364option='';
1365if iscell(menu)
1366    option=menu{choice};
1367end
1368Coord=get(handles.ListCoord,'Data');
1369if ~isempty(Coord)
1370    if isequal(option,'phys')
1371        Coord_plot=Coord(:,1:3);
1372    elseif isempty(option);%optionoption,'px')||isequal(option,'')
1373        Coord_plot=Coord(:,4:5);
1374    else
1375        msgbox_uvmat('ERROR','the choice in menu_coord of uvmat must be blank or phys ')
1376    end
1377end
1378
1379set(0,'CurrentFigure',huvmat)
1380set(huvmat,'CurrentAxes',hhuvmat.PlotAxes)
1381hh=findobj('Tag','calib_points');
1382if  ~isempty(Coord) && isempty(hh)
1383    hh=line(Coord_plot(:,1),Coord_plot(:,2),'Color','m','Tag','calib_points','LineStyle','none','Marker','+','MarkerSize',10);
1384elseif isempty(Coord)%empty list of points, suppress the plot
1385    delete(hh)
1386else
1387    set(hh,'XData',Coord_plot(:,1))
1388    set(hh,'YData',Coord_plot(:,2))
1389end
1390pause(.1)
1391figure(handles.geometry_calib)
1392
1393%------------------------------------------------------------------------
1394% --- Executes on button press in Copy: display Coord on the Matlab work space
1395%------------------------------------------------------------------------
1396function Copy_Callback(hObject, eventdata, handles)
1397global Coord
1398evalin('base','global Coord')%make CurData global in the workspace
1399Coord=get(handles.ListCoord,'Data');
1400display('coordinates of calibration points (phys,px,marker) :')
1401evalin('base','Coord') %display CurData in the workspace
1402commandwindow; %brings the Matlab command window to the front
1403
1404%------------------------------------------------------------------------
1405% --- Executes when selected cell(s) is changed in ListCoord.
1406%------------------------------------------------------------------------
1407function ListCoord_CellSelectionCallback(hObject, eventdata, handles)
1408if ~isempty(eventdata.Indices)
1409    iline=eventdata.Indices(1);% selected line number
1410    set(handles.CoordLine,'String',num2str(iline))
1411     Data=get(handles.ListCoord,'Data');
1412     update_calib_marker(Data(iline,:))
1413end
1414
1415%------------------------------------------------------------------------
1416% --- Executes when entered data in editable cell(s) in ListCoord.
1417%------------------------------------------------------------------------
1418function ListCoord_CellEditCallback(hObject, eventdata, handles)
1419
1420Input=str2num(eventdata.EditData);%pasted input
1421Coord=get(handles.ListCoord,'Data');
1422iline=eventdata.Indices(1);% selected line number
1423if size(Coord,1)<iline+numel(Input)
1424    Coord=[Coord ; zeros(iline+numel(Input)-size(Coord,1),6)];% append zeros to fit the new column
1425end
1426Coord(iline:iline+numel(Input)-1,eventdata.Indices(2))=Input';
1427set(handles.ListCoord,'Data',Coord)
1428PLOT_Callback(hObject, eventdata, handles)
1429
1430%------------------------------------------------------------------------
1431% --- 'key_press_fcn:' function activated when a key is pressed on the keyboard
1432%------------------------------------------------------------------------
1433function ListCoord_KeyPressFcn(hObject, eventdata, handles)
1434iline=str2num(get(handles.CoordLine,'String'));
1435xx=double(get(handles.geometry_calib,'CurrentCharacter'));%get the keyboard character
1436if ismember(xx,[28 29 30 31])% directional arrow
1437    Coord=get(handles.ListCoord,'Data');
1438    switch xx
1439        case 30 % arrow upward
1440            iline=iline-1;
1441        case 31% arrow downward
1442            iline=iline+1;
1443    end
1444    if iline>=1 && iline<=size(Coord,1)
1445        set(handles.CoordLine,'String',num2str(iline))
1446        update_calib_marker(Coord(iline,1:5))% show the point corresponding to the selected line
1447    end
1448else
1449    set(handles.APPLY,'BackgroundColor',[1 0 1])% paint APPLY in magenta to indicate that the table content has be modified
1450    if ismember(xx,[127 31])% delete, or downward
1451        Coord=get(handles.ListCoord,'Data');
1452        iline=str2double(get(handles.CoordLine,'String'));
1453        if isequal(xx, 31)
1454            if isequal(iline,size(Coord,1))% arrow downward
1455                Coord=[Coord;zeros(1,size(Coord,2))];
1456            end
1457        else
1458            Coord(iline,:)=[];% suppress the current line
1459        end
1460        set(handles.ListCoord,'Data',Coord);
1461    end
1462end
1463
1464%------------------------------------------------------------------------
1465% --- update the plot of calibration points
1466%------------------------------------------------------------------------
1467% draw a circle around the point defined by the input coordinates Coord as given by line in the table Listcoord
1468function update_calib_marker(Coord)
1469
1470%% read config on uvmat
1471huvmat=findobj(allchild(0),'Name','uvmat');%find the current uvmat interface handle
1472hhuvmat=guidata(huvmat);
1473hhh=findobj(hhuvmat.PlotAxes,'Tag','calib_marker');
1474if numel(Coord)<5
1475    if ~isempty(hhh)
1476        delete(hhh)%delete the circle marker in case of no valid input
1477    end
1478    return
1479end
1480menu=get(hhuvmat.TransformName,'String');
1481choice=get(hhuvmat.TransformName,'Value');
1482option='';
1483if iscell(menu)
1484    option=menu{choice};
1485end
1486
1487%% read appropriate coordinates (px or phys) in the table ListCoord
1488if isequal(option,'phys') % use phys coord
1489    XCoord=Coord(1);
1490    YCoord=Coord(2);
1491elseif isempty(option)% use coord in pixels
1492    XCoord=Coord(4);
1493    YCoord=Coord(5);
1494else
1495    msgbox_uvmat('ERROR','the choice in menu_coord of uvmat must be blank or phys ')
1496    return
1497end
1498
1499%% adjust the plot limits if needed
1500xlim=get(hhuvmat.PlotAxes,'XLim');
1501ylim=get(hhuvmat.PlotAxes,'YLim');
1502ind_range=max(abs(xlim(2)-xlim(1)),abs(ylim(end)-ylim(1)))/25;%defines the size of the circle marker
1503check_xlim=0;
1504if XCoord>xlim(2)
1505    xlim=xlim+XCoord-xlim(2)+ind_range;% translate plot limit
1506    check_xlim=1;
1507elseif XCoord<xlim(1)
1508    xlim=xlim-XCoord+xlim(1)-ind_range;% translate plot limit
1509    check_xlim=1;
1510end
1511if check_xlim
1512    set(hhuvmat.PlotAxes,'XLim',xlim);
1513    set(hhuvmat.num_MaxX,'String',num2str(xlim(2)));
1514    set(hhuvmat.num_MinX,'String',num2str(xlim(1)));
1515end
1516check_ylim=0;
1517if YCoord>ylim(2)
1518    ylim=ylim+YCoord-ylim(2)+ind_range;% translate plot limit
1519    check_ylim=1;
1520elseif YCoord<ylim(1)
1521    ylim=ylim-YCoord+ylim(1)-ind_range;% translate plot limit
1522    check_ylim=1;
1523end
1524if check_ylim
1525    set(hhuvmat.PlotAxes,'YLim',ylim);
1526    set(hhuvmat.num_MaxY,'String',num2str(ylim(2)));
1527    set(hhuvmat.num_MinY,'String',num2str(ylim(1)));
1528end
1529
1530%% plot a circle around the selected point
1531if isempty(hhh)
1532    set(0,'CurrentFig',huvmat)
1533    set(huvmat,'CurrentAxes',hhuvmat.PlotAxes)
1534    rectangle('Curvature',[1 1],...
1535        'Position',[XCoord-ind_range/2 YCoord-ind_range/2 ind_range ind_range],'EdgeColor','m',...
1536        'LineStyle','-','Tag','calib_marker');
1537else
1538    set(hhh,'Position',[XCoord-ind_range/2 YCoord-ind_range/2 ind_range ind_range])
1539end
1540
1541%------------------------------------------------------------------------
1542% --- Executes on button press in ClearPoint: remove the selected line in the table Coord
1543%------------------------------------------------------------------------
1544function ClearPoint_Callback(hObject, eventdata, handles)
1545
1546Coord=get(handles.ListCoord,'Data');
1547iline=str2num(get(handles.CoordLine,'String'));
1548if isempty(iline)
1549    msgbox_uvmat('WARNING','no line suppressed, select a line in the table')
1550else
1551    answer=msgbox_uvmat('INPUT_Y-N',['suppress line ' num2str(iline) '?']);
1552    if isequal(answer,'Yes')
1553        Coord(iline,:)=[];
1554        set(handles.APPLY,'BackgroundColor',[1 0 1])
1555        set(handles.ListCoord,'Data',Coord);
1556        set(handles.CoordLine,'String','')
1557        PLOT_Callback(hObject,eventdata,handles)
1558        update_calib_marker([]);%remove circle marker
1559    end
1560end
1561
1562
1563
Note: See TracBrowser for help on using the repository browser.