Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
HMatrixEstimation.hh
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 
26 #ifndef __HMatrixEstimation_hh__
27 #define __HMatrixEstimation_hh__
28 #include "bias_config.h"
29 
30 #include <Geometry/HMatrix.hh>
31 #include <Base/Geometry/Normalization.hh>
32 
33 namespace BIAS {
34 
35 #define HE_OK 0
36 #define HE_NOT_ENOUGH_POINTS -78
37 #define HE_SVD_ERROR -79
38 #define HE_LM_ERROR -80
39 
40 
41  /** @class HMatrixEstimation
42  @ingroup g_geometry
43  @brief Estimate 3x3 matrix relating image coordinates with each other,
44  i.e. a homography or affine transformation.
45 
46  @author koeser 12/2005
47  */
48  class BIASGeometry_EXPORT HMatrixEstimation
49  {
50  public:
51 
52  /** @brief Create homography estimation instance.
53  @param normalizeHartley Use normalization of input vectors proposed by
54  Hartley (strongly recommended) or use the DLT on raw data
55  */
56  explicit HMatrixEstimation(bool normalizeHartley = true)
57  { normalizeHartley_ = normalizeHartley; }
58 
60 
61  /** @brief Calculate homography between 2d points using the DLT.
62 
63  The direct linear transformation algorithm (see Hartley & Zisserman:
64  "Multiple View Geometry", p.73) solves the equation system
65  (p' x (H * p) = 0 in order to solve p' ~ H * p where p, p' are
66  2d image coordinates in homogeneous coordinates.
67  Each correspondence provides 3 equation, but the linear equation
68  system has but rank 2, so we take only the first two rows.
69  The resulting equation system A*h = 0 s.t. |h| = 1 is solved via
70  SVD where h is vector representation of matrix H.
71 
72  @param fromPoints Homogenized points in image 1
73  @param toPoints Corresponding homogenized points in image 2
74  @param H Returns estimated homography matrix
75  @author woelk/frahm
76  */
77  int Compute(const std::vector<BIAS::HomgPoint2D>& fromPoints,
78  const std::vector<BIAS::HomgPoint2D>& toPoints,
79  HMatrix& H);
80 
81  /** @brief Optimize a given homography matrix from 2d point correspondences.
82 
83  Minimize an error function on the homography matrix using iterative
84  nonlinear optimization techniques such as Levenberg-Marquardt.
85 
86  @attention Susceptible to local minima, provide good starting point!
87  @param H Homography matrix to optimize, provides starting point
88  @param p1 Homogenized points in image 1
89  @param p2 Corresponding homogenized points in image 2
90  @return Return 0 on success, <0 for errors
91  @author koeser 06/2006
92  */
93  int Optimize(BIAS::HMatrix& H,
94  const std::vector<BIAS::HomgPoint2D> &p1,
95  const std::vector<BIAS::HomgPoint2D> &p2);
96 
97  /** @brief Compute affine transformation between 2d points, i.e. rotation
98  and translation. This method is not using the DLT!
99  @param fromPoints Homogenized points in image 1
100  @param toPoints Corresponding homogenized points in image 2
101  @param H Returns estimated affine transformation matrix
102  @author koeser 12/2005
103  */
104  int ComputeAffine(const std::vector<BIAS::HomgPoint2D>& fromPoints,
105  const std::vector<BIAS::HomgPoint2D>& toPoints,
106  HMatrix& H);
107 
108  /** @brief Returns the average squared error of the estimation applying the
109  estimated homography matrix to all correspondences.
110  */
111  double GetError() const;
112 
113  /** @brief Decide whether to normalize input vector as proposed by Hartley
114  (strongly recommended) or use the DLT on raw data.
115  */
116  void SetNormalizeHartley(bool normalize) { normalizeHartley_ = normalize; }
117 
118  /** @brief Calculate homography between 2d points using the DLT.
119  @param fromPoints Homogenized points in image 1
120  @param toPoints Corresponding homogenized points in image 2
121  @param H Returns estimated homography matrix
122  @author woelk/frahm
123  */
124  int Compute(const std::vector<BIAS::Vector2<double> >& fromPoints,
125  const std::vector<BIAS::Vector2<double> >& toPoints,
126  HMatrix &H);
127 
128  protected:
129 
130  /** @brief Flag indicating whether we should normalize the given points before
131  and denormalize the homography matrix after computation, so that it
132  refers to the original points */
134 
135  /** @brief The normalization algorithm instance */
137 
138  /** @brief Stores the estimated homography matrix (used in GetError) */
140 
141  /** @brief Stores the points in image 1 (used in Optimize and GetError) */
142  std::vector<BIAS::HomgPoint2D> pointsFrom_;
143 
144  /** @brief Stores the points in image 2 (used in Optimize and GetError) */
145  std::vector<BIAS::HomgPoint2D> pointsTo_;
146 
147  /** @brief Objective function for Levenberg-Marquardt optimization */
148  static int HErrorFunction_(void *p, int m, int n, const double *x,
149  double *fvec, int iflag);
150 
151  };
152 
153 
154 }// namespace
155 
156 #endif // __HMatrixEstimation_hh__
Normalization normalization_
The normalization algorithm instance.
a 3x3 Matrix describing projective transformations between planes
Definition: HMatrix.hh:39
std::vector< BIAS::HomgPoint2D > pointsFrom_
Stores the points in image 1 (used in Optimize and GetError)
HMatrixEstimation(bool normalizeHartley=true)
Create homography estimation instance.
HMatrix H_
Stores the estimated homography matrix (used in GetError)
bool normalizeHartley_
Flag indicating whether we should normalize the given points before and denormalize the homography ma...
class Normalization Functions, e.g.
std::vector< BIAS::HomgPoint2D > pointsTo_
Stores the points in image 2 (used in Optimize and GetError)
Estimate 3x3 matrix relating image coordinates with each other, i.e.
void SetNormalizeHartley(bool normalize)
Decide whether to normalize input vector as proposed by Hartley (strongly recommended) or use the DLT...