Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ContourBSplineShapeMatrix.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 #include <Base/Debug/Debug.hh>
26 #include <MathAlgo/SVD.hh>
27 
28 #include "ContourBSplineShapeMatrix.hh"
29 
30 using namespace BIAS;
31 
32 //public:
33 
35  const BIAS::Matrix<double>& matrix){
37  //copy matrix
38  mat->newsize(matrix.num_rows(),matrix.num_cols());
39  int i,j;
40  for(i=0;i<matrix.num_rows();i++){
41  for(j=0;j<matrix.num_cols();j++){
42  (*mat)[i][j] = matrix[i][j];
43  }
44  }
45  (mat->reference_)++;
46  pntr_.push_back(mat);
47  return mat;
48 }
49 
51  const unsigned int numBasePolynoms){
53  mat->newsize(2*numBasePolynoms,2*numBasePolynoms);
54  mat->SetIdentity();
55  (mat->reference_)++;
56  pntr_.push_back(mat);
57  return mat;
58 }
59 
61  const unsigned int numBasePolynoms,
62  const BIAS::Vector<double>& Q){
63  unsigned int i; //counter variable;
65  mat->newsize(2*numBasePolynoms,4);
66  mat->SetZero();
67 
68  for(i=0;i<numBasePolynoms;i++){
69  (*mat)[i][0]=1.;
70  (*mat)[i+numBasePolynoms][1]=1.;
71  (*mat)[i][2]=Q[i];
72  (*mat)[i+numBasePolynoms][2]=Q[i+numBasePolynoms];
73  (*mat)[i][3]=-Q[i+numBasePolynoms];
74  (*mat)[i+numBasePolynoms][3]=Q[i];
75  }
76  (mat->reference_)++;
77  pntr_.push_back(mat);
78  return mat;
79 }
80 
82  const unsigned int numBasePolynoms,
83  const BIAS::Vector<double>& Q){
84  unsigned int i; //counter variable
86  mat->newsize(2*numBasePolynoms,6);
87  mat->SetZero();
88 
89  for(i=0;i<numBasePolynoms;i++){
90  (*mat)[i][0]=1.;
91  (*mat)[i+numBasePolynoms][1]=1.;
92  (*mat)[i][2]=Q[i];
93  (*mat)[i+numBasePolynoms][3]=Q[i+numBasePolynoms];
94  (*mat)[i+numBasePolynoms][4]=Q[i];
95  (*mat)[i][5]=Q[i+numBasePolynoms];
96  }
97  (mat->reference_)++;
98  pntr_.push_back(mat);
99  return mat;
100 }
101 
103  ContourBSplineShapeMatrix* shapeSpace){
105  mat->newsize(shapeSpace->num_rows(),shapeSpace->num_cols());
106  mat->SetZero();
107  (mat->reference_)++;
108  pntr_.push_back(mat);
109  return mat;
110 }
111 
114  ContourBSplineShapeMatrix* shapeSpace,
115  ContourBSplineShapeMatrix* subShapeSpace){
116  //build Hscript and HscriptSub
117  BIAS::Matrix<double> Hscript(shapeSpace->num_cols(),shapeSpace->num_cols());
118  BIAS::Matrix<double> HscriptSub(subShapeSpace->num_cols(),
119  subShapeSpace->num_cols());
120 
122 
123  Hscript=*shapeSpace;
124  HscriptSub=*subShapeSpace;
125  Hscript.MultLeft(Uscript);
126  HscriptSub.MultLeft(Uscript);
127  Hscript.MultLeft(shapeSpace->Transpose());
128  HscriptSub.MultLeft(subShapeSpace->Transpose());
129 
130  //build (pseudo) inverses of Hscript and HscriptSub
131  BIAS::SVD HscriptSVD(Hscript);
132  BIAS::SVD HscriptSubSVD(HscriptSub);
133  BIAS::Matrix<double> HscriptInverse = HscriptSVD.Invert();
134  BIAS::Matrix<double> HscriptSubInverse = HscriptSubSVD.Invert();
135 
136  //build pseudo inverses for shapeSpaceMatrix and subShapeSpaceMatrix
137  BIAS::SVD shapeSpaceSVD(*shapeSpace);
138  BIAS::SVD subShapeSpaceSVD(*subShapeSpace);
139  BIAS::Matrix<double> shapeSpaceInverse = shapeSpaceSVD.Invert();
140  BIAS::Matrix<double> subShapeSpaceInverse = subShapeSpaceSVD.Invert();
141 
142 // below uncommented version of inverse computing after book "active contours"
143 // BIAS::Matrix<double> shapeSpaceMatrixPInverse(2*numBasePolynoms_,
144 // 2*numBasePolynoms_);
145 // BIAS::Matrix<double> subShapeSpaceMatrixPInverse(
146 // 2*numBasePolynoms_,
147 // 2*numBasePolynoms_);
148 //
149 // shapeSpaceMatrixPInverse.SetIdentity();
150 // shapeSpaceMatrixPInverse.MultLeft(Uscript);
151 // shapeSpaceMatrixPInverse.MultLeft(shapeSpaceMatrix_.Transpose());
152 // shapeSpaceMatrixPInverse.MultLeft(HscriptInverse);
153 //
154 // subShapeSpaceMatrixPInverse.SetIdentity();
155 // subShapeSpaceMatrixPInverse.MultLeft(Uscript);
156 // subShapeSpaceMatrixPInverse.MultLeft(subShapeSpaceMatrix_.Transpose());
157 // subShapeSpaceMatrixPInverse.MultLeft(HscriptSubInverse);
158 
159  BIAS::Matrix<double> Es(shapeSpace->num_cols(),
160  shapeSpace->num_cols());
161  Es=*shapeSpace;
162  Es.MultLeft(subShapeSpaceInverse);
163  Es.MultLeft(*subShapeSpace);
164  Es.MultLeft(shapeSpaceInverse);
165 
166  BIAS::Matrix<double> Ed(shapeSpace->num_cols(),
167  shapeSpace->num_cols());
168  Ed.SetIdentity();
169  Ed.SubIP(Es);
170 
171  //final regularisation matrix
173  mat->newsize(shapeSpace->num_cols(),shapeSpace->num_cols());
174  int i,j;//conter variables
175  for(i=0;i<mat->num_rows();i++){
176  for(j=0;j<mat->num_cols();j++){
177  (*mat)[i][j]=Ed[i][j];
178  }
179  }
180  mat->MultLeft(Hscript);
181  mat->MultLeft(Ed.Transpose());
182 
183  (mat->reference_)++;
184  pntr_.push_back(mat);
185  return mat;
186 }
187 
188 //protected:
189 
190 std::list<ContourBSplineShapeMatrix*> ContourBSplineShapeMatrix::pntr_;
computes and holds the singular value decomposition of a rectangular (not necessarily quadratic) Matr...
Definition: SVD.hh:92
Subscript num_cols() const
Definition: cmat.h:320
Matrix< T > Transpose() const
transpose function, storing data destination matrix
Definition: Matrix.hh:823
data object which holds a shape matrix or regularisation matrix for a b-spline curve; could be shared...
Matrix< T > & newsize(Subscript M, Subscript N)
Definition: cmat.h:269
unsigned int reference_
reference counter - for handling unregister
data object which holds all infomations of a B-Spline curve (ContourBSpline); its shared by B-Spline ...
static ContourBSplineShapeMatrix * ComputeRegularisationMatrix(ContourBSplineData *data, ContourBSplineShapeMatrix *shapeSpace, ContourBSplineShapeMatrix *subShapeSpace)
generates a new regularisation matrix as ContourBSplineShapeMatrix object and returns pointer to it ...
static ContourBSplineShapeMatrix * SetShapeSpacePlanarAffin(const unsigned int numBasePolynoms, const BIAS::Vector< double > &Q)
generates a new shape-matrix (invariant as well) as ContourBSplineShapeMatrix object and returns a po...
ContourBSplineShapeMatrix()
standard constructor
void SetZero()
Sets all values to zero.
Definition: Matrix.hh:856
static ContourBSplineShapeMatrix * SetShapeSpaceMatrix(const BIAS::Matrix< double > &matrix)
generates a new shape-matrix (invariant as well) as ContourBSplineShapeMatrix object and returns a po...
void SetIdentity()
Converts matrix to identity matrix.
Definition: Matrix.cpp:383
static ContourBSplineShapeMatrix * SetShapeSpaceIdentity(const unsigned int numBasePolynoms)
generates a new shape-matrix (invariant as well) as ContourBSplineShapeMatrix object and returns a po...
Matrix< double > Invert()
returns pseudoinverse of A = U * S * V^T A^+ = V * S^+ * U^T
Definition: SVD.cpp:214
static ContourBSplineShapeMatrix * SetSubShapeSpaceZero(ContourBSplineShapeMatrix *shapeSpace)
generates a new invariant shape-matrix as ContourBSplineShapeMatrix object and returns a pointer to i...
void MultLeft(const Matrix< T > &arg)
in Place matrix multiplication this is equal to M = arg*M, but faster
Definition: Matrix.hh:947
Subscript num_rows() const
Definition: cmat.h:319
static std::list< ContourBSplineShapeMatrix * > pntr_
list of all pointers to ContourBSplineShapeMatrix objects
BIAS::Matrix< double > splineMetricMatrixParametric_
metric matrix - confer to &quot;Active Contours&quot;, chapter 3, p.59
static ContourBSplineShapeMatrix * SetShapeSpaceEuclidian(const unsigned int numBasePolynoms, const BIAS::Vector< double > &Q)
generates a new shape-matrix (invariant as well) as ContourBSplineShapeMatrix object and returns a po...