source: trunk/src/geometry_calib.m @ 1068

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

replicate updated

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