source: trunk/src/px_XYZ.m @ 1111

Last change on this file since 1111 was 1111, checked in by sommeria, 2 years ago

3D calibration corrected

File size: 2.7 KB
Line 
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-2022, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
14%   http://www.legi.grenoble-inp.fr
15%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.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
30function [X,Y]=px_XYZ(Calib,Xphys,Yphys,Zphys)
31if ~exist('Zphys','var')
32    Zphys=0;
33end
34if ~isfield(Calib,'fx_fy')
35     Calib.fx_fy=[1 1];
36end
37if ~isfield(Calib,'Tx_Ty_Tz')
38     Calib.Tx_Ty_Tz=[0 0 1];
39end
40
41%%%%%%%%%%%%%
42% general case
43if isfield(Calib,'R')
44    R=(Calib.R)';
45    %correct z for refraction if needed
46    if isfield(Calib,'InterfaceCoord') && isfield(Calib,'RefractionIndex')
47        H=Calib.InterfaceCoord(3);
48        if H>Zphys
49            Zphys=H-(H-Zphys)/Calib.RefractionIndex; %corrected z (virtual object)Calib
50           
51          %  test_refraction=1;
52        end
53    end
54   
55    %camera coordinates
56    Zphys=-Zphys;%flip z coordinates
57    xc=R(1)*Xphys+R(2)*Yphys+R(3)*Zphys+Calib.Tx_Ty_Tz(1);
58    yc=R(4)*Xphys+R(5)*Yphys+R(6)*Zphys+Calib.Tx_Ty_Tz(2);
59    zc=R(7)*Xphys+R(8)*Yphys+R(9)*Zphys+Calib.Tx_Ty_Tz(3);
60   
61    %undistorted image coordinates
62    Xu=xc./zc;
63    Yu=yc./zc;
64   
65    %radial quadratic correction factor
66    if ~isfield(Calib,'kc')
67        r2=1; %no quadratic distortion
68    else
69        r2=1+Calib.kc*(Xu.*Xu+Yu.*Yu);
70    end
71   
72    %pixel coordinates
73    if ~isfield(Calib,'Cx_Cy')
74        Calib.Cx_Cy=[0 0];%default value
75    end
76    X=Calib.fx_fy(1)*Xu.*r2+Calib.Cx_Cy(1);
77    Y=Calib.fx_fy(2)*Yu.*r2+Calib.Cx_Cy(2); 
78   
79%case 'rescale'   
80else
81    X=Calib.fx_fy(1)*(Xphys+Calib.Tx_Ty_Tz(1));
82    Y=Calib.fx_fy(2)*(Yphys+Calib.Tx_Ty_Tz(2)); 
83end
84
85
86
Note: See TracBrowser for help on using the repository browser.