Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Covariance3Dto2D.cpp
1 #include <Geometry/Covariance3Dto2D.hh>
2 
3 #include <Base/Geometry/PMatrixBase.hh>
4 #include <Base/Geometry/HomgPoint2D.hh>
5 #include <Base/Geometry/HomgPoint3D.hh>
6 #include <Base/Debug/Exception.hh>
7 
8 using namespace BIAS;
9 using namespace std;
10 
13  : UnscentedTransform(), K_(K), Parametrization_(par)
14 {
15  //AddDebugLevel(D_C3DT2D_COV);
16 }
17 
20 {}
21 
24  const BIAS::Matrix<double>& Cov_Pose,
25  const BIAS::Vector3<double>& X,
26  const BIAS::Matrix3x3<double>& Cov_X,
28  BIAS::Matrix2x2<double>& Cov_x) const
29 {
30  BIASCDOUT(D_C3DT2D_COV, "X: "<<X<<"\nCov_X: "<<Cov_X<<endl);
31  const int pose_dim = Pose.Size();
32  const int dim = pose_dim + 3;
33  Vector<double> src, dst;
34  Matrix<double> src_cov, dst_cov;
35 
36  src.newsize(dim);
37  src_cov.newsize(dim, dim);
38  src_cov.SetZero();
39  for (int c=0; c<3; c++){
40  src[c] = X[c];
41  for (int r=c; r<3; r++){
42  src_cov[c][r] = src_cov[r][c] = 0.5 * (Cov_X[r][c] + Cov_X[c][r]);
43  }
44  }
45 
46  for (int c=0; c<pose_dim; c++){
47  src[c+3] = Pose[c];
48  for (int r=c; r<pose_dim; r++){
49  src_cov[c+3][r+3] = src_cov[r+3][c+3] =
50  0.5 * (Cov_Pose[r][c] + Cov_Pose[c][r]);
51  }
52  }
53 
54  BIASCDOUT(D_C3DT2D_COV, "src: "<<src<<"\nsrc_cov: "<<src_cov<<endl);
55  if (Transform(src, src_cov, dst, dst_cov)!=0){
56  return -1;
57  }
58  BIASCDOUT(D_C3DT2D_COV, "dst: "<<dst<<"\ndst_cov: "<<dst_cov<<endl);
59  x = dst;
60  Cov_x = dst_cov;
61  return 0;
62 }
63 
65 Transform_(const Vector<double>& src, Vector<double>& dst) const
66 {
67  PMatrixBase P;
68  HomgPoint3D X;
69  HomgPoint2D x;
70 
71  const int pose_size = src.Size() - 3;
72  Vector<double> param(pose_size);
73  for (int i=0; i<pose_size; i++){
74  param[i] = src[i+3];
75  }
76  BIASCDOUT(D_C3DT2D_COV, "K: "<<K_<<"\tparam: "<<param<<endl);
77  P.Compose(K_, param, Parametrization_);
78 
79  X.Set(src[0], src[1], src[2], 1.0);
80  x = P * X;
81 
82  // dst is no 2-vector but a 3-vector in the camera coordinate system!
83  // To avoid problems with 180 degrees, return error on large angles:
84  const double len = x.NormL2();
85  if (len < 1e-9 || x[2]/len < 1e-9) return -1;
86 
87  x.Homogenize();
88  BIASCDOUT(D_C3DT2D_COV, "Pc32: "<<P<<"\tXc32: "<<X<<"\txx32: "<<x<<endl);
89 
90  dst.newsize(2);
91  dst[0] = x[0];
92  dst[1] = x[1];
93  BIASCDOUT(D_C3DT2D_COV, "dst_c32: "<<dst<<endl);
94  return 0;
95 }
96 
97 
int Project(const BIAS::Vector< double > &Pose, const BIAS::Matrix< double > &Cov_Pose, const BIAS::Vector3< double > &X, const BIAS::Matrix3x3< double > &Cov_X, BIAS::Vector2< double > &x, BIAS::Matrix2x2< double > &Cov_x) const
project covariance onto w=1 plane in camera coordinate system
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
void Set(const HOMGPOINT3D_TYPE &x, const HOMGPOINT3D_TYPE &y, const HOMGPOINT3D_TYPE &z)
set elementwise with given 3 euclidean scalar values.
Definition: HomgPoint3D.hh:321
HomgPoint2D & Homogenize()
homogenize class data member elements to W==1 by divison by W
Definition: HomgPoint2D.hh:215
Matrix< T > & newsize(Subscript M, Subscript N)
Definition: cmat.h:269
unsigned int Size() const
length of the vector
Definition: Vector.hh:143
E_ParametrizationType
description of supported parametrization types
Definition: PMatrixBase.hh:166
Covariance3Dto2D(BIAS::KMatrix &K, enum BIAS::PMatrixBase::E_ParametrizationType par)
virtual int Transform_(const BIAS::Vector< double > &src, BIAS::Vector< double > &dst) const
src is a vector consisting of [ 3d point, camera center, orientation ] The orientation must be repres...
Vector< T > & newsize(Subscript N)
Definition: vec.h:220
Represents 3d pose transformations, parametrized as Euclidean translation and unit quaternion orienta...
Definition: Pose.hh:73
void SetZero()
Sets all values to zero.
Definition: Matrix.hh:856
int Transform(const Vector< double > &src_mean, const Matrix< double > &src_cov, Vector< double > &dst_mean, Matrix< double > &dst_cov) const
computes the second order approximation of the transformations of the mean and the associated covaria...
class HomgPoint3D describes a point with 3 degrees of freedom in projective coordinates.
Definition: HomgPoint3D.hh:61
describes a projective 3D -&gt; 2D mapping in homogenous coordinates
Definition: PMatrixBase.hh:74
uses the unscented transformation to map a normal distribututed random variable using a nonlinear tra...
K describes the mapping from world coordinates (wcs) to pixel coordinates (pcs).
Definition: KMatrix.hh:48
void Compose(const Matrix3x3< PMATRIX_TYPE > &K, const Matrix3x3< PMATRIX_TYPE > &R, const Vector3< PMATRIX_TYPE > &C)
composes this from K, R and C using P = [ K R&#39; | -K R&#39; C ] with R&#39; = transpose(R) ...
Definition: PMatrixBase.cpp:34
enum BIAS::PMatrixBase::E_ParametrizationType Parametrization_
double NormL2() const
the L2 norm sqrt(a^2 + b^2 + c^2)
Definition: Vector3.hh:633