Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleSVD3x3.cpp
1 /* This file is part of the BIAS library (Basic ImageAlgorithmS).
2 
3 Copyright (C) 2003-2009 (see file CONTACT for details)
4  Multimediale Systeme der Informationsverarbeitung
5  Institut fuer Informatik
6  Christian-Albrechts-Universitaet Kiel
7 
8 
9 BIAS is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
13 
14 BIAS is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Lesser General Public License for more details.
18 
19 You should have received a copy of the GNU Lesser General Public License
20 along with BIAS; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23 
24 /** @example ExampleSVD3x3.cpp
25  @relates SVD3x3, SVD
26  @ingroup g_examples
27  @brief Example for singular value decomposition of a 3x3 matrix
28  @author woelk 07/2004
29 */
30 
31 #include <MathAlgo/SVD.hh>
32 #include <MathAlgo/SVD3x3.hh>
33 #include <Base/Math/Random.hh>
34 #include <Base/Math/Matrix3x3.hh>
35 #include <Base/Math/Matrix.hh>
36 
37 using namespace BIAS;
38 using namespace std;
39 
40 #define F_SVD_THRESH 0.1
41 
42 int main()
43 {
44  // Define 3x3 matrix (fundamental matrix)
45  Matrix<double> F(3, 3, MatrixZero);
46  F[0][0] =0.00075284066373225704;
47  F[0][1] =-0.099906426438442303;
48  F[0][2] =0.16267766990357244;
49  F[1][0] =0.10333163045064053;
50  F[1][1] =0.0014727467665194025;
51  F[1][2] =0.6803859609102173;
52  F[2][0] =-0.16234020366691171;
53  F[2][1] =-0.68087420277635458;
54  F[2][2] =0.0021896762792443826;
55  F.PrintPretty(cout, "F");
56 
57  // Compute with SVD3x3
58  SVD3x3 svdF3x3(F, F_SVD_THRESH);
59  Matrix<double> S(3, 3, MatrixZero);
60  for (int i = 0; i < 3; i++)
61  S[i][i] = svdF3x3.GetS()[i];
62 
63  cout << endl << "-- SVD3x3 --" << endl;
64  svdF3x3.GetU().PrintPretty(cout, "U");
65  S.PrintPretty(cout, "S");
66  svdF3x3.GetVT().PrintPretty(cout, "VT");
67 
68  Matrix<double> USVT = svdF3x3.GetU() * S * svdF3x3.GetVT();
69  USVT.PrintPretty(cout, "U*S*VT");
70 
71  cout << endl << "|F - U*S*VT| = " << Matrix<double>(F-USVT).NormL2() << endl
72  << endl << "NullspaceDim() = " << svdF3x3.NullspaceDim() << endl;
73 
74  // Compare results with SVD
75  SVD svdF(F, F_SVD_THRESH);
76  for (int i = 0; i < 3; i++)
77  S[i][i] = svdF.GetS()[i];
78 
79  cout << endl << "-- SVD --" << endl;
80  svdF.GetU().PrintPretty(cout, "U");
81  S.PrintPretty(cout, "S");
82  svdF.GetVT().PrintPretty(cout, "VT");
83 
84  USVT = svdF.GetU() * S * svdF.GetVT();
85  USVT.PrintPretty(cout, "U*S*VT");
86 
87  cout << endl << "|F - U*S*VT| = " << Matrix<double>(F-USVT).NormL2() << endl
88  << endl << "NullspaceDim() = " << svdF3x3.NullspaceDim() << endl
89  << endl;
90 
91  return 0;
92 }
93 
std::ostream & PrintPretty(std::ostream &s, const std::string &name="", const int width=8, const bool alignLeft=true) const
computes and holds the singular value decomposition of a rectangular (not necessarily quadratic) Matr...
Definition: SVD.hh:92
singular value decomposition for 3x3 matrices
Definition: SVD3x3.hh:74