[2] | 1 | %'geometry_calib': performs geometric calibration from a set of reference points |
---|
| 2 | % |
---|
| 3 | % function varargout = geometry_calib(varargin) |
---|
| 4 | % |
---|
| 5 | %A%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
---|
| 6 | % Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org. |
---|
| 7 | %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
---|
| 8 | % This file is part of the toolbox UVMAT. |
---|
| 9 | % |
---|
| 10 | % UVMAT is free software; you can redistribute it and/or modify |
---|
| 11 | % it under the terms of the GNU General Public License as published by |
---|
| 12 | % the Free Software Foundation; either version 2 of the License, or |
---|
| 13 | % (at your option) any later version. |
---|
| 14 | % |
---|
| 15 | % UVMAT is distributed in the hope that it will be useful, |
---|
| 16 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 18 | % GNU General Public License (file UVMAT/COPYING.txt) for more details. |
---|
| 19 | %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
---|
| 20 | |
---|
| 21 | function varargout = geometry_calib(varargin) |
---|
| 22 | % GEOMETRY_CALIB M-file for geometry_calib.fig |
---|
| 23 | % GEOMETRY_CALIB, by itself, creates a MenuCoord GEOMETRY_CALIB or raises the existing |
---|
| 24 | % singleton*. |
---|
| 25 | % |
---|
| 26 | % H = GEOMETRY_CALIB returns the handle to a MenuCoord GEOMETRY_CALIB or the handle to |
---|
| 27 | % the existing singleton*. |
---|
| 28 | % |
---|
| 29 | % GEOMETRY_CALIB('CALLBACK',hObject,eventData,handles,...) calls the local |
---|
| 30 | % function named CALLBACK in GEOMETRY_CALIB.M with the given input arguments. |
---|
| 31 | % |
---|
| 32 | % GEOMETRY_CALIB('Property','Value',...) creates a MenuCoord GEOMETRY_CALIB or raises the |
---|
| 33 | % existing singleton*. Starting from the left, property value pairs are |
---|
| 34 | % applied to the GUI before geometry_calib_OpeningFunction gets called. An |
---|
| 35 | % unrecognized property name or invalid value makes property application |
---|
| 36 | % stop. All inputs are passed to geometry_calib_OpeningFcn via varargin. |
---|
| 37 | % |
---|
| 38 | % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one |
---|
| 39 | % instance to run (singleton)". |
---|
| 40 | % |
---|
| 41 | % See also: GUIDE, GUIDATA, GUIHANDLES |
---|
| 42 | |
---|
| 43 | % Edit the above text to modify the response to help geometry_calib |
---|
| 44 | |
---|
[60] | 45 | % Last Modified by GUIDE v2.5 23-Mar-2010 06:22:33 |
---|
[2] | 46 | |
---|
| 47 | % Begin initialization code - DO NOT edit |
---|
| 48 | gui_Singleton = 1; |
---|
| 49 | gui_State = struct('gui_Name', mfilename, ... |
---|
| 50 | 'gui_Singleton', gui_Singleton, ... |
---|
| 51 | 'gui_OpeningFcn', @geometry_calib_OpeningFcn, ... |
---|
| 52 | 'gui_OutputFcn', @geometry_calib_OutputFcn, ... |
---|
| 53 | 'gui_LayoutFcn', [] , ... |
---|
| 54 | 'gui_Callback', []); |
---|
| 55 | if nargin & isstr(varargin{1}) |
---|
| 56 | gui_State.gui_Callback = str2func(varargin{1}); |
---|
| 57 | end |
---|
| 58 | |
---|
| 59 | if nargout |
---|
| 60 | [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); |
---|
| 61 | else |
---|
| 62 | gui_mainfcn(gui_State, varargin{:}); |
---|
| 63 | end |
---|
| 64 | % End initialization code - DO NOT edit |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | % --- Executes just before geometry_calib is made visible. |
---|
| 68 | %INPUT: |
---|
| 69 | %handles: handles of the geometry_calib interface elements |
---|
| 70 | % PlotHandles: set of handles of the elements contolling the plotting |
---|
| 71 | % parameters on the uvmat interface (obtained by 'get_plot_handle.m') |
---|
[60] | 72 | %------------------------------------------------------------------------ |
---|
[2] | 73 | function geometry_calib_OpeningFcn(hObject, eventdata, handles, handles_uvmat,pos,inputfile) |
---|
[60] | 74 | %------------------------------------------------------------------------ |
---|
[2] | 75 | % Choose default command line output for geometry_calib |
---|
| 76 | handles.output = hObject; |
---|
| 77 | |
---|
| 78 | % Update handles structure |
---|
| 79 | guidata(hObject, handles); |
---|
| 80 | movegui(hObject,'east');% position the GUI ton the right of the screen |
---|
| 81 | if exist('handles_uvmat','var') %& isfield(data,'ParentButton') |
---|
| 82 | set(hObject,'DeleteFcn',{@closefcn,handles_uvmat})% |
---|
| 83 | end |
---|
| 84 | %set the position of the interface |
---|
| 85 | if exist('pos','var')& length(pos)>2 |
---|
| 86 | pos_gui=get(hObject,'Position'); |
---|
| 87 | pos_gui(1)=pos(1); |
---|
| 88 | pos_gui(2)=pos(2); |
---|
| 89 | set(hObject,'Position',pos_gui); |
---|
| 90 | end |
---|
| 91 | inputxml=''; |
---|
| 92 | if exist('inputfile','var')& ~isempty(inputfile) |
---|
| 93 | [Path,Name,ext]=fileparts(inputfile); |
---|
| 94 | form=imformats(ext([2:end])); |
---|
| 95 | if ~isempty(form)% if the input file is an image |
---|
| 96 | struct.XmlInputfile=inputfile; |
---|
| 97 | set(hObject,'UserData',struct) |
---|
| 98 | [Pathsub,RootFile,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(inputfile); |
---|
| 99 | inputxml=[fullfile(Pathsub,RootFile) '.xml']; |
---|
| 100 | end |
---|
| 101 | end |
---|
| 102 | set(handles.ListCoord,'String',{''}) |
---|
| 103 | if exist(inputxml,'file') |
---|
| 104 | loadfile(handles,inputxml)% load the point coordiantes existing in the xml file |
---|
| 105 | end |
---|
| 106 | |
---|
| 107 | set(handles.ListCoord,'KeyPressFcn',{@key_press_fcn,handles})%set keyboard action function |
---|
| 108 | %set(hObject,'KeyPressFcn',{'keyboard_callback',handles})%set keyboard action function on uvmat interface when geometry_calib is on top |
---|
| 109 | %htable=uitable(10,5) |
---|
| 110 | %set(htable,'ColumnNames',{'x','y','z','X(pixels)','Y(pixels)'}) |
---|
| 111 | |
---|
[17] | 112 | %------------------------------------------------------------------------ |
---|
[2] | 113 | % --- Outputs from this function are returned to the command line. |
---|
| 114 | function varargout = geometry_calib_OutputFcn(hObject, eventdata, handles) |
---|
[17] | 115 | %------------------------------------------------------------------------ |
---|
[2] | 116 | % Get default command line output from handles structure |
---|
| 117 | varargout{1} = handles.output; |
---|
| 118 | varargout{2}=handles; |
---|
| 119 | |
---|
| 120 | %------------ |
---|
| 121 | function Phi_Callback(hObject, eventdata, handles) |
---|
| 122 | |
---|
[17] | 123 | %------------------------------------------------------------------------ |
---|
[2] | 124 | %read input xml file and update the edit boxes |
---|
| 125 | function loadfile(handles,fileinput) |
---|
[17] | 126 | %------------------------------------------------------------------------ |
---|
[2] | 127 | %read the input xml file |
---|
| 128 | t=xmltree(fileinput); |
---|
| 129 | s=convert(t);%convert to matlab structure |
---|
| 130 | %read data currently displayed on the interface |
---|
| 131 | PointCoord=[]; |
---|
| 132 | Coord_cell=get(handles.ListCoord,'String'); |
---|
| 133 | data=read_geometry_calib(Coord_cell); |
---|
| 134 | %data=read_geometry_calib(handles); |
---|
| 135 | Coord=[]; %default |
---|
| 136 | if isfield(data,'Coord') |
---|
| 137 | Coord=data.Coord; |
---|
| 138 | end |
---|
| 139 | TabChar_0=get(handles.ListCoord,'String'); |
---|
| 140 | nbcoord_0=size(TabChar_0,1); |
---|
| 141 | if isequal(get(handles.edit_append,'Value'),2) %edit mode A REVOIR |
---|
| 142 | val=get(handles.ListCoord,'Value')-1; |
---|
| 143 | else |
---|
| 144 | val=length(TabChar_0); |
---|
| 145 | end |
---|
| 146 | nbcoord=0; |
---|
| 147 | |
---|
| 148 | %case of calibration (ImaDoc) input file |
---|
[36] | 149 | % hcalib=get(handles.calib_type,'parent'); |
---|
| 150 | CalibData=get(handles.figure1,'UserData'); |
---|
[2] | 151 | CalibData.XmlInput=fileinput; |
---|
| 152 | if isfield(s,'Heading') |
---|
| 153 | CalibData.Heading=s.Heading; |
---|
| 154 | end |
---|
| 155 | |
---|
[36] | 156 | set(handles.figure1,'UserData',CalibData);%store the heading in the interface 'UserData' |
---|
[2] | 157 | if isfield(s,'GeometryCalib') |
---|
| 158 | Calib=s.GeometryCalib; |
---|
| 159 | if isfield(Calib,'CalibrationType') |
---|
| 160 | CalibrationType=Calib.CalibrationType; |
---|
| 161 | switch CalibrationType |
---|
| 162 | case 'linear' |
---|
| 163 | set(handles.calib_type,'Value',2) |
---|
| 164 | case 'tsai' |
---|
| 165 | set(handles.calib_type,'Value',3) |
---|
| 166 | end |
---|
| 167 | end |
---|
| 168 | if isfield(Calib,'SourceCalib') |
---|
| 169 | if isfield(Calib.SourceCalib,'PointCoord') |
---|
| 170 | PointCoord=Calib.SourceCalib.PointCoord; |
---|
| 171 | end |
---|
| 172 | end |
---|
| 173 | nbcoord=length(PointCoord); |
---|
| 174 | if ~isfield(Calib,'ErrorRms')&~isfield(Calib,'ErrorMax') %old convention of Gauthier (cord in mm) |
---|
| 175 | for i=1:length(PointCoord) |
---|
| 176 | line=str2num(PointCoord{i}); |
---|
| 177 | Coord(i+val,4:5)=line(4:5);%px x |
---|
| 178 | Coord(i+val,1:3)=line(1:3)/10;%phys x |
---|
| 179 | end |
---|
| 180 | else |
---|
| 181 | for i=1:length(PointCoord) |
---|
| 182 | line=str2num(PointCoord{i}); |
---|
| 183 | Coord(i,4:5)=line(4:5);%px x |
---|
| 184 | Coord(i,1:3)=line(1:3);%phys x |
---|
| 185 | end |
---|
| 186 | end |
---|
| 187 | end |
---|
| 188 | %case of xml files of points |
---|
| 189 | if isfield(s,'Coord') |
---|
| 190 | PointCoord=s.Coord; |
---|
| 191 | nbcoord=length(PointCoord); |
---|
| 192 | %case of image coordinates |
---|
| 193 | if isfield(s,'CoordType')& isequal(s.CoordType,'px') |
---|
| 194 | for i=1:nbcoord |
---|
| 195 | line=str2num(PointCoord{i}); |
---|
| 196 | Coord(i+val,4:5)=line(1:2); |
---|
| 197 | end |
---|
| 198 | %case of physical coordinates |
---|
| 199 | else |
---|
| 200 | for i=1:nbcoord |
---|
| 201 | line=str2num(PointCoord{i}); |
---|
| 202 | Coord(i+val,1:3)=line(1:3); |
---|
| 203 | nbcolumn=size(Coord,2); |
---|
| 204 | if nbcolumn<5 |
---|
| 205 | Coord(i+val,nbcolumn+1:5)=zeros(1,5-nbcolumn); |
---|
| 206 | end |
---|
| 207 | end |
---|
| 208 | end |
---|
| 209 | end |
---|
| 210 | CoordCell={}; |
---|
| 211 | for iline=1:size(Coord,1) |
---|
| 212 | for j=1:5 |
---|
| 213 | CoordCell{iline,j}=num2str(Coord(iline,j),4); |
---|
| 214 | end |
---|
| 215 | end |
---|
| 216 | Tabchar=cell2tab(CoordCell,' | ');%transform cells into table ready for display |
---|
| 217 | set(handles.ListCoord,'Value',1) |
---|
| 218 | set(handles.ListCoord,'String',Tabchar) |
---|
| 219 | |
---|
| 220 | |
---|
[60] | 221 | %------------------------------------------------------------------------ |
---|
[2] | 222 | % executed when closing: set the parent interface button to value 0 |
---|
| 223 | function closefcn(gcbo,eventdata,handles_uvmat) |
---|
[60] | 224 | %------------------------------------------------------------------------ |
---|
[2] | 225 | huvmat=findobj(allchild(0),'Name','uvmat'); |
---|
| 226 | if exist('handles_uvmat','var') |
---|
| 227 | set(handles_uvmat.cal,'Value',0) |
---|
| 228 | uvmat('cal_Callback',huvmat,[],handles_uvmat); |
---|
| 229 | % set(parent_button,'Value',0)%put unactivated buttons to green |
---|
| 230 | % set(parent_button,'BackgroundColor',[0 1 0]); |
---|
| 231 | end |
---|
| 232 | |
---|
[60] | 233 | %------------------------------------------------------------------------ |
---|
[2] | 234 | % --- Executes on button press in calibrate_lin. |
---|
| 235 | function APPLY_Callback(hObject, eventdata, handles) |
---|
[60] | 236 | %------------------------------------------------------------------------ |
---|
[2] | 237 | calib_cell=get(handles.calib_type,'String'); |
---|
| 238 | val=get(handles.calib_type,'Value'); |
---|
| 239 | calib_type=calib_cell{val}; |
---|
| 240 | Coord_cell=get(handles.ListCoord,'String'); |
---|
| 241 | Object=read_geometry_calib(Coord_cell); |
---|
| 242 | |
---|
| 243 | if isequal(calib_type,'rescale') |
---|
| 244 | GeometryCalib=calib_rescale(Object.Coord); |
---|
| 245 | elseif isequal(calib_type,'linear') |
---|
| 246 | GeometryCalib=calib_linear(Object.Coord); |
---|
| 247 | elseif isequal(calib_type,'tsai') |
---|
| 248 | GeometryCalib=calib_tsai(Object.Coord); |
---|
| 249 | end |
---|
| 250 | unitlist=get(handles.CoordUnit,'String'); |
---|
| 251 | unit=unitlist{get(handles.CoordUnit,'value')}; |
---|
| 252 | GeometryCalib.CoordUnit=unit; |
---|
[17] | 253 | GeometryCalib.SourceCalib.PointCoord=Object.Coord; |
---|
[2] | 254 | huvmat=findobj(allchild(0),'Name','uvmat'); |
---|
| 255 | hhuvmat=guidata(huvmat);%handles of elements in the GUI uvmat |
---|
| 256 | RootPath=''; |
---|
| 257 | RootFile=''; |
---|
| 258 | if ~isempty(hhuvmat.RootPath)& ~isempty(hhuvmat.RootFile) |
---|
| 259 | testhandle=1; |
---|
| 260 | RootPath=get(hhuvmat.RootPath,'String'); |
---|
| 261 | RootFile=get(hhuvmat.RootFile,'String'); |
---|
| 262 | filebase=fullfile(RootPath,RootFile); |
---|
| 263 | outputfile=[filebase '.xml']; |
---|
| 264 | else |
---|
| 265 | question={'save the calibration data and point coordinates in'}; |
---|
| 266 | def={fullfile(RootPath,['ObjectCalib.xml'])}; |
---|
| 267 | options.Resize='on'; |
---|
| 268 | answer=inputdlg(question,'save average in a new file',1,def,options); |
---|
| 269 | outputfile=answer{1}; |
---|
| 270 | end |
---|
[17] | 271 | update_imadoc(GeometryCalib,outputfile) |
---|
[2] | 272 | msgbox_uvmat('CONFIRMATION',{[outputfile ' updated with calibration data'];... |
---|
| 273 | ['Error rms (along x,y)=' num2str(GeometryCalib.ErrorRms) ' pixels'];... |
---|
| 274 | ['Error max (along x,y)=' num2str(GeometryCalib.ErrorMax) ' pixels']}) |
---|
| 275 | |
---|
| 276 | %display image with new calibration in the currently opened uvmat interface |
---|
| 277 | hhh=findobj(hhuvmat.axes3,'Tag','calib_marker');% delete calib points and markers |
---|
| 278 | if ~isempty(hhh) |
---|
| 279 | delete(hhh); |
---|
| 280 | end |
---|
| 281 | hhh=findobj(hhuvmat.axes3,'Tag','calib_points'); |
---|
| 282 | if ~isempty(hhh) |
---|
| 283 | delete(hhh); |
---|
| 284 | end |
---|
| 285 | set(hhuvmat.FixedLimits,'Value',0)% put FixedLimits option to 'off' |
---|
| 286 | set(hhuvmat.FixedLimits,'BackgroundColor',[0.7 0.7 0.7]) |
---|
| 287 | uvmat('RootPath_Callback',hObject,eventdata,hhuvmat); %file input with xml reading in uvmat |
---|
| 288 | |
---|
[60] | 289 | %------------------------------------------------------------------------ |
---|
[2] | 290 | % --- Executes on button press in calibrate_lin. |
---|
| 291 | function REPLICATE_Callback(hObject, eventdata, handles) |
---|
[60] | 292 | %------------------------------------------------------------------------ |
---|
[2] | 293 | calib_cell=get(handles.calib_type,'String'); |
---|
| 294 | val=get(handles.calib_type,'Value'); |
---|
| 295 | calib_type=calib_cell{val}; |
---|
| 296 | Coord_cell=get(handles.ListCoord,'String'); |
---|
| 297 | Object=read_geometry_calib(Coord_cell); |
---|
| 298 | |
---|
| 299 | if isequal(calib_type,'rescale') |
---|
| 300 | GeometryCalib=calib_rescale(Object.Coord); |
---|
| 301 | elseif isequal(calib_type,'linear') |
---|
| 302 | GeometryCalib=calib_linear(Object.Coord); |
---|
| 303 | elseif isequal(calib_type,'tsai') |
---|
| 304 | GeometryCalib=calib_tsai(Object.Coord); |
---|
| 305 | end |
---|
| 306 | % %record image source |
---|
| 307 | GeometryCalib.SourceCalib.PointCoord=Object.Coord; |
---|
| 308 | |
---|
| 309 | %open and read the dataview GUI |
---|
| 310 | h_dataview=findobj(allchild(0),'name','dataview'); |
---|
[12] | 311 | if ~isempty(h_dataview) |
---|
| 312 | delete(h_dataview) |
---|
[2] | 313 | end |
---|
[12] | 314 | CalibData=get(handles.figure1,'UserData');%read the calibration image source on the interface userdata |
---|
[2] | 315 | |
---|
[12] | 316 | if isfield(CalibData,'XmlInput') |
---|
| 317 | XmlInput=fileparts(CalibData.XmlInput); |
---|
| 318 | [XmlInput,filename,ext]=fileparts(XmlInput); |
---|
[2] | 319 | end |
---|
[12] | 320 | SubCampaignTest='n'; %default |
---|
| 321 | testinput=0; |
---|
| 322 | if isfield(CalibData,'Heading') |
---|
| 323 | Heading=CalibData.Heading; |
---|
| 324 | if isfield(Heading,'Record') && isequal([filename ext],Heading.Record) |
---|
| 325 | [XmlInput,filename,ext]=fileparts(XmlInput); |
---|
[2] | 326 | end |
---|
[12] | 327 | if isfield(Heading,'Device') && isequal([filename ext],Heading.Device) |
---|
| 328 | [XmlInput,filename,ext]=fileparts(XmlInput); |
---|
| 329 | Device=Heading.Device; |
---|
| 330 | end |
---|
| 331 | if isfield(Heading,'Experiment') && isequal([filename ext],Heading.Experiment) |
---|
| 332 | [PP,filename,ext]=fileparts(XmlInput); |
---|
| 333 | end |
---|
| 334 | testinput=0; |
---|
| 335 | if isfield(Heading,'SubCampaign') && isequal([filename ext],Heading.SubCampaign) |
---|
| 336 | SubCampaignTest='y'; |
---|
| 337 | testinput=1; |
---|
| 338 | elseif isfield(Heading,'Campaign') && isequal([filename ext],Heading.Campaign) |
---|
| 339 | testinput=1; |
---|
| 340 | end |
---|
[2] | 341 | end |
---|
[12] | 342 | if ~testinput |
---|
| 343 | filename='PROJETS';%default |
---|
| 344 | if isfield(CalibData,'XmlInput') |
---|
| 345 | [pp,filename]=fileparts(CalibData.XmlInput); |
---|
[2] | 346 | end |
---|
[12] | 347 | while ~isequal(filename,'PROJETS') && numel(filename)>1 |
---|
| 348 | filename_1=filename; |
---|
| 349 | pp_1=pp; |
---|
| 350 | [pp,filename]=fileparts(pp); |
---|
| 351 | end |
---|
| 352 | XmlInput=fullfile(pp_1,filename_1); |
---|
| 353 | testinput=1; |
---|
[2] | 354 | end |
---|
[12] | 355 | if testinput |
---|
[36] | 356 | outcome=dataview(XmlInput,SubCampaignTest,GeometryCalib); |
---|
[2] | 357 | end |
---|
| 358 | |
---|
[60] | 359 | %------------------------------------------------------------------------ |
---|
[2] | 360 | % determine the parameters for a calibration by an affine function (rescaling and offset, no rotation) |
---|
| 361 | function GeometryCalib=calib_rescale(Coord) |
---|
[60] | 362 | %------------------------------------------------------------------------ |
---|
[2] | 363 | |
---|
| 364 | X=Coord(:,1); |
---|
| 365 | Y=Coord(:,2); |
---|
| 366 | x_ima=Coord(:,4); |
---|
| 367 | y_ima=Coord(:,5); |
---|
| 368 | [px,sx]=polyfit(X,x_ima,1); |
---|
| 369 | [py,sy]=polyfit(Y,y_ima,1); |
---|
| 370 | T_x=px(2); |
---|
| 371 | T_y=py(2); |
---|
| 372 | GeometryCalib.CalibrationType='rescale'; |
---|
| 373 | GeometryCalib.focal=1; |
---|
| 374 | GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function |
---|
| 375 | GeometryCalib.Tx_Ty_Tz=[T_x T_y 1]; |
---|
| 376 | GeometryCalib.R=[px(1),0,0;0,py(1),0;0,0,1]; |
---|
| 377 | |
---|
| 378 | %check error |
---|
| 379 | Calib.dpx=1; |
---|
| 380 | Calib.dpy=1; |
---|
| 381 | Calib.sx=1; |
---|
| 382 | Calib.Cx=0; |
---|
| 383 | Calib.Cy=0; |
---|
| 384 | Calib.Tz=1; |
---|
| 385 | Calib.kappa1=0; |
---|
| 386 | Calib.f=GeometryCalib.focal; |
---|
| 387 | Calib.Tx=T_x; |
---|
| 388 | Calib.Ty=T_y; |
---|
| 389 | Calib.R=GeometryCalib.R; |
---|
| 390 | [Xpoints,Ypoints]=px_XYZ(Calib,X,Y,0); |
---|
| 391 | GeometryCalib.ErrorRms(1)=sqrt(mean((Xpoints-x_ima).*(Xpoints-x_ima))); |
---|
| 392 | GeometryCalib.ErrorMax(1)=max(abs(Xpoints-x_ima)); |
---|
| 393 | GeometryCalib.ErrorRms(2)=sqrt(mean((Ypoints-y_ima).*(Ypoints-y_ima))); |
---|
| 394 | GeometryCalib.ErrorMax(2)=max(abs(Ypoints-y_ima)); |
---|
| 395 | |
---|
[60] | 396 | %------------------------------------------------------------------------ |
---|
[2] | 397 | % determine the parameters for a calibration by a linear transform matrix (rescale and rotation) |
---|
| 398 | function GeometryCalib=calib_linear(Coord) |
---|
[60] | 399 | %------------------------------------------------------------------------ |
---|
[2] | 400 | X=Coord(:,1); |
---|
| 401 | Y=Coord(:,2); |
---|
| 402 | x_ima=Coord(:,4); |
---|
| 403 | y_ima=Coord(:,5); |
---|
| 404 | XY_mat=[ones(size(X)) X Y]; |
---|
| 405 | a_X1=XY_mat\x_ima; %transformation matrix for X |
---|
| 406 | x1=XY_mat*a_X1;%reconstruction |
---|
| 407 | err_X1=max(abs(x1-x_ima));%error |
---|
| 408 | a_Y1=XY_mat\y_ima;%transformation matrix for X |
---|
| 409 | y1=XY_mat*a_Y1; |
---|
| 410 | err_Y1=max(abs(y1-y_ima));%error |
---|
| 411 | T_x=a_X1(1); |
---|
| 412 | T_y=a_Y1(1); |
---|
| 413 | GeometryCalib.CalibrationType='linear'; |
---|
| 414 | GeometryCalib.focal=1; |
---|
| 415 | GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function |
---|
| 416 | GeometryCalib.Tx_Ty_Tz=[T_x T_y 1]; |
---|
| 417 | GeometryCalib.R=[a_X1(2),a_X1(3),0;a_Y1(2),a_Y1(3),0;0,0,1]; |
---|
| 418 | |
---|
| 419 | %check error |
---|
| 420 | GeometryCalib.ErrorRms(1)=sqrt(mean((x1-x_ima).*(x1-x_ima))); |
---|
| 421 | GeometryCalib.ErrorMax(1)=max(abs(x1-x_ima)); |
---|
| 422 | GeometryCalib.ErrorRms(2)=sqrt(mean((y1-y_ima).*(y1-y_ima))); |
---|
| 423 | GeometryCalib.ErrorMax(2)=max(abs(y1-y_ima)); |
---|
| 424 | |
---|
[60] | 425 | %------------------------------------------------------------------------ |
---|
[2] | 426 | function GeometryCalib=calib_tsai(Coord) |
---|
[60] | 427 | %------------------------------------------------------------------------ |
---|
[2] | 428 | %TSAI |
---|
| 429 | % 'calibration_lin' provides a linear transform on coordinates, |
---|
| 430 | path_uvmat=which('uvmat');% check the path detected for source file uvmat |
---|
| 431 | path_UVMAT=fileparts(path_uvmat); %path to UVMAT |
---|
[54] | 432 | % if isunix |
---|
[2] | 433 | %fid = fopen(fullfile(path_UVMAT,'PARAM_LINUX.txt'),'r');%open the file with civ binary names |
---|
[54] | 434 | xmlfile=fullfile(path_UVMAT,'PARAM.xml'); |
---|
| 435 | if exist(xmlfile,'file') |
---|
| 436 | t=xmltree(xmlfile); |
---|
| 437 | sparam=convert(t); |
---|
| 438 | end |
---|
[2] | 439 | if ~isfield(sparam,'GeometryCalib_exe') |
---|
[42] | 440 | msgbox_uvmat('ERROR',['calibration program <GeometryCalib_exe> undefined in parameter file ' xmlfile]) |
---|
[2] | 441 | return |
---|
| 442 | end |
---|
| 443 | Tsai_exe=sparam.GeometryCalib_exe; |
---|
[54] | 444 | if ~exist(Tsai_exe,'file')%the binary is defined in /bin, default setting |
---|
| 445 | Tsai_exe=fullfile(path_UVMAT,Tsai_exe); |
---|
| 446 | end |
---|
[2] | 447 | if ~exist(Tsai_exe,'file') |
---|
[54] | 448 | msgbox_uvmat('ERROR',['calibration program ' sparam.GeometryCalib_exe ' defined in PARAM.xml does not exist']) |
---|
[2] | 449 | return |
---|
| 450 | end |
---|
| 451 | |
---|
| 452 | textcoord=num2str(Coord,4); |
---|
| 453 | dlmwrite('t.txt',textcoord,''); |
---|
| 454 | % ['!' Tsai_exe ' -f1 0 -f2 t.txt'] |
---|
| 455 | eval(['!' Tsai_exe ' -f t.txt > tsaicalib.log']); |
---|
| 456 | if ~exist('calib.dat','file') |
---|
[42] | 457 | msgbox_uvmat('ERROR','no output from calibration program Tsai_exe: possibly too few points') |
---|
[2] | 458 | end |
---|
| 459 | calibdat=dlmread('calib.dat'); |
---|
[60] | 460 | delete('calib.dat') |
---|
| 461 | delete('t.txt') |
---|
[2] | 462 | GeometryCalib.CalibrationType='tsai'; |
---|
| 463 | GeometryCalib.focal=calibdat(10); |
---|
| 464 | GeometryCalib.dpx_dpy=[calibdat(5) calibdat(6)]; |
---|
| 465 | GeometryCalib.Cx_Cy=[calibdat(7) calibdat(8)]; |
---|
| 466 | GeometryCalib.sx=calibdat(9); |
---|
| 467 | GeometryCalib.kappa1=calibdat(11); |
---|
| 468 | GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function |
---|
| 469 | GeometryCalib.Tx_Ty_Tz=[calibdat(12) calibdat(13) calibdat(14)]; |
---|
| 470 | Rx_Ry_Rz=calibdat([15:17]); |
---|
| 471 | sa = sin(Rx_Ry_Rz(1)) ; |
---|
| 472 | ca=cos(Rx_Ry_Rz(1)); |
---|
| 473 | sb=sin(Rx_Ry_Rz(2)); |
---|
| 474 | cb =cos(Rx_Ry_Rz(2)); |
---|
| 475 | sg =sin(Rx_Ry_Rz(3)); |
---|
| 476 | cg =cos(Rx_Ry_Rz(3)); |
---|
| 477 | r1 = cb * cg; |
---|
| 478 | r2 = cg * sa * sb - ca * sg; |
---|
| 479 | r3 = sa * sg + ca * cg * sb; |
---|
| 480 | r4 = cb * sg; |
---|
| 481 | r5 = sa * sb * sg + ca * cg; |
---|
| 482 | r6 = ca * sb * sg - cg * sa; |
---|
| 483 | r7 = -sb; |
---|
| 484 | r8 = cb * sa; |
---|
| 485 | r9 = ca * cb; |
---|
| 486 | %EN DEDUIRE MATRICE R ?? |
---|
| 487 | GeometryCalib.R=[r1,r2,r3;r4,r5,r6;r7,r8,r9]; |
---|
| 488 | %erreur a caracteriser? |
---|
| 489 | %check error |
---|
| 490 | Calib.dpx=GeometryCalib.dpx_dpy(1); |
---|
| 491 | Calib.dpy=GeometryCalib.dpx_dpy(2); |
---|
| 492 | Calib.sx=GeometryCalib.sx; |
---|
| 493 | Calib.Cx=GeometryCalib.Cx_Cy(1); |
---|
| 494 | Calib.Cy=GeometryCalib.Cx_Cy(2); |
---|
| 495 | Calib.kappa1=GeometryCalib.kappa1; |
---|
| 496 | Calib.f=GeometryCalib.focal; |
---|
| 497 | Calib.Tx=GeometryCalib.Tx_Ty_Tz(1); |
---|
| 498 | Calib.Ty=GeometryCalib.Tx_Ty_Tz(2); |
---|
| 499 | Calib.Tz=GeometryCalib.Tx_Ty_Tz(3); |
---|
| 500 | Calib.R=GeometryCalib.R; |
---|
| 501 | X=Coord(:,1); |
---|
| 502 | Y=Coord(:,2); |
---|
| 503 | Z=Coord(:,3); |
---|
| 504 | x_ima=Coord(:,4); |
---|
| 505 | y_ima=Coord(:,5); |
---|
| 506 | [Xpoints,Ypoints]=px_XYZ(Calib,X,Y,Z); |
---|
| 507 | |
---|
| 508 | GeometryCalib.ErrorRms(1)=sqrt(mean((Xpoints-x_ima).*(Xpoints-x_ima))); |
---|
| 509 | GeometryCalib.ErrorMax(1)=max(abs(Xpoints-x_ima)); |
---|
| 510 | GeometryCalib.ErrorRms(2)=sqrt(mean((Ypoints-y_ima).*(Ypoints-y_ima))); |
---|
| 511 | GeometryCalib.ErrorMax(2)=max(abs(Ypoints-y_ima)); |
---|
| 512 | % Nfx |
---|
| 513 | % dx |
---|
| 514 | % dy |
---|
| 515 | % 5 dpx |
---|
| 516 | % 6 dpy |
---|
| 517 | % cx |
---|
| 518 | % cy |
---|
| 519 | % sx |
---|
| 520 | % f |
---|
| 521 | % kappa1 |
---|
| 522 | % tx |
---|
| 523 | % ty |
---|
| 524 | % tz |
---|
| 525 | % rx |
---|
| 526 | % ry |
---|
| 527 | % rz |
---|
| 528 | % p1 |
---|
| 529 | % p2 |
---|
| 530 | |
---|
| 531 | %calibcoeff=str2num(calibdat) |
---|
| 532 | |
---|
| 533 | |
---|
[60] | 534 | %------------------------------------------------------------------------ |
---|
[2] | 535 | % --- Executes on button press in rotation. |
---|
| 536 | function rotation_Callback(hObject, eventdata, handles) |
---|
[60] | 537 | %------------------------------------------------------------------------ |
---|
[2] | 538 | angle_rot=(pi/180)*str2num(get(handles.Phi,'String')); |
---|
| 539 | Coord_cell=get(handles.ListCoord,'String'); |
---|
| 540 | data=read_geometry_calib(Coord_cell); |
---|
| 541 | data.Coord(:,1)=cos(angle_rot)*data.Coord(:,1)+sin(angle_rot)*data.Coord(:,2); |
---|
| 542 | data.Coord(:,1)=-sin(angle_rot)*data.Coord(:,1)+cos(angle_rot)*data.Coord(:,2); |
---|
| 543 | set(handles.XObject,'String',num2str(data.Coord(:,1),4)); |
---|
| 544 | set(handles.YObject,'String',num2str(data.Coord(:,2),4)); |
---|
| 545 | |
---|
[60] | 546 | %------------------------------------------------------------------------ |
---|
[2] | 547 | function XImage_Callback(hObject, eventdata, handles) |
---|
[60] | 548 | %------------------------------------------------------------------------ |
---|
[2] | 549 | update_list(hObject, eventdata,handles) |
---|
| 550 | |
---|
[60] | 551 | %------------------------------------------------------------------------ |
---|
[2] | 552 | function YImage_Callback(hObject, eventdata, handles) |
---|
[60] | 553 | %------------------------------------------------------------------------ |
---|
[2] | 554 | update_list(hObject, eventdata,handles) |
---|
| 555 | |
---|
| 556 | function XObject_Callback(hObject, eventdata, handles) |
---|
| 557 | update_list(hObject, eventdata,handles) |
---|
| 558 | |
---|
| 559 | function YObject_Callback(hObject, eventdata, handles) |
---|
| 560 | update_list(hObject, eventdata,handles) |
---|
| 561 | |
---|
| 562 | function ZObject_Callback(hObject, eventdata, handles) |
---|
| 563 | update_list(hObject, eventdata,handles) |
---|
| 564 | |
---|
[60] | 565 | %------------------------------------------------------------------------ |
---|
[2] | 566 | function update_list(hObject, eventdata, handles) |
---|
[60] | 567 | %------------------------------------------------------------------------ |
---|
[2] | 568 | str4=get(handles.XImage,'String'); |
---|
| 569 | str5=get(handles.YImage,'String'); |
---|
| 570 | str1=get(handles.XObject,'String'); |
---|
| 571 | tt=double(str1); |
---|
| 572 | str2=get(handles.YObject,'String'); |
---|
| 573 | str3=get(handles.ZObject,'String'); |
---|
| 574 | if ~isempty(str1) & ~isequal(double(str1),32) & (isempty(str3)|isequal(double(str3),32)) |
---|
| 575 | str3='0';%put z to 0 by default |
---|
| 576 | end |
---|
| 577 | strline=[str1 ' | ' str2 ' | ' str3 ' | ' str4 ' | ' str5]; |
---|
| 578 | Coord=get(handles.ListCoord,'String'); |
---|
| 579 | val=get(handles.ListCoord,'Value'); |
---|
| 580 | Coord{val}=strline; |
---|
| 581 | set(handles.ListCoord,'String',Coord) |
---|
[60] | 582 | %update the plot |
---|
| 583 | ListCoord_Callback(hObject, eventdata, handles) |
---|
[2] | 584 | |
---|
[60] | 585 | %------------------------------------------------------------------------ |
---|
[2] | 586 | % --- Executes on selection change in ListCoord. |
---|
| 587 | function ListCoord_Callback(hObject, eventdata, handles) |
---|
[60] | 588 | %------------------------------------------------------------------------ |
---|
[2] | 589 | Coord_cell=get(handles.ListCoord,'String'); |
---|
| 590 | val=get(handles.ListCoord,'Value'); |
---|
| 591 | if length(Coord_cell)>0 |
---|
| 592 | coord_str=Coord_cell{val}; |
---|
| 593 | k=findstr('|',coord_str); |
---|
| 594 | if isempty(k) |
---|
| 595 | return |
---|
| 596 | end |
---|
| 597 | set(handles.XObject,'String',coord_str(1:k(1)-5)) |
---|
| 598 | set(handles.YObject,'String',coord_str(k(1)+5:k(2)-5)) |
---|
| 599 | set(handles.ZObject,'String',coord_str(k(2)+5:k(3)-5)) |
---|
| 600 | set(handles.XImage,'String',coord_str(k(3)+5:k(4)-5)) |
---|
| 601 | set(handles.YImage,'String',coord_str(k(4)+5:end)) |
---|
| 602 | huvmat=findobj(allchild(0),'Name','uvmat');%find the current uvmat interface handle |
---|
| 603 | hplot=findobj(huvmat,'Tag','axes3');%main plotting axis of uvmat |
---|
| 604 | h_menu_coord=findobj(huvmat,'Tag','menu_coord'); |
---|
| 605 | menu=get(h_menu_coord,'String'); |
---|
| 606 | choice=get(h_menu_coord,'Value'); |
---|
| 607 | if iscell(menu) |
---|
| 608 | option=menu{choice}; |
---|
| 609 | else |
---|
| 610 | option='px'; %default |
---|
| 611 | end |
---|
| 612 | if isequal(option,'phys') |
---|
| 613 | XCoord=str2num(coord_str(1:k(1)-5)); |
---|
| 614 | YCoord=str2num(coord_str(k(1)+5:k(2)-5)); |
---|
| 615 | elseif isequal(option,'px')|| isequal(option,'') |
---|
| 616 | XCoord=str2num(coord_str(k(3)+5:k(4)-5)); |
---|
| 617 | YCoord=str2num(coord_str(k(4)+5:end)); |
---|
| 618 | else |
---|
[42] | 619 | msgbox_uvmat('ERROR','the choice in menu_coord of uvmat must be px or phys ') |
---|
[2] | 620 | end |
---|
| 621 | huvmat=findobj(allchild(0),'Name','uvmat');%find the current uvmat interface handle |
---|
| 622 | hplot=findobj(huvmat,'Tag','axes3');%main plotting axis of uvmat |
---|
| 623 | hhh=findobj(hplot,'Tag','calib_marker'); |
---|
| 624 | if isempty(hhh) |
---|
| 625 | axes(hplot) |
---|
| 626 | line(XCoord,YCoord,'Color','m','Tag','calib_marker','LineStyle','.','Marker','o','MarkerSize',20); |
---|
| 627 | else |
---|
| 628 | set(hhh,'XData',XCoord) |
---|
| 629 | set(hhh,'YData',YCoord) |
---|
| 630 | end |
---|
| 631 | end |
---|
| 632 | |
---|
[60] | 633 | %------------------------------------------------------------------------ |
---|
[2] | 634 | % --- Executes on selection change in edit_append. |
---|
| 635 | function edit_append_Callback(hObject, eventdata, handles) |
---|
[60] | 636 | %------------------------------------------------------------------------ |
---|
[2] | 637 | choice=get(handles.edit_append,'Value'); |
---|
| 638 | if choice==1 |
---|
| 639 | Coord=get(handles.ListCoord,'String'); |
---|
| 640 | val=length(Coord); |
---|
| 641 | if val>=1 & isequal(Coord{val},'') |
---|
| 642 | val=val-1; %do not take into account blank |
---|
| 643 | end |
---|
| 644 | Coord{val+1}=''; |
---|
| 645 | set(handles.ListCoord,'String',Coord) |
---|
| 646 | set(handles.ListCoord,'Value',val+1) |
---|
| 647 | end |
---|
| 648 | |
---|
| 649 | |
---|
| 650 | |
---|
| 651 | function NEW_Callback(hObject, eventdata, handles) |
---|
| 652 | %A METTRE SOUS UN BOUTON |
---|
| 653 | huvmat=findobj(allchild(0),'Name','uvmat'); |
---|
| 654 | hchild=get(huvmat,'children'); |
---|
| 655 | hcoord=findobj(hchild,'Tag','menu_coord'); |
---|
| 656 | coordtype=get(hcoord,'Value'); |
---|
| 657 | haxes=findobj(hchild,'Tag','axes3'); |
---|
| 658 | AxeData=get(haxes,'UserData'); |
---|
| 659 | if ~isequal(hcoord,2) |
---|
| 660 | set(hcoord,'Value',2) |
---|
| 661 | huvmat=uvmat(AxeData); |
---|
| 662 | 'relancer uvmat'; |
---|
| 663 | end |
---|
| 664 | if ~isfield(AxeData,'ZoomAxes') |
---|
[42] | 665 | msgbox_uvmat('ERROR','first draw a window around a grid marker') |
---|
[2] | 666 | return |
---|
| 667 | end |
---|
| 668 | XLim=get(AxeData.ZoomAxes,'XLim'); |
---|
| 669 | YLim=get(AxeData.ZoomAxes,'YLim'); |
---|
| 670 | np=size(AxeData.A); |
---|
| 671 | ind_sub_x=round(XLim); |
---|
| 672 | ind_sub_y=np(1)-round(YLim); |
---|
| 673 | Mfiltre=AxeData.A([ind_sub_y(2):ind_sub_y(1)] ,ind_sub_x,:); |
---|
| 674 | Mfiltre_norm=double(Mfiltre); |
---|
| 675 | Mfiltre_norm=Mfiltre_norm/sum(sum(Mfiltre_norm)); |
---|
| 676 | Mfiltre_norm=100*(Mfiltre_norm-mean(mean(Mfiltre_norm))); |
---|
| 677 | Atype=class(AxeData.A); |
---|
| 678 | Data.NbDim=2; |
---|
| 679 | Data.A=filter2(Mfiltre_norm,double(AxeData.A)); |
---|
| 680 | Data.A=feval(Atype,Data.A); |
---|
| 681 | Data.AName='image'; |
---|
| 682 | Data.AX=AxeData.AX; |
---|
| 683 | Data.AY=AxeData.AY; |
---|
| 684 | Data.CoordType='px'; |
---|
| 685 | plot_field(Data) |
---|
| 686 | |
---|
| 687 | |
---|
[60] | 688 | %------------------------------------------------------------------------ |
---|
| 689 | % --- 'key_press_fcn:' function activated when a key is pressed on the keyboard |
---|
[2] | 690 | function key_press_fcn(hObject,eventdata,handles) |
---|
[60] | 691 | %------------------------------------------------------------------------ |
---|
[2] | 692 | hh=get(hObject,'parent'); |
---|
| 693 | xx=double(get(hh,'CurrentCharacter')); %get the keyboard character |
---|
| 694 | |
---|
| 695 | if ismember(xx,[8 127])%backspace or delete |
---|
| 696 | Coord_cell=get(handles.ListCoord,'String'); |
---|
| 697 | data=read_geometry_calib(Coord_cell); |
---|
| 698 | Coord=[]; %default |
---|
| 699 | if isfield(data,'Coord') |
---|
| 700 | Coord=data.Coord; |
---|
| 701 | end |
---|
| 702 | val=get(handles.ListCoord,'Value'); |
---|
| 703 | Coord(val,:)=[];%suppress the selected item in the list |
---|
| 704 | CoordCell={}; |
---|
| 705 | for iline=1:size(Coord,1) |
---|
| 706 | for j=1:5 |
---|
| 707 | CoordCell{iline,j}=num2str(Coord(iline,j),4); |
---|
| 708 | end |
---|
| 709 | end |
---|
| 710 | Tabchar=cell2tab(CoordCell,' | ');%transform cells into table ready for display |
---|
| 711 | val=min(size(Coord,1),val); |
---|
| 712 | set(handles.ListCoord,'Value',max(val,1)) |
---|
| 713 | set(handles.ListCoord,'String',Tabchar) |
---|
| 714 | ListCoord_Callback(hObject, eventdata, handles) |
---|
[17] | 715 | MenuPlot_Callback(hObject,eventdata,handles) |
---|
[2] | 716 | end |
---|
| 717 | |
---|
[60] | 718 | %------------------------------------------------------------------------ |
---|
[2] | 719 | % --- Executes on button press in append_point. |
---|
| 720 | function append_point_Callback(hObject, eventdata, handles) |
---|
[60] | 721 | %------------------------------------------------------------------------ |
---|
[2] | 722 | Coord=get(handles.ListCoord,'String'); |
---|
| 723 | val=length(Coord); |
---|
| 724 | if val>=1 & isequal(Coord{val},'') |
---|
| 725 | val=val-1; %do not take into account blank |
---|
| 726 | end |
---|
| 727 | Coord{val+1}=''; |
---|
| 728 | set(handles.ListCoord,'String',Coord) |
---|
| 729 | set(handles.ListCoord,'Value',val+1) |
---|
| 730 | |
---|
[60] | 731 | %------------------------------------------------------------------------ |
---|
[2] | 732 | function MenuOpen_Callback(hObject, eventdata, handles) |
---|
[60] | 733 | %------------------------------------------------------------------------ |
---|
[2] | 734 | %get the object file |
---|
| 735 | huvmat=findobj(allchild(0),'Name','uvmat'); |
---|
| 736 | UvData=get(huvmat,'UserData'); |
---|
| 737 | hchild=get(huvmat,'Children'); |
---|
| 738 | hrootpath=findobj(hchild,'Tag','RootPath'); |
---|
| 739 | oldfile=get(hrootpath,'String'); |
---|
| 740 | if isempty(oldfile) |
---|
| 741 | oldfile=''; |
---|
| 742 | end |
---|
| 743 | %[FileName,PathName] = uigetfile('*.civ','Select a .civ file',oldfile) |
---|
| 744 | [FileName, PathName, filterindex] = uigetfile( ... |
---|
| 745 | {'*.xml;*.mat', ' (*.xml,*.mat)'; |
---|
| 746 | '*.xml', '.xml files '; ... |
---|
| 747 | '*.mat', '.mat matlab files '}, ... |
---|
| 748 | 'Pick a file',oldfile); |
---|
| 749 | fileinput=[PathName FileName];%complete file name |
---|
| 750 | testblank=findstr(fileinput,' ');%look for blanks |
---|
| 751 | if ~isempty(testblank) |
---|
| 752 | msgbox_uvmat('ERROR','forbidden input file name or path: no blank character allowed') |
---|
| 753 | return |
---|
| 754 | end |
---|
| 755 | sizf=size(fileinput); |
---|
| 756 | if (~ischar(fileinput)|~isequal(sizf(1),1)),return;end |
---|
| 757 | loadfile(handles,fileinput) |
---|
| 758 | |
---|
| 759 | |
---|
[60] | 760 | %------------------------------------------------------------------------ |
---|
[2] | 761 | function MenuPlot_Callback(hObject, eventdata, handles) |
---|
[60] | 762 | %------------------------------------------------------------------------ |
---|
[2] | 763 | huvmat=findobj(allchild(0),'Name','uvmat');%find the current uvmat interface handle |
---|
| 764 | UvData=get(huvmat,'UserData');%Data associated to the current uvmat interface |
---|
| 765 | hhuvmat=guidata(huvmat); %handles of GUI elements in uvmat |
---|
| 766 | hplot=findobj(huvmat,'Tag','axes3');%main plotting axis of uvmat |
---|
[60] | 767 | h_menu_coord=findobj(huvmat,'Tag','transform_fct'); |
---|
[2] | 768 | menu=get(h_menu_coord,'String'); |
---|
| 769 | choice=get(h_menu_coord,'Value'); |
---|
| 770 | if iscell(menu) |
---|
| 771 | option=menu{choice}; |
---|
| 772 | else |
---|
| 773 | option='px'; %default |
---|
| 774 | end |
---|
| 775 | Coord_cell=get(handles.ListCoord,'String'); |
---|
| 776 | ObjectData=read_geometry_calib(Coord_cell); |
---|
| 777 | %ObjectData=read_geometry_calib(handles);%read the interface input parameters defining the object |
---|
| 778 | if isequal(option,'phys') |
---|
| 779 | ObjectData.Coord=ObjectData.Coord(:,[1:3]); |
---|
| 780 | elseif isequal(option,'px')||isequal(option,'') |
---|
| 781 | ObjectData.Coord=ObjectData.Coord(:,[4:5]); |
---|
| 782 | else |
---|
| 783 | msgbox_uvmat('ERROR','the choice in menu_coord of uvmat must be px or phys ') |
---|
| 784 | end |
---|
| 785 | axes(hhuvmat.axes3) |
---|
| 786 | hh=findobj('Tag','calib_points'); |
---|
| 787 | if isempty(hh) |
---|
| 788 | hh=line(ObjectData.Coord(:,1),ObjectData.Coord(:,2),'Color','m','Tag','calib_points','LineStyle','.','Marker','+'); |
---|
| 789 | else |
---|
| 790 | set(hh,'XData',ObjectData.Coord(:,1)) |
---|
| 791 | set(hh,'YData',ObjectData.Coord(:,2)) |
---|
| 792 | end |
---|
| 793 | |
---|
| 794 | % -------------------------------------------------------------------- |
---|
| 795 | function MenuHelp_Callback(hObject, eventdata, handles) |
---|
| 796 | path_to_uvmat=which ('uvmat');% check the path of uvmat |
---|
| 797 | pathelp=fileparts(path_to_uvmat); |
---|
[36] | 798 | helpfile=fullfile(pathelp,'uvmat_doc','uvmat_doc.html'); |
---|
| 799 | 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] | 800 | else |
---|
[36] | 801 | addpath (fullfile(pathelp,'uvmat_doc')) |
---|
[2] | 802 | web([helpfile '#geometry_calib']) |
---|
| 803 | end |
---|
| 804 | |
---|
[17] | 805 | %------------------------------------------------------------------------ |
---|
[2] | 806 | function MenuCreateGrid_Callback(hObject, eventdata, handles) |
---|
[17] | 807 | %------------------------------------------------------------------------ |
---|
[36] | 808 | %hcalib=get(handles.calib_type,'parent');%handles of the GUI geometry_calib |
---|
| 809 | CalibData=get(handles.figure1,'UserData'); |
---|
[12] | 810 | Tinput=[];%default |
---|
| 811 | if isfield(CalibData,'grid') |
---|
| 812 | Tinput=CalibData.grid; |
---|
| 813 | end |
---|
[60] | 814 | [T,CalibData.grid]=create_grid(grid_input);%display the GUI create_grid |
---|
[36] | 815 | set(handles.figure1,'UserData',CalibData) |
---|
[2] | 816 | |
---|
[12] | 817 | %grid in phys space |
---|
| 818 | Coord_cell=get(handles.ListCoord,'String'); |
---|
[36] | 819 | data=read_geometry_calib(Coord_cell); |
---|
[17] | 820 | nbpoints=size(data.Coord,1); %nbre of calibration points |
---|
[36] | 821 | data.Coord(1:size(T,1),1:3)=T;%update the existing list of phys coordinates from the GUI create_grid |
---|
[17] | 822 | for i=1:nbpoints |
---|
| 823 | for j=1:5 |
---|
| 824 | Coord{i,j}=num2str(data.Coord(i,j),4);%display coordiantes with 4 digits |
---|
[12] | 825 | end |
---|
| 826 | end |
---|
[17] | 827 | for i=nbpoints+1:size(data.Coord,1) |
---|
| 828 | for j=1:3 |
---|
| 829 | Coord{i,j}=num2str(data.Coord(i,j),4);%display coordiantes with 4 digits |
---|
| 830 | end |
---|
| 831 | for j=4:5 |
---|
| 832 | Coord{i,j}='';%display coordiantes with 4 digi |
---|
| 833 | end |
---|
| 834 | end |
---|
| 835 | |
---|
| 836 | |
---|
| 837 | %size(data.Coord,1) |
---|
[12] | 838 | Tabchar=cell2tab(Coord,' | '); |
---|
| 839 | set(handles.ListCoord,'Value',1) |
---|
| 840 | set(handles.ListCoord,'String',Tabchar) |
---|
[2] | 841 | |
---|
[17] | 842 | %----------------------------------------------------------------------- |
---|
[2] | 843 | function MenuTranslatePoints_Callback(hObject, eventdata, handles) |
---|
[17] | 844 | %----------------------------------------------------------------------- |
---|
[36] | 845 | %hcalib=get(handles.calib_type,'parent');%handles of the GUI geometry_calib |
---|
| 846 | CalibData=get(handles.figure1,'UserData'); |
---|
[2] | 847 | Tinput=[];%default |
---|
| 848 | if isfield(CalibData,'translate') |
---|
| 849 | Tinput=CalibData.translate; |
---|
| 850 | end |
---|
| 851 | T=translate_points(Tinput);%display translate_points GUI and get shift parameters |
---|
| 852 | CalibData.translate=T; |
---|
[36] | 853 | set(handles.figure1,'UserData',CalibData) |
---|
[12] | 854 | %translation |
---|
[2] | 855 | Coord_cell=get(handles.ListCoord,'String'); |
---|
| 856 | data=read_geometry_calib(Coord_cell); |
---|
| 857 | data.Coord(:,1)=T(1)+data.Coord(:,1); |
---|
| 858 | data.Coord(:,2)=T(2)+data.Coord(:,2); |
---|
| 859 | data.Coord(:,3)=T(3)+data.Coord(:,3); |
---|
| 860 | data.Coord(:,[4 5])=data.Coord(:,[4 5]); |
---|
| 861 | for i=1:size(data.Coord,1) |
---|
| 862 | for j=1:5 |
---|
| 863 | Coord{i,j}=num2str(data.Coord(i,j),4);%phys x,y,z |
---|
| 864 | end |
---|
| 865 | end |
---|
| 866 | Tabchar=cell2tab(Coord,' | '); |
---|
| 867 | set(handles.ListCoord,'Value',1) |
---|
| 868 | set(handles.ListCoord,'String',Tabchar) |
---|
| 869 | |
---|
| 870 | |
---|
| 871 | % -------------------------------------------------------------------- |
---|
| 872 | function MenuRotatePoints_Callback(hObject, eventdata, handles) |
---|
[36] | 873 | %hcalib=get(handles.calib_type,'parent');%handles of the GUI geometry_calib |
---|
| 874 | CalibData=get(handles.figure1,'UserData'); |
---|
[12] | 875 | Tinput=[];%default |
---|
| 876 | if isfield(CalibData,'rotate') |
---|
| 877 | Tinput=CalibData.rotate; |
---|
[2] | 878 | end |
---|
[12] | 879 | T=rotate_points(Tinput);%display translate_points GUI and get shift parameters |
---|
| 880 | CalibData.rotate=T; |
---|
[36] | 881 | set(handles.figure1,'UserData',CalibData) |
---|
[12] | 882 | %----------------------------------------------------- |
---|
| 883 | %rotation |
---|
| 884 | Phi=T(1); |
---|
| 885 | O_x=0;%default |
---|
| 886 | O_y=0;%default |
---|
| 887 | if numel(T)>=2 |
---|
| 888 | O_x=T(2);%default |
---|
[2] | 889 | end |
---|
[12] | 890 | if numel(T)>=3 |
---|
| 891 | O_y=T(3);%default |
---|
[2] | 892 | end |
---|
[12] | 893 | Coord_cell=get(handles.ListCoord,'String'); |
---|
| 894 | data=read_geometry_calib(Coord_cell); |
---|
| 895 | r1=cos(pi*Phi/180); |
---|
| 896 | r2=-sin(pi*Phi/180); |
---|
| 897 | r3=sin(pi*Phi/180); |
---|
| 898 | r4=cos(pi*Phi/180); |
---|
| 899 | x=data.Coord(:,1)-O_x; |
---|
| 900 | y=data.Coord(:,2)-O_y; |
---|
| 901 | data.Coord(:,1)=r1*x+r2*y; |
---|
| 902 | data.Coord(:,2)=r3*x+r4*y; |
---|
| 903 | % data.Coord(:,[4 5])=data.Coord(:,[4 5]); |
---|
| 904 | for i=1:size(data.Coord,1) |
---|
| 905 | for j=1:5 |
---|
| 906 | Coord{i,j}=num2str(data.Coord(i,j),4);%phys x,y,z |
---|
| 907 | end |
---|
[2] | 908 | end |
---|
[12] | 909 | Tabchar=cell2tab(Coord,' | '); |
---|
| 910 | set(handles.ListCoord,'Value',1) |
---|
| 911 | set(handles.ListCoord,'String',Tabchar) |
---|
[2] | 912 | |
---|
| 913 | |
---|
[60] | 914 | % -------------------------------------------------------------------- |
---|
| 915 | function MenuDetectGrid_Callback(hObject, eventdata, handles) |
---|
| 916 | |
---|
| 917 | CalibData=get(handles.figure1,'UserData'); |
---|
| 918 | grid_input=[];%default |
---|
| 919 | if isfield(CalibData,'grid') |
---|
| 920 | grid_input=CalibData.grid;%retrieve the previously used grid |
---|
| 921 | end |
---|
| 922 | [T,CalibData.grid]=create_grid(grid_input);%display the GUI create_grid |
---|
| 923 | set(handles.figure1,'UserData',CalibData)%store the phys grid for later use |
---|
| 924 | |
---|
| 925 | %read the four last point coordiantes in pixels |
---|
| 926 | Coord_cell=get(handles.ListCoord,'String');%read list of coordiantes on geometry_calib |
---|
| 927 | data=read_geometry_calib(Coord_cell); |
---|
| 928 | nbpoints=size(data.Coord,1); %nbre of calibration points |
---|
| 929 | if nbpoints<4 |
---|
| 930 | msgbox_uvmat('ERROR','four points must be selected by the mouse to delimitate the detection area') |
---|
| 931 | end |
---|
| 932 | corners_X=(data.Coord(end-3:end,4)); %pixel absissa of the four corners |
---|
| 933 | corners_Y=(data.Coord(end-3:end,5)); |
---|
| 934 | |
---|
| 935 | %read the current image |
---|
| 936 | huvmat=findobj(allchild(0),'Name','uvmat'); |
---|
| 937 | UvData=get(huvmat,'UserData'); |
---|
| 938 | A=UvData.Field.A; |
---|
| 939 | npxy=size(A); |
---|
| 940 | %linear transform on the current image |
---|
| 941 | X=[CalibData.grid.x_0 CalibData.grid.x_1 CalibData.grid.x_0 CalibData.grid.x_1]';%corner absissa in the rectified image |
---|
| 942 | Y=[CalibData.grid.y_0 CalibData.grid.y_0 CalibData.grid.y_1 CalibData.grid.y_1]';%corner absissa in the rectified image |
---|
| 943 | XY_mat=[ones(size(X)) X Y]; |
---|
| 944 | a_X1=XY_mat\corners_X; %transformation matrix for X |
---|
| 945 | x1=XY_mat*a_X1;%reconstruction |
---|
| 946 | err_X1=max(abs(x1-corners_X))%error |
---|
| 947 | a_Y1=XY_mat\corners_Y;%transformation matrix for X |
---|
| 948 | y1=XY_mat*a_Y1; |
---|
| 949 | err_Y1=max(abs(y1-corners_Y))%error |
---|
| 950 | GeometryCalib.CalibrationType='linear'; |
---|
| 951 | GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function |
---|
| 952 | GeometryCalib.f=1; |
---|
| 953 | GeometryCalib.dpx=1; |
---|
| 954 | GeometryCalib.dpy=1; |
---|
| 955 | GeometryCalib.sx=1; |
---|
| 956 | GeometryCalib.Cx=0; |
---|
| 957 | GeometryCalib.Cy=0; |
---|
| 958 | GeometryCalib.kappa1=0; |
---|
| 959 | GeometryCalib.Tx=a_X1(1); |
---|
| 960 | GeometryCalib.Ty=a_Y1(1); |
---|
| 961 | GeometryCalib.Tz=1; |
---|
| 962 | GeometryCalib.R=[a_X1(2),a_X1(3),0;a_Y1(2),a_Y1(3),0;0,0,1]; |
---|
| 963 | [Amod,Rangx,Rangy]=phys_Ima(A-min(min(A)),GeometryCalib,0); |
---|
| 964 | Amod=double(Amod); |
---|
| 965 | %figure(12) |
---|
| 966 | %Amax=max(max(Amod)) |
---|
| 967 | %image(Rangx,Rangy,uint8(255*Amod/Amax)) |
---|
| 968 | ind_range=10;% range of search of image ma around each point obtained by linear interpolation from the marked points |
---|
| 969 | nbpoints=size(T,1); |
---|
| 970 | for ipoint=1:nbpoints |
---|
| 971 | Dx=(Rangx(2)-Rangx(1))/(npxy(2)-1); %x mesh in real space |
---|
| 972 | Dy=(Rangy(2)-Rangy(1))/(npxy(1)-1); %y mesh in real space |
---|
| 973 | i0=1+round((T(ipoint,1)-Rangx(1))/Dx);%round(Xpx(ipoint)); |
---|
| 974 | j0=1+round((T(ipoint,2)-Rangy(1))/Dy);%round(Xpx(ipoint)); |
---|
| 975 | Asub=Amod(j0-ind_range:j0+ind_range,i0-ind_range:i0+ind_range); |
---|
| 976 | x_profile=sum(Asub,1); |
---|
| 977 | y_profile=sum(Asub,2); |
---|
| 978 | [Amax,ind_x_max]=max(x_profile); |
---|
| 979 | [Amax,ind_y_max]=max(y_profile); |
---|
| 980 | Delta(ipoint,1)=(ind_x_max-ind_range-1)*Dx;%shift from the initial guess |
---|
| 981 | Delta(ipoint,2)=(ind_y_max-ind_range-1)*Dy; |
---|
| 982 | end |
---|
| 983 | Tmod=T(:,(1:2))+Delta; |
---|
| 984 | [Xpx,Ypx]=px_XYZ(GeometryCalib,Tmod(:,1),Tmod(:,2)); |
---|
| 985 | for i=1:nbpoints |
---|
| 986 | Coord{i,1}=num2str(T(i,1),4);%display coordiantes with 4 digits |
---|
| 987 | Coord{i,2}=num2str(T(i,2),4);%display coordiantes with 4 digits |
---|
| 988 | Coord{i,3}='0'; |
---|
| 989 | Coord{i,4}=num2str(Xpx(i),4);%display coordiantes with 4 digi |
---|
| 990 | Coord{i,5}=num2str(Ypx(i),4);%display coordiantes with 4 digi |
---|
| 991 | end |
---|
| 992 | Tabchar=cell2tab(Coord,' | '); |
---|
| 993 | set(handles.ListCoord,'Value',1) |
---|
| 994 | set(handles.ListCoord,'String',Tabchar) |
---|
| 995 | |
---|
| 996 | |
---|
| 997 | %%%%%%%%%%%%%%%%%%%% |
---|
| 998 | function [A_out,Rangx,Rangy]=phys_Ima(A,Calib,ZIndex) |
---|
| 999 | xcorner=[]; |
---|
| 1000 | ycorner=[]; |
---|
| 1001 | npx=[]; |
---|
| 1002 | npy=[]; |
---|
| 1003 | siz=size(A); |
---|
| 1004 | npx=[npx siz(2)]; |
---|
| 1005 | npy=[npy siz(1)]; |
---|
| 1006 | xima=[0.5 siz(2)-0.5 0.5 siz(2)-0.5];%image coordiantes of corners |
---|
| 1007 | yima=[0.5 0.5 siz(1)-0.5 siz(1)-0.5]; |
---|
| 1008 | [xcorner,ycorner]=phys_XYZ(Calib,xima,yima,ZIndex);%corresponding physical coordinates |
---|
| 1009 | Rangx(1)=min(xcorner); |
---|
| 1010 | Rangx(2)=max(xcorner); |
---|
| 1011 | Rangy(2)=min(ycorner); |
---|
| 1012 | Rangy(1)=max(ycorner); |
---|
| 1013 | test_multi=(max(npx)~=min(npx)) | (max(npy)~=min(npy)); |
---|
| 1014 | npx=max(npx); |
---|
| 1015 | npy=max(npy); |
---|
| 1016 | x=linspace(Rangx(1),Rangx(2),npx); |
---|
| 1017 | y=linspace(Rangy(1),Rangy(2),npy); |
---|
| 1018 | [X,Y]=meshgrid(x,y);%grid in physical coordiantes |
---|
| 1019 | vec_B=[]; |
---|
| 1020 | |
---|
| 1021 | zphys=0; %default |
---|
| 1022 | if isfield(Calib,'SliceCoord') %.Z= index of plane |
---|
| 1023 | SliceCoord=Calib.SliceCoord(ZIndex,:); |
---|
| 1024 | zphys=SliceCoord(3); %to generalize for non-parallel planes |
---|
| 1025 | end |
---|
| 1026 | [XIMA,YIMA]=px_XYZ(Calib,X,Y,zphys);%corresponding image indices for each point in the real space grid |
---|
| 1027 | XIMA=reshape(round(XIMA),1,npx*npy);%indices reorganized in 'line' |
---|
| 1028 | YIMA=reshape(round(YIMA),1,npx*npy); |
---|
| 1029 | flagin=XIMA>=1 & XIMA<=npx & YIMA >=1 & YIMA<=npy;%flagin=1 inside the original image |
---|
| 1030 | testuint8=isa(A,'uint8'); |
---|
| 1031 | testuint16=isa(A,'uint16'); |
---|
| 1032 | if numel(siz)==2 %(B/W images) |
---|
| 1033 | vec_A=reshape(A,1,npx*npy);%put the original image in line |
---|
| 1034 | ind_in=find(flagin); |
---|
| 1035 | ind_out=find(~flagin); |
---|
| 1036 | ICOMB=((XIMA-1)*npy+(npy+1-YIMA)); |
---|
| 1037 | ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A |
---|
| 1038 | vec_B(ind_in)=vec_A(ICOMB); |
---|
| 1039 | vec_B(ind_out)=zeros(size(ind_out)); |
---|
| 1040 | A_out=reshape(vec_B,npy,npx);%new image in real coordinates |
---|
| 1041 | elseif numel(siz)==3 |
---|
| 1042 | for icolor=1:siz(3) |
---|
| 1043 | vec_A=reshape(A{icell}(:,:,icolor),1,npx*npy);%put the original image in line |
---|
| 1044 | ind_in=find(flagin); |
---|
| 1045 | ind_out=find(~flagin); |
---|
| 1046 | ICOMB=((XIMA-1)*npy+(npy+1-YIMA)); |
---|
| 1047 | ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A |
---|
| 1048 | vec_B(ind_in)=vec_A(ICOMB); |
---|
| 1049 | vec_B(ind_out)=zeros(size(ind_out)); |
---|
| 1050 | A_out(:,:,icolor)=reshape(vec_B,npy,npx);%new image in real coordinates |
---|
| 1051 | end |
---|
| 1052 | end |
---|
| 1053 | if testuint8 |
---|
| 1054 | A_out=uint8(A_out); |
---|
| 1055 | end |
---|
| 1056 | if testuint16 |
---|
| 1057 | A_out=uint16(A_out); |
---|
| 1058 | end |
---|
| 1059 | |
---|
| 1060 | %INPUT: |
---|
| 1061 | %Z: index of plane |
---|
| 1062 | function [Xphys,Yphys,Zphys]=phys_XYZ(Calib,X,Y,Z) |
---|
| 1063 | if exist('Z','var')& isequal(Z,round(Z))& Z>0 & isfield(Calib,'SliceCoord')&length(Calib.SliceCoord)>=Z |
---|
| 1064 | Zindex=Z; |
---|
| 1065 | Zphys=Calib.SliceCoord(Zindex,3);%GENERALISER AUX CAS AVEC ANGLE |
---|
| 1066 | else |
---|
| 1067 | % if exist('Z','var') |
---|
| 1068 | % Zphys=Z; |
---|
| 1069 | % else |
---|
| 1070 | Zphys=0; |
---|
| 1071 | % end |
---|
| 1072 | end |
---|
| 1073 | if ~exist('X','var')||~exist('Y','var') |
---|
| 1074 | Xphys=[]; |
---|
| 1075 | Yphys=[];%default |
---|
| 1076 | return |
---|
| 1077 | end |
---|
| 1078 | Xphys=X;%default |
---|
| 1079 | Yphys=Y; |
---|
| 1080 | %image transform |
---|
| 1081 | if isfield(Calib,'R') |
---|
| 1082 | R=(Calib.R)'; |
---|
| 1083 | Dx=R(5)*R(7)-R(4)*R(8); |
---|
| 1084 | Dy=R(1)*R(8)-R(2)*R(7); |
---|
| 1085 | D0=Calib.f*(R(2)*R(4)-R(1)*R(5)); |
---|
| 1086 | Z11=R(6)*R(8)-R(5)*R(9); |
---|
| 1087 | Z12=R(2)*R(9)-R(3)*R(8); |
---|
| 1088 | Z21=R(4)*R(9)-R(6)*R(7); |
---|
| 1089 | Z22=R(3)*R(7)-R(1)*R(9); |
---|
| 1090 | Zx0=R(3)*R(5)-R(2)*R(6); |
---|
| 1091 | Zy0=R(1)*R(6)-R(3)*R(4); |
---|
| 1092 | A11=R(8)*Calib.Ty-R(5)*Calib.Tz+Z11*Zphys; |
---|
| 1093 | A12=R(2)*Calib.Tz-R(8)*Calib.Tx+Z12*Zphys; |
---|
| 1094 | A21=-R(7)*Calib.Ty+R(4)*Calib.Tz+Z21*Zphys; |
---|
| 1095 | A22=-R(1)*Calib.Tz+R(7)*Calib.Tx+Z11*Zphys; |
---|
| 1096 | X0=Calib.f*(R(5)*Calib.Tx-R(2)*Calib.Ty+Zx0*Zphys); |
---|
| 1097 | Y0=Calib.f*(-R(4)*Calib.Tx+R(1)*Calib.Ty+Zy0*Zphys); |
---|
| 1098 | %px to camera: |
---|
| 1099 | Xd=(Calib.dpx/Calib.sx)*(X-Calib.Cx); % sensor coordinates |
---|
| 1100 | Yd=Calib.dpy*(Y-Calib.Cy); |
---|
| 1101 | dist_fact=1+Calib.kappa1*(Xd.*Xd+Yd.*Yd); %distortion factor |
---|
| 1102 | Xu=dist_fact.*Xd;%undistorted sensor coordinates |
---|
| 1103 | Yu=dist_fact.*Yd; |
---|
| 1104 | denom=Dx*Xu+Dy*Yu+D0; |
---|
| 1105 | % denom2=denom.*denom; |
---|
| 1106 | Xphys=(A11.*Xu+A12.*Yu+X0)./denom;%world coordinates |
---|
| 1107 | Yphys=(A21.*Xu+A22.*Yu+Y0)./denom; |
---|
| 1108 | end |
---|