source: trunk/src/geometry_calib.m @ 755

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