source: trunk/src/geometry_calib.m @ 1060

Last change on this file since 1060 was 1059, checked in by sommeria, 5 years ago

various bugs repaired

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