Visual Servoing Platform  version 3.1.0
tutorial-pose-from-points-image.cpp
1 
2 #include <visp3/blob/vpDot2.h>
3 #include <visp3/core/vpPixelMeterConversion.h>
4 #include <visp3/gui/vpDisplayGDI.h>
5 #include <visp3/gui/vpDisplayOpenCV.h>
6 #include <visp3/gui/vpDisplayX.h>
7 #include <visp3/io/vpImageIo.h>
8 #include <visp3/vision/vpPose.h>
9 
10 void computePose(std::vector<vpPoint> &point, const std::vector<vpDot2> &dot, const vpCameraParameters &cam, bool init,
12 {
13  vpPose pose;
14  double x = 0, y = 0;
15  for (unsigned int i = 0; i < point.size(); i++) {
16  vpPixelMeterConversion::convertPoint(cam, dot[i].getCog(), x, y);
17  point[i].set_x(x);
18  point[i].set_y(y);
19  pose.addPoint(point[i]);
20  }
21 
22  if (init == true) {
23  vpHomogeneousMatrix cMo_dem;
24  vpHomogeneousMatrix cMo_lag;
25  pose.computePose(vpPose::DEMENTHON, cMo_dem);
26  pose.computePose(vpPose::LAGRANGE, cMo_lag);
27  double residual_dem = pose.computeResidual(cMo_dem);
28  double residual_lag = pose.computeResidual(cMo_lag);
29  if (residual_dem < residual_lag)
30  cMo = cMo_dem;
31  else
32  cMo = cMo_lag;
33  }
35 }
36 
37 int main()
38 {
39  try {
41  vpImageIo::read(I, "square.pgm");
42 
43 #if defined(VISP_HAVE_X11)
44  vpDisplayX d(I);
45 #elif defined(VISP_HAVE_GDI)
46  vpDisplayGDI d(I);
47 #elif defined(VISP_HAVE_OPENCV)
48  vpDisplayOpenCV d(I);
49 #endif
50 
51  vpCameraParameters cam(840, 840, I.getWidth() / 2, I.getHeight() / 2);
52  std::vector<vpDot2> dot(4);
53  dot[0].initTracking(I, vpImagePoint(193, 157));
54  dot[1].initTracking(I, vpImagePoint(203, 366));
55  dot[2].initTracking(I, vpImagePoint(313, 402));
56  dot[3].initTracking(I, vpImagePoint(304, 133));
57  std::vector<vpPoint> point;
58  point.push_back(vpPoint(-0.06, -0.06, 0));
59  point.push_back(vpPoint(0.06, -0.06, 0));
60  point.push_back(vpPoint(0.06, 0.06, 0));
61  point.push_back(vpPoint(-0.06, 0.06, 0));
63  bool init = true;
64 
65  while (1) {
66  vpImageIo::read(I, "square.pgm");
68  for (unsigned int i = 0; i < dot.size(); i++) {
69  dot[i].setGraphics(true);
70  dot[i].track(I);
71  }
72  computePose(point, dot, cam, init, cMo);
73  vpDisplay::displayFrame(I, cMo, cam, 0.05, vpColor::none);
75 
76  if (init)
77  init = false; // turn off pose initialisation
78 
79  if (vpDisplay::getClick(I, false))
80  break;
81 
82  vpTime::wait(40);
83  }
84  } catch (const vpException &e) {
85  std::cout << "Catch an exception: " << e.getMessage() << std::endl;
86  }
87 }
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:150
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
Implementation of an homogeneous matrix and operations on such kind of matrices.
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:151
static const vpColor none
Definition: vpColor.h:192
error that can be emited by ViSP classes.
Definition: vpException.h:71
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
Point coordinates conversion from pixel coordinates to normalized coordinates in meter...
static void flush(const vpImage< unsigned char > &I)
Class that defines what is a point.
Definition: vpPoint.h:58
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Class used for pose computation from N points (pose from point only). Some of the algorithms implemen...
Definition: vpPose.h:79
Generic class defining intrinsic camera parameters.
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, bool(*func)(vpHomogeneousMatrix *)=NULL)
Definition: vpPose.cpp:362
const char * getMessage(void) const
Definition: vpException.cpp:90
unsigned int getHeight() const
Definition: vpImage.h:178
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:207
static void displayFrame(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double size, const vpColor &color=vpColor::none, unsigned int thickness=1, const vpImagePoint &offset=vpImagePoint(0, 0))
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
double computeResidual(const vpHomogeneousMatrix &cMo) const
Compute and return the residual expressed in meter for the pose matrix &#39;cMo&#39;.
Definition: vpPose.cpp:324
void addPoint(const vpPoint &P)
Definition: vpPose.cpp:137
unsigned int getWidth() const
Definition: vpImage.h:229