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