Visual Servoing Platform  version 3.1.0
tutorial-pose-from-points-tracking.cpp
1 
2 #include <visp3/core/vpConfig.h>
3 #ifdef VISP_HAVE_MODULE_SENSOR
4 #include <visp3/sensor/vp1394CMUGrabber.h>
5 #include <visp3/sensor/vp1394TwoGrabber.h>
6 #endif
7 #include <visp3/blob/vpDot2.h>
8 #include <visp3/core/vpPixelMeterConversion.h>
9 #include <visp3/gui/vpDisplayGDI.h>
10 #include <visp3/gui/vpDisplayOpenCV.h>
11 #include <visp3/gui/vpDisplayX.h>
12 #include <visp3/vision/vpPose.h>
13 
14 void computePose(std::vector<vpPoint> &point, const std::vector<vpDot2> &dot, const vpCameraParameters &cam, bool init,
15  vpHomogeneousMatrix &cMo);
16 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV)
17 void track(vpImage<unsigned char> &I, std::vector<vpDot2> &dot, bool init);
18 #endif
19 
20 void computePose(std::vector<vpPoint> &point, const std::vector<vpDot2> &dot, const vpCameraParameters &cam, bool init,
22 {
23  vpPose pose;
24  double x = 0, y = 0;
25  for (unsigned int i = 0; i < point.size(); i++) {
26  vpPixelMeterConversion::convertPoint(cam, dot[i].getCog(), x, y);
27  point[i].set_x(x);
28  point[i].set_y(y);
29  pose.addPoint(point[i]);
30  }
31 
32  if (init == true) {
33  vpHomogeneousMatrix cMo_dem;
34  vpHomogeneousMatrix cMo_lag;
35  pose.computePose(vpPose::DEMENTHON, cMo_dem);
36  pose.computePose(vpPose::LAGRANGE, cMo_lag);
37  double residual_dem = pose.computeResidual(cMo_dem);
38  double residual_lag = pose.computeResidual(cMo_lag);
39  if (residual_dem < residual_lag)
40  cMo = cMo_dem;
41  else
42  cMo = cMo_lag;
43  }
45 }
46 
47 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV)
48 void track(vpImage<unsigned char> &I, std::vector<vpDot2> &dot, bool init)
49 {
50  if (init) {
52  for (unsigned int i = 0; i < dot.size(); i++) {
53  dot[i].setGraphics(true);
54  dot[i].setGraphicsThickness(2);
55  dot[i].initTracking(I);
57  }
58  } else {
59  for (unsigned int i = 0; i < dot.size(); i++) {
60  dot[i].track(I);
61  }
62  }
63 }
64 #endif
65 
66 int main()
67 {
68 #if (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV)) && \
69  (defined(VISP_HAVE_DC1394) || defined(VISP_HAVE_CMU1394) || (VISP_HAVE_OPENCV_VERSION >= 0x020100))
70  try {
72 
73 #if defined(VISP_HAVE_DC1394)
75  g.open(I);
76 #elif defined(VISP_HAVE_CMU1394)
78  g.open(I);
79 #elif defined(VISP_HAVE_OPENCV)
80  cv::VideoCapture g(0); // open the default camera
81  if (!g.isOpened()) { // check if we succeeded
82  std::cout << "Failed to open the camera" << std::endl;
83  return -1;
84  }
85  cv::Mat frame;
86  g >> frame; // get a new frame from camera
87  vpImageConvert::convert(frame, I);
88 #endif
89 
90  // Parameters of our camera
91  vpCameraParameters cam(840, 840, I.getWidth() / 2, I.getHeight() / 2);
92 
93  // The pose container
95 
96  std::vector<vpDot2> dot(4);
97  std::vector<vpPoint> point;
98  double L = 0.06;
99  point.push_back(vpPoint(-L, -L, 0));
100  point.push_back(vpPoint(L, -L, 0));
101  point.push_back(vpPoint(L, L, 0));
102  point.push_back(vpPoint(-L, L, 0));
103 
104  bool init = true;
105 #if defined(VISP_HAVE_X11)
106  vpDisplayX d(I);
107 #elif defined(VISP_HAVE_GDI)
108  vpDisplayGDI d(I);
109 #elif defined(VISP_HAVE_OPENCV)
110  vpDisplayOpenCV d(I);
111 #endif
112 
113  while (1) {
114 // Image Acquisition
115 #if defined(VISP_HAVE_DC1394) || defined(VISP_HAVE_CMU1394)
116  g.acquire(I);
117 #elif defined(VISP_HAVE_OPENCV)
118  g >> frame;
119  vpImageConvert::convert(frame, I);
120 #endif
121 
123  track(I, dot, init);
124  computePose(point, dot, cam, init, cMo);
125  vpDisplay::displayFrame(I, cMo, cam, 0.05, vpColor::none, 3);
126  vpDisplay::flush(I);
127  if (init)
128  init = false; // turn off the initialisation specific stuff
129 
130  if (vpDisplay::getClick(I, false))
131  break;
132  }
133  } catch (vpException &e) {
134  std::cout << "Catch an exception: " << e << std::endl;
135  }
136 #endif
137 }
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Implementation of an homogeneous matrix and operations on such kind of matrices.
void open(vpImage< unsigned char > &I)
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
void open(vpImage< unsigned char > &I)
Firewire cameras video capture based on CMU 1394 Digital Camera SDK.
void acquire(vpImage< unsigned char > &I)
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
unsigned int getHeight() const
Definition: vpImage.h:178
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 for firewire ieee1394 video devices using libdc1394-2.x api.
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