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