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