Changeset 569 for trunk/src/geometry_calib.m
- Timestamp:
- Feb 19, 2013, 7:19:07 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/geometry_calib.m
r565 r569 150 150 151 151 %------------------------------------------------------------------------ 152 % --- Executes on button press in calibrate_lin.152 % --- Executes on button press APPLY (used to launch the calibration). 153 153 function APPLY_Callback(hObject, eventdata, handles) 154 154 %------------------------------------------------------------------------ … … 156 156 huvmat=findobj(allchild(0),'Name','uvmat'); 157 157 hhuvmat=guidata(huvmat);%handles of elements in the GUI uvmat 158 FileExt=get(hhuvmat.FileExt,'String'); 159 % check_input=0; 160 % if ~isempty(FileExt) 161 % if ~isempty(imformats(FileExt(2:end))) ||strcmpi(FileExt,'.avi') 162 % check_input=1; 163 % end 164 % end 165 % % if ~check_input 166 % msgbox_uvmat('ERROR','open an image with uvmat to perform calibration') 167 % return 168 % end 169 158 159 RootPath=''; 160 if ~isempty(hhuvmat.RootPath)&& ~isempty(hhuvmat.RootFile) 161 RootPath=get(hhuvmat.RootPath,'String'); 162 SubDirBase=regexprep(get(hhuvmat.SubDir,'String'),'\..+$',''); 163 outputfile=[fullfile(RootPath,SubDirBase) '.xml'];%xml file associated with the currently displayed image 164 else 165 question={'save the calibration data and point coordinates in'}; 166 def={fullfile(RootPath,'ObjectCalib.xml')}; 167 options.Resize='on'; 168 answer=inputdlg(question,'',1,def,options); 169 outputfile=answer{1}; 170 end 171 [GeometryCalib,index]=calibrate(handles,hhuvmat);% apply calibration 172 173 %% copy the xml file from the old location if appropriate, then update with the calibration parameters 174 if ~exist(outputfile,'file') && ~isempty(SubDirBase) 175 oldxml=[fullfile(RootPath,SubDirBase,get(hhuvmat.RootFile,'String')) '.xml']; 176 if exist(oldxml,'file') 177 [success,message]=copyfile(oldxml,outputfile);%copy the old xml file to a new one with the new convention 178 end 179 end 180 errormsg=update_imadoc(GeometryCalib,outputfile);% introduce the calibration data in the xml file 181 if ~strcmp(errormsg,'') 182 msgbox_uvmat('ERROR',errormsg); 183 end 184 185 %% display image with new calibration in the currently opened uvmat interface 186 hhh=findobj(hhuvmat.PlotAxes,'Tag','calib_marker');% delete calib points and markers 187 if ~isempty(hhh) 188 delete(hhh); 189 end 190 hhh=findobj(hhuvmat.PlotAxes,'Tag','calib_points'); 191 if ~isempty(hhh) 192 delete(hhh); 193 end 194 set(hhuvmat.CheckFixLimits,'Value',0)% put FixedLimits option to 'off' 195 set(hhuvmat.CheckFixLimits,'BackgroundColor',[0.7 0.7 0.7]) 196 UserData=get(handles.geometry_calib,'UserData'); 197 UserData.XmlInputFile=outputfile;%save the current xml file name 198 set(handles.geometry_calib,'UserData',UserData) 199 uvmat('RootPath_Callback',hObject,eventdata,hhuvmat); %file input with xml reading in uvmat, show the image in phys coordinates 200 MenuPlot_Callback(hObject, eventdata, handles) 201 set(handles.ListCoord,'Value',index)% indicate in the list the point with max deviation (possible mistake) 202 ListCoord_Callback(hObject, eventdata, handles) 203 figure(handles.geometry_calib) 204 205 %------------------------------------------------------------------------ 206 % --- Executes on button press in REPLICATE 207 function REPLICATE_Callback(hObject, eventdata, handles) 208 %------------------------------------------------------------------------ 209 210 %% look for the GUI uvmat and check for an image as input 211 huvmat=findobj(allchild(0),'Name','uvmat'); 212 hhuvmat=guidata(huvmat);%handles of elements in the GUI uvmat 213 GeometryCalib=calibrate(handles,hhuvmat);% apply calibration 214 215 %% open the GUI browse_data 216 CalibData=get(handles.geometry_calib,'UserData');%read the calibration image source on the interface userdata 217 if isfield(CalibData,'XmlInputFile') 218 InputDir=fileparts(fileparts(CalibData.XmlInputFile)); 219 end 220 answer=msgbox_uvmat('INPUT_TXT','Campaign to calibrate?',InputDir); 221 if strcmp(answer,'Cancel') 222 return 223 end 224 OutPut=browse_data(answer); 225 nbcalib=0; 226 for ilist=1:numel(OutPut.Experiment) 227 SubDirBase=regexprep(OutPut.Device{1},'\..+$',''); 228 XmlName=fullfile(OutPut.Campaign,OutPut.Experiment{ilist},[SubDirBase '.xml']); 229 % copy the xml file from the old location if appropriate, then update with the calibration parameters 230 if ~exist(XmlName,'file') && ~isempty(SubDirBase) 231 oldxml=fullfile(OutPut.Campaign,OutPut.Experiment{ilist},SubDirBase,[get(hhuvmat.RootFile,'String') '.xml']); 232 if exist(oldxml,'file') 233 [success,message]=copyfile(oldxml,XmlName);%copy the old xml file to a new one with the new convention 234 end 235 end 236 errormsg=update_imadoc(GeometryCalib,XmlName);% introduce the calibration data in the xml file 237 if ~strcmp(errormsg,'') 238 msgbox_uvmat('ERROR',errormsg); 239 else 240 display([XmlName ' updated with calibration parameters']) 241 nbcalib=nbcalib+1; 242 end 243 end 244 msgbox_uvmat('CONFIMATION',[SubDirBase ' calibrated for ' num2str(nbcalib) ' experiments']); 245 246 %------------------------------------------------------------------------ 247 % --- activate calibration and store parameters in ouputfile . 248 function [GeometryCalib,index]=calibrate(handles,hhuvmat) 249 %------------------------------------------------------------------------ 170 250 %% read the current calibration points 171 251 Coord_cell=get(handles.ListCoord,'String'); … … 220 300 221 301 %% store the calibration data, by default in the xml file of the currently displayed image 222 UvData=get(h uvmat,'UserData');302 UvData=get(hhuvmat.uvmat,'UserData'); 223 303 NbSlice_j=1;%default 224 304 ZStart=Z_plane; … … 233 313 end 234 314 end 235 RootPath=''; 236 % RootFile=''; 237 if ~isempty(hhuvmat.RootPath)&& ~isempty(hhuvmat.RootFile) 238 RootPath=get(hhuvmat.RootPath,'String'); 239 SubDirBase=regexprep(get(hhuvmat.SubDir,'String'),'\..+$',''); 240 outputfile=[fullfile(RootPath,SubDirBase) '.xml'];%xml file associated with the currently displayed image 241 else 242 SubDirBase=''; 243 question={'save the calibration data and point coordinates in'}; 244 def={fullfile(RootPath,'ObjectCalib.xml')}; 245 options.Resize='on'; 246 answer=inputdlg(question,'save average in a new file',1,def,options); 247 outputfile=answer{1}; 248 end 249 answer=msgbox_uvmat('INPUT_Y-N',{[outputfile ' updated with calibration data'];... 315 316 answer=msgbox_uvmat('INPUT_Y-N',{'store calibration data';... 250 317 ['Error rms (along x,y)=' num2str(GeometryCalib.ErrorRms) ' pixels'];... 251 318 ['Error max (along x,y)=' num2str(GeometryCalib.ErrorMax) ' pixels']}); 252 319 253 %% record the calibration parameters and display the current image of uvmat in the new phys coordinates 254 if strcmp(answer,'Yes') 255 if strcmp(calib_cell{val}(1:2),'3D')%set the plane position for 3D (projection) calibration 256 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'}; 257 input_val=[{num2str(ZEnd)} {num2str(ZStart)} {num2str(ZStart)} {'1.333'} num2str(NbSlice_j) {volume_scan} {'0'} {'0'}]; 258 answer=inputdlg(input_key,'slice position(s)',ones(1,8), input_val,'on'); 259 %answer_1=msgbox_uvmat('INPUT_TXT',' Z= ',num2str(Z_plane)); 260 GeometryCalib.NbSlice=str2double(answer{5}); 261 GeometryCalib.VolumeScan=answer{6}; 262 if isempty(answer) 263 Z_plane=0; %default 264 else 265 Z_plane=linspace(str2double(answer{1}),str2double(answer{2}),GeometryCalib.NbSlice); 266 end 267 GeometryCalib.SliceCoord=Z_plane'*[0 0 1]; 268 GeometryCalib.SliceAngle(:,3)=0; 269 GeometryCalib.SliceAngle(:,2)=str2double(answer{7})*ones(GeometryCalib.NbSlice,1);%rotation around y axis (to generalise) 270 GeometryCalib.SliceAngle(:,1)=str2double(answer{8})*ones(GeometryCalib.NbSlice,1);%rotation around x axis (to generalise) 271 GeometryCalib.InterfaceCoord=[0 0 str2double(answer{3})]; 272 GeometryCalib.RefractionIndex=str2double(answer{4}); 273 end 274 UserData=get(handles.geometry_calib,'UserData'); 275 276 % get the timing from the xml file using the old convention if appropriate 277 if ~exist(outputfile,'file') && ~isempty(SubDirBase) 278 oldxml=[fullfile(RootPath,SubDirBase,get(hhuvmat.RootFile,'String')) '.xml']; 279 if exist(oldxml,'file') 280 [success,message]=copyfile(oldxml,outputfile);%copy the old xml file to a new one with the new convention 281 end 282 end 283 errormsg=update_imadoc(GeometryCalib,outputfile);% introduce the calibration data in the xml file 284 if ~strcmp(errormsg,'') 285 msgbox_uvmat('ERROR',errormsg); 286 end 287 288 %display image with new calibration in the currently opened uvmat interface 289 hhh=findobj(hhuvmat.PlotAxes,'Tag','calib_marker');% delete calib points and markers 290 if ~isempty(hhh) 291 delete(hhh); 292 end 293 hhh=findobj(hhuvmat.PlotAxes,'Tag','calib_points'); 294 if ~isempty(hhh) 295 delete(hhh); 296 end 297 set(hhuvmat.CheckFixLimits,'Value',0)% put FixedLimits option to 'off' 298 set(hhuvmat.CheckFixLimits,'BackgroundColor',[0.7 0.7 0.7]) 299 300 UserData.XmlInputFile=outputfile;%save the current xml file name 301 set(handles.geometry_calib,'UserData',UserData) 302 uvmat('RootPath_Callback',hObject,eventdata,hhuvmat); %file input with xml reading in uvmat, show the image in phys coordinates 303 MenuPlot_Callback(hObject, eventdata, handles) 304 set(handles.ListCoord,'Value',index)% indicate in the list the point with max deviation (possible mistake) 305 ListCoord_Callback(hObject, eventdata, handles) 306 figure(handles.geometry_calib) 307 end 308 309 %------------------------------------------------------------------ 310 % --- Executes on button press in calibrate_lin. 311 312 function REPLICATE_Callback(hObject, eventdata, handles) 313 %------------------------------------------------------------------------ 314 315 %% Apply calibration 316 calib_cell=get(handles.calib_type,'String'); %#ok<NASGU> 317 val=get(handles.calib_type,'Value'); %#ok<NASGU> 318 319 %read the current calibration points 320 Coord_cell=get(handles.ListCoord,'String'); 321 Object=read_geometry_calib(Coord_cell); 322 Coord=Object.Coord; 323 324 % apply the calibration, whose type is selected in handles.calib_type 325 if ~isempty(Coord) 326 calib_cell=get(handles.calib_type,'String'); 327 val=get(handles.calib_type,'Value'); 328 GeometryCalib=feval(['calib_' calib_cell{val}],Coord,handles); 329 else 330 msgbox_uvmat('ERROR','No calibration points, abort') 320 %% get plane position(s) 321 if ~strcmp(answer,'Yes') 331 322 return 332 end 333 334 if ~isempty(Coord) 335 %check error 336 X=Coord(:,1); 337 Y=Coord(:,2); 338 Z=Coord(:,3); 339 x_ima=Coord(:,4); 340 y_ima=Coord(:,5); 341 [Xpoints,Ypoints]=px_XYZ(GeometryCalib,X,Y,Z); 342 GeometryCalib.ErrorRms(1)=sqrt(mean((Xpoints-x_ima).*(Xpoints-x_ima))); 343 [GeometryCalib.ErrorMax(1)]=max(abs(Xpoints-x_ima)); 344 GeometryCalib.ErrorRms(2)=sqrt(mean((Ypoints-y_ima).*(Ypoints-y_ima))); 345 [GeometryCalib.ErrorMax(2)]=max(abs(Ypoints-y_ima)); 346 % [EM,ind_dim]=max(GeometryCalib.ErrorMax); 347 %set the Z position of the reference plane used for calibration 348 Z_plane=[]; 349 if isequal(max(Z),min(Z)) 350 Z_plane=Z(1); 351 end 352 answer_1=msgbox_uvmat('INPUT_TXT',' Z= ',num2str(Z_plane)); 353 Z_plane=str2double(answer_1); 354 GeometryCalib.NbSlice=1; 355 GeometryCalib.SliceCoord=[0 0 Z_plane]; 356 %set the coordinate unit 357 unitlist=get(handles.CoordUnit,'String'); 358 unit=unitlist{get(handles.CoordUnit,'value')}; 359 GeometryCalib.CoordUnit=unit; 360 %record the points 361 GeometryCalib.SourceCalib.PointCoord=Coord; 362 end 363 364 %% display calibration paprameters 365 display_intrinsic(GeometryCalib,handles)%display calibration intrinsic parameters 366 367 % Display extrinsinc parameters (rotation and translation of camera with respect to the phys coordiantes) 368 set(handles.Tx,'String',num2str(GeometryCalib.Tx_Ty_Tz(1),4)) 369 set(handles.Ty,'String',num2str(GeometryCalib.Tx_Ty_Tz(2),4)) 370 set(handles.Tz,'String',num2str(GeometryCalib.Tx_Ty_Tz(3),4)) 371 set(handles.Phi,'String',num2str(GeometryCalib.omc(1),4)) 372 set(handles.Theta,'String',num2str(GeometryCalib.omc(2),4)) 373 set(handles.Psi,'String',num2str(GeometryCalib.omc(3),4)) 374 375 %% open the GUI dataview 376 h_dataview=findobj(allchild(0),'name','dataview'); 377 if ~isempty(h_dataview) 378 delete(h_dataview) 379 end 380 CalibData=get(handles.geometry_calib,'UserData');%read the calibration image source on the interface userdata 381 % InputFile=''; 382 if isfield(CalibData,'XmlInputFile') 383 InputDir=fileparts(CalibData.XmlInputFile); 384 [InputDir,DirName]=fileparts(InputDir); 385 end 386 SubCampaignTest='n'; %default 387 testup=0; 388 if isfield(CalibData,'SubCampaign') 389 SubCampaignTest='y'; 390 dir_ref=CalibData.SubCampaign; 391 testup=1; 392 elseif isfield(CalibData,'Campaign') 393 dir_ref=CalibData.Campaign; 394 testup=1; 395 end 396 while testup 397 [InputDir,DirName]=fileparts(InputDir); 398 if strcmp(DirName,dir_ref) 399 break 400 end 401 end 402 InputDir=fullfile(InputDir,DirName); 403 answer=msgbox_uvmat('INPUT_TXT','Campaign ?',InputDir); 404 if strcmp(answer,'Cancel') 405 return 406 end 407 408 dataview(answer,SubCampaignTest,GeometryCalib); 323 end 324 if strcmp(calib_cell{val}(1:2),'3D')%set the plane position for 3D (projection) calibration 325 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'}; 326 input_val=[{num2str(ZEnd)} {num2str(ZStart)} {num2str(ZStart)} {'1.333'} num2str(NbSlice_j) {volume_scan} {'0'} {'0'}]; 327 answer=inputdlg(input_key,'slice position(s)',ones(1,8), input_val,'on'); 328 GeometryCalib.NbSlice=str2double(answer{5}); 329 GeometryCalib.VolumeScan=answer{6}; 330 if isempty(answer) 331 Z_plane=0; %default 332 else 333 Z_plane=linspace(str2double(answer{1}),str2double(answer{2}),GeometryCalib.NbSlice); 334 end 335 GeometryCalib.SliceCoord=Z_plane'*[0 0 1]; 336 GeometryCalib.SliceAngle(:,3)=0; 337 GeometryCalib.SliceAngle(:,2)=str2double(answer{7})*ones(GeometryCalib.NbSlice,1);%rotation around y axis (to generalise) 338 GeometryCalib.SliceAngle(:,1)=str2double(answer{8})*ones(GeometryCalib.NbSlice,1);%rotation around x axis (to generalise) 339 GeometryCalib.InterfaceCoord=[0 0 str2double(answer{3})]; 340 GeometryCalib.RefractionIndex=str2double(answer{4}); 341 end 342 343 409 344 410 345 %------------------------------------------------------------------------ … … 456 391 GeometryCalib.R=[R [0;0;-epsilon]]; 457 392 GeometryCalib.omc=(180/pi)*[acos(GeometryCalib.R(1,1)) 0 0]; 393 458 394 %------------------------------------------------------------------------ 459 395 % determine the tsai parameters for a view normal to the grid plane
Note: See TracChangeset
for help on using the changeset viewer.