Changeset 60 for trunk/src/geometry_calib.m
- Timestamp:
- Mar 23, 2010, 2:52:49 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/geometry_calib.m
r54 r60 43 43 % Edit the above text to modify the response to help geometry_calib 44 44 45 % Last Modified by GUIDE v2.5 05-Jan-2010 23:22:0445 % Last Modified by GUIDE v2.5 23-Mar-2010 06:22:33 46 46 47 47 % Begin initialization code - DO NOT edit … … 70 70 % PlotHandles: set of handles of the elements contolling the plotting 71 71 % parameters on the uvmat interface (obtained by 'get_plot_handle.m') 72 %------------------------------------------------------------------------ 72 73 function geometry_calib_OpeningFcn(hObject, eventdata, handles, handles_uvmat,pos,inputfile) 73 74 %------------------------------------------------------------------------ 74 75 % Choose default command line output for geometry_calib 75 76 handles.output = hObject; … … 218 219 219 220 220 %---------------------------------------------------- 221 %------------------------------------------------------------------------ 221 222 % executed when closing: set the parent interface button to value 0 222 223 function closefcn(gcbo,eventdata,handles_uvmat) 224 %------------------------------------------------------------------------ 223 225 huvmat=findobj(allchild(0),'Name','uvmat'); 224 226 if exist('handles_uvmat','var') … … 229 231 end 230 232 231 232 % % --- Executes on button press in MenuCoord. 233 % function MenuCoord_Callback(hObject, eventdata, handles) 234 235 % 236 % % --- Executes on button press in delete. 237 % function delete_Callback(hObject, eventdata, handles) 238 % SetData=get(gcbf,'UserData');%get the interface data 239 % IndexObj=SetData.IndexObj; 240 % delete_object(IndexObj); 241 242 243 %------------------------------------------------------------------ 233 %------------------------------------------------------------------------ 244 234 % --- Executes on button press in calibrate_lin. 245 235 function APPLY_Callback(hObject, eventdata, handles) 246 %------------------------------------------------------------------ 236 %------------------------------------------------------------------------ 247 237 calib_cell=get(handles.calib_type,'String'); 248 238 val=get(handles.calib_type,'Value'); … … 280 270 end 281 271 update_imadoc(GeometryCalib,outputfile) 282 % testappend=0;283 % if exist(outputfile,'file');%=1 if the output file already exists, 0 else284 % t=xmltree(outputfile); %read the file285 % backupfile=outputfile;286 % testexist=2;287 % while testexist==2288 % backupfile=[backupfile '~'];% make a backup name by adding ~ to the xml file name289 % testexist=exist(backupfile,'file');290 % end291 % [success,message]=copyfile(outputfile,backupfile);%make backup292 % t=xmltree(outputfile); %read the file293 % uid=find(t,'ImaDoc');294 % if ~isequal(uid,1)%if the xml file is not ImaDoc, delete it (after backup)295 % if isequal(success,1)296 % delete(outputfile)297 % else298 % msgbox_uvmat('ERROR',['error in the backup of the existing xml file: ' message])299 % return300 % end301 % else302 % uid_calib=find(t,'ImaDoc/GeometryCalib');303 % testappend=1;304 % if isempty(uid_calib)305 % [t,uid_calib]=add(t,1,'element','GeometryCalib');306 % else %if GeometryCalib already exists, delete its content307 % uid_child=children(t,uid_calib);308 % t=delete(t,uid_child);309 % % testappend=1;310 % end311 % end312 % end313 % if ~testappend %create a new xml file for calibration data314 % t=xmltree;315 % t=set(t,1,'name','ImaDoc');316 % [t,uid_calib]=add(t,1,'element','GeometryCalib');317 % end318 % % hgrid=get(handles.REPLICATE,'parent');%read the calibration image source on the interface userdata319 % % imagename=get(hgrid,'UserData');320 % % if exist(imagename,'file')321 % % GeometryCalib.SourceCalib.ImageCalib=imagename;322 % % end323 % GeometryCalib.SourceCalib.PointCoord=Object.Coord;324 % t=struct2xml(GeometryCalib,t,uid_calib);325 % save(t,outputfile);326 272 msgbox_uvmat('CONFIRMATION',{[outputfile ' updated with calibration data'];... 327 273 ['Error rms (along x,y)=' num2str(GeometryCalib.ErrorRms) ' pixels'];... … … 341 287 uvmat('RootPath_Callback',hObject,eventdata,hhuvmat); %file input with xml reading in uvmat 342 288 343 %------------------------------------------------------------------ 289 %------------------------------------------------------------------------ 344 290 % --- Executes on button press in calibrate_lin. 345 291 function REPLICATE_Callback(hObject, eventdata, handles) 346 %------------------------------------------------------------------ 292 %------------------------------------------------------------------------ 347 293 calib_cell=get(handles.calib_type,'String'); 348 294 val=get(handles.calib_type,'Value'); … … 388 334 testinput=0; 389 335 if isfield(Heading,'SubCampaign') && isequal([filename ext],Heading.SubCampaign) 390 % set(hhdataview.RootDirectory,'String',XmlInput)391 % set(hhdataview.SubCampaignTest,'Value',1)392 336 SubCampaignTest='y'; 393 337 testinput=1; 394 338 elseif isfield(Heading,'Campaign') && isequal([filename ext],Heading.Campaign) 395 % set(hhdataview.RootDirectory,'String',XmlInput)396 % set(hhdataview.SubCampaignTest,'Value',0)397 339 testinput=1; 398 340 end … … 414 356 outcome=dataview(XmlInput,SubCampaignTest,GeometryCalib); 415 357 end 416 % %A COMPLETER 417 % dataview('RootDirectory_Callback',hObject,eventdata,hhdataview) 418 % ListDevices=get(hhdataview.ListDevices,'String'); 419 % for ilist=1:length(ListDevices) 420 % if isequal(ListDevices{ilist},Device) 421 % set(hhdataview.ListDevices,'Value',ilist) 422 % dataview('ListDevices_Callback',hObject,eventdata,hhdataview) 423 % break 424 % end 425 % end 426 427 % % hhdataview=guidata(h_dataview); 428 % CurrentPath=get(hhdataview.RootDirectory,'String'); 429 % ListExperiments=get(hhdataview.ListExperiments,'String'); 430 % Value=get(hhdataview.ListExperiments,'Value'); 431 % if ~isequal(Value,1) 432 % ListExperiments=ListExperiments(Value); 433 % end 434 % ListDevices=get(hhdataview.ListDevices,'String'); 435 % Value=get(hhdataview.ListDevices,'Value'); 436 % if isequal(Value,1) 437 % msgbox_uvmat('ERROR','manually select in the GUI dataview the device being calibrated') 438 % return 439 % else 440 % ListDevices=ListDevices(Value); 441 % end 442 % ListRecords=get(hhdataview.ListRecords,'String'); 443 % Value=get(hhdataview.ListRecords,'Value'); 444 % if ~isequal(Value,1) 445 % ListRecords=ListRecords(Value); 446 % end 447 % [ListDevices,ListRecords,ListXml,List]=ListDir(CurrentPath,ListExperiments,ListDevices,ListRecords); 448 % ListXml=get(hhdataview.ListXml,'String'); 449 % Value=get(hhdataview.ListXml,'Value'); 450 % if isequal(Value,1) 451 % msgbox_uvmat('ERROR','you need to select in the GUI dataview the xml files to edit') 452 % return 453 % else 454 % ListXml=ListXml(Value); 455 % end 456 % 457 % %update all the selected xml files 458 % answer=msgbox_uvmat('INPUT_Y-N',[num2str(length(Value)) ' xml files for device ' ListDevices{1} ' will be refreshed with ' calib_type ' calibration data']) 459 % if ~isequal(answer,'Yes') 460 % return 461 % end 462 % 'TESTcalib' 463 % List=DataFiles.List 464 % for iexp=1:length(List.Experiment) 465 % ExpName=List.Experiment{iexp}.name; 466 % if isfield(List.Experiment{iexp},'Device') 467 % for idevice=1:length(List.Experiment{iexp}.Device) 468 % DeviceName=List.Experiment{iexp}.Device{idevice}.name; 469 % if isfield(List.Experiment{iexp}.Device{idevice},'xmlfile') 470 % for ixml=1:length(List.Experiment{iexp}.Device{idevice}.xmlfile) 471 % FileName=List.Experiment{iexp}.Device{idevice}.xmlfile{ixml}; 472 % for ilistxml=1:length(ListXml) 473 % if isequal(FileName,ListXml{ilistxml}) 474 % set(hhdataview.ListXml,'Value',Value(ilistxml)) 475 % drawnow 476 % xmlfullname=fullfile(CurrentPath,ExpName,DeviceName,FileName); 477 % update_imadoc(GeometryCalib,xmlfullname) 478 % break 479 % end 480 % end 481 % end 482 % elseif isfield(List.Experiment{iexp}.Device{idevice},'Record') 483 % for irecord=1:length(List.Experiment{iexp}.Device{idevice}.Record) 484 % RecordName=List.Experiment{iexp}.Device{idevice}.Record{irecord}.name; 485 % if isfield(List.Experiment{iexp}.Device{idevice}.Record{irecord},'xmlfile') 486 % for ixml=1:length(List.Experiment{iexp}.Device{idevice}.Record{irecord}.xmlfile) 487 % FileName=List.Experiment{iexp}.Device{idevice}.Record{irecord}.xmlfile{ixml}; 488 % for ilistxml=1:length(ListXml) 489 % if isequal(FileName,ListXml{ilistxml}) 490 % set(hhdataview.ListXml,'Value',Value(ilistxml)) 491 % drawnow 492 % xmlfullname=fullfile(CurrentPath,ExpName,DeviceName,RecordName,FileName); 493 % update_imadoc(GeometryCalib,xmlfullname) 494 % break 495 % end 496 % end 497 % end 498 % end 499 % end 500 % end 501 % end 502 % end 503 % end 504 % set(hhdataview.ListXml,'Value',Value) 505 506 507 %----------------------------------------------------------------- 358 359 %------------------------------------------------------------------------ 508 360 % determine the parameters for a calibration by an affine function (rescaling and offset, no rotation) 509 361 function GeometryCalib=calib_rescale(Coord) 510 %------------------------------------------------------------------ 362 %------------------------------------------------------------------------ 511 363 512 364 X=Coord(:,1); … … 542 394 GeometryCalib.ErrorMax(2)=max(abs(Ypoints-y_ima)); 543 395 544 545 %------------------------------------------------------------------ 396 %------------------------------------------------------------------------ 546 397 % determine the parameters for a calibration by a linear transform matrix (rescale and rotation) 547 398 function GeometryCalib=calib_linear(Coord) 548 %------------------------------------------------------------------ 399 %------------------------------------------------------------------------ 549 400 X=Coord(:,1); 550 401 Y=Coord(:,2); … … 572 423 GeometryCalib.ErrorMax(2)=max(abs(y1-y_ima)); 573 424 574 575 576 577 %------------------------------------------------------------------ 425 %------------------------------------------------------------------------ 578 426 function GeometryCalib=calib_tsai(Coord) 579 %------------------------------------------------------------------ 427 %------------------------------------------------------------------------ 580 428 %TSAI 581 429 % 'calibration_lin' provides a linear transform on coordinates, … … 589 437 sparam=convert(t); 590 438 end 591 % else592 % %fid = fopen(fullfile(path_UVMAT,'PARAM_WIN.txt'),'r');%open the file with civ binary names593 % xmlfile=fullfile(path_UVMAT,'PARAM_WIN.xml');594 % if exist(xmlfile,'file')595 % t=xmltree(xmlfile);596 % sparam=convert(t);597 % end598 % end599 439 if ~isfield(sparam,'GeometryCalib_exe') 600 440 msgbox_uvmat('ERROR',['calibration program <GeometryCalib_exe> undefined in parameter file ' xmlfile]) … … 618 458 end 619 459 calibdat=dlmread('calib.dat'); 460 delete('calib.dat') 461 delete('t.txt') 620 462 GeometryCalib.CalibrationType='tsai'; 621 463 GeometryCalib.focal=calibdat(10); … … 690 532 691 533 692 534 %------------------------------------------------------------------------ 693 535 % --- Executes on button press in rotation. 694 536 function rotation_Callback(hObject, eventdata, handles) 537 %------------------------------------------------------------------------ 695 538 angle_rot=(pi/180)*str2num(get(handles.Phi,'String')); 696 539 Coord_cell=get(handles.ListCoord,'String'); … … 701 544 set(handles.YObject,'String',num2str(data.Coord(:,2),4)); 702 545 703 546 %------------------------------------------------------------------------ 704 547 function XImage_Callback(hObject, eventdata, handles) 548 %------------------------------------------------------------------------ 705 549 update_list(hObject, eventdata,handles) 706 550 551 %------------------------------------------------------------------------ 707 552 function YImage_Callback(hObject, eventdata, handles) 553 %------------------------------------------------------------------------ 708 554 update_list(hObject, eventdata,handles) 709 555 … … 717 563 update_list(hObject, eventdata,handles) 718 564 565 %------------------------------------------------------------------------ 719 566 function update_list(hObject, eventdata, handles) 567 %------------------------------------------------------------------------ 720 568 str4=get(handles.XImage,'String'); 721 569 str5=get(handles.YImage,'String'); … … 732 580 Coord{val}=strline; 733 581 set(handles.ListCoord,'String',Coord) 734 735 %-------------------------------------------------------------------- 582 %update the plot 583 ListCoord_Callback(hObject, eventdata, handles) 584 585 %------------------------------------------------------------------------ 736 586 % --- Executes on selection change in ListCoord. 737 %--------------------------------------------------------------------738 587 function ListCoord_Callback(hObject, eventdata, handles) 739 % hObject handle to ListCoord (see GCBO) 740 % eventdata reserved - to be defined in a future version of MATLAB 741 % handles structure with handles and user data (see GUIDATA) 742 743 % Hints: contents = get(hObject,'String') returns ListCoord contents as cell array 744 % contents{get(hObject,'Value')} returns selected item from ListCoord 745 %set(handles.edit_append,'Value',2); %set to edit mode 588 %------------------------------------------------------------------------ 746 589 Coord_cell=get(handles.ListCoord,'String'); 747 590 val=get(handles.ListCoord,'Value'); … … 788 631 end 789 632 790 791 %---------------------------------------------------- 792 % --- Executes on button press in rotation_plus. 793 function rotation_plus_Callback(hObject, eventdata, handles) 794 Phi=0; 795 Phi=get(handles.Phi,'String'); 796 if ~isempty(Phi) 797 Phi=str2num(Phi); 798 end 799 rotation(handles,Phi) 800 801 %------------------------------------------------- 802 % --- Executes on button press in rotation_minus. 803 function rotation_minus_Callback(hObject, eventdata, handles) 804 Phi=0; 805 Phi=get(handles.Phi,'String'); 806 if ~isempty(Phi) 807 Phi=-str2num(Phi); 808 end 809 rotation(handles,Phi) 810 811 812 813 function O_x_Callback(hObject, eventdata, handles) 814 815 816 function O_y_Callback(hObject, eventdata, handles) 817 818 819 function O_z_Callback(hObject, eventdata, handles) 820 821 633 %------------------------------------------------------------------------ 822 634 % --- Executes on selection change in edit_append. 823 635 function edit_append_Callback(hObject, eventdata, handles) 824 % val=get(handles.PLOT_append,'Value'); 825 % if isequal(val,2); %append mode 826 % %appeler mouse 827 % end 636 %------------------------------------------------------------------------ 828 637 choice=get(handles.edit_append,'Value'); 829 638 if choice==1 … … 839 648 840 649 841 %A REVOIR842 % if choice==2843 % %display image with px coordinates844 % hrootpath=findobj(huvmat,'Tag','RootPath');845 % hrootfile=findobj(huvmat,'Tag','RootFile');846 % RootPath='';847 % RootFile='';848 % % if ~isempty(hrootpath)& ~isempty(hrootfile)849 % testhandle=1;850 % RootPath=get(hrootpath,'String');851 % RootFile=get(hrootfile,'String');852 % % filebase=fullfile(RootPath,RootFile);853 % % outputfile=[filebase '.xml'];854 % Indices=get(findobj(huvmat,'Tag','FileIndex'),'String');855 % Ext=get(findobj(huvmat,'Tag','FileExt'),'String');856 % imagename=[fullfile(RootPath,RootFile) Indices Ext];857 % % input.menu_coord=1;858 % h_menu_coord=findobj(huvmat,'Tag','menu_coord');859 % set(h_menu_coord,'Value',3)860 % huvmat=uvmat(imagename);%open uvmat, set phys coord (Value 1)861 %862 % % end863 % end864 650 865 651 function NEW_Callback(hObject, eventdata, handles) … … 900 686 901 687 902 903 %'key_press_fcn:' function activated when a key is pressed on the keyboard 904 %----------------------------------- 688 %------------------------------------------------------------------------ 689 % --- 'key_press_fcn:' function activated when a key is pressed on the keyboard 905 690 function key_press_fcn(hObject,eventdata,handles) 691 %------------------------------------------------------------------------ 906 692 hh=get(hObject,'parent'); 907 693 xx=double(get(hh,'CurrentCharacter')); %get the keyboard character … … 930 716 end 931 717 932 718 %------------------------------------------------------------------------ 933 719 % --- Executes on button press in append_point. 934 720 function append_point_Callback(hObject, eventdata, handles) 935 721 %------------------------------------------------------------------------ 936 722 Coord=get(handles.ListCoord,'String'); 937 723 val=length(Coord); … … 943 729 set(handles.ListCoord,'Value',val+1) 944 730 945 946 % -------------------------------------------------------------------- 731 %------------------------------------------------------------------------ 947 732 function MenuOpen_Callback(hObject, eventdata, handles) 733 %------------------------------------------------------------------------ 948 734 %get the object file 949 735 huvmat=findobj(allchild(0),'Name','uvmat'); … … 972 758 973 759 974 % -------------------------------------------------------------------- 975 function Untitled_3_Callback(hObject, eventdata, handles) 976 % hObject handle to Untitled_3 (see GCBO) 977 % eventdata reserved - to be defined in a future version of MATLAB 978 % handles structure with handles and user data (see GUIDATA) 979 980 981 % -------------------------------------------------------------------- 760 %------------------------------------------------------------------------ 982 761 function MenuPlot_Callback(hObject, eventdata, handles) 983 762 %------------------------------------------------------------------------ 984 763 huvmat=findobj(allchild(0),'Name','uvmat');%find the current uvmat interface handle 985 764 UvData=get(huvmat,'UserData');%Data associated to the current uvmat interface 986 765 hhuvmat=guidata(huvmat); %handles of GUI elements in uvmat 987 766 hplot=findobj(huvmat,'Tag','axes3');%main plotting axis of uvmat 988 h_menu_coord=findobj(huvmat,'Tag',' menu_coord');767 h_menu_coord=findobj(huvmat,'Tag','transform_fct'); 989 768 menu=get(h_menu_coord,'String'); 990 769 choice=get(h_menu_coord,'Value'); … … 1033 812 Tinput=CalibData.grid; 1034 813 end 1035 T=create_grid(Tinput);%display the GUI create_grid 1036 CalibData.grid=T; 814 [T,CalibData.grid]=create_grid(grid_input);%display the GUI create_grid 1037 815 set(handles.figure1,'UserData',CalibData) 1038 816 … … 1134 912 1135 913 914 % -------------------------------------------------------------------- 915 function MenuDetectGrid_Callback(hObject, eventdata, handles) 916 917 CalibData=get(handles.figure1,'UserData'); 918 grid_input=[];%default 919 if isfield(CalibData,'grid') 920 grid_input=CalibData.grid;%retrieve the previously used grid 921 end 922 [T,CalibData.grid]=create_grid(grid_input);%display the GUI create_grid 923 set(handles.figure1,'UserData',CalibData)%store the phys grid for later use 924 925 %read the four last point coordiantes in pixels 926 Coord_cell=get(handles.ListCoord,'String');%read list of coordiantes on geometry_calib 927 data=read_geometry_calib(Coord_cell); 928 nbpoints=size(data.Coord,1); %nbre of calibration points 929 if nbpoints<4 930 msgbox_uvmat('ERROR','four points must be selected by the mouse to delimitate the detection area') 931 end 932 corners_X=(data.Coord(end-3:end,4)); %pixel absissa of the four corners 933 corners_Y=(data.Coord(end-3:end,5)); 934 935 %read the current image 936 huvmat=findobj(allchild(0),'Name','uvmat'); 937 UvData=get(huvmat,'UserData'); 938 A=UvData.Field.A; 939 npxy=size(A); 940 %linear transform on the current image 941 X=[CalibData.grid.x_0 CalibData.grid.x_1 CalibData.grid.x_0 CalibData.grid.x_1]';%corner absissa in the rectified image 942 Y=[CalibData.grid.y_0 CalibData.grid.y_0 CalibData.grid.y_1 CalibData.grid.y_1]';%corner absissa in the rectified image 943 XY_mat=[ones(size(X)) X Y]; 944 a_X1=XY_mat\corners_X; %transformation matrix for X 945 x1=XY_mat*a_X1;%reconstruction 946 err_X1=max(abs(x1-corners_X))%error 947 a_Y1=XY_mat\corners_Y;%transformation matrix for X 948 y1=XY_mat*a_Y1; 949 err_Y1=max(abs(y1-corners_Y))%error 950 GeometryCalib.CalibrationType='linear'; 951 GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function 952 GeometryCalib.f=1; 953 GeometryCalib.dpx=1; 954 GeometryCalib.dpy=1; 955 GeometryCalib.sx=1; 956 GeometryCalib.Cx=0; 957 GeometryCalib.Cy=0; 958 GeometryCalib.kappa1=0; 959 GeometryCalib.Tx=a_X1(1); 960 GeometryCalib.Ty=a_Y1(1); 961 GeometryCalib.Tz=1; 962 GeometryCalib.R=[a_X1(2),a_X1(3),0;a_Y1(2),a_Y1(3),0;0,0,1]; 963 [Amod,Rangx,Rangy]=phys_Ima(A-min(min(A)),GeometryCalib,0); 964 Amod=double(Amod); 965 %figure(12) 966 %Amax=max(max(Amod)) 967 %image(Rangx,Rangy,uint8(255*Amod/Amax)) 968 ind_range=10;% range of search of image ma around each point obtained by linear interpolation from the marked points 969 nbpoints=size(T,1); 970 for ipoint=1:nbpoints 971 Dx=(Rangx(2)-Rangx(1))/(npxy(2)-1); %x mesh in real space 972 Dy=(Rangy(2)-Rangy(1))/(npxy(1)-1); %y mesh in real space 973 i0=1+round((T(ipoint,1)-Rangx(1))/Dx);%round(Xpx(ipoint)); 974 j0=1+round((T(ipoint,2)-Rangy(1))/Dy);%round(Xpx(ipoint)); 975 Asub=Amod(j0-ind_range:j0+ind_range,i0-ind_range:i0+ind_range); 976 x_profile=sum(Asub,1); 977 y_profile=sum(Asub,2); 978 [Amax,ind_x_max]=max(x_profile); 979 [Amax,ind_y_max]=max(y_profile); 980 Delta(ipoint,1)=(ind_x_max-ind_range-1)*Dx;%shift from the initial guess 981 Delta(ipoint,2)=(ind_y_max-ind_range-1)*Dy; 982 end 983 Tmod=T(:,(1:2))+Delta; 984 [Xpx,Ypx]=px_XYZ(GeometryCalib,Tmod(:,1),Tmod(:,2)); 985 for i=1:nbpoints 986 Coord{i,1}=num2str(T(i,1),4);%display coordiantes with 4 digits 987 Coord{i,2}=num2str(T(i,2),4);%display coordiantes with 4 digits 988 Coord{i,3}='0'; 989 Coord{i,4}=num2str(Xpx(i),4);%display coordiantes with 4 digi 990 Coord{i,5}=num2str(Ypx(i),4);%display coordiantes with 4 digi 991 end 992 Tabchar=cell2tab(Coord,' | '); 993 set(handles.ListCoord,'Value',1) 994 set(handles.ListCoord,'String',Tabchar) 995 996 997 %%%%%%%%%%%%%%%%%%%% 998 function [A_out,Rangx,Rangy]=phys_Ima(A,Calib,ZIndex) 999 xcorner=[]; 1000 ycorner=[]; 1001 npx=[]; 1002 npy=[]; 1003 siz=size(A); 1004 npx=[npx siz(2)]; 1005 npy=[npy siz(1)]; 1006 xima=[0.5 siz(2)-0.5 0.5 siz(2)-0.5];%image coordiantes of corners 1007 yima=[0.5 0.5 siz(1)-0.5 siz(1)-0.5]; 1008 [xcorner,ycorner]=phys_XYZ(Calib,xima,yima,ZIndex);%corresponding physical coordinates 1009 Rangx(1)=min(xcorner); 1010 Rangx(2)=max(xcorner); 1011 Rangy(2)=min(ycorner); 1012 Rangy(1)=max(ycorner); 1013 test_multi=(max(npx)~=min(npx)) | (max(npy)~=min(npy)); 1014 npx=max(npx); 1015 npy=max(npy); 1016 x=linspace(Rangx(1),Rangx(2),npx); 1017 y=linspace(Rangy(1),Rangy(2),npy); 1018 [X,Y]=meshgrid(x,y);%grid in physical coordiantes 1019 vec_B=[]; 1020 1021 zphys=0; %default 1022 if isfield(Calib,'SliceCoord') %.Z= index of plane 1023 SliceCoord=Calib.SliceCoord(ZIndex,:); 1024 zphys=SliceCoord(3); %to generalize for non-parallel planes 1025 end 1026 [XIMA,YIMA]=px_XYZ(Calib,X,Y,zphys);%corresponding image indices for each point in the real space grid 1027 XIMA=reshape(round(XIMA),1,npx*npy);%indices reorganized in 'line' 1028 YIMA=reshape(round(YIMA),1,npx*npy); 1029 flagin=XIMA>=1 & XIMA<=npx & YIMA >=1 & YIMA<=npy;%flagin=1 inside the original image 1030 testuint8=isa(A,'uint8'); 1031 testuint16=isa(A,'uint16'); 1032 if numel(siz)==2 %(B/W images) 1033 vec_A=reshape(A,1,npx*npy);%put the original image in line 1034 ind_in=find(flagin); 1035 ind_out=find(~flagin); 1036 ICOMB=((XIMA-1)*npy+(npy+1-YIMA)); 1037 ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A 1038 vec_B(ind_in)=vec_A(ICOMB); 1039 vec_B(ind_out)=zeros(size(ind_out)); 1040 A_out=reshape(vec_B,npy,npx);%new image in real coordinates 1041 elseif numel(siz)==3 1042 for icolor=1:siz(3) 1043 vec_A=reshape(A{icell}(:,:,icolor),1,npx*npy);%put the original image in line 1044 ind_in=find(flagin); 1045 ind_out=find(~flagin); 1046 ICOMB=((XIMA-1)*npy+(npy+1-YIMA)); 1047 ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A 1048 vec_B(ind_in)=vec_A(ICOMB); 1049 vec_B(ind_out)=zeros(size(ind_out)); 1050 A_out(:,:,icolor)=reshape(vec_B,npy,npx);%new image in real coordinates 1051 end 1052 end 1053 if testuint8 1054 A_out=uint8(A_out); 1055 end 1056 if testuint16 1057 A_out=uint16(A_out); 1058 end 1059 1060 %INPUT: 1061 %Z: index of plane 1062 function [Xphys,Yphys,Zphys]=phys_XYZ(Calib,X,Y,Z) 1063 if exist('Z','var')& isequal(Z,round(Z))& Z>0 & isfield(Calib,'SliceCoord')&length(Calib.SliceCoord)>=Z 1064 Zindex=Z; 1065 Zphys=Calib.SliceCoord(Zindex,3);%GENERALISER AUX CAS AVEC ANGLE 1066 else 1067 % if exist('Z','var') 1068 % Zphys=Z; 1069 % else 1070 Zphys=0; 1071 % end 1072 end 1073 if ~exist('X','var')||~exist('Y','var') 1074 Xphys=[]; 1075 Yphys=[];%default 1076 return 1077 end 1078 Xphys=X;%default 1079 Yphys=Y; 1080 %image transform 1081 if isfield(Calib,'R') 1082 R=(Calib.R)'; 1083 Dx=R(5)*R(7)-R(4)*R(8); 1084 Dy=R(1)*R(8)-R(2)*R(7); 1085 D0=Calib.f*(R(2)*R(4)-R(1)*R(5)); 1086 Z11=R(6)*R(8)-R(5)*R(9); 1087 Z12=R(2)*R(9)-R(3)*R(8); 1088 Z21=R(4)*R(9)-R(6)*R(7); 1089 Z22=R(3)*R(7)-R(1)*R(9); 1090 Zx0=R(3)*R(5)-R(2)*R(6); 1091 Zy0=R(1)*R(6)-R(3)*R(4); 1092 A11=R(8)*Calib.Ty-R(5)*Calib.Tz+Z11*Zphys; 1093 A12=R(2)*Calib.Tz-R(8)*Calib.Tx+Z12*Zphys; 1094 A21=-R(7)*Calib.Ty+R(4)*Calib.Tz+Z21*Zphys; 1095 A22=-R(1)*Calib.Tz+R(7)*Calib.Tx+Z11*Zphys; 1096 X0=Calib.f*(R(5)*Calib.Tx-R(2)*Calib.Ty+Zx0*Zphys); 1097 Y0=Calib.f*(-R(4)*Calib.Tx+R(1)*Calib.Ty+Zy0*Zphys); 1098 %px to camera: 1099 Xd=(Calib.dpx/Calib.sx)*(X-Calib.Cx); % sensor coordinates 1100 Yd=Calib.dpy*(Y-Calib.Cy); 1101 dist_fact=1+Calib.kappa1*(Xd.*Xd+Yd.*Yd); %distortion factor 1102 Xu=dist_fact.*Xd;%undistorted sensor coordinates 1103 Yu=dist_fact.*Yd; 1104 denom=Dx*Xu+Dy*Yu+D0; 1105 % denom2=denom.*denom; 1106 Xphys=(A11.*Xu+A12.*Yu+X0)./denom;%world coordinates 1107 Yphys=(A21.*Xu+A22.*Yu+Y0)./denom; 1108 end
Note: See TracChangeset
for help on using the changeset viewer.