Visual Servoing Platform  version 3.6.1 under development (2024-02-13)
vpHomographyDLT.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Homography estimation.
32  */
33 
41 #include <visp3/core/vpMatrix.h>
42 #include <visp3/core/vpMatrixException.h>
43 #include <visp3/vision/vpHomography.h>
44 
45 #include <cmath> // std::fabs
46 #include <limits> // numeric_limits
47 
48 #ifndef DOXYGEN_SHOULD_SKIP_THIS
49 
50 void vpHomography::hartleyNormalization(const std::vector<double> &x, const std::vector<double> &y,
51  std::vector<double> &xn, std::vector<double> &yn, double &xg, double &yg,
52  double &coef)
53 {
54  if (x.size() != y.size()) {
55  throw(vpException(vpException::dimensionError, "Hartley normalization require that x and y vector "
56  "have the same dimension"));
57  }
58 
59  unsigned int n = static_cast<unsigned int>(x.size());
60  if (xn.size() != n) {
61  xn.resize(n);
62  }
63  if (yn.size() != n) {
64  yn.resize(n);
65  }
66 
67  xg = 0;
68  yg = 0;
69 
70  for (unsigned int i = 0; i < n; ++i) {
71  xg += x[i];
72  yg += y[i];
73  }
74  xg /= n;
75  yg /= n;
76 
77  // Changement d'origine : le centre de gravite doit correspondre
78  // a l'origine des coordonnees
79  double distance = 0;
80  for (unsigned int i = 0; i < n; ++i) {
81  double xni = x[i] - xg;
82  double yni = y[i] - yg;
83  xn[i] = xni;
84  yn[i] = yni;
85  distance += sqrt(vpMath::sqr(xni) + vpMath::sqr(yni));
86  } // for translation sur tous les points
87 
88  // Changement d'echelle
89  distance /= n;
90  // calcul du coef de changement d'echelle
91  // if(distance ==0)
92  if (std::fabs(distance) <= std::numeric_limits<double>::epsilon()) {
93  coef = 1;
94  }
95  else {
96  coef = sqrt(2.0) / distance;
97  }
98 
99  for (unsigned int i = 0; i < n; ++i) {
100  xn[i] *= coef;
101  yn[i] *= coef;
102  }
103 }
104 
105 void vpHomography::hartleyNormalization(unsigned int n, const double *x, const double *y, double *xn, double *yn,
106  double &xg, double &yg, double &coef)
107 {
108  unsigned int i;
109  xg = 0;
110  yg = 0;
111 
112  for (i = 0; i < n; ++i) {
113  xg += x[i];
114  yg += y[i];
115  }
116  xg /= n;
117  yg /= n;
118 
119  // Changement d'origine : le centre de gravite doit correspondre
120  // a l'origine des coordonnees
121  double distance = 0;
122  for (i = 0; i < n; ++i) {
123  double xni = x[i] - xg;
124  double yni = y[i] - yg;
125  xn[i] = xni;
126  yn[i] = yni;
127  distance += sqrt(vpMath::sqr(xni) + vpMath::sqr(yni));
128  } // for translation sur tous les points
129 
130  // Changement d'echelle
131  distance /= n;
132  // calcul du coef de changement d'echelle
133  // if(distance ==0)
134  if (std::fabs(distance) <= std::numeric_limits<double>::epsilon()) {
135  coef = 1;
136  }
137  else {
138  coef = sqrt(2.0) / distance;
139  }
140 
141  for (i = 0; i < n; ++i) {
142  xn[i] *= coef;
143  yn[i] *= coef;
144  }
145 }
146 
147 //---------------------------------------------------------------------------------------
148 
149 void vpHomography::hartleyDenormalization(vpHomography &aHbn, vpHomography &aHb, double xg1, double yg1, double coef1,
150  double xg2, double yg2, double coef2)
151 {
152 
153  // calcul des transformations a appliquer sur M_norm pour obtenir M
154  // en fonction des deux normalizations effectuees au debut sur
155  // les points: aHb = T2^ aHbn T1
156  vpMatrix T1(3, 3);
157  vpMatrix T2(3, 3);
158  vpMatrix T2T(3, 3);
159 
160  T1.eye();
161  T2.eye();
162  T2T.eye();
163 
164  T1[0][0] = (T1[1][1] = coef1);
165  T1[0][2] = -coef1 * xg1;
166  T1[1][2] = -coef1 * yg1;
167 
168  T2[0][0] = (T2[1][1] = coef2);
169  T2[0][2] = -coef2 * xg2;
170  T2[1][2] = -coef2 * yg2;
171 
172  T2T = T2.pseudoInverse(1e-16);
173 
174  vpMatrix aHbn_(3, 3);
175  for (unsigned int i = 0; i < 3; ++i) {
176  for (unsigned int j = 0; j < 3; ++j) {
177  aHbn_[i][j] = aHbn[i][j];
178  }
179  }
180 
181  vpMatrix maHb = T2T * aHbn_ * T1;
182 
183  for (unsigned int i = 0; i < 3; ++i) {
184  for (unsigned int j = 0; j < 3; ++j) {
185  aHb[i][j] = maHb[i][j];
186  }
187  }
188 }
189 
190 #endif // #ifndef DOXYGEN_SHOULD_SKIP_THIS
191 
192 void vpHomography::DLT(const std::vector<double> &xb, const std::vector<double> &yb, const std::vector<double> &xa,
193  const std::vector<double> &ya, vpHomography &aHb, bool normalization)
194 {
195  unsigned int n = static_cast<unsigned int>(xb.size());
196  if ((yb.size() != n) || (xa.size() != n) || (ya.size() != n)) {
197  throw(vpException(vpException::dimensionError, "Bad dimension for DLT homography estimation"));
198  }
199 
200  // 4 point are required
201  const unsigned int nbRequiredPoints = 4;
202  if (n < nbRequiredPoints) {
203  throw(vpException(vpException::fatalError, "There must be at least 4 matched points"));
204  }
205 
206  std::vector<double> xan, yan, xbn, ybn;
207 
208  double xg1 = 0., yg1 = 0., coef1 = 0., xg2 = 0., yg2 = 0., coef2 = 0.;
209 
210  vpHomography aHbn;
211 
212  if (normalization) {
213  vpHomography::hartleyNormalization(xb, yb, xbn, ybn, xg1, yg1, coef1);
214  vpHomography::hartleyNormalization(xa, ya, xan, yan, xg2, yg2, coef2);
215  }
216  else {
217  xbn = xb;
218  ybn = yb;
219  xan = xa;
220  yan = ya;
221  }
222 
223  vpMatrix A(2 * n, 9);
224  vpColVector h(9);
225  vpColVector D(9);
226  vpMatrix V(9, 9);
227 
228  // We need here to compute the SVD on a (n*2)*9 matrix (where n is
229  // the number of points). if n == 4, the matrix has more columns
230  // than rows. This kind of matrix is not supported by GSL for
231  // SVD. The solution is to add an extra line with zeros
232  if (n == 4) {
233  A.resize((2 * n) + 1, 9);
234  }
235 
236  // build matrix A
237  for (unsigned int i = 0; i < n; ++i) {
238  A[2 * i][0] = 0;
239  A[2 * i][1] = 0;
240  A[2 * i][2] = 0;
241  A[2 * i][3] = -xbn[i];
242  A[2 * i][4] = -ybn[i];
243  A[2 * i][5] = -1;
244  A[2 * i][6] = xbn[i] * yan[i];
245  A[2 * i][7] = ybn[i] * yan[i];
246  A[2 * i][8] = yan[i];
247 
248  A[(2 * i) + 1][0] = xbn[i];
249  A[(2 * i) + 1][1] = ybn[i];
250  A[(2 * i) + 1][2] = 1;
251  A[(2 * i) + 1][3] = 0;
252  A[(2 * i) + 1][4] = 0;
253  A[(2 * i) + 1][5] = 0;
254  A[(2 * i) + 1][6] = -xbn[i] * xan[i];
255  A[(2 * i) + 1][7] = -ybn[i] * xan[i];
256  A[(2 * i) + 1][8] = -xan[i];
257  }
258 
259  // Add an extra line with zero.
260  if (n == 4) {
261  for (unsigned int i = 0; i < 9; ++i) {
262  A[2 * n][i] = 0;
263  }
264  }
265 
266  // solve Ah = 0
267  // SVD Decomposition A = UDV^T (destructive wrt A)
268  A.svd(D, V);
269 
270  // on en profite pour effectuer un controle sur le rang de la matrice :
271  // pas plus de 2 valeurs singulieres quasi=0
272  int rank = 0;
273  for (unsigned int i = 0; i < 9; ++i) {
274  if (D[i] > 1e-7) {
275  ++rank;
276  }
277  }
278  if (rank < 7) {
279  throw(vpMatrixException(vpMatrixException::rankDeficient, "Matrix rank %d is deficient (should be 8)", rank));
280  }
281  // h = is the column of V associated with the smallest singular value of A
282 
283  // since we are not sure that the svd implemented sort the
284  // singular value... we seek for the smallest
285  double smallestSv = 1e30;
286  unsigned int indexSmallestSv = 0;
287  for (unsigned int i = 0; i < 9; ++i) {
288  if (D[i] < smallestSv) {
289  smallestSv = D[i];
290  indexSmallestSv = i;
291  }
292  }
293 
294  h = V.getCol(indexSmallestSv);
295 
296  // build the homography
297  for (unsigned int i = 0; i < 3; ++i) {
298  for (unsigned int j = 0; j < 3; ++j) {
299  aHbn[i][j] = h[(3 * i) + j];
300  }
301  }
302 
303  if (normalization) {
304  // H after denormalization
305  vpHomography::hartleyDenormalization(aHbn, aHb, xg1, yg1, coef1, xg2, yg2, coef2);
306  }
307  else {
308  aHb = aHbn;
309  }
310 }
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:299
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ dimensionError
Bad dimension.
Definition: vpException.h:83
@ fatalError
Fatal error.
Definition: vpException.h:84
Implementation of an homography and operations on homographies.
Definition: vpHomography.h:168
static void DLT(const std::vector< double > &xb, const std::vector< double > &yb, const std::vector< double > &xa, const std::vector< double > &ya, vpHomography &aHb, bool normalization=true)
static double sqr(double x)
Definition: vpMath.h:201
error that can be emitted by the vpMatrix class and its derivatives
@ rankDeficient
Rank deficient.
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:146
void svd(vpColVector &w, vpMatrix &V)
Definition: vpMatrix.cpp:2075
vpColVector getCol(unsigned int j) const
Definition: vpMatrix.cpp:5181