source: trunk/src/phys_ima.m @ 902

Last change on this file since 902 was 892, checked in by sommeria, 9 years ago

bug in refraction index fixed

File size: 3.8 KB
Line 
1
2% phys_ima: transform several images in phys coordinates on a common pixel grid
3%------------------------------------------------------------------------
4% OUTPUT:
5% A_out: cell array of oitput images corresponding to the transform of the input images
6% Rangx, Rangy; vectors with two elements defining the phys positions of first and last pixels in each direction
7%  (the same for all the ouput images)
8%
9% INPUT: 
10% A: cell array of input images
11% XmlData: cell array of structures defining the calibration parameters for each image
12% ZIndex: index of the reference plane used to define the phys position in 3D
13
14function [A_out,Rangx,Rangy]=phys_ima(A,XmlData,ZIndex)
15xcorner=[];
16ycorner=[];
17npx=[];
18npy=[];
19dx=ones(1,numel(A));
20dy=ones(1,numel(A));
21if isstruct(XmlData)
22    XmlData={XmlData};
23end
24for icell=1:numel(A)
25    siz=size(A{icell});
26    npx=[npx siz(2)];
27    npy=[npy siz(1)];
28    Calib=XmlData{icell}.GeometryCalib;
29    xima=[0.5 siz(2)-0.5 0.5 siz(2)-0.5];%image coordinates of corners
30    yima=[0.5 0.5 siz(1)-0.5 siz(1)-0.5];
31    [xcorner_new,ycorner_new]=phys_XYZ(Calib,xima,yima,ZIndex);%corresponding physical coordinates
32    dx(icell)=(max(xcorner_new)-min(xcorner_new))/(siz(2)-1);
33    dy(icell)=(max(ycorner_new)-min(ycorner_new))/(siz(1)-1);
34    xcorner=[xcorner xcorner_new];
35    ycorner=[ycorner ycorner_new];
36end
37Rangx(1)=min(xcorner);
38Rangx(2)=max(xcorner);
39Rangy(2)=min(ycorner);
40Rangy(1)=max(ycorner);
41test_multi=(max(npx)~=min(npx)) || (max(npy)~=min(npy)); %different image lengths
42npX=1+round((Rangx(2)-Rangx(1))/min(dx));% nbre of pixels in the new image (use the finest resolution min(dx) in the set of images)
43npY=1+round((Rangy(1)-Rangy(2))/min(dy));
44x=linspace(Rangx(1),Rangx(2),npX);
45y=linspace(Rangy(1),Rangy(2),npY);
46[X,Y]=meshgrid(x,y);%grid in physical coordiantes
47%vec_B=[];
48A_out=cell(1,numel(A));
49for icell=1:length(A)
50    Calib=XmlData{icell}.GeometryCalib;
51    % rescaling of the image coordinates without change of the image array
52    if strcmp(Calib.CalibrationType,'rescale') && isequal(Calib,XmlData{1}.GeometryCalib)
53        A_out{icell}=A{icell};%no transform
54        Rangx=[0.5 npx-0.5];%image coordiantes of corners
55        Rangy=[npy-0.5 0.5];
56        [Rangx]=phys_XYZ(Calib,Rangx,[0.5 0.5],ZIndex);%case of translations without rotation and quadratic deformation
57        [xx,Rangy]=phys_XYZ(Calib,[0.5 0.5],Rangy,ZIndex);
58    else         
59        % the image needs to be interpolated to the new coordinates
60        zphys=0; %default
61        if isfield(Calib,'SliceCoord') %.Z= index of plane
62           SliceCoord=Calib.SliceCoord(ZIndex,:);
63           zphys=SliceCoord(3); %to generalize for non-parallel planes
64%            if isfield(Calib,'InterfaceCoord') && isfield(Calib,'RefractionIndex')
65%                 H=Calib.InterfaceCoord(3);
66%                 if H>zphys
67%                     zphys=H-(H-zphys)/Calib.RefractionIndex; %corrected z (virtual object)
68%                 end
69%            end
70        end
71        xima=0.5:npx-0.5;%image coordinates of corners
72        yima=npy-0.5:-1:0.5;
73        [XIMA_init,YIMA_init]=meshgrid(xima,yima);%grid of initial image in px coordinates
74        [XIMA,YIMA]=px_XYZ(XmlData{icell}.GeometryCalib,X,Y,zphys);% image coordinates for each point in the real
75        testuint8=isa(A{icell},'uint8');
76        testuint16=isa(A{icell},'uint16');
77        if ndims(A{icell})==2 %(B/W images)
78        A_out{icell}=interp2(XIMA_init,YIMA_init,double(A{icell}),XIMA,YIMA);
79         elseif ndims(A{icell})==3     
80             for icolor=1:size(A{icell},3)
81                 A{icell}=double(A{icell});
82                 A_out{icell}(:,:,icolor)=interp2(XIMA_init,YIMA_init,A{icell}(:,:,icolor),XIMA,YIMA);
83             end
84         end
85        if testuint8
86            A_out{icell}=uint8(A_out{icell});
87        end
88        if testuint16
89            A_out{icell}=uint16(A_out{icell});
90        end     
91    end
92end
Note: See TracBrowser for help on using the repository browser.