41 #include <visp3/core/vpMatrix.h>
42 #include <visp3/core/vpMatrixException.h>
43 #include <visp3/vision/vpHomography.h>
50 #ifndef DOXYGEN_SHOULD_SKIP_THIS
52 void vpHomography::hartleyNormalization(
const std::vector<double> &x,
const std::vector<double> &y,
53 std::vector<double> &xn, std::vector<double> &yn,
double &xg,
double &yg,
56 if (x.size() != y.size()) {
58 "have the same dimension"));
61 unsigned int n =
static_cast<unsigned int>(x.size());
72 for (
unsigned int i = 0; i < n; ++i) {
82 for (
unsigned int i = 0; i < n; ++i) {
83 double xni = x[i] - xg;
84 double yni = y[i] - yg;
94 if (std::fabs(distance) <= std::numeric_limits<double>::epsilon()) {
98 coef = sqrt(2.0) / distance;
101 for (
unsigned int i = 0; i < n; ++i) {
107 void vpHomography::hartleyNormalization(
unsigned int n,
const double *x,
const double *y,
double *xn,
double *yn,
108 double &xg,
double &yg,
double &coef)
114 for (i = 0; i < n; ++i) {
124 for (i = 0; i < n; ++i) {
125 double xni = x[i] - xg;
126 double yni = y[i] - yg;
136 if (std::fabs(distance) <= std::numeric_limits<double>::epsilon()) {
140 coef = sqrt(2.0) / distance;
143 for (i = 0; i < n; ++i) {
151 void vpHomography::hartleyDenormalization(
vpHomography &aHbn,
vpHomography &aHb,
double xg1,
double yg1,
double coef1,
152 double xg2,
double yg2,
double coef2)
166 T1[0][0] = (T1[1][1] = coef1);
167 T1[0][2] = -coef1 * xg1;
168 T1[1][2] = -coef1 * yg1;
170 T2[0][0] = (T2[1][1] = coef2);
171 T2[0][2] = -coef2 * xg2;
172 T2[1][2] = -coef2 * yg2;
174 T2T = T2.pseudoInverse(1e-16);
177 const unsigned int val_3 = 3;
178 for (
unsigned int i = 0; i < val_3; ++i) {
179 for (
unsigned int j = 0; j < val_3; ++j) {
180 aHbn_[i][j] = aHbn[i][j];
186 for (
unsigned int i = 0; i < val_3; ++i) {
187 for (
unsigned int j = 0; j < val_3; ++j) {
188 aHb[i][j] = maHb[i][j];
195 void vpHomography::DLT(
const std::vector<double> &xb,
const std::vector<double> &yb,
const std::vector<double> &xa,
196 const std::vector<double> &ya,
vpHomography &aHb,
bool normalization)
198 unsigned int n =
static_cast<unsigned int>(xb.size());
199 if ((yb.size() != n) || (xa.size() != n) || (ya.size() != n)) {
204 const unsigned int nbRequiredPoints = 4;
205 if (n < nbRequiredPoints) {
209 std::vector<double> xan, yan, xbn, ybn;
211 double xg1 = 0., yg1 = 0., coef1 = 0., xg2 = 0., yg2 = 0., coef2 = 0.;
216 vpHomography::hartleyNormalization(xb, yb, xbn, ybn, xg1, yg1, coef1);
217 vpHomography::hartleyNormalization(xa, ya, xan, yan, xg2, yg2, coef2);
240 for (
unsigned int i = 0; i < n; ++i) {
244 A[2 * i][3] = -xbn[i];
245 A[2 * i][4] = -ybn[i];
247 A[2 * i][6] = xbn[i] * yan[i];
248 A[2 * i][7] = ybn[i] * yan[i];
249 A[2 * i][8] = yan[i];
251 A[(2 * i) + 1][0] = xbn[i];
252 A[(2 * i) + 1][1] = ybn[i];
253 A[(2 * i) + 1][2] = 1;
254 A[(2 * i) + 1][3] = 0;
255 A[(2 * i) + 1][4] = 0;
256 A[(2 * i) + 1][5] = 0;
257 A[(2 * i) + 1][6] = -xbn[i] * xan[i];
258 A[(2 * i) + 1][7] = -ybn[i] * xan[i];
259 A[(2 * i) + 1][8] = -xan[i];
264 for (
unsigned int i = 0; i < 9; ++i) {
276 for (
unsigned int i = 0; i < 9; ++i) {
288 double smallestSv = 1e30;
289 unsigned int indexSmallestSv = 0;
290 for (
unsigned int i = 0; i < 9; ++i) {
291 if (D[i] < smallestSv) {
297 h = V.
getCol(indexSmallestSv);
300 const unsigned int val_3 = 3;
301 for (
unsigned int i = 0; i < val_3; ++i) {
302 for (
unsigned int j = 0; j < val_3; ++j) {
303 aHbn[i][j] = h[(3 * i) + j];
309 vpHomography::hartleyDenormalization(aHbn, aHb, xg1, yg1, coef1, xg2, yg2, coef2);
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Implementation of column vector and the associated operations.
error that can be emitted by ViSP classes.
@ dimensionError
Bad dimension.
Implementation of an homography and operations on homographies.
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)
error that can be emitted by the vpMatrix class and its derivatives
@ rankDeficient
Rank deficient.
Implementation of a matrix and operations on matrices.
void svd(vpColVector &w, vpMatrix &V)
vpColVector getCol(unsigned int j) const