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