Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ImageAlignment.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003, 2004 (see file CONTACTS 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 #ifndef __ImageAlignment__hh__
26 #define __ImageAlignment__hh__
27 
28 #include <Base/Common/BIASpragmaStart.hh>
29 
30 #include <Base/Debug/Debug.hh>
31 #include <Base/Geometry/HomgPoint2D.hh>
32 #include <Base/Math/Matrix2x2.hh>
33 #include <Image/PyramidImage.hh>
34 #include <Image/TextureTransform.hh>
35 #include <vector>
36 #include <Geometry/LocalAffineFrame.hh>
37 
38 
39 #define D_IMAGEALIGNMENT_PROGRESS 1
40 #define D_IMAGEALIGNMENT_PARAMETER 2
41 #define D_IMAGEALIGNMENT_PERPIXEL 4
42 #define D_IMAGEALIGNMENT_IMAGES 8
43 
44 namespace BIAS {
45 
46 
47  /** @class ImageAlignment
48  * @ingroup g_tracker
49  * @brief Inverse Compositional Image Alignment ("Registration")
50  *
51  * This class implements generic inverse compositional image alignment as
52  * proposed by Baker and Matthews in "Lucas-Kanade 20 years on". The model
53  * for which you estimate the parameters must be of type TextureTransform
54  * (e.g. affine, homography, displacement, camera pose, calibration, ...).
55  *
56  * The key idea is that the required inversion of the hessian and the
57  * incorporation of the gradients are not recomputed per iteration in this
58  * formulation as opposed to the standard KLT.
59  *
60  * @author koeser 01/2008 */
61  class BIASMatcher2D_EXPORT ImageAlignment : public Debug
62  {
63  public :
65  ~ImageAlignment() { if (T_!=NULL) delete T_;};
66 
67  /** @brief aligns image1 with image2 with respect to parameters params,
68  * scale space tracking is used recursively until maximum
69  * resolution reached. Requires correct Cov as input.
70  */
71  int AutoAlign(const PyramidImage<float>& I1, const PyramidImage<float>& I2,
72  Vector<double>& params, Matrix<double>& Cov);
73 
74  /** @brief aligns image1 with image2 with respect to parameters params,
75  * traditional pyramid based alignment is used, mainly for
76  * comparison reasons. Can be used also if Cov is totally unknown.
77  */
78  int StrictPyramidAlign(const PyramidImage<float>& I1,
79  const PyramidImage<float>& I2,
80  Vector<double>& params, Matrix<double>& Cov);
81 
82  /** @brief aligns image1 with image2 with respect to parameters params
83  *
84  * if no pyramidlevel is supplied the best pyramid level for each
85  * pixel is chosen automatically using Cov (scale space tracking) */
86  int Align(const PyramidImage<float>& I1, const PyramidImage<float>& I2,
87  Vector<double>& params, Matrix<double>& Cov, int PyramidLevel=-1);
88 
89  /** @brief pass object type which holds texture transform */
91  if (T_!=NULL) delete T_;
92  T_ = T.Clone();
93  }
94 
95  /** @brief number of iterations, before the system stops unsuccessfully */
96  inline void SetMaxIterations(const int maxiter)
97  { MaxIterations_ = maxiter; }
98 
99  /** @brief if a step smaller (norm L2) than this is taken, convergence
100  is declared and the system finishes */
101  inline void SetUpdateThreshold(const double maxerr)
102  { MaxUpdate_ = maxerr; }
103 
104  /** enable brightness variance and offset invariant computation */
106  AffineBrightnessInvariance_ = bi;
107  }
108 
109  inline int GetMaxIterations() { return MaxIterations_; }
110 
111  inline float GetMaxUpdate() { return MaxUpdate_; }
112 
113  /** @brief choose any rectangular tracking region and resolution
114  *
115  * The localaffineframe determines the shape, scale and orientation of the
116  * rectangular region used for tracking. The numberofpixelsis a
117  * windowsize, e.g. if you pass 9, a 9x9 grid will be used for tracking
118  * If you pass -1, each pixel is used for tracking */
119  void SetAlignmentPixelsRectangular(const LocalAffineFrame& LAF,
120  int numberOfPixels = -1);
121 
122  /** @brief return relative affine transform between i1 and i2 if
123  * AffineBrightnessInvariance was active
124  *
125  * i2 = scale * i1 + shift
126  */
127  void GetLastBrightnessChange(double& scale, double& shift) {
128  scale = dev2_ / dev1_;
129  shift = mean2_ - scale * mean1_;
130  }
131 
132  /** @brief enable/disable and set step width for line search: <=0 disables,
133  * 0.1 or 0.5 may be good values. 1.0 would mean original step */
134  void SetLineSearch(const double& lineSearch) {
135  LineSearch_ = lineSearch;
136  BIASASSERT(LineSearch_<1.0);
137  }
138  /** @brief adds a dampening d to hessian's diag
139  @param dampening: >=0: d, <0 -d*largest eigvalue */
140  void SetDampening(const double& dampening) {
141  Dampening_ = dampening;
142  }
143  /** @brief set expected standard deviation for grey value noise
144 
145  This is important if maximum a posteriori-estimation is desired
146  and you provide a correctly scaled prior cov for the warp parameters.
147  Pixels which have smaller gradient than this are rejected.
148  */
149  void SetIntensitySigma(const double& sigma) {
150  intensitySigma_ = sigma;
151  }
152 
153  protected:
154 
155  /** @brief check for given cov and params, whether control points move more
156  * than pixelAccuracy */
157  int CheckConvergence_(const Vector<double>& params,
158  const Matrix<double>& Cov,
159  const double& pixelAccuracy = 0.3);
160 
161  ///////////////////////////////////////////////////////////////////////////
162  // params
163  ///////////////////////////////////////////////////////////////////////////
164 
165  /// iteration stops after
167 
168  /// iteration stops if parameter update is less than
169  float MaxUpdate_;
170 
171  /// adds a dampening to hessian's diag: >=0: d, <0 d*largest eigvalue
172  double Dampening_;
173 
174  /// compute brightness "invariant"
176 
177  /// if error is not reduced in one step, linesearch times this step is tried
178  /// set to <=0 to disable line search
179  double LineSearch_;
180 
181  /// how much noise is expected on intensity values
183  ///////////////////////////////////////////////////////////////////////////
184 
185  /// the warp model
187 
188  /// mean and std deviation of image patch
189  double mean1_, mean2_, dev1_, dev2_;
190 
191  /// gradients and grey values from image 1
192  std::vector<double> TemplateGreyValues_;
193  std::vector<Vector2<double> > TemplateGradients_;
194  std::vector<double> TemplateScales_;
195  std::vector<HomgPoint2D> TemplatePositions_;
196  std::vector<HomgPoint2D> ControlPositions_;
197 
198  std::vector<HomgPoint2D> ImagePositions_;
199  std::vector<Vector<double> > SteepestDescentImages_;
200  std::vector<bool> active_;
201 
202  /// internal counter for numbering debug images
204 
205  }; // class
206 
207 } // namespace
208 
209 #endif
void SetIntensitySigma(const double &sigma)
set expected standard deviation for grey value noise
std::vector< bool > active_
void GetLastBrightnessChange(double &scale, double &shift)
return relative affine transform between i1 and i2 if AffineBrightnessInvariance was active ...
void SetAffineBrightnessInvariance(bool bi)
enable brightness variance and offset invariant computation
affine transformation of 2D image plane which relates image coordinate system and local affine featur...
class for representing parameterized image warps, such as homography, displacement, ...
std::vector< HomgPoint2D > ControlPositions_
std::vector< HomgPoint2D > ImagePositions_
double Dampening_
adds a dampening to hessian&#39;s diag: &gt;=0: d, &lt;0 d*largest eigvalue
void SetUpdateThreshold(const double maxerr)
if a step smaller (norm L2) than this is taken, convergence is declared and the system finishes ...
int iterationDebug_
internal counter for numbering debug images
std::vector< HomgPoint2D > TemplatePositions_
std::vector< Vector2< double > > TemplateGradients_
double intensitySigma_
how much noise is expected on intensity values
int MaxIterations_
iteration stops after
Inverse Compositional Image Alignment (&quot;Registration&quot;)
virtual TextureTransform * Clone() const =0
virtual covariant copy constructor, caller must eventually destroy the created object ...
TextureTransform * T_
the warp model
std::vector< Vector< double > > SteepestDescentImages_
void SetLineSearch(const double &lineSearch)
enable/disable and set step width for line search: &lt;=0 disables, 0.1 or 0.5 may be good values...
std::vector< double > TemplateGreyValues_
gradients and grey values from image 1
void SetTextureTransform(const TextureTransform &T)
pass object type which holds texture transform
bool AffineBrightnessInvariance_
compute brightness &quot;invariant&quot;
std::vector< double > TemplateScales_
void SetDampening(const double &dampening)
adds a dampening d to hessian&#39;s diag
double LineSearch_
if error is not reduced in one step, linesearch times this step is tried set to &lt;=0 to disable line s...
float MaxUpdate_
iteration stops if parameter update is less than
void SetMaxIterations(const int maxiter)
number of iterations, before the system stops unsuccessfully