Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleRegressionPlane.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 /** @example ExampleRegressionPlane.cpp
26  @relates RANSAC, SVD
27  @ingroup g_examples
28  @brief this is a class using RANSAC to compute a regression plane with SVD
29  @author MIP
30 */
31 
32 
33 
34 #include <iostream>
35 #include <iomanip>
36 
37 // use BIAS Base Math library and Algos:
38 #include <Base/Math/Math.hh>
39 
40 #include <MathAlgo/SVD.hh>
41 
42 
43 using namespace std;
44 using namespace BIAS;
45 
46 
47 
48 void set_example_designmatrix( BIAS::Matrix<double> & A) {
49  // fill the 3d points rowwise into A using the scheme
50  const int nr_of_points = 7;
51 
52  A.newsize( nr_of_points, 4 );
53 
54  // -1 X_i Y_i Z_i
55  A[0][0] = -1; A[0][1] = 0; A[0][2] = 0; A[0][3] = 1; // 1. example points
56  A[1][0] = -1; A[1][1] = 1; A[1][2] = 0; A[1][3] = 1; // 2.
57  A[2][0] = -1; A[2][1] = 0; A[2][2] = 1; A[2][3] = 1; // 3.
58  A[3][0] = -1; A[3][1] = 1; A[3][2] = 1; A[3][3] = 1; // 4.
59  A[4][0] = -1; A[4][1] = 0.5; A[4][2] = 0.5; A[4][3] = 1; // 5.
60 
61  A[5][0] = -1; A[5][1] = 0.51; A[5][2] = 0.59; A[5][3] = 0.9; // 6.
62  A[6][0] = -1; A[6][1] = 3.49; A[6][2] = 0.55; A[6][3] = 1.1; // 7.
63 }
64 
65 
66 void calc_Regressionsebene( const BIAS::Matrix<double> & A ) {
67  cout << "calc_Regressionsebene " << endl;
68  cout << "Designmatrix (Matrix<double>) A: " << A << endl;
69 
70  // get a solution (linear fit) of this equation system by its SVD
71  SVD svd( A );
72 
73  // cout<<"U = " << svd.GetU() <<endl;
74  // cout<<"S = " << svd.GetS() <<endl;
75  // cout<<"V = " << svd.GetV() <<endl;
76  // cout<<"Nullspace dim = " << svd.NullspaceDim() <<endl;;
77 
78  BIAS::Vector<double> solution(4);
79  solution = svd.GetNullvector(); // get the last Nullvector which is the solution
80  cout <<"solution: " << solution << endl;
81 
82  Vector3<double> n_vec;
83  double d;
84  // decompose the solution into HNF of a plane
85  d = solution[0];
86  n_vec[0] = solution[1];
87  n_vec[1] = solution[2];
88  n_vec[2] = solution[3];
89  // now (d, n_vec ) is a plane in normalform
90 
91  // convert to HNF representation:
92  d = d / n_vec.NormL2(); // divide by actual length for HNF
93  n_vec.Normalize();// normalize n_vec to length 1 (for HNF)
94 
95  cout << " plane in HNF: d = "<<d<<endl;
96  cout << " n_vce'0 : " << n_vec << endl;
97 } // ------------------------------------------------------------
98 
99 
100 
101 // ---------------------------------------------------------------------------
102 // ---------------------------------------------------------------------------
103 int main() {
105  set_example_designmatrix( M ); // set example points into this nPoints x 4 designmatrix
106  calc_Regressionsebene( M ); // calculate the best fit for the regression plane
107  return 0;
108 }
computes and holds the singular value decomposition of a rectangular (not necessarily quadratic) Matr...
Definition: SVD.hh:92
Matrix< T > & newsize(Subscript M, Subscript N)
Definition: cmat.h:269
Vector3< T > & Normalize()
normalize this vector to length 1
Definition: Vector3.hh:663
double NormL2() const
the L2 norm sqrt(a^2 + b^2 + c^2)
Definition: Vector3.hh:633