Visual Servoing Platform  version 3.0.0
tutorial-pose-from-points-image.cpp
1 
2 #include <visp3/gui/vpDisplayGDI.h>
3 #include <visp3/gui/vpDisplayOpenCV.h>
4 #include <visp3/gui/vpDisplayX.h>
5 #include <visp3/blob/vpDot2.h>
6 #include <visp3/io/vpImageIo.h>
7 #include <visp3/core/vpPixelMeterConversion.h>
8 #include <visp3/vision/vpPose.h>
9 
10 void computePose(std::vector<vpPoint> &point, const std::vector<vpDot2> &dot,
11  const vpCameraParameters &cam, bool init, vpHomogeneousMatrix &cMo);
12 
13 void computePose(std::vector<vpPoint> &point, const std::vector<vpDot2> &dot,
14  const vpCameraParameters &cam, bool init, vpHomogeneousMatrix &cMo)
15 {
16  vpPose pose; double x=0, y=0;
17  for (unsigned int i=0; i < point.size(); i ++) {
18  vpPixelMeterConversion::convertPoint(cam, dot[i].getCog(), x, y);
19  point[i].set_x(x);
20  point[i].set_y(y);
21  pose.addPoint(point[i]);
22  }
23 
24  if (init == true) {
25  vpHomogeneousMatrix cMo_dem;
26  vpHomogeneousMatrix cMo_lag;
27  pose.computePose(vpPose::DEMENTHON, cMo_dem);
28  pose.computePose(vpPose::LAGRANGE, cMo_lag);
29  double residual_dem = pose.computeResidual(cMo_dem);
30  double residual_lag = pose.computeResidual(cMo_lag);
31  if (residual_dem < residual_lag)
32  cMo = cMo_dem;
33  else
34  cMo = cMo_lag;
35  }
37 }
38 
39 int main()
40 {
41  try {
43  vpImageIo::read(I, "square.pgm");
44 
45 #if defined(VISP_HAVE_X11)
46  vpDisplayX d(I);
47 #elif defined(VISP_HAVE_GDI)
48  vpDisplayGDI d(I);
49 #elif defined(VISP_HAVE_OPENCV)
50  vpDisplayOpenCV d(I);
51 #endif
52 
53  vpCameraParameters cam(840, 840, I.getWidth()/2, I.getHeight()/2);
54  std::vector<vpDot2> dot(4);
55  dot[0].initTracking(I, vpImagePoint(193, 157));
56  dot[1].initTracking(I, vpImagePoint(203, 366));
57  dot[2].initTracking(I, vpImagePoint(313, 402));
58  dot[3].initTracking(I, vpImagePoint(304, 133));
59  std::vector<vpPoint> point;
60  point.push_back( vpPoint(-0.06, -0.06, 0) );
61  point.push_back( vpPoint( 0.06, -0.06, 0) );
62  point.push_back( vpPoint( 0.06, 0.06, 0) );
63  point.push_back( vpPoint(-0.06, 0.06, 0) );
65  bool init = true;
66 
67  while(1){
68  vpImageIo::read(I, "square.pgm");
70  for (unsigned int i=0; i < dot.size(); i ++) {
71  dot[i].setGraphics(true);
72  dot[i].track(I);
73  }
74  computePose(point, dot, cam, init, cMo);
75  vpDisplay::displayFrame(I, cMo, cam, 0.05, vpColor::none);
77 
78  if (init) init = false; // turn off pose initialisation
79 
80  if (vpDisplay::getClick(I, false)) break;
81 
82  vpTime::wait(40);
83  }
84  }
85  catch(vpException e) {
86  std::cout << "Catch an exception: " << e << std::endl;
87  }
88 }
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:150
unsigned int getWidth() const
Definition: vpImage.h:161
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:128
Define the X11 console to display images.
Definition: vpDisplayX.h:148
static const vpColor none
Definition: vpColor.h:175
error that can be emited by ViSP classes.
Definition: vpException.h:73
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)
Definition: vpDisplay.cpp:2233
Class that defines what is a point.
Definition: vpPoint.h:59
bool computePose(vpPoseMethodType methode, vpHomogeneousMatrix &cMo, bool(*func)(vpHomogeneousMatrix *)=NULL)
compute the pose for a given method
Definition: vpPose.cpp:382
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
The vpDisplayOpenCV allows to display image using the opencv library.
Class used for pose computation from N points (pose from point only).
Definition: vpPose.h:74
Generic class defining intrinsic camera parameters.
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, vpImagePoint offset=vpImagePoint(0, 0))
Definition: vpDisplay.cpp:373
unsigned int getHeight() const
Definition: vpImage.h:152
virtual bool getClick(bool blocking=true)=0
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:274
void addPoint(const vpPoint &P)
Add a new point in this array.
Definition: vpPose.cpp:151
double computeResidual(const vpHomogeneousMatrix &cMo) const
Compute and return the residual expressed in meter for the pose matrix 'cMo'.
Definition: vpPose.cpp:340