source: trunk/src/px_XYZ.m @ 1201

Last change on this file since 1201 was 1201, checked in by sommeria, 7 hours ago

various improvements...

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