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 | |
---|
14 | function [A_out,Rangx,Rangy]=phys_ima(A,XmlData,ZIndex) |
---|
15 | xcorner=[]; |
---|
16 | ycorner=[]; |
---|
17 | npx=[]; |
---|
18 | npy=[]; |
---|
19 | dx=ones(1,numel(A)); |
---|
20 | dy=ones(1,numel(A)); |
---|
21 | if isstruct(XmlData) |
---|
22 | XmlData={XmlData}; |
---|
23 | end |
---|
24 | |
---|
25 | for icell=1:numel(A) |
---|
26 | siz=size(A{icell}); |
---|
27 | npx=[npx siz(2)]; |
---|
28 | npy=[npy siz(1)]; |
---|
29 | Calib=XmlData{icell}.GeometryCalib; |
---|
30 | coord_x=[0.5 siz(2)-0.5]; |
---|
31 | coord_y=[0.5 siz(1)-0.5]; |
---|
32 | x_edge=[linspace(coord_x(1),coord_x(end),npx(icell)) coord_x(end)*ones(1,npy(icell))... |
---|
33 | linspace(coord_x(end),coord_x(1),npx(icell)) coord_x(1)*ones(1,npy(icell))];%x coordinates of the image edge(four sides) |
---|
34 | y_edge=[coord_y(1)*ones(1,npx(icell)) linspace(coord_y(1),coord_y(end),npy(icell))... |
---|
35 | coord_y(end)*ones(1,npx(icell)) linspace(coord_y(end),coord_y(1),npy(icell))];%y coordinates of the image edge(four sides) |
---|
36 | [xcorner_new,ycorner_new]=phys_XYZ(Calib,x_edge,y_edge,ZIndex);%corresponding physical coordinates |
---|
37 | dx(icell)=(max(xcorner_new)-min(xcorner_new))/(siz(2)-1); |
---|
38 | dy(icell)=(max(ycorner_new)-min(ycorner_new))/(siz(1)-1); |
---|
39 | xcorner=[xcorner xcorner_new]; |
---|
40 | ycorner=[ycorner ycorner_new]; |
---|
41 | end |
---|
42 | Rangx(1)=min(xcorner); |
---|
43 | Rangx(2)=max(xcorner); |
---|
44 | Rangy(2)=min(ycorner); |
---|
45 | Rangy(1)=max(ycorner); |
---|
46 | test_multi=(max(npx)~=min(npx)) || (max(npy)~=min(npy)); %different image lengths |
---|
47 | |
---|
48 | npX=1+round((Rangx(2)-Rangx(1))/max(dx));% nbre of pixels in the new image (use the largest resolution max(dx) in the set of images) |
---|
49 | npY=1+round((Rangy(1)-Rangy(2))/max(dy)); |
---|
50 | |
---|
51 | x=linspace(Rangx(1),Rangx(2),npX); |
---|
52 | y=linspace(Rangy(1),Rangy(2),npY); |
---|
53 | [X,Y]=meshgrid(x,y);%grid in physical coordinates |
---|
54 | A_out=cell(1,numel(A)); |
---|
55 | |
---|
56 | for icell=1:length(A) |
---|
57 | Calib=XmlData{icell}.GeometryCalib; |
---|
58 | % rescaling of the image coordinates without change of the image array |
---|
59 | if strcmp(Calib.CalibrationType,'rescale') && isequal(Calib,XmlData{1}.GeometryCalib) |
---|
60 | A_out{icell}=A{icell};%no transform |
---|
61 | Rangx=[0.5 npx-0.5];%image coordiantes of corners |
---|
62 | Rangy=[npy-0.5 0.5]; |
---|
63 | [Rangx]=phys_XYZ(Calib,Rangx,[0.5 0.5],ZIndex);%case of translations without rotation and quadratic deformation |
---|
64 | [~,Rangy]=phys_XYZ(Calib,[0.5 0.5],Rangy,ZIndex); |
---|
65 | else |
---|
66 | % the image needs to be interpolated to the new coordinates |
---|
67 | Z=0; %default |
---|
68 | if isfield(Calib,'SliceCoord')&& size(Calib.SliceCoord,1)>=ZIndex %.Z= index of plane |
---|
69 | SliceCoord=Calib.SliceCoord(ZIndex,:); |
---|
70 | Z=SliceCoord(3); |
---|
71 | if isfield(Calib, 'SliceAngle') && size(Calib.SliceAngle,1)>=ZIndex && ~isequal(Calib.SliceAngle(ZIndex,:),[0 0 0]) |
---|
72 | norm_plane=angle2normal(Calib.SliceAngle(ZIndex,:)); |
---|
73 | Z=Z-(norm_plane(1)*(X-SliceCoord(1))+norm_plane(2)*(Y-SliceCoord(2)))/norm_plane(3); |
---|
74 | end |
---|
75 | end |
---|
76 | xima=0.5:npx(icell)-0.5;%image coordinates of corners |
---|
77 | yima=npy(icell)-0.5:-1:0.5; |
---|
78 | [XIMA_init,YIMA_init]=meshgrid(xima,yima);%grid of initial image in px coordinates |
---|
79 | [XIMA,YIMA]=px_XYZ(XmlData{icell}.GeometryCalib,X,Y,Z);% image coordinates for each point in the real |
---|
80 | testuint8=isa(A{icell},'uint8'); |
---|
81 | testuint16=isa(A{icell},'uint16'); |
---|
82 | if ismatrix(A{icell}) %(B/W images) |
---|
83 | A_out{icell}=interp2(XIMA_init,YIMA_init,double(A{icell}),XIMA,YIMA); |
---|
84 | elseif ndims(A{icell})==3 |
---|
85 | for icolor=1:size(A{icell},3) |
---|
86 | A{icell}=double(A{icell}); |
---|
87 | A_out{icell}(:,:,icolor)=interp2(XIMA_init,YIMA_init,A{icell}(:,:,icolor),XIMA,YIMA); |
---|
88 | end |
---|
89 | end |
---|
90 | if testuint8 |
---|
91 | A_out{icell}=uint8(A_out{icell}); |
---|
92 | end |
---|
93 | if testuint16 |
---|
94 | A_out{icell}=uint16(A_out{icell}); |
---|
95 | end |
---|
96 | end |
---|
97 | end |
---|