ViSP  2.9.0
tutorial-pose-from-points-image.cpp
1 
2 #include <visp/vpDisplayGDI.h>
3 #include <visp/vpDisplayX.h>
4 #include <visp/vpDot2.h>
5 #include <visp/vpImageIo.h>
6 #include <visp/vpPixelMeterConversion.h>
7 #include <visp/vpPose.h>
8 
9 void computePose(std::vector<vpPoint> &point, const std::vector<vpDot2> &dot,
10  const vpCameraParameters &cam, bool init, vpHomogeneousMatrix &cMo);
11 
12 void computePose(std::vector<vpPoint> &point, const std::vector<vpDot2> &dot,
13  const vpCameraParameters &cam, bool init, vpHomogeneousMatrix &cMo)
14 {
15  vpPose pose; double x=0, y=0;
16  for (unsigned int i=0; i < point.size(); i ++) {
17  vpPixelMeterConversion::convertPoint(cam, dot[i].getCog(), x, y);
18  point[i].set_x(x);
19  point[i].set_y(y);
20  pose.addPoint(point[i]);
21  }
22 
23  if (init == true) pose.computePose(vpPose::DEMENTHON_VIRTUAL_VS, cMo);
24  else pose.computePose(vpPose::VIRTUAL_VS, cMo) ;
25 }
26 
27 int main()
28 {
29  try {
31  vpImageIo::read(I, "square.pgm");
32 
33 #if defined(VISP_HAVE_X11)
34  vpDisplayX d(I);
35 #elif defined(VISP_HAVE_GDI)
36  vpDisplayGDI d(I);
37 #endif
38 
39  vpCameraParameters cam(840, 840, I.getWidth()/2, I.getHeight()/2);
40  std::vector<vpDot2> dot(4);
41  dot[0].initTracking(I, vpImagePoint(193, 157));
42  dot[1].initTracking(I, vpImagePoint(203, 366));
43  dot[2].initTracking(I, vpImagePoint(313, 402));
44  dot[3].initTracking(I, vpImagePoint(304, 133));
45  std::vector<vpPoint> point(4);
46  point[0].setWorldCoordinates(-0.06, -0.06, 0);
47  point[1].setWorldCoordinates( 0.06, -0.06, 0);
48  point[2].setWorldCoordinates( 0.06, 0.06, 0);
49  point[3].setWorldCoordinates(-0.06, 0.06, 0);
51  bool init = true;
52 
53  while(1){
54  vpImageIo::read(I, "square.pgm");
56  for (unsigned int i=0; i < dot.size(); i ++) {
57  dot[i].setGraphics(true);
58  dot[i].track(I);
59  }
60  computePose(point, dot, cam, init, cMo);
61  vpDisplay::displayFrame(I, cMo, cam, 0.05, vpColor::none);
63 
64  if (init) init = false; // turn off pose initialisation
65 
66  if (vpDisplay::getClick(I, false)) break;
67 
68  vpTime::wait(40);
69  }
70  }
71  catch(vpException e) {
72  std::cout << "Catch an exception: " << e << std::endl;
73  }
74 }
unsigned int getWidth() const
Definition: vpImage.h:159
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:132
Define the X11 console to display images.
Definition: vpDisplayX.h:152
static const vpColor none
Definition: vpColor.h:179
error that can be emited by ViSP classes.
Definition: vpException.h:76
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 int wait(double t0, double t)
Definition: vpTime.cpp:149
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1994
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
Class used for pose computation from N points (pose from point only).
Definition: vpPose.h:78
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, unsigned int thickness=1)
Definition: vpDisplay.cpp:371
unsigned int getHeight() const
Definition: vpImage.h:150
void computePose(vpPoseMethodType methode, vpHomogeneousMatrix &cMo)
compute the pose for a given method
Definition: vpPose.cpp:386
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:92
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:278
void addPoint(const vpPoint &P)
Add a new point in this array.
Definition: vpPose.cpp:155