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