Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TrackerBaseHomography.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 "TrackerBaseHomography.hh"
26 #include "Geometry/HMatrix.hh"
27 #include "MathAlgo/Lapack.hh"
28 #include "MathAlgo/SVD.hh"
29 
30 using namespace BIAS;
31 using namespace std;
32 
33 
34 template <class StorageType>
37  Vector2<KLT_TYPE>& result, KLT_TYPE& error,
38  int &iter,
39  const Matrix2x2<KLT_TYPE>& AffinePred,
40  Matrix2x2<KLT_TYPE>& AffineResult)
41 {
42  HMatrix H;
43  for (unsigned int i=0; i<2; i++) {
44  for (unsigned int j=0; j<2; j++) {
45  H[i][j] = AffinePred[i][j];
46  }
47  H[i][2] = p2[i]-p1[i];
48  H[2][i]=0;
49  }
50  H[2][2]=1;
51 
52  const int hh = _WinSize/2;
53 
54  HomgPoint2D x1;
55  x1[2] = 1;
56 
57  Matrix<double> Jh(2,3);
58  Jh[0][1] = Jh[1][0] = 0;
59 
60  Matrix<double> x1mat(1,3);
61  Matrix<double> I3(3,3);
62  I3.SetIdentity();
63 
64  Matrix<double> grad(1,2);
65 
66  BilinearRegion1_(p1[0], p1[1], hh);
67 
68  Matrix<double> N(8,8);
69  Vector<double> y(8);
70  Vector<double> a(8);
71 
72  for (iter=0; iter<_MaxIterations; iter++) {
73  N.SetZero();
74  y.SetZero();
75 
76  double Omega=0;
77  for (int i = -hh ; i <= hh ; i++) {
78  x1[0] = i;
79  for (int j = -hh ; j <= hh ; j++) {
80  x1[1] = j;
81 
82  x1mat[0][0] = x1[0];
83  x1mat[0][1] = x1[1];
84  x1mat[0][2] = x1[2];
85 
86  HomgPoint2D x2=H*x1;
87  Jh[0][0] = Jh[1][1] = 1/x2[2];
88  Jh[0][2] = -x2[0]/(x2[2]*x2[2]);
89  Jh[1][2] = -x2[1]/(x2[2]*x2[2]);
90 
91  double ex2_x = x2[0]/x2[2] + p1[0];
92  double ex2_y = x2[1]/x2[2] + p1[1];
93  grad[0][0] = _gradim2x->FastBilinearInterpolationGrey(ex2_x, ex2_y);
94  grad[0][1] = _gradim2y->FastBilinearInterpolationGrey(ex2_x, ex2_y);
95 
96  Matrix<double> x1I3;
97  x1mat.Kronecker(I3,x1I3);
98  Matrix<double> dh=grad*Jh*x1I3;
99 
100  for (unsigned int t=0; t<8; t++)
101  a[t] = dh[0][t];
102 
103  BilinearRegion2_(ex2_x, ex2_y, 0);
104 
105  double l = _bl1[j+hh][i+hh] - _bl2[0][0];
106  Omega += l*l;
107 
108  N += a.OuterProduct(a);
109  y += a*l;
110  }
111  }
112 
113  Vector<double> update = Lapack_LU_linear_solve(N, y);
114 
115  // line search
116  double alpha=1;
117 
118  double Omega_new;
119  do {
120  for (unsigned i=0,k=0; i<3; i++) {
121  for (unsigned j=0; (j<3) && (k<8); j++,k++) {
122  H[j][i] += alpha*update[k];
123  }
124  }
125 
126  Omega_new=0;
127  for (int i = -hh ; i <= hh ; i++) {
128  x1[0] = i;
129  for (int j = -hh ; j <= hh ; j++) {
130  x1[1] = j;
131  HomgPoint2D x2=H*x1;
132  double ex2_x = x2[0]/x2[2] + p1[0];
133  double ex2_y = x2[1]/x2[2] + p1[1];
134  BilinearRegion2_(ex2_x, ex2_y, 0);
135  double l = _bl1[j+hh][i+hh] - _bl2[0][0];
136  Omega_new += l*l;
137  }
138  }
139  if (Omega_new>Omega) {
140  for (unsigned i=0,k=0; i<3; i++) {
141  for (unsigned j=0; (j<3) && (k<8); j++,k++) {
142  H[j][i] -= alpha*update[k];
143  }
144  }
145  alpha /= 2;
146  }
147  } while ((alpha>1e-4) && (Omega_new>Omega));
148 
149  // cout << "alpha = " << alpha << endl;
150 
151  double relativeUpdate = alpha*alpha*update.ScalarProduct(N*update);
152 
153  // cout << "Relative update = " << relativeUpdate << endl;
154 
155  if (relativeUpdate<1e-4)
156  break;
157  }
158 
159 // SVD svdN(N);
160 // cout << "Singular values = " << svdN.GetS() << endl;
161 
162 
163  for (unsigned int i=0; i<2; i++) {
164  result[i] = p1[i] + H[i][2];
165  for (unsigned int j=0; j<2; j++) {
166  AffineResult[i][j] = H[i][j] - H[i][2]*H[2][j];
167  }
168  }
169 
170 
171  // cout << "H = " << H << endl;
172 
173  return 0;
174 }
175 
176 template <class StorageType>
178 EvaluateResult_(KLT_TYPE& mad, KLT_TYPE& msd, Matrix<KLT_TYPE>& CovMatrix) {
179 }
180 
181 
182 //////////////////////////////////////////////////////////////////////////
183 // instantiation
184 //////////////////////////////////////////////////////////////////////////
185 namespace BIAS{
186 template class BIASMatcher2D_EXPORT TrackerBaseHomography<unsigned char>;
187 template class BIASMatcher2D_EXPORT TrackerBaseHomography<float>;
188 
189 // fill in instances as required
190 #ifdef BUILD_IMAGE_INT
191 template class TrackerBaseHomography<int>;
192 #endif
193 #ifdef BUILD_IMAGE_CHAR
194 template class TrackerBaseHomography<char>;
195 #endif
196 #ifdef BUILD_IMAGE_SHORT
197 template class TrackerBaseHomography<short>;
198 #endif
199 #ifdef BUILD_IMAGE_USHORT
201 #endif
202 #ifdef BUILD_IMAGE_UINT
204 #endif
205 #ifdef BUILD_IMAGE_DOUBLE
206 #endif
207 }
T ScalarProduct(const Vector< T > &argvec) const
scalar product (inner product) of two vectors returning a scalr
Definition: Vector.hh:355
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
a 3x3 Matrix describing projective transformations between planes
Definition: HMatrix.hh:39
void Kronecker(Matrix< T > const B, Matrix< T > &dest) const
Kronecker-product with matrix, result in dest.
Definition: Matrix.cpp:702
void SetZero()
equivalent to matrix call
Definition: Vector.hh:156
BIAS::Vector< double > Lapack_LU_linear_solve(const BIAS::Matrix< double > &A, const BIAS::Vector< double > &b)
solve linear equations using LU factorization
Definition: Lapack.cpp:269
virtual int Track_(Vector2< KLT_TYPE > &p1, Vector2< KLT_TYPE > &p2, Vector2< KLT_TYPE > &result, KLT_TYPE &error, int &iter, const Matrix2x2< KLT_TYPE > &AffinePred, Matrix2x2< KLT_TYPE > &AffineResult)
Interface of all tracking algorithms implemented in derived classes.
Matrix< T > OuterProduct(const Vector< T > &v) const
outer product, constructs a matrix.
Definition: Vector.cpp:99
void SetZero()
Sets all values to zero.
Definition: Matrix.hh:856
void SetIdentity()
Converts matrix to identity matrix.
Definition: Matrix.cpp:383
virtual void EvaluateResult_(KLT_TYPE &mad, KLT_TYPE &msd, Matrix< KLT_TYPE > &cov)
fill in computed residui from Track_