source: trunk/src/phys_ima.m @ 1119

Last change on this file since 1119 was 1112, checked in by sommeria, 3 years ago

set_slice separated from geometrey_calib

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