1 | %'px_XYZ': transform physical to image coordinates. |
---|
2 | %------------------------------------------------------------------------ |
---|
3 | %[X,Y]=px_XYZ(Calib,Xphys,Yphys,Zphys) |
---|
4 | %------------------------------------------------------------------------ |
---|
5 | % OUTPUT: |
---|
6 | % [X,Y]: image coordinates(in pixels) |
---|
7 | %------------------------------------------------------------------------ |
---|
8 | % INPUT: |
---|
9 | % Calib: structure containing calibration parameters |
---|
10 | % Xphys,Yphys,Zphys; vectors of physical coordinates for a set of points |
---|
11 | |
---|
12 | %======================================================================= |
---|
13 | % Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France |
---|
14 | % http://www.legi.grenoble-inp.fr |
---|
15 | % Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.fr |
---|
16 | % |
---|
17 | % This file is part of the toolbox UVMAT. |
---|
18 | % |
---|
19 | % UVMAT is free software; you can redistribute it and/or modify |
---|
20 | % it under the terms of the GNU General Public License as published |
---|
21 | % by the Free Software Foundation; either version 2 of the license, |
---|
22 | % or (at your option) any later version. |
---|
23 | % |
---|
24 | % UVMAT is distributed in the hope that it will be useful, |
---|
25 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
26 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
27 | % GNU General Public License (see LICENSE.txt) for more details. |
---|
28 | %======================================================================= |
---|
29 | |
---|
30 | function [X,Y]=px_XYZ(Calib,Slice,Xphys,Yphys,Zphys) |
---|
31 | if ~exist('Zphys','var') |
---|
32 | Zphys=0; |
---|
33 | end |
---|
34 | if ~isfield(Calib,'fx_fy') |
---|
35 | Calib.fx_fy=[1 1]; |
---|
36 | end |
---|
37 | if ~isfield(Calib,'Tx_Ty_Tz') |
---|
38 | Calib.Tx_Ty_Tz=[0 0 1]; |
---|
39 | end |
---|
40 | |
---|
41 | %%%%%%%%%%%%% |
---|
42 | if isempty(Slice) |
---|
43 | Slice=Calib; |
---|
44 | end |
---|
45 | % general case |
---|
46 | if isfield(Calib,'R') |
---|
47 | R=(Calib.R)'; |
---|
48 | %correct z for refraction if needed |
---|
49 | if isfield(Slice,'InterfaceCoord') && isfield(Slice,'RefractionIndex') |
---|
50 | H=Slice.InterfaceCoord(3); |
---|
51 | if H>Zphys |
---|
52 | Zphys=H-(H-Zphys)/Slice.RefractionIndex; %corrected z (virtual object)Calib |
---|
53 | |
---|
54 | % test_refraction=1; |
---|
55 | end |
---|
56 | end |
---|
57 | |
---|
58 | %camera coordinates |
---|
59 | Zphys=Zphys;%flip z coordinates |
---|
60 | xc=R(1)*Xphys+R(2)*Yphys+R(3)*Zphys+Calib.Tx_Ty_Tz(1); |
---|
61 | yc=R(4)*Xphys+R(5)*Yphys+R(6)*Zphys+Calib.Tx_Ty_Tz(2); |
---|
62 | zc=R(7)*Xphys+R(8)*Yphys+R(9)*Zphys+Calib.Tx_Ty_Tz(3); |
---|
63 | |
---|
64 | %undistorted image coordinates |
---|
65 | Xu=xc./zc; |
---|
66 | Yu=yc./zc; |
---|
67 | |
---|
68 | %radial quadratic correction factor |
---|
69 | if ~isfield(Calib,'kc') |
---|
70 | r2=1; %no quadratic distortion |
---|
71 | elseif numel(Calib.kc)==1 |
---|
72 | r2=1+Calib.kc*(Xu.*Xu+Yu.*Yu); |
---|
73 | else |
---|
74 | R2=Xu.*Xu+Yu.*Yu; |
---|
75 | r2=1+Calib.kc(1)*R2+Calib.kc(2)*R2.*R2; |
---|
76 | end |
---|
77 | |
---|
78 | %pixel coordinates |
---|
79 | if ~isfield(Calib,'Cx_Cy') |
---|
80 | Calib.Cx_Cy=[0 0];%default value |
---|
81 | end |
---|
82 | X=Calib.fx_fy(1)*Xu.*r2+Calib.Cx_Cy(1); |
---|
83 | Y=Calib.fx_fy(2)*Yu.*r2+Calib.Cx_Cy(2); |
---|
84 | |
---|
85 | %case 'rescale' |
---|
86 | else |
---|
87 | X=Calib.fx_fy(1)*(Xphys+Calib.Tx_Ty_Tz(1)); |
---|
88 | Y=Calib.fx_fy(2)*(Yphys+Calib.Tx_Ty_Tz(2)); |
---|
89 | end |
---|
90 | |
---|
91 | |
---|
92 | |
---|