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