source: trunk/src/geometry_calib.m @ 207

Last change on this file since 207 was 207, checked in by sommeria, 13 years ago

bug fixes to deal with volumes

File size: 63.4 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
[114]52% Last Modified by GUIDE v2.5 05-Oct-2010 13:47:00
[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',   []);
[128]62if nargin && ischar(varargin{1}) && ~isempty(regexp(varargin{1},'_Callback','once'))
[2]63    gui_State.gui_Callback = str2func(varargin{1});
64end
65
66if nargout
67    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
68else
69    gui_mainfcn(gui_State, varargin{:});
70end
71% End initialization code - DO NOT edit
72
73
74% --- Executes just before geometry_calib is made visible.
75%INPUT:
76%handles: handles of the geometry_calib interface elements
77% PlotHandles: set of handles of the elements contolling the plotting
78% parameters on the uvmat interface (obtained by 'get_plot_handle.m')
[60]79%------------------------------------------------------------------------
[116]80function geometry_calib_OpeningFcn(hObject, eventdata, handles,inputfile,pos)
[60]81%------------------------------------------------------------------------
[2]82% Choose default command line output for geometry_calib
[156]83
[2]84handles.output = hObject;
85
86% Update handles structure
87guidata(hObject, handles);
[71]88set(hObject,'DeleteFcn',{@closefcn})%
89
[2]90%set the position of the interface
[156]91if exist('pos','var')&& length(pos)>=4
92%     %pos_gui=get(hObject,'Position');
93%     pos_gui(1)=pos(1);
94%     pos_gui(2)=pos(2);
95    set(hObject,'Position',pos);
[2]96end
[109]97
98%set menu of calibration options
[121]99set(handles.calib_type,'String',{'rescale';'linear';'3D_linear';'3D_quadr';'3D_extrinsic'})
[2]100inputxml='';
[116]101if exist('inputfile','var')&& ~isempty(inputfile)
[114]102    struct.XmlInputFile=inputfile;
[128]103    [Pathsub,RootFile,field_count,str2,str_a,str_b,ext]=name2display(inputfile);
[116]104    if ~strcmp(ext,'.xml')
[121]105        inputfile=[fullfile(Pathsub,RootFile) '.xml'];%xml file corresponding to the input file
[116]106    end
[156]107    set(handles.ListCoord,'String',{'......'})
108    if exist(inputfile,'file')
109        Heading=loadfile(handles,inputfile);% load the point coordiantes existing in the xml file
110        if isfield(Heading,'Campaign')&& ischar(Heading.Campaign)
111            struct.Campaign=Heading.Campaign;
112        end
113    end   
114    set(hObject,'UserData',struct)
[2]115end
[156]116
[2]117set(handles.ListCoord,'KeyPressFcn',{@key_press_fcn,handles})%set keyboard action function
118
[71]119
[17]120%------------------------------------------------------------------------
[2]121% --- Outputs from this function are returned to the command line.
122function varargout = geometry_calib_OutputFcn(hObject, eventdata, handles)
[17]123%------------------------------------------------------------------------
[2]124% Get default command line output from handles structure
125varargout{1} = handles.output;
126varargout{2}=handles;
[67]127%
[60]128%------------------------------------------------------------------------
[2]129% executed when closing: set the parent interface button to value 0
[67]130function closefcn(gcbo,eventdata)
[60]131%------------------------------------------------------------------------
[2]132huvmat=findobj(allchild(0),'Name','uvmat');
[67]133if ~isempty(huvmat)
134    handles=guidata(huvmat);
[156]135%     set(handles.MenuMask,'enable','on')
136%     set(handles.MenuGrid,'enable','on')
137%     set(handles.MenuObject,'enable','on')
138%     set(handles.MenuEdit,'enable','on')
139%     set(handles.edit,'enable','on')
[67]140    hobject=findobj(handles.axes3,'tag','calib_points');
141    if ~isempty(hobject)
142        delete(hobject)
143    end
144    hobject=findobj(handles.axes3,'tag','calib_marker');
145    if ~isempty(hobject)
146        delete(hobject)
147    end   
[2]148end
149
[60]150%------------------------------------------------------------------------
[2]151% --- Executes on button press in calibrate_lin.
152function APPLY_Callback(hObject, eventdata, handles)
[60]153%------------------------------------------------------------------------
[109]154%read the current calibration points
[2]155Coord_cell=get(handles.ListCoord,'String');
156Object=read_geometry_calib(Coord_cell);
[109]157Coord=Object.Coord;
158% apply the calibration, whose type is selected in  handles.calib_type
159if ~isempty(Coord)
160    calib_cell=get(handles.calib_type,'String');
161    val=get(handles.calib_type,'Value');
162    GeometryCalib=feval(['calib_' calib_cell{val}],Coord,handles);
163else
164    msgbox_uvmat('ERROR','No calibration points, abort')
165    return
166end
[114]167Z_plane=[];
[109]168if ~isempty(Coord)
[114]169    %check error
[109]170    X=Coord(:,1);
171    Y=Coord(:,2);
172    Z=Coord(:,3);
173    x_ima=Coord(:,4);
174    y_ima=Coord(:,5);
[114]175    [Xpoints,Ypoints]=px_XYZ(GeometryCalib,X,Y,Z);
[109]176    GeometryCalib.ErrorRms(1)=sqrt(mean((Xpoints-x_ima).*(Xpoints-x_ima)));
177    [GeometryCalib.ErrorMax(1),index(1)]=max(abs(Xpoints-x_ima));
178    GeometryCalib.ErrorRms(2)=sqrt(mean((Ypoints-y_ima).*(Ypoints-y_ima)));
179    [GeometryCalib.ErrorMax(2),index(2)]=max(abs(Ypoints-y_ima));
180    [EM,ind_dim]=max(GeometryCalib.ErrorMax);
181    index=index(ind_dim);
[114]182    %set the Z position of the reference plane used for calibration
[109]183    if isequal(max(Z),min(Z))%Z constant
[114]184        Z_plane=Z(1);
[109]185        GeometryCalib.NbSlice=1;
[114]186        GeometryCalib.SliceCoord=[0 0 Z_plane];
[109]187    end
188end
[114]189%set the coordinate unit
[2]190unitlist=get(handles.CoordUnit,'String');
191unit=unitlist{get(handles.CoordUnit,'value')};
192GeometryCalib.CoordUnit=unit;
[114]193%record the points
[109]194GeometryCalib.SourceCalib.PointCoord=Coord;
[114]195display_intrinsic(GeometryCalib,handles)%display calibration intrinsic parameters
[109]196
[114]197% Display extrinsinc parameters (rotation and translation of camera with  respect to the phys coordiantes)
[109]198set(handles.Tx,'String',num2str(GeometryCalib.Tx_Ty_Tz(1),4))
199set(handles.Ty,'String',num2str(GeometryCalib.Tx_Ty_Tz(2),4))
200set(handles.Tz,'String',num2str(GeometryCalib.Tx_Ty_Tz(3),4))
[114]201set(handles.Phi,'String',num2str(GeometryCalib.omc(1),4))
202set(handles.Theta,'String',num2str(GeometryCalib.omc(2),4))
203set(handles.Psi,'String',num2str(GeometryCalib.omc(3),4))
[109]204
[114]205% store the calibration data, by default in the xml file of the currently displayed image
[191]206huvmat=findobj(allchild(0),'Name','uvmat');
207UvData=get(huvmat,'UserData');
208NbSlice_j=1;%default
209ZStart=Z_plane;
210ZEnd=Z_plane;
[196]211volume_scan='n';
[191]212if isfield(UvData,'XmlData')
213    UvData.XmlData
214    if isfield(UvData.XmlData,'TranslationMotor')
215        NbSlice_j=UvData.XmlData.TranslationMotor.Nbslice;
[196]216        ZStart=UvData.XmlData.TranslationMotor.ZStart/10;
217        ZEnd=UvData.XmlData.TranslationMotor.ZEnd/10;
218        volume_scan='y';
[191]219    end
220end
221hhuvmat=guidata(huvmat);%handles of elements in the GUI uvmat
[2]222RootPath='';
223RootFile='';
[121]224if ~isempty(hhuvmat.RootPath)&& ~isempty(hhuvmat.RootFile)
[2]225    testhandle=1;
226    RootPath=get(hhuvmat.RootPath,'String');
227    RootFile=get(hhuvmat.RootFile,'String');
228    filebase=fullfile(RootPath,RootFile);
[114]229    outputfile=[filebase '.xml'];%xml file associated with the currently displayed image
[2]230else
231    question={'save the calibration data and point coordinates in'};
[121]232    def={fullfile(RootPath,'ObjectCalib.xml')};
[2]233    options.Resize='on';
234    answer=inputdlg(question,'save average in a new file',1,def,options);
235    outputfile=answer{1};
236end
[84]237answer=msgbox_uvmat('INPUT_Y-N',{[outputfile ' updated with calibration data'];...
[2]238    ['Error rms (along x,y)=' num2str(GeometryCalib.ErrorRms) ' pixels'];...
[84]239    ['Error max (along x,y)=' num2str(GeometryCalib.ErrorMax) ' pixels']});
[128]240
241%% record the calibration parameters and display the current image of uvmat in the new phys coordinates
[121]242if strcmp(answer,'Yes')
243    if strcmp(calib_cell{val}(1:2),'3D')%set the plane position for 3D (projection) calibration
[191]244       input_key={'Z (first position)','Z (last position)','Z (water surface)', 'refractive index','NbSlice','volume scan (y/n)','tilt angle'};
[196]245       input_val=[{num2str(ZEnd)} {num2str(ZStart)} {num2str(ZStart)} {'1.33'} num2str(NbSlice_j) {volume_scan} {'0'}];
[191]246        answer=inputdlg(input_key,'slice position(s)',ones(1,7), input_val,'on');
247        %answer_1=msgbox_uvmat('INPUT_TXT',' Z= ',num2str(Z_plane));
248        GeometryCalib.NbSlice=str2double(answer{5});
249        GeometryCalib.VolumeScan=answer{6};
250        if isempty(answer)
[121]251            Z_plane=0; %default
252        else
[191]253            Z_plane=linspace(str2double(answer{1}),str2double(answer{2}),GeometryCalib.NbSlice);
254        end     
[196]255        GeometryCalib.SliceCoord=Z_plane'*[0 0 1];
[207]256        GeometryCalib.SliceAngle=answer{7}*ones(GeometryCalib.NbSlice,1)*[0 1 0];%rotation around y axis (to generalise)
[196]257        GeometryCalib.InterfaceCoord=[0 0 str2double(answer{3})];
[207]258        GeometryCalib.RefractionIndex=str2double(answer{4});     
[121]259    end
[114]260    errormsg=update_imadoc(GeometryCalib,outputfile);% introduce the calibration data in the xml file
261    if ~strcmp(errormsg,'')
262        msgbox_uvmat('ERROR',errormsg);
263    end
[128]264   
[84]265    %display image with new calibration in the currently opened uvmat interface
266    hhh=findobj(hhuvmat.axes3,'Tag','calib_marker');% delete calib points and markers
267    if ~isempty(hhh)
[109]268        delete(hhh);     
[84]269    end
270    hhh=findobj(hhuvmat.axes3,'Tag','calib_points');
271    if ~isempty(hhh)
272        delete(hhh);
273    end
[191]274    set(hhuvmat.FixLimits,'Value',0)% put FixedLimits option to 'off'
275    set(hhuvmat.FixLimits,'BackgroundColor',[0.7 0.7 0.7])
[116]276    UserData=get(handles.geometry_calib,'UserData');
277    UserData.XmlInputFile=outputfile;%save the current xml file name
278    set(handles.geometry_calib,'UserData',UserData)
[114]279    uvmat('RootPath_Callback',hObject,eventdata,hhuvmat); %file input with xml reading  in uvmat, show the image in phys coordinates
280    MenuPlot_Callback(hObject, eventdata, handles)
281    set(handles.ListCoord,'Value',index)% indicate in the list the point with max deviation (possible mistake)
282    ListCoord_Callback(hObject, eventdata, handles)
[84]283    figure(handles.geometry_calib)
[2]284end
[69]285
286%------------------------------------------------------------------
[2]287% --- Executes on button press in calibrate_lin.
[128]288
[2]289function REPLICATE_Callback(hObject, eventdata, handles)
[60]290%------------------------------------------------------------------------
[2]291
[128]292%% Apply calibration
293calib_cell=get(handles.calib_type,'String'); %#ok<NASGU>
294val=get(handles.calib_type,'Value'); %#ok<NASGU>
295
[114]296%read the current calibration points
297Coord_cell=get(handles.ListCoord,'String');
298Object=read_geometry_calib(Coord_cell);
299Coord=Object.Coord;
[2]300
[114]301% apply the calibration, whose type is selected in  handles.calib_type
302if ~isempty(Coord)
303    calib_cell=get(handles.calib_type,'String');
304    val=get(handles.calib_type,'Value');
305    GeometryCalib=feval(['calib_' calib_cell{val}],Coord,handles);
306else
307    msgbox_uvmat('ERROR','No calibration points, abort')
308    return
309end
310
311if ~isempty(Coord)
312    %check error
313    X=Coord(:,1);
314    Y=Coord(:,2);
315    Z=Coord(:,3);
316    x_ima=Coord(:,4);
317    y_ima=Coord(:,5);
318    [Xpoints,Ypoints]=px_XYZ(GeometryCalib,X,Y,Z);
319    GeometryCalib.ErrorRms(1)=sqrt(mean((Xpoints-x_ima).*(Xpoints-x_ima)));
320    [GeometryCalib.ErrorMax(1),index(1)]=max(abs(Xpoints-x_ima));
321    GeometryCalib.ErrorRms(2)=sqrt(mean((Ypoints-y_ima).*(Ypoints-y_ima)));
322    [GeometryCalib.ErrorMax(2),index(2)]=max(abs(Ypoints-y_ima));
323    [EM,ind_dim]=max(GeometryCalib.ErrorMax);
[128]324%     index=index(ind_dim);
[114]325    %set the Z position of the reference plane used for calibration
326    Z_plane=[];
327    if isequal(max(Z),min(Z))
328        Z_plane=Z(1);
329    end
330    answer_1=msgbox_uvmat('INPUT_TXT',' Z= ',num2str(Z_plane));
[128]331    Z_plane=str2double(answer_1);
[114]332    GeometryCalib.NbSlice=1;
333    GeometryCalib.SliceCoord=[0 0 Z_plane];
334    %set the coordinate unit
335    unitlist=get(handles.CoordUnit,'String');
336    unit=unitlist{get(handles.CoordUnit,'value')};
337    GeometryCalib.CoordUnit=unit;
338    %record the points
339    GeometryCalib.SourceCalib.PointCoord=Coord;
340end
[128]341
342%% display calibration paprameters
[114]343display_intrinsic(GeometryCalib,handles)%display calibration intrinsic parameters
344
345% Display extrinsinc parameters (rotation and translation of camera with  respect to the phys coordiantes)
346set(handles.Tx,'String',num2str(GeometryCalib.Tx_Ty_Tz(1),4))
347set(handles.Ty,'String',num2str(GeometryCalib.Tx_Ty_Tz(2),4))
348set(handles.Tz,'String',num2str(GeometryCalib.Tx_Ty_Tz(3),4))
349set(handles.Phi,'String',num2str(GeometryCalib.omc(1),4))
350set(handles.Theta,'String',num2str(GeometryCalib.omc(2),4))
351set(handles.Psi,'String',num2str(GeometryCalib.omc(3),4))
352
[128]353%% open the GUI dataview
[2]354h_dataview=findobj(allchild(0),'name','dataview');
[12]355if ~isempty(h_dataview)
356    delete(h_dataview)
[2]357end
[61]358CalibData=get(handles.geometry_calib,'UserData');%read the calibration image source on the interface userdata
[128]359InputFile='';
[114]360if isfield(CalibData,'XmlInputFile')
[128]361    InputDir=fileparts(CalibData.XmlInputFile);
362    [InputDir,DirName]=fileparts(InputDir);
[2]363end
[12]364SubCampaignTest='n'; %default
[128]365testup=0;
366if isfield(CalibData,'SubCampaign')
367    SubCampaignTest='y';
368    dir_ref=CalibData.SubCampaign;
369    testup=1;
370elseif isfield(CalibData,'Campaign')
371    dir_ref=CalibData.Campaign;
372    testup=1;
[2]373end
[128]374while testup
375    [InputDir,DirName]=fileparts(InputDir);
376    if strcmp(DirName,dir_ref)
377        break
[2]378    end
379end
[128]380InputDir=fullfile(InputDir,DirName);
381answer=msgbox_uvmat('INPUT_TXT','Campaign ?',InputDir);
382if strcmp(answer,'Cancel')
383    return
[2]384end
385
[128]386dataview(answer,SubCampaignTest,GeometryCalib);
387       
388%     if isfield(Heading,'Device') && isequal([filename ext],Heading.Device)
389%         [XmlInput,filename,ext]=fileparts(XmlInput);
390%         Device=Heading.Device;
391%     end
392%     if isfield(Heading,'Experiment') && isequal([filename ext],Heading.Experiment)
393%         [PP,filename,ext]=fileparts(XmlInput);
394%     end
395%     testinput=0;
396%     if isfield(Heading,'SubCampaign') && isequal([filename ext],Heading.SubCampaign)
397%         SubCampaignTest='y';
398%         testinput=1;
399%     elseif isfield(Heading,'Campaign') && isequal([filename ext],Heading.Campaign)
400%         testinput=1;
401% %     end
402% end
403% if ~testinput
404%     filename='PROJETS';%default
405%     if isfield(CalibData,'XmlInputFile')
406%          [pp,filename]=fileparts(CalibData.XmlInputFile);
407%     end
408%     while ~isequal(filename,'PROJETS') && numel(filename)>1
409%         filename_1=filename;
410%         pp_1=pp;
411%         [pp,filename]=fileparts(pp);
412%     end
413%     XmlInput=fullfile(pp_1,filename_1);
414%     testinput=1;
415% end
416% if testinput
417%     outcome=dataview(XmlInput,SubCampaignTest,GeometryCalib);
418% end
419
[60]420%------------------------------------------------------------------------
[2]421% determine the parameters for a calibration by an affine function (rescaling and offset, no rotation)
[109]422function GeometryCalib=calib_rescale(Coord,handles)
[60]423%------------------------------------------------------------------------
[2]424X=Coord(:,1);
[109]425Y=Coord(:,2);% Z not used
[2]426x_ima=Coord(:,4);
427y_ima=Coord(:,5);
428[px,sx]=polyfit(X,x_ima,1);
429[py,sy]=polyfit(Y,y_ima,1);
430T_x=px(2);
431T_y=py(2);
432GeometryCalib.CalibrationType='rescale';
[121]433GeometryCalib.fx_fy=[px(1) py(1)];%.fx_fy corresponds to pxcm along x and y
[2]434GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
[114]435GeometryCalib.Tx_Ty_Tz=[px(2)/px(1) py(2)/py(1) 1];
436%GeometryCalib.R=[1,0,0;0,1,0;0,0,0];
437GeometryCalib.omc=[0 0 0];
[2]438
[60]439%------------------------------------------------------------------------
[2]440% determine the parameters for a calibration by a linear transform matrix (rescale and rotation)
[114]441function GeometryCalib=calib_linear(Coord,handles) %TO UPDATE
[60]442%------------------------------------------------------------------------
[2]443X=Coord(:,1);
[109]444Y=Coord(:,2);% Z not used
[2]445x_ima=Coord(:,4);
446y_ima=Coord(:,5);
447XY_mat=[ones(size(X)) X Y];
448a_X1=XY_mat\x_ima; %transformation matrix for X
[109]449% x1=XY_mat*a_X1;%reconstruction
450% err_X1=max(abs(x1-x_ima));%error
[2]451a_Y1=XY_mat\y_ima;%transformation matrix for X
[109]452% y1=XY_mat*a_Y1;
453% err_Y1=max(abs(y1-y_ima));%error
[121]454% R=[a_X1(2),a_X1(3),0;a_Y1(2),a_Y1(3),0;0,0,1];
455R=[a_X1(2),a_X1(3);a_Y1(2),a_Y1(3)];
[116]456norm=abs(det(R));
[2]457GeometryCalib.CalibrationType='linear';
[121]458GeometryCalib.fx_fy(1)=sqrt((a_X1(2)/a_Y1(3))*norm);
459GeometryCalib.fx_fy(2)=(a_Y1(3)/a_X1(2))*GeometryCalib.fx_fy(1);
[2]460GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
[116]461GeometryCalib.Tx_Ty_Tz=[a_X1(1) a_Y1(1) 1];
[121]462R(1,:)=R(1,:)/GeometryCalib.fx_fy(1);
463R(2,:)=R(2,:)/GeometryCalib.fx_fy(2);
464R=[R;[0 0]];
465GeometryCalib.R=[R [0;0;1]];
[128]466GeometryCalib.omc=(180/pi)*[acos(GeometryCalib.R(1,1)) 0 0];
[109]467%------------------------------------------------------------------------
468% determine the tsai parameters for a view normal to the grid plane
[114]469% NOT USED
[109]470function GeometryCalib=calib_normal(Coord,handles)
471%------------------------------------------------------------------------
472Calib.f1=str2num(get(handles.fx,'String'));
473Calib.f2=str2num(get(handles.fy,'String'));
[114]474Calib.k=str2num(get(handles.kc,'String'));
[109]475Calib.Cx=str2num(get(handles.Cx,'String'));
476Calib.Cy=str2num(get(handles.Cy,'String'));
477%default
478if isempty(Calib.f1)
479    Calib.f1=25/0.012;
480end
481if isempty(Calib.f2)
482    Calib.f2=25/0.012;
483end
484if isempty(Calib.k)
485    Calib.k=0;
486end
487if isempty(Calib.Cx)||isempty(Calib.Cy)
488    huvmat=findobj(allchild(0),'Tag','uvmat');
489    hhuvmat=guidata(huvmat);
490    Calib.Cx=str2num(get(hhuvmat.npx,'String'))/2;
491    Calib.Cx=str2num(get(hhuvmat.npy,'String'))/2;
492end   
493%tsai parameters
494Calib.dpx=0.012;%arbitrary
495Calib.dpy=0.012;
496Calib.sx=Calib.f1*Calib.dpx/(Calib.f2*Calib.dpy);
497Calib.f=Calib.f2*Calib.dpy;
498Calib.kappa1=Calib.k/(Calib.f*Calib.f);
[2]499
[109]500%initial guess
501X=Coord(:,1);
502Y=Coord(:,2);
503Zmean=mean(Coord(:,3));
504x_ima=Coord(:,4)-Calib.Cx;
505y_ima=Coord(:,5)-Calib.Cy;
506XY_mat=[ones(size(X)) X Y];
507a_X1=XY_mat\x_ima; %transformation matrix for X
508a_Y1=XY_mat\y_ima;%transformation matrix for Y
509R=[a_X1(2),a_X1(3),0;a_Y1(2),a_Y1(3),0;0,0,-1];% rotation+ z axis reversal (upward)
510norm=sqrt(det(-R));
511calib_param(1)=0;% quadratic distortion
512calib_param(2)=a_X1(1);
513calib_param(3)=a_Y1(1);
514calib_param(4)=Calib.f/(norm*Calib.dpx)-R(3,3)*Zmean;
[121]515calib_param(5)=angle(a_X1(2)+1i*a_X1(3));
[109]516display(['initial guess=' num2str(calib_param)])
517
518%optimise the parameters: minimisation of error
[121]519calib_param = fminsearch(@(calib_param) error_calib(calib_param,Calib,Coord),calib_param);
[109]520
521GeometryCalib.CalibrationType='tsai_normal';
522GeometryCalib.focal=Calib.f;
523GeometryCalib.dpx_dpy=[Calib.dpx Calib.dpy];
524GeometryCalib.Cx_Cy=[Calib.Cx Calib.Cy];
525GeometryCalib.sx=Calib.sx;
526GeometryCalib.kappa1=calib_param(1);
527GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
528GeometryCalib.Tx_Ty_Tz=[calib_param(2) calib_param(3) calib_param(4)];
529alpha=calib_param(5);
530GeometryCalib.R=[cos(alpha) sin(alpha) 0;-sin(alpha) cos(alpha) 0;0 0 -1];
531
[60]532%------------------------------------------------------------------------
[109]533function GeometryCalib=calib_3D_linear(Coord,handles)
[69]534%------------------------------------------------------------------
535path_uvmat=which('uvmat');% check the path detected for source file uvmat
536path_UVMAT=fileparts(path_uvmat); %path to UVMAT
[78]537huvmat=findobj(allchild(0),'Tag','uvmat');
538hhuvmat=guidata(huvmat);
[121]539coord_files=get(handles.coord_files,'String');
540if ischar(coord_files)
541    coord_files={coord_files};
542end
543if isempty(coord_files{1}) || isequal(coord_files,{''})
544    coord_files={};
545end
546%retrieve the calibration points stored in the files listed in the popup list coord_files
547x_1=Coord(:,4:5)';%px coordinates of the ref points
[109]548nx=str2num(get(hhuvmat.npx,'String'));
549ny=str2num(get(hhuvmat.npy,'String'));
[121]550x_1(2,:)=ny-x_1(2,:);%reverse the y image coordinates
551X_1=Coord(:,1:3)';%phys coordinates of the ref points
552n_ima=numel(coord_files)+1;
553if ~isempty(coord_files)
554    msgbox_uvmat('CONFIRMATION',['The xy coordinates of the calibration points in ' num2str(n_ima) ' planes will be used'])
555    for ifile=1:numel(coord_files)
556    t=xmltree(coord_files{ifile});
557    s=convert(t);%convert to matlab structure
558        if isfield(s,'GeometryCalib')
559            if isfield(s.GeometryCalib,'SourceCalib')
560                if isfield(s.GeometryCalib.SourceCalib,'PointCoord')
561                PointCoord=s.GeometryCalib.SourceCalib.PointCoord;
562                Coord_file=zeros(length(PointCoord),5);%default
563                for i=1:length(PointCoord)
564                    line=str2num(PointCoord{i});
565                    Coord_file(i,4:5)=line(4:5);%px x
566                    Coord_file(i,1:3)=line(1:3);%phys x
567                end
568                eval(['x_' num2str(ifile+1) '=Coord_file(:,4:5)'';']);
569                eval(['x_' num2str(ifile+1) '(2,:)=ny-x_' num2str(ifile+1) '(2,:);' ]);
570                eval(['X_' num2str(ifile+1) '=Coord_file(:,1:3)'';']);
571                end
572            end
573        end
574    end
575end
576n_ima=numel(coord_files)+1;
[109]577est_dist=[0;0;0;0;0];
578est_aspect_ratio=0;
579est_fc=[1;1];
580%fc=[25;25]/0.012;
581center_optim=0;
582run(fullfile(path_UVMAT,'toolbox_calib','go_calib_optim'));
583GeometryCalib.CalibrationType='3D_linear';
[121]584GeometryCalib.fx_fy=fc';
585%GeometryCalib.focal=fc(2);
586%GeometryCalib.dpx_dpy=[1 1];
[109]587GeometryCalib.Cx_Cy=cc';
[121]588%GeometryCalib.sx=fc(1)/fc(2);
589GeometryCalib.kc=kc(1);
590%GeometryCalib.kappa1=-kc(1)/fc(2)^2;
[109]591GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
592GeometryCalib.Tx_Ty_Tz=Tc_1';
593GeometryCalib.R=Rc_1;
[121]594GeometryCalib.R(2,1:3)=-GeometryCalib.R(2,1:3);%inversion of the y image coordinate
595GeometryCalib.Tx_Ty_Tz(2)=-GeometryCalib.Tx_Ty_Tz(2);%inversion of the y image coordinate
596GeometryCalib.Cx_Cy(2)=ny-GeometryCalib.Cx_Cy(2);%inversion of the y image coordinate
597GeometryCalib.omc=(180/pi)*omc_1;%angles in degrees
598GeometryCalib.ErrorRMS=[];
599GeometryCalib.ErrorMax=[];
[109]600
601%------------------------------------------------------------------------
602function GeometryCalib=calib_3D_quadr(Coord,handles)
603%------------------------------------------------------------------
604
605path_uvmat=which('uvmat');% check the path detected for source file uvmat
606path_UVMAT=fileparts(path_uvmat); %path to UVMAT
607huvmat=findobj(allchild(0),'Tag','uvmat');
608hhuvmat=guidata(huvmat);
609% check_cond=0;
610coord_files=get(handles.coord_files,'String');
611if ischar(coord_files)
612    coord_files={coord_files};
613end
614if isempty(coord_files{1}) || isequal(coord_files,{''})
615    coord_files={};
616end
617
618%retrieve the calibration points stored in the files listed in the popup list coord_files
[114]619x_1=Coord(:,4:5)';%px coordinates of the ref points
620nx=str2num(get(hhuvmat.npx,'String'));
621ny=str2num(get(hhuvmat.npy,'String'));
622x_1(2,:)=ny-x_1(2,:);%reverse the y image coordinates
623X_1=Coord(:,1:3)';%phys coordinates of the ref points
[109]624n_ima=numel(coord_files)+1;
625if ~isempty(coord_files)
626    msgbox_uvmat('CONFIRMATION',['The xy coordinates of the calibration points in ' num2str(n_ima) ' planes will be used'])
627    for ifile=1:numel(coord_files)
628    t=xmltree(coord_files{ifile});
629    s=convert(t);%convert to matlab structure
630        if isfield(s,'GeometryCalib')
631            if isfield(s.GeometryCalib,'SourceCalib')
632                if isfield(s.GeometryCalib.SourceCalib,'PointCoord')
633                PointCoord=s.GeometryCalib.SourceCalib.PointCoord;
[121]634                Coord_file=zeros(length(PointCoord),5);%default
[109]635                for i=1:length(PointCoord)
636                    line=str2num(PointCoord{i});
637                    Coord_file(i,4:5)=line(4:5);%px x
638                    Coord_file(i,1:3)=line(1:3);%phys x
639                end
640                eval(['x_' num2str(ifile+1) '=Coord_file(:,4:5)'';']);
[114]641                eval(['x_' num2str(ifile+1) '(2,:)=ny-x_' num2str(ifile+1) '(2,:);' ]);
[109]642                eval(['X_' num2str(ifile+1) '=Coord_file(:,1:3)'';']);
643                end
644            end
645        end
646    end
647end
648n_ima=numel(coord_files)+1;
[108]649est_dist=[1;0;0;0;0];
[109]650est_aspect_ratio=1;
651%est_fc=[0;0];
652%fc=[25;25]/0.012;
653center_optim=0;
[83]654run(fullfile(path_UVMAT,'toolbox_calib','go_calib_optim'));
[69]655
[109]656GeometryCalib.CalibrationType='3D_quadr';
[114]657GeometryCalib.fx_fy=fc';
658%GeometryCalib.focal=fc(2);
659%GeometryCalib.dpx_dpy=[1 1];
[69]660GeometryCalib.Cx_Cy=cc';
[114]661%GeometryCalib.sx=fc(1)/fc(2);
662GeometryCalib.kc=kc(1);
663%GeometryCalib.kappa1=-kc(1)/fc(2)^2;
[69]664GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
665GeometryCalib.Tx_Ty_Tz=Tc_1';
666GeometryCalib.R=Rc_1;
[114]667GeometryCalib.R(2,1:3)=-GeometryCalib.R(2,1:3);%inversion of the y image coordinate
668GeometryCalib.Tx_Ty_Tz(2)=-GeometryCalib.Tx_Ty_Tz(2);%inversion of the y image coordinate
669GeometryCalib.Cx_Cy(2)=ny-GeometryCalib.Cx_Cy(2);%inversion of the y image coordinate
670GeometryCalib.omc=(180/pi)*omc_1;%angles in degrees
[109]671GeometryCalib.ErrorRMS=[];
672GeometryCalib.ErrorMax=[];
[69]673
[109]674
[60]675%------------------------------------------------------------------------
[109]676function GeometryCalib=calib_3D_extrinsic(Coord,handles)
677%------------------------------------------------------------------
678path_uvmat=which('geometry_calib');% check the path detected for source file uvmat
679path_UVMAT=fileparts(path_uvmat); %path to UVMAT
[116]680x_1=double(Coord(:,4:5)');%image coordiantes
681X_1=double(Coord(:,1:3)');% phys coordinates
[114]682huvmat=findobj(allchild(0),'Tag','uvmat');
683hhuvmat=guidata(huvmat);
[116]684ny=str2double(get(hhuvmat.npy,'String'));
[114]685x_1(2,:)=ny-x_1(2,:);%reverse the y image coordinates
[109]686n_ima=1;
[114]687GeometryCalib.CalibrationType='3D_extrinsic';
688GeometryCalib.fx_fy(1)=str2num(get(handles.fx,'String'));
689GeometryCalib.fx_fy(2)=str2num(get(handles.fy,'String'));
690GeometryCalib.Cx_Cy(1)=str2num(get(handles.Cx,'String'));
691GeometryCalib.Cx_Cy(2)=str2num(get(handles.Cy,'String'));
692GeometryCalib.kc=str2num(get(handles.kc,'String'));
[109]693fct_path=fullfile(path_UVMAT,'toolbox_calib');
694addpath(fct_path)
[116]695GeometryCalib.Cx_Cy(2)=ny-GeometryCalib.Cx_Cy(2);%reverse Cx_Cy(2) for calibration (inversion of px ordinate)
[109]696% [omc1,Tc1,Rc1,H,x,ex,JJ] = compute_extrinsic(x_1,X_1,...
697%     [Calib.f Calib.f*Calib.sx]',...
698%     [Calib.Cx Calib.Cy]',...
699%     [-Calib.kappa1*Calib.f^2 0 0 0 0]);
[114]700[omc,Tc1,Rc1,H,x,ex,JJ] = compute_extrinsic(x_1,X_1,...
[116]701    (GeometryCalib.fx_fy)',GeometryCalib.Cx_Cy',[GeometryCalib.kc 0 0 0 0]);
[109]702rmpath(fct_path);
703GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
704GeometryCalib.Tx_Ty_Tz=Tc1';
705%inversion of z axis
706GeometryCalib.R=Rc1;
[114]707GeometryCalib.R(2,1:3)=-GeometryCalib.R(2,1:3);%inversion of the y image coordinate
708GeometryCalib.Tx_Ty_Tz(2)=-GeometryCalib.Tx_Ty_Tz(2);%inversion of the y image coordinate
[116]709GeometryCalib.Cx_Cy(2)=ny-GeometryCalib.Cx_Cy(2);%inversion of the y image coordinate
[114]710GeometryCalib.omc=(180/pi)*omc';
[109]711%GeometryCalib.R(3,1:3)=-GeometryCalib.R(3,1:3);%inversion for z upward
712
713
[116]714
[109]715%------------------------------------------------------------------------
716%function GeometryCalib=calib_tsai_heikkila(Coord)
717% TEST: NOT IMPLEMENTED
718%------------------------------------------------------------------
719% path_uvmat=which('uvmat');% check the path detected for source file uvmat
720% path_UVMAT=fileparts(path_uvmat); %path to UVMAT
721% path_calib=fullfile(path_UVMAT,'toolbox_calib_heikkila');
722% addpath(path_calib)
723% npoints=size(Coord,1);
724% Coord(:,1:3)=10*Coord(:,1:3);
725% Coord=[Coord zeros(npoints,2) -ones(npoints,1)];
726% [par,pos,iter,res,er,C]=cacal('dalsa',Coord);
727% GeometryCalib.CalibrationType='tsai';
728% GeometryCalib.focal=par(2);
729
730
731%--------------------------------------------------------------------------
[128]732function GeometryCalib=calib_tsai(Coord,handles)% OBSOLETE: old version using gauthier's bianry ccal_fo
[114]733% NOT USED
[109]734%------------------------------------------------------------------------
[2]735%TSAI
736path_uvmat=which('uvmat');% check the path detected for source file uvmat
737path_UVMAT=fileparts(path_uvmat); %path to UVMAT
[109]738xmlfile=fullfile(path_UVMAT,'PARAM.xml');%name of the file containing names of binary executables
[54]739if exist(xmlfile,'file')
[109]740    t=xmltree(xmlfile);% read the (xml) file containing names of binary executables
741    sparam=convert(t);% convert to matlab structure
[54]742end
[71]743if ~isfield(sparam,'GeometryCalibBin')
744    msgbox_uvmat('ERROR',['calibration program <GeometryCalibBin> undefined in parameter file ' xmlfile])
[2]745    return
746end
[71]747Tsai_exe=sparam.GeometryCalibBin;
[54]748if ~exist(Tsai_exe,'file')%the binary is defined in /bin, default setting
749     Tsai_exe=fullfile(path_UVMAT,Tsai_exe);
750end
[2]751if ~exist(Tsai_exe,'file')
[71]752    msgbox_uvmat('ERROR',['calibration program ' sparam.GeometryCalibBin ' defined in PARAM.xml does not exist'])
[2]753    return
754end
755
756textcoord=num2str(Coord,4);
757dlmwrite('t.txt',textcoord,''); 
[109]758% ['!' Tsai_exe ' -fx 0 -fy t.txt']
759eval(['!' Tsai_exe ' -f t.txt > tsaicalib.log']);
[2]760if ~exist('calib.dat','file')
[42]761    msgbox_uvmat('ERROR','no output from calibration program Tsai_exe: possibly too few points')
[2]762end
763calibdat=dlmread('calib.dat');
[60]764delete('calib.dat')
[109]765%delete('t.txt')
[2]766GeometryCalib.CalibrationType='tsai';
767GeometryCalib.focal=calibdat(10);
768GeometryCalib.dpx_dpy=[calibdat(5) calibdat(6)];
769GeometryCalib.Cx_Cy=[calibdat(7) calibdat(8)];
770GeometryCalib.sx=calibdat(9);
771GeometryCalib.kappa1=calibdat(11);
772GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
773GeometryCalib.Tx_Ty_Tz=[calibdat(12) calibdat(13) calibdat(14)];
[121]774Rx_Ry_Rz=calibdat(15:17);
[2]775sa = sin(Rx_Ry_Rz(1)) ;
776ca=cos(Rx_Ry_Rz(1));
777sb=sin(Rx_Ry_Rz(2));
778cb =cos(Rx_Ry_Rz(2));
779sg =sin(Rx_Ry_Rz(3));
780cg =cos(Rx_Ry_Rz(3));
781r1 = cb * cg;
782r2 = cg * sa * sb - ca * sg;
783r3 = sa * sg + ca * cg * sb;
784r4 = cb * sg;
785r5 = sa * sb * sg + ca * cg;
786r6 = ca * sb * sg - cg * sa;
787r7 = -sb;
788r8 = cb * sa;
789r9 = ca * cb;
790%EN DEDUIRE MATRICE R ??
791GeometryCalib.R=[r1,r2,r3;r4,r5,r6;r7,r8,r9];
792
[60]793%------------------------------------------------------------------------
[109]794% --- determine the rms of calibration error
795function ErrorRms=error_calib(calib_param,Calib,Coord)
796%calib_param: vector of free calibration parameters (to optimise)
797%Calib: structure of the given calibration parameters
798%Coord: list of phys coordinates (columns 1-3, and pixel coordinates (columns 4-5)
799Calib.f=25;
800Calib.dpx=0.012;
801Calib.dpy=0.012;
802Calib.sx=1;
803Calib.Cx=512;
804Calib.Cy=512;
805Calib.kappa1=calib_param(1);
806Calib.Tx=calib_param(2);
807Calib.Ty=calib_param(3);
808Calib.Tz=calib_param(4);
809alpha=calib_param(5);
810Calib.R=[cos(alpha) sin(alpha) 0;-sin(alpha) cos(alpha) 0;0 0 -1];
[2]811
[109]812X=Coord(:,1);
813Y=Coord(:,2);
814Z=Coord(:,3);
815x_ima=Coord(:,4);
816y_ima=Coord(:,5);
817[Xpoints,Ypoints]=px_XYZ(Calib,X,Y,Z);
818ErrorRms(1)=sqrt(mean((Xpoints-x_ima).*(Xpoints-x_ima)));
819ErrorRms(2)=sqrt(mean((Ypoints-y_ima).*(Ypoints-y_ima)));
820ErrorRms=mean(ErrorRms);
821
[60]822%------------------------------------------------------------------------
[2]823function XImage_Callback(hObject, eventdata, handles)
[60]824%------------------------------------------------------------------------
[2]825update_list(hObject, eventdata,handles)
826
[60]827%------------------------------------------------------------------------
[2]828function YImage_Callback(hObject, eventdata, handles)
[60]829%------------------------------------------------------------------------
[2]830update_list(hObject, eventdata,handles)
831
[109]832%------------------------------------------------------------------------
833% --- Executes on button press in STORE.
834function STORE_Callback(hObject, eventdata, handles)
835Coord_cell=get(handles.ListCoord,'String');
836Object=read_geometry_calib(Coord_cell);
837unitlist=get(handles.CoordUnit,'String');
838unit=unitlist{get(handles.CoordUnit,'value')};
839GeometryCalib.CoordUnit=unit;
840GeometryCalib.SourceCalib.PointCoord=Object.Coord;
841huvmat=findobj(allchild(0),'Name','uvmat');
842hhuvmat=guidata(huvmat);%handles of elements in the GUI uvmat
[128]843% RootPath='';
844% RootFile='';
[121]845if ~isempty(hhuvmat.RootPath)&& ~isempty(hhuvmat.RootFile)
[109]846    testhandle=1;
847    RootPath=get(hhuvmat.RootPath,'String');
848    RootFile=get(hhuvmat.RootFile,'String');
849    filebase=fullfile(RootPath,RootFile);
850    while exist([filebase '.xml'],'file')
851        filebase=[filebase '~'];
852    end
853    outputfile=[filebase '.xml'];
[114]854    errormsg=update_imadoc(GeometryCalib,outputfile);
855    if ~strcmp(errormsg,'')
856        msgbox_uvmat('ERROR',errormsg);
857    end
[109]858    listfile=get(handles.coord_files,'string');
859    if isequal(listfile,{''})
860        listfile={outputfile};
861    else
[128]862        listfile=[listfile;{outputfile}];%update the list of coord files
[109]863    end
864    set(handles.coord_files,'string',listfile);
865end
866set(handles.ListCoord,'Value',1)% refresh the display of coordinates
867set(handles.ListCoord,'String',{'......'})
868
[114]869% --------------------------------------------------------------------
870% --- Executes on button press in CLEAR_PTS: clear the list of calibration points
871function CLEAR_PTS_Callback(hObject, eventdata, handles)
872% --------------------------------------------------------------------
873set(handles.ListCoord,'Value',1)% refresh the display of coordinates
874set(handles.ListCoord,'String',{'......'})
875MenuPlot_Callback(hObject, eventdata, handles)
876
[109]877%------------------------------------------------------------------------
878% --- Executes on button press in CLEAR.
879function CLEAR_Callback(hObject, eventdata, handles)
880%------------------------------------------------------------------------
881set(handles.coord_files,'Value',1)
882set(handles.coord_files,'String',{''})
883
884%------------------------------------------------------------------------
[2]885function XObject_Callback(hObject, eventdata, handles)
[109]886%------------------------------------------------------------------------
[2]887update_list(hObject, eventdata,handles)
888
[109]889%------------------------------------------------------------------------
[2]890function YObject_Callback(hObject, eventdata, handles)
[109]891%------------------------------------------------------------------------
[2]892update_list(hObject, eventdata,handles)
893
[109]894%------------------------------------------------------------------------
[2]895function ZObject_Callback(hObject, eventdata, handles)
[109]896%------------------------------------------------------------------------
[2]897update_list(hObject, eventdata,handles)
898
[60]899%------------------------------------------------------------------------
[2]900function update_list(hObject, eventdata, handles)
[60]901%------------------------------------------------------------------------
[149]902newval(4)=str2double(get(handles.XImage,'String'));
903newval(5)=str2double(get(handles.YImage,'String'));
904newval(1)=str2double(get(handles.XObject,'String'));
905newval(2)=str2double(get(handles.YObject,'String'));
906newval(3)=str2double(get(handles.ZObject,'String'));
907if isnan(newval(3))
908    newval(3)=0;%put z to 0 by default
[2]909end
910Coord=get(handles.ListCoord,'String');
[149]911Coord(end)=[]; %remove last string '.....'
[2]912val=get(handles.ListCoord,'Value');
[149]913data=read_geometry_calib(Coord);
914data.Coord(val,:)=newval;
915for i=1:size(data.Coord,1)
916    for j=1:5
917          Coord_cell{i,j}=num2str(data.Coord(i,j),4);%display coordiantes with 4 digits
918    end
919end
920
921Tabchar=cell2tab(Coord_cell,' | ');
922Tabchar=[Tabchar ;{'......'}];
923set(handles.ListCoord,'String',Tabchar)
924
[60]925%update the plot
926ListCoord_Callback(hObject, eventdata, handles)
[67]927MenuPlot_Callback(hObject, eventdata, handles)
[71]928
[60]929%------------------------------------------------------------------------
[2]930% --- Executes on selection change in ListCoord.
931function ListCoord_Callback(hObject, eventdata, handles)
[60]932%------------------------------------------------------------------------
[71]933huvmat=findobj(allchild(0),'Name','uvmat');%find the current uvmat interface handle
934hplot=findobj(huvmat,'Tag','axes3');%main plotting axis of uvmat
935hhh=findobj(hplot,'Tag','calib_marker');
[2]936Coord_cell=get(handles.ListCoord,'String');
937val=get(handles.ListCoord,'Value');
[78]938if numel(val)>1
939    return %no action if several lines have been selected
940end
[71]941coord_str=Coord_cell{val};
[128]942k=findstr(' | ',coord_str);
[71]943if isempty(k)%last line '.....' selected
944    if ~isempty(hhh)
945        delete(hhh)%delete the circle marker
[2]946    end
[71]947    return
948end
949%fill the edit boxex
[149]950set(handles.XObject,'String',coord_str(1:k(1)-1))
951set(handles.YObject,'String',coord_str(k(1)+3:k(2)-1))
952set(handles.ZObject,'String',coord_str(k(2)+3:k(3)-1))
953set(handles.XImage,'String',coord_str(k(3)+3:k(4)-1))
954set(handles.YImage,'String',coord_str(k(4)+3:end))
[71]955h_menu_coord=findobj(huvmat,'Tag','transform_fct');
956menu=get(h_menu_coord,'String');
957choice=get(h_menu_coord,'Value');
958if iscell(menu)
959    option=menu{choice};
960else
961    option='px'; %default
962end
963if isequal(option,'phys')
[149]964    XCoord=str2double(coord_str(1:k(1)-1));
965    YCoord=str2double(coord_str(k(1)+3:k(2)-1));
[71]966elseif isequal(option,'px')|| isequal(option,'')
[149]967    XCoord=str2double(coord_str(k(3)+3:k(4)-1));
968    YCoord=str2double(coord_str(k(4)+3:end));
[71]969else
970    msgbox_uvmat('ERROR','the choice in menu_coord of uvmat must be px or phys ')
971end
972if isempty(XCoord)||isempty(YCoord)
973     if ~isempty(hhh)
974        delete(hhh)%delete the circle marker
[2]975    end
[71]976    return
[2]977end
[71]978xlim=get(hplot,'XLim');
979ylim=get(hplot,'YLim');
980ind_range=max(abs(xlim(2)-xlim(1)),abs(ylim(end)-ylim(1)))/20;%defines the size of the circle marker
981if isempty(hhh)
[149]982    set(0,'CurrentFig',huvmat)
983    set(huvmat,'CurrentAxes',hplot)
[71]984    rectangle('Curvature',[1 1],...
985              'Position',[XCoord-ind_range/2 YCoord-ind_range/2 ind_range ind_range],'EdgeColor','m',...
986              'LineStyle','-','Tag','calib_marker');
987else
988    set(hhh,'Position',[XCoord-ind_range/2 YCoord-ind_range/2 ind_range ind_range])
989end
[2]990
[60]991%------------------------------------------------------------------------
[2]992% --- Executes on selection change in edit_append.
993function edit_append_Callback(hObject, eventdata, handles)
[60]994%------------------------------------------------------------------------
[2]995choice=get(handles.edit_append,'Value');
[78]996if choice
997    set(handles.edit_append,'BackgroundColor',[1 1 0])
[177]998    huvmat=findobj(allchild(0),'tag','uvmat');
999    if ishandle(huvmat)
1000        hhuvmat=guidata(huvmat);
1001        set(hhuvmat.edit_object,'Value',0)
1002        set(hhuvmat.edit_object,'BackgroundColor',[0.7 0.7 0.7])
1003    end
[78]1004else
1005    set(handles.edit_append,'BackgroundColor',[0.7 0.7 0.7])
[2]1006end
1007   
1008function NEW_Callback(hObject, eventdata, handles)
1009%A METTRE SOUS UN BOUTON
1010huvmat=findobj(allchild(0),'Name','uvmat');
1011hchild=get(huvmat,'children');
1012hcoord=findobj(hchild,'Tag','menu_coord');
1013coordtype=get(hcoord,'Value');
1014haxes=findobj(hchild,'Tag','axes3');
1015AxeData=get(haxes,'UserData');
1016if ~isequal(hcoord,2)
1017    set(hcoord,'Value',2)
1018    huvmat=uvmat(AxeData);
1019    'relancer uvmat';
1020end
1021if ~isfield(AxeData,'ZoomAxes')
[42]1022    msgbox_uvmat('ERROR','first draw a window around a grid marker')
[2]1023    return
1024end
1025XLim=get(AxeData.ZoomAxes,'XLim');
1026YLim=get(AxeData.ZoomAxes,'YLim');
1027np=size(AxeData.A);
1028ind_sub_x=round(XLim);
1029ind_sub_y=np(1)-round(YLim);
1030Mfiltre=AxeData.A([ind_sub_y(2):ind_sub_y(1)] ,ind_sub_x,:);
1031Mfiltre_norm=double(Mfiltre);
1032Mfiltre_norm=Mfiltre_norm/sum(sum(Mfiltre_norm));
1033Mfiltre_norm=100*(Mfiltre_norm-mean(mean(Mfiltre_norm)));
1034Atype=class(AxeData.A);
1035Data.NbDim=2;
1036Data.A=filter2(Mfiltre_norm,double(AxeData.A));
1037Data.A=feval(Atype,Data.A);
1038Data.AName='image';
1039Data.AX=AxeData.AX;
1040Data.AY=AxeData.AY;
1041Data.CoordType='px';
1042plot_field(Data)
1043
[60]1044%------------------------------------------------------------------------
[2]1045function MenuPlot_Callback(hObject, eventdata, handles)
[60]1046%------------------------------------------------------------------------
[2]1047huvmat=findobj(allchild(0),'Name','uvmat');%find the current uvmat interface handle
[128]1048%UvData=get(huvmat,'UserData');%Data associated to the current uvmat interface
[2]1049hhuvmat=guidata(huvmat); %handles of GUI elements in uvmat
[128]1050%hplot=findobj(huvmat,'Tag','axes3');%main plotting axis of uvmat
[60]1051h_menu_coord=findobj(huvmat,'Tag','transform_fct');
[2]1052menu=get(h_menu_coord,'String');
1053choice=get(h_menu_coord,'Value');
1054if iscell(menu)
1055    option=menu{choice};
1056else
1057    option='px'; %default
1058end
1059Coord_cell=get(handles.ListCoord,'String');
1060ObjectData=read_geometry_calib(Coord_cell);
1061%ObjectData=read_geometry_calib(handles);%read the interface input parameters defining the object
[71]1062if ~isempty(ObjectData.Coord)
1063    if isequal(option,'phys')
1064        ObjectData.Coord=ObjectData.Coord(:,[1:3]);
1065    elseif isequal(option,'px')||isequal(option,'')
1066        ObjectData.Coord=ObjectData.Coord(:,[4:5]);
1067    else
1068        msgbox_uvmat('ERROR','the choice in menu_coord of uvmat must be '''', px or phys ')
1069    end
[2]1070end
1071axes(hhuvmat.axes3)
1072hh=findobj('Tag','calib_points');
[71]1073if  ~isempty(ObjectData.Coord) && isempty(hh)
[2]1074    hh=line(ObjectData.Coord(:,1),ObjectData.Coord(:,2),'Color','m','Tag','calib_points','LineStyle','.','Marker','+');
[71]1075elseif isempty(ObjectData.Coord)%empty list of points, suppress the plot
1076    delete(hh)
[2]1077else
1078    set(hh,'XData',ObjectData.Coord(:,1))
1079    set(hh,'YData',ObjectData.Coord(:,2))
1080end
[61]1081pause(.1)
1082figure(handles.geometry_calib)
[2]1083
1084% --------------------------------------------------------------------
1085function MenuHelp_Callback(hObject, eventdata, handles)
[116]1086path_to_uvmat=which('uvmat');% check the path of uvmat
[2]1087pathelp=fileparts(path_to_uvmat);
[116]1088helpfile=fullfile(pathelp,'uvmat_doc','uvmat_doc.html');
[36]1089if 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]1090else
[36]1091   addpath (fullfile(pathelp,'uvmat_doc'))
[2]1092   web([helpfile '#geometry_calib'])
1093end
1094
[17]1095%------------------------------------------------------------------------
[2]1096function MenuCreateGrid_Callback(hObject, eventdata, handles)
[17]1097%------------------------------------------------------------------------
[36]1098%hcalib=get(handles.calib_type,'parent');%handles of the GUI geometry_calib
[61]1099CalibData=get(handles.geometry_calib,'UserData');
[12]1100Tinput=[];%default
1101if isfield(CalibData,'grid')
1102    Tinput=CalibData.grid;
1103end
[71]1104[T,CalibData.grid]=create_grid(Tinput);%display the GUI create_grid
[61]1105set(handles.geometry_calib,'UserData',CalibData)
[2]1106
[12]1107%grid in phys space
[71]1108Coord=get(handles.ListCoord,'String');
1109val=get(handles.ListCoord,'Value');
1110data=read_geometry_calib(Coord);
1111%nbpoints=size(data.Coord,1); %nbre of calibration points
1112data.Coord(val:val+size(T,1)-1,1:3)=T(end:-1:1,:);%update the existing list of phys coordinates from the GUI create_grid
1113% for i=1:nbpoints
1114%    for j=1:5
1115%           Coord{i,j}=num2str(data.Coord(i,j),4);%display coordiantes with 4 digits
1116%    end
1117% end
1118%update the phys coordinates starting from the selected point (down in the
1119Coord(end,:)=[]; %remove last string '.....'
1120for i=1:size(data.Coord,1)
1121    for j=1:5
[17]1122          Coord{i,j}=num2str(data.Coord(i,j),4);%display coordiantes with 4 digits
1123    end
1124end
1125
1126%size(data.Coord,1)
[128]1127Tabchar=cell2tab(Coord,' | ');
[71]1128Tabchar=[Tabchar ;{'......'}];
[12]1129set(handles.ListCoord,'String',Tabchar)
[2]1130
[71]1131% -----------------------------------------------------------------------
1132% --- automatic grid dectection from local maxima of the images
[60]1133function MenuDetectGrid_Callback(hObject, eventdata, handles)
[71]1134%------------------------------------------------------------------------
[159]1135%% initiate the grid
[108]1136CalibData=get(handles.geometry_calib,'UserData');%get information stored on the GUI geometry_calib
[60]1137grid_input=[];%default
1138if isfield(CalibData,'grid')
1139    grid_input=CalibData.grid;%retrieve the previously used grid
1140end
[156]1141[T,CalibData.grid,white_test]=create_grid(grid_input,'detect_grid');%display the GUI create_grid, read the set of phys coordinates T
[108]1142set(handles.geometry_calib,'UserData',CalibData)%store the phys grid parameters for later use
[60]1143
[159]1144%% read the four last point coordinates in pixels
[108]1145Coord_cell=get(handles.ListCoord,'String');%read list of coordinates on geometry_calib
[60]1146data=read_geometry_calib(Coord_cell);
1147nbpoints=size(data.Coord,1); %nbre of calibration points
[62]1148if nbpoints~=4
[159]1149    msgbox_uvmat('ERROR','four points must have be selected by the mouse, beginning by the new x axis, to delimitate the phys grid area')
[71]1150    return
[60]1151end
[71]1152corners_X=(data.Coord(end:-1:end-3,4)); %pixel absissa of the four corners
1153corners_Y=(data.Coord(end:-1:end-3,5));
[60]1154
[71]1155%reorder the last two points (the two first in the list) if needed
[121]1156angles=angle((corners_X-corners_X(1))+1i*(corners_Y-corners_Y(1)));
[62]1157if abs(angles(4)-angles(2))>abs(angles(3)-angles(2))
1158      X_end=corners_X(4);
1159      Y_end=corners_Y(4);
1160      corners_X(4)=corners_X(3);
1161      corners_Y(4)=corners_Y(3);
1162      corners_X(3)=X_end;
1163      corners_Y(3)=Y_end;
1164end
1165
[159]1166%% read the current image, displayed in the GUI uvmat
[60]1167huvmat=findobj(allchild(0),'Name','uvmat');
1168UvData=get(huvmat,'UserData');
1169A=UvData.Field.A;
1170npxy=size(A);
[159]1171X=[CalibData.grid.x_0 CalibData.grid.x_1 CalibData.grid.x_0 CalibData.grid.x_1]';%corner absissa in the phys coordinates (cm)
1172Y=[CalibData.grid.y_0 CalibData.grid.y_0 CalibData.grid.y_1 CalibData.grid.y_1]';%corner ordinates in the phys coordinates (cm)
[109]1173
[159]1174%calculate transform matrices for plane projection
[108]1175% reference: http://alumni.media.mit.edu/~cwren/interpolator/ by Christopher R. Wren
1176B = [ X Y ones(size(X)) zeros(4,3)        -X.*corners_X -Y.*corners_X ...
1177      zeros(4,3)        X Y ones(size(X)) -X.*corners_Y -Y.*corners_Y ];
1178B = reshape (B', 8 , 8 )';
1179D = [ corners_X , corners_Y ];
1180D = reshape (D', 8 , 1 );
[121]1181l = (B' * B)\B' * D;
[108]1182Amat = reshape([l(1:6)' 0 0 1 ],3,3)';
1183C = [l(7:8)' 1];
1184
[159]1185% transform grid image into 'phys' coordinates
[114]1186GeometryCalib.fx_fy=[1 1];
1187GeometryCalib.Tx_Ty_Tz=[Amat(1,3) Amat(2,3) 1];
[108]1188GeometryCalib.R=[Amat(1,1),Amat(1,2),0;Amat(2,1),Amat(2,2),0;C(1),C(2),0];
[159]1189GeometryCalib.CoordUnit='cm';
[114]1190path_uvmat=which('uvmat');% check the path detected for source file uvmat
1191path_UVMAT=fileparts(path_uvmat); %path to UVMAT
1192addpath(fullfile(path_UVMAT,'transform_field'))
1193Data.ListVarName={'AY','AX','A'};
1194Data.VarDimName={'AY','AX',{'AY','AX'}};
1195if ndims(A)==3
1196    A=mean(A,3);
1197end
1198Data.A=A-min(min(A));
1199Data.AY=[npxy(1)-0.5 0.5];
1200Data.AX=[0.5 npxy(2)];
[158]1201Data.CoordUnit='pixel';
[114]1202Calib.GeometryCalib=GeometryCalib;
[121]1203DataOut=phys(Data,Calib);
[114]1204rmpath(fullfile(path_UVMAT,'transform_field'))
1205Amod=DataOut.A;
1206Rangx=DataOut.AX;
1207Rangy=DataOut.AY;
[156]1208if white_test
[159]1209    Amod=double(Amod);%case of white grid markers: will look for image maxima
[156]1210else
[159]1211    Amod=-double(Amod);%case of black grid markers: will look for image minima
[156]1212end
[114]1213% figure(12) %display corrected image
[109]1214% Amax=max(max(Amod));
1215% image(Rangx,Rangy,uint8(255*Amod/Amax))
[114]1216
[159]1217%% detection of local image extrema in each direction
[88]1218Dx=(Rangx(2)-Rangx(1))/(npxy(2)-1); %x mesh in real space
1219Dy=(Rangy(2)-Rangy(1))/(npxy(1)-1); %y mesh in real space
[121]1220ind_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
1221ind_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]1222nbpoints=size(T,1);
1223for ipoint=1:nbpoints
1224    i0=1+round((T(ipoint,1)-Rangx(1))/Dx);%round(Xpx(ipoint));
1225    j0=1+round((T(ipoint,2)-Rangy(1))/Dy);%round(Xpx(ipoint));
[109]1226    j0min=max(j0-ind_range_y,1);
1227    j0max=min(j0+ind_range_y,size(Amod,1));
1228    i0min=max(i0-ind_range_x,1);
1229    i0max=min(i0+ind_range_x,size(Amod,2));
1230    Asub=Amod(j0min:j0max,i0min:i0max);
[60]1231    x_profile=sum(Asub,1);
1232    y_profile=sum(Asub,2);
1233    [Amax,ind_x_max]=max(x_profile);
1234    [Amax,ind_y_max]=max(y_profile);
[61]1235    %sub-pixel improvement using moments
1236    x_shift=0;
1237    y_shift=0;
[109]1238    if ind_x_max+2<=numel(x_profile) && ind_x_max-2>=1
[61]1239        Atop=x_profile(ind_x_max-2:ind_x_max+2);
1240        x_shift=sum(Atop.*[-2 -1 0 1 2])/sum(Atop);
1241    end
[114]1242    if ind_y_max+2<=numel(y_profile) && ind_y_max-2>=1
[61]1243        Atop=y_profile(ind_y_max-2:ind_y_max+2);
1244        y_shift=sum(Atop.*[-2 -1 0 1 2]')/sum(Atop);
1245    end
[109]1246    Delta(ipoint,1)=(x_shift+ind_x_max+i0min-i0-1)*Dx;%shift from the initial guess
1247    Delta(ipoint,2)=(y_shift+ind_y_max+j0min-j0-1)*Dy;
[60]1248end
1249Tmod=T(:,(1:2))+Delta;
1250[Xpx,Ypx]=px_XYZ(GeometryCalib,Tmod(:,1),Tmod(:,2));
[63]1251for ipoint=1:nbpoints
1252     Coord{ipoint,1}=num2str(T(ipoint,1),4);%display coordiantes with 4 digits
1253     Coord{ipoint,2}=num2str(T(ipoint,2),4);%display coordiantes with 4 digits
[109]1254     Coord{ipoint,3}=num2str(T(ipoint,3),4);%display coordiantes with 4 digits;
1255     Coord{ipoint,4}=num2str(Xpx(ipoint),4);%display coordiantes with 4 digits
1256     Coord{ipoint,5}=num2str(Ypx(ipoint),4);%display coordiantes with 4 digits
[60]1257end
[128]1258Tabchar=cell2tab(Coord(end:-1:1,:),' | ');
[71]1259Tabchar=[Tabchar ;{'......'}];
[60]1260set(handles.ListCoord,'Value',1)
1261set(handles.ListCoord,'String',Tabchar)
[67]1262MenuPlot_Callback(hObject, eventdata, handles)
[60]1263
[71]1264%-----------------------------------------------------------------------
1265function MenuTranslatePoints_Callback(hObject, eventdata, handles)
1266%-----------------------------------------------------------------------
1267%hcalib=get(handles.calib_type,'parent');%handles of the GUI geometry_calib
1268CalibData=get(handles.geometry_calib,'UserData');
1269Tinput=[];%default
1270if isfield(CalibData,'translate')
1271    Tinput=CalibData.translate;
1272end
1273T=translate_points(Tinput);%display translate_points GUI and get shift parameters
1274CalibData.translate=T;
1275set(handles.geometry_calib,'UserData',CalibData)
1276%translation
1277Coord_cell=get(handles.ListCoord,'String');
1278data=read_geometry_calib(Coord_cell);
1279data.Coord(:,1)=T(1)+data.Coord(:,1);
1280data.Coord(:,2)=T(2)+data.Coord(:,2);
1281data.Coord(:,3)=T(3)+data.Coord(:,3);
1282data.Coord(:,[4 5])=data.Coord(:,[4 5]);
1283for i=1:size(data.Coord,1)
1284    for j=1:5
1285          Coord{i,j}=num2str(data.Coord(i,j),4);%phys x,y,z
1286   end
1287end
[128]1288Tabchar=cell2tab(Coord,' | ');
[88]1289Tabchar=[Tabchar; {'.....'}];
[71]1290%set(handles.ListCoord,'Value',1)
1291set(handles.ListCoord,'String',Tabchar)
1292
1293
1294% --------------------------------------------------------------------
1295function MenuRotatePoints_Callback(hObject, eventdata, handles)
1296%hcalib=get(handles.calib_type,'parent');%handles of the GUI geometry_calib
1297CalibData=get(handles.geometry_calib,'UserData');
1298Tinput=[];%default
1299if isfield(CalibData,'rotate')
1300    Tinput=CalibData.rotate;
1301end
1302T=rotate_points(Tinput);%display translate_points GUI and get shift parameters
1303CalibData.rotate=T;
1304set(handles.geometry_calib,'UserData',CalibData)
1305%-----------------------------------------------------
1306%rotation
1307Phi=T(1);
1308O_x=0;%default
1309O_y=0;%default
1310if numel(T)>=2
1311    O_x=T(2);%default
1312end
1313if numel(T)>=3
1314    O_y=T(3);%default
1315end
1316Coord_cell=get(handles.ListCoord,'String');
1317data=read_geometry_calib(Coord_cell);
1318r1=cos(pi*Phi/180);
1319r2=-sin(pi*Phi/180);
1320r3=sin(pi*Phi/180);
1321r4=cos(pi*Phi/180);
1322x=data.Coord(:,1)-O_x;
1323y=data.Coord(:,2)-O_y;
1324data.Coord(:,1)=r1*x+r2*y;
1325data.Coord(:,2)=r3*x+r4*y;
1326% data.Coord(:,[4 5])=data.Coord(:,[4 5]);
1327for i=1:size(data.Coord,1)
1328    for j=1:5
1329          Coord{i,j}=num2str(data.Coord(i,j),4);%phys x,y,z
1330   end
1331end
[148]1332Tabchar=cell2tab(Coord,' | ');
[71]1333Tabchar=[Tabchar;{'......'}];
1334set(handles.ListCoord,'Value',1)
1335set(handles.ListCoord,'String',Tabchar)
1336
[109]1337
1338% %------------------------------------------------------------------------
1339% % --- Executes on button press in rotation.
1340% function rotation_Callback(hObject, eventdata, handles)
1341% %------------------------------------------------------------------------
1342% angle_rot=(pi/180)*str2num(get(handles.Phi,'String'));
1343% Coord_cell=get(handles.ListCoord,'String');
1344% data=read_geometry_calib(Coord_cell);
1345% data.Coord(:,1)=cos(angle_rot)*data.Coord(:,1)+sin(angle_rot)*data.Coord(:,2);
1346% data.Coord(:,1)=-sin(angle_rot)*data.Coord(:,1)+cos(angle_rot)*data.Coord(:,2);
1347% set(handles.XObject,'String',num2str(data.Coord(:,1),4));
1348% set(handles.YObject,'String',num2str(data.Coord(:,2),4));
1349
1350
[108]1351%------------------------------------------------------------------------
1352% image transform from px to phys
1353%INPUT:
1354%Zindex: index of plane
[114]1355% function [A_out,Rangx,Rangy]=phys_Ima(A,Calib,ZIndex)
1356% %------------------------------------------------------------------------
1357% xcorner=[];
1358% ycorner=[];
1359% npx=[];
1360% npy=[];
1361% siz=size(A)
1362% npx=[npx siz(2)];
1363% npy=[npy siz(1)]
1364% xima=[0.5 siz(2)-0.5 0.5 siz(2)-0.5];%image coordinates of corners
1365% yima=[0.5 0.5 siz(1)-0.5 siz(1)-0.5];
1366% [xcorner,ycorner]=phys_XYZ(Calib,xima,yima,ZIndex);%corresponding physical coordinates
1367% Rangx(1)=min(xcorner);
1368% Rangx(2)=max(xcorner);
1369% Rangy(2)=min(ycorner);
1370% Rangy(1)=max(ycorner);
1371% test_multi=(max(npx)~=min(npx)) | (max(npy)~=min(npy));
1372% npx=max(npx);
1373% npy=max(npy);
1374% x=linspace(Rangx(1),Rangx(2),npx);
1375% y=linspace(Rangy(1),Rangy(2),npy);
1376% [X,Y]=meshgrid(x,y);%grid in physical coordiantes
1377% vec_B=[];
1378%
1379% zphys=0; %default
1380% if isfield(Calib,'SliceCoord') %.Z= index of plane
1381%    SliceCoord=Calib.SliceCoord(ZIndex,:);
1382%    zphys=SliceCoord(3); %to generalize for non-parallel planes
1383% end
1384% [XIMA,YIMA]=px_XYZ(Calib,X,Y,zphys);%corresponding image indices for each point in the real space grid
1385% XIMA=reshape(round(XIMA),1,npx*npy);%indices reorganized in 'line'
1386% YIMA=reshape(round(YIMA),1,npx*npy);
1387% flagin=XIMA>=1 & XIMA<=npx & YIMA >=1 & YIMA<=npy;%flagin=1 inside the original image
1388% testuint8=isa(A,'uint8');
1389% testuint16=isa(A,'uint16');
1390% if numel(siz)==2 %(B/W images)
1391%     vec_A=reshape(A,1,npx*npy);%put the original image in line
1392%     ind_in=find(flagin);
1393%     ind_out=find(~flagin);
1394%     ICOMB=((XIMA-1)*npy+(npy+1-YIMA));
1395%     ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A
1396%     vec_B(ind_in)=vec_A(ICOMB);
1397%     vec_B(ind_out)=zeros(size(ind_out));
1398%     A_out=reshape(vec_B,npy,npx);%new image in real coordinates
1399% elseif numel(siz)==3     
1400%     for icolor=1:siz(3)
1401%         vec_A=reshape(A{icell}(:,:,icolor),1,npx*npy);%put the original image in line
1402%         ind_in=find(flagin);
1403%         ind_out=find(~flagin);
1404%         ICOMB=((XIMA-1)*npy+(npy+1-YIMA));
1405%         ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A
1406%         vec_B(ind_in)=vec_A(ICOMB);
1407%         vec_B(ind_out)=zeros(size(ind_out));
1408%         A_out(:,:,icolor)=reshape(vec_B,npy,npx);%new image in real coordinates
1409%     end
1410% end
1411% if testuint8
1412%     A_out=uint8(A_out);
1413% end
1414% if testuint16
1415%     A_out=uint16(A_out);
1416% end
[60]1417
[108]1418%------------------------------------------------------------------------
1419% pointwise transform from px to phys
[60]1420%INPUT:
1421%Z: index of plane
[114]1422% function [Xphys,Yphys,Zphys]=phys_XYZ(Calib,X,Y,Z)
1423% %------------------------------------------------------------------------
1424% if exist('Z','var')& isequal(Z,round(Z))& Z>0 & isfield(Calib,'SliceCoord')&length(Calib.SliceCoord)>=Z
1425%     Zindex=Z;
1426%     Zphys=Calib.SliceCoord(Zindex,3);%GENERALISER AUX CAS AVEC ANGLE
1427% else
1428%     Zphys=0;
1429% end
1430% if ~exist('X','var')||~exist('Y','var')
1431%     Xphys=[];
1432%     Yphys=[];%default
1433%     return
1434% end
1435% Xphys=X;%default
1436% Yphys=Y;
1437% %image transform
1438% if isfield(Calib,'R')
1439%     R=(Calib.R)';
1440%     Dx=R(5)*R(7)-R(4)*R(8);
1441%     Dy=R(1)*R(8)-R(2)*R(7);
1442%     D0=Calib.f*(R(2)*R(4)-R(1)*R(5));
1443%     Z11=R(6)*R(8)-R(5)*R(9);
1444%     Z12=R(2)*R(9)-R(3)*R(8); 
1445%     Z21=R(4)*R(9)-R(6)*R(7);
1446%     Z22=R(3)*R(7)-R(1)*R(9);
1447%     Zx0=R(3)*R(5)-R(2)*R(6);
1448%     Zy0=R(1)*R(6)-R(3)*R(4);
1449%     A11=R(8)*Calib.Ty-R(5)*Calib.Tz+Z11*Zphys;
1450%     A12=R(2)*Calib.Tz-R(8)*Calib.Tx+Z12*Zphys;
1451%     A21=-R(7)*Calib.Ty+R(4)*Calib.Tz+Z21*Zphys;
1452%     A22=-R(1)*Calib.Tz+R(7)*Calib.Tx+Z11*Zphys;
1453%     X0=Calib.f*(R(5)*Calib.Tx-R(2)*Calib.Ty+Zx0*Zphys);
1454%     Y0=Calib.f*(-R(4)*Calib.Tx+R(1)*Calib.Ty+Zy0*Zphys);
1455%         %px to camera:
1456%     Xd=(Calib.dpx/Calib.sx)*(X-Calib.Cx); % sensor coordinates
1457%     Yd=Calib.dpy*(Y-Calib.Cy);
1458%     dist_fact=1+Calib.kappa1*(Xd.*Xd+Yd.*Yd); %distortion factor
1459%     Xu=dist_fact.*Xd;%undistorted sensor coordinates
1460%     Yu=dist_fact.*Yd;
1461%     denom=Dx*Xu+Dy*Yu+D0;
1462%     % denom2=denom.*denom;
1463%     Xphys=(A11.*Xu+A12.*Yu+X0)./denom;%world coordinates
1464%     Yphys=(A21.*Xu+A22.*Yu+Y0)./denom;
1465% end
[109]1466
1467
1468% --------------------------------------------------------------------
1469function MenuImportPoints_Callback(hObject, eventdata, handles)
[121]1470fileinput=browse_xml(hObject, eventdata, handles);
[109]1471if isempty(fileinput)
1472    return
1473end
1474[s,errormsg]=imadoc2struct(fileinput,'GeometryCalib');
1475GeometryCalib=s.GeometryCalib;
[114]1476%GeometryCalib=load_calib(hObject, eventdata, handles)
[116]1477calib=reshape(GeometryCalib.PointCoord,[],1);
[109]1478for ilist=1:numel(calib)
1479    CoordCell{ilist}=num2str(calib(ilist));
1480end
1481CoordCell=reshape(CoordCell,[],5);
[128]1482Tabchar=cell2tab(CoordCell,' | ');%transform cells into table ready for display
[109]1483Tabchar=[Tabchar;{'......'}];
1484set(handles.ListCoord,'Value',1)
1485set(handles.ListCoord,'String',Tabchar)
1486MenuPlot_Callback(handles.geometry_calib, [], handles)
1487
1488% -----------------------------------------------------------------------
1489function MenuImportIntrinsic_Callback(hObject, eventdata, handles)
1490%------------------------------------------------------------------------
1491fileinput=browse_xml(hObject, eventdata, handles);
1492if isempty(fileinput)
1493    return
1494end
1495[s,errormsg]=imadoc2struct(fileinput,'GeometryCalib');
1496GeometryCalib=s.GeometryCalib;
[114]1497display_intrinsic(GeometryCalib,handles)
[109]1498
1499% -----------------------------------------------------------------------
1500function MenuImportAll_Callback(hObject, eventdata, handles)
1501%------------------------------------------------------------------------
[121]1502fileinput=browse_xml(hObject, eventdata, handles);
[109]1503if ~isempty(fileinput)
1504    loadfile(handles,fileinput)
1505end
1506
1507% -----------------------------------------------------------------------
1508% --- Executes on menubar option Import/Grid file: introduce previous grid files
1509function MenuGridFile_Callback(hObject, eventdata, handles)
1510% -----------------------------------------------------------------------
[121]1511inputfile=browse_xml(hObject, eventdata, handles);
[109]1512listfile=get(handles.coord_files,'string');
1513if isequal(listfile,{''})
1514    listfile={inputfile};
1515else
[121]1516    listfile=[listfile;{inputfile}];%update the list of coord files
[109]1517end
1518set(handles.coord_files,'string',listfile);
1519
1520%------------------------------------------------------------------------
[114]1521% --- 'key_press_fcn:' function activated when a key is pressed on the keyboard
1522function key_press_fcn(hObject,eventdata,handles)
1523%------------------------------------------------------------------------
1524xx=double(get(handles.geometry_calib,'CurrentCharacter')); %get the keyboard character
1525if ismember(xx,[8 127])%backspace or delete
1526    Coord_cell=get(handles.ListCoord,'String');
1527    val=get(handles.ListCoord,'Value');
1528     if max(val)<numel(Coord_cell) % the last element '...' has not been selected
1529        Coord_cell(val)=[];%remove the selected line
1530        set(handles.ListCoord,'Value',min(val))
1531        set(handles.ListCoord,'String',Coord_cell)         
1532        ListCoord_Callback(hObject, eventdata, handles)
1533        MenuPlot_Callback(hObject,eventdata,handles)
1534     end
1535end
1536
1537%------------------------------------------------------------------------
[109]1538function fileinput=browse_xml(hObject, eventdata, handles)
1539%------------------------------------------------------------------------
1540fileinput=[];%default
1541oldfile=''; %default
[116]1542UserData=get(handles.geometry_calib,'UserData');
[114]1543if isfield(UserData,'XmlInputFile')
1544    oldfile=UserData.XmlInputFile;
[109]1545end
1546[FileName, PathName, filterindex] = uigetfile( ...
1547       {'*.xml;*.mat', ' (*.xml,*.mat)';
1548       '*.xml',  '.xml files '; ...
1549        '*.mat',  '.mat matlab files '}, ...
1550        'Pick a file',oldfile);
1551fileinput=[PathName FileName];%complete file name
1552testblank=findstr(fileinput,' ');%look for blanks
1553if ~isempty(testblank)
1554    msgbox_uvmat('ERROR','forbidden input file name or path: no blank character allowed')
1555    return
1556end
1557sizf=size(fileinput);
1558if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end
[114]1559UserData.XmlInputFile=fileinput;
[109]1560set(handles.geometry_calib,'UserData',UserData)%record current file foer further use of browser
1561
1562% -----------------------------------------------------------------------
[128]1563function Heading=loadfile(handles,fileinput)
[109]1564%------------------------------------------------------------------------
[128]1565Heading=[];%default
[121]1566[s,errormsg]=imadoc2struct(fileinput,'GeometryCalib');
[128]1567if ~isempty(errormsg)
1568    msgbox_uvmat('ERROR',['Error for reading ' fileinput ': '  errormsg])
1569    return
1570end
1571if ~isempty(s.Heading)
1572    Heading=s.Heading;
1573end
1574   
[116]1575GeometryCalib=s.GeometryCalib;
[114]1576fx=1;fy=1;Cx=0;Cy=0;kc=0; %default
1577%     Tabchar={};
1578CoordCell={};
1579%     kc=0;%default
1580%     f1=1000;
1581%     f2=1000;
1582%     hhuvmat=guidata(findobj(allchild(0),'Name','uvmat'));
1583%     Cx=str2num(get(hhuvmat.npx,'String'))/2;
1584%     Cy=str2num(get(hhuvmat.npy,'String'))/2;
1585Tabchar={};%default
1586val_cal=1;%default
1587if ~isempty(GeometryCalib)
1588    % choose the calibration option
1589    if isfield(GeometryCalib,'CalibrationType')
1590       calib_list=get(handles.calib_type,'String');
1591       for ilist=1:numel(calib_list)
1592           if strcmp(calib_list{ilist},GeometryCalib.CalibrationType)
1593               val_cal=ilist;
1594               break
1595           end
1596       end
1597    end
1598    display_intrinsic(GeometryCalib,handles)%intrinsic param
1599    %extrinsic param
1600    if isfield(GeometryCalib,'Tx_Ty_Tz')
1601        Tx_Ty_Tz=GeometryCalib.Tx_Ty_Tz;
1602        set(handles.Tx,'String',num2str(GeometryCalib.Tx_Ty_Tz(1),4))
1603        set(handles.Ty,'String',num2str(GeometryCalib.Tx_Ty_Tz(2),4))
1604        set(handles.Tz,'String',num2str(GeometryCalib.Tx_Ty_Tz(3),4))
1605    end
1606    if isfield(GeometryCalib,'omc')
1607        set(handles.Phi,'String',num2str(GeometryCalib.omc(1),4))
1608        set(handles.Theta,'String',num2str(GeometryCalib.omc(2),4))
1609        set(handles.Psi,'String',num2str(GeometryCalib.omc(3),4))
1610    end
[116]1611    calib=reshape(GeometryCalib.PointCoord,[],1);
[109]1612    for ilist=1:numel(calib)
1613        CoordCell{ilist}=num2str(calib(ilist));
1614    end
1615    CoordCell=reshape(CoordCell,[],5);
[128]1616    Tabchar=cell2tab(CoordCell,' | ');%transform cells into table ready for display
[109]1617    MenuPlot_Callback(handles.geometry_calib, [], handles)
1618end
[114]1619set(handles.calib_type,'Value',val_cal)
[109]1620Tabchar=[Tabchar;{'......'}];
[114]1621set(handles.ListCoord,'Value',1)
1622set(handles.ListCoord,'String',Tabchar)
1623
[109]1624if isempty(CoordCell)% allow mouse action by default in the absence of input points
1625    set(handles.edit_append,'Value',1)
1626    set(handles.edit_append,'BackgroundColor',[1 1 0])
1627else % does not allow mouse action by default in the presence of input points
1628    set(handles.edit_append,'Value',0)
1629    set(handles.edit_append,'BackgroundColor',[0.7 0.7 0.7])
1630end
1631
[114]1632%------------------------------------------------------------------------
1633%---display calibration intrinsic parameters
1634function display_intrinsic(GeometryCalib,handles)
1635%------------------------------------------------------------------------
1636fx=[];
1637fy=[];
1638if isfield(GeometryCalib,'fx_fy')
1639    fx=GeometryCalib.fx_fy(1);
1640    fy=GeometryCalib.fx_fy(2);
1641end
1642Cx_Cy=[0 0];%default
1643if isfield(GeometryCalib,'Cx_Cy')
1644    Cx_Cy=GeometryCalib.Cx_Cy;
1645end
1646kc=0;
1647if isfield(GeometryCalib,'kc')
[121]1648    kc=GeometryCalib.kc; %* GeometryCalib.focal*GeometryCalib.focal;
[114]1649end
1650set(handles.fx,'String',num2str(fx,5))
1651set(handles.fy,'String',num2str(fy,5))
1652set(handles.Cx,'String',num2str(Cx_Cy(1),'%1.1f'))
1653set(handles.Cy,'String',num2str(Cx_Cy(2),'%1.1f'))
1654set(handles.kc,'String',num2str(kc,'%1.4f'))
[109]1655
1656
Note: See TracBrowser for help on using the repository browser.