Visual Servoing Platform  version 3.6.1 under development (2024-12-17)
tutorial-homography-from-points.cpp
1 
3 #include <visp3/core/vpConfig.h>
5 #include <visp3/vision/vpHomography.h>
7 #include <visp3/core/vpMeterPixelConversion.h>
8 
9 int main()
10 {
11 #ifdef ENABLE_VISP_NAMESPACE
12  using namespace VISP_NAMESPACE_NAME;
13 #endif
15  double L = 0.1;
16  std::vector<vpPoint> oP;
17  oP.push_back(vpPoint(-L, -L, 0));
18  oP.push_back(vpPoint(2 * L, -L, 0));
19  oP.push_back(vpPoint(L, 3 * L, 0));
20  oP.push_back(vpPoint(-L, 4 * L, 0));
22 
24  vpHomogeneousMatrix bMo(0.1, 0, 1, 0, vpMath::rad(15), 0);
25  vpHomogeneousMatrix aMb(0.2, -0.1, 0.1, vpMath::rad(-3), vpMath::rad(20), vpMath::rad(5));
26  vpHomogeneousMatrix aMo = aMb * bMo;
28 
30  std::vector<vpPoint> aP(4), bP(4);
31  std::vector<double> xa(4), ya(4), xb(4), yb(4);
32  for (unsigned int i = 0; i < 4; i++) {
33  oP[i].project(aMo);
34  xa[i] = oP[i].get_x();
35  ya[i] = oP[i].get_y();
36  oP[i].project(bMo);
37  xb[i] = oP[i].get_x();
38  yb[i] = oP[i].get_y();
39  }
41 
43  vpHomography aHb;
44  vpHomography::DLT(xb, yb, xa, ya, aHb, true);
45  std::cout << "Estimated homography using DLT:\n" << aHb / aHb[2][2] << std::endl;
46 
47  vpHomography::HLM(xb, yb, xa, ya, true, aHb);
48  std::cout << "Estimated homography using HLM:\n" << aHb / aHb[2][2] << std::endl;
50 
52  vpRotationMatrix aRb;
54  vpColVector n;
55  aHb.computeDisplacement(aRb, atb, n);
57 
59  std::cout << "\nEstimated displacement:" << std::endl;
60  std::cout << " atb: " << atb.t() << std::endl;
61  vpThetaUVector atub;
62  atub.buildFrom(aRb);
63  std::cout << " athetaub: ";
64  for (unsigned int i = 0; i < 3; i++)
65  std::cout << vpMath::deg(atub[i]) << " ";
66  std::cout << std::endl;
67  std::cout << " n: " << n.t() << std::endl;
69 
71  vpImagePoint iPa, iPb;
73  vpMeterPixelConversion::convertPoint(cam, xb[3], yb[3], iPb);
74  vpMeterPixelConversion::convertPoint(cam, xa[3], ya[3], iPa);
75 
76  std::cout << "Ground truth: Point 3 in pixels in frame b: " << iPb << std::endl;
77  std::cout << "Ground truth: Point 3 in pixels in frame a: " << iPa << std::endl;
79 
81  // Project the position in pixel of point 3 from the homography
82  std::cout << "Estimation from homography: Point 3 in pixels in frame a: " << vpHomography::project(cam, aHb, iPb)
83  << std::endl;
85 }
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
vpRowVector t() const
Implementation of an homogeneous matrix and operations on such kind of matrices.
Implementation of an homography and operations on homographies.
Definition: vpHomography.h:174
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)
void computeDisplacement(vpRotationMatrix &aRb, vpTranslationVector &atb, vpColVector &n)
static void HLM(const std::vector< double > &xb, const std::vector< double > &yb, const std::vector< double > &xa, const std::vector< double > &ya, bool isplanar, vpHomography &aHb)
static vpImagePoint project(const vpCameraParameters &cam, const vpHomography &bHa, const vpImagePoint &iPa)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
static double rad(double deg)
Definition: vpMath.h:129
static double deg(double rad)
Definition: vpMath.h:119
static void convertPoint(const vpCameraParameters &cam, const double &x, const double &y, double &u, double &v)
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:79
Implementation of a rotation matrix and operations on such kind of matrices.
Implementation of a rotation vector as axis-angle minimal representation.
vpThetaUVector & buildFrom(const vpHomogeneousMatrix &M)
Class that consider the case of a translation vector.
vpRowVector t() const