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