ViSP  2.10.0
tutorial-pose-from-points-tracking.cpp
1 
2 #include <visp/vp1394CMUGrabber.h>
3 #include <visp/vp1394TwoGrabber.h>
4 #include <visp/vpDisplayGDI.h>
5 #include <visp/vpDisplayOpenCV.h>
6 #include <visp/vpDisplayX.h>
7 #include <visp/vpDot2.h>
8 #include <visp/vpPixelMeterConversion.h>
9 #include <visp/vpPose.h>
10 
11 void computePose(std::vector<vpPoint> &point, const std::vector<vpDot2> &dot,
12  const vpCameraParameters &cam, bool init, vpHomogeneousMatrix &cMo);
13 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV)
14 void track(vpImage<unsigned char> &I, std::vector<vpDot2> &dot, bool init);
15 #endif
16 
17 void computePose(std::vector<vpPoint> &point, const std::vector<vpDot2> &dot,
18  const vpCameraParameters &cam, bool init, vpHomogeneousMatrix &cMo)
19 {
20  vpPose pose; double x=0, y=0;
21  for (unsigned int i=0; i < point.size(); i ++) {
22  vpPixelMeterConversion::convertPoint(cam, dot[i].getCog(), x, y);
23  point[i].set_x(x);
24  point[i].set_y(y);
25  pose.addPoint(point[i]);
26  }
27 
28  if (init == true) {
29  vpHomogeneousMatrix cMo_dem;
30  vpHomogeneousMatrix cMo_lag;
31  pose.computePose(vpPose::DEMENTHON, cMo_dem);
32  pose.computePose(vpPose::LAGRANGE, cMo_lag);
33  double residual_dem = pose.computeResidual(cMo_dem);
34  double residual_lag = pose.computeResidual(cMo_lag);
35  if (residual_dem < residual_lag)
36  cMo = cMo_dem;
37  else
38  cMo = cMo_lag;
39  }
41 }
42 
43 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV)
44 void track(vpImage<unsigned char> &I, std::vector<vpDot2> &dot, bool init)
45 {
46  if (init) {
48  for(unsigned int i=0; i<dot.size(); i++) {
49  dot[i].setGraphics(true);
50  dot[i].setGraphicsThickness(2);
51  dot[i].initTracking(I);
53  }
54  }
55  else {
56  for(unsigned int i=0; i<dot.size(); i++) {
57  dot[i].track(I);
58  }
59  }
60 }
61 #endif
62 
63 int main()
64 {
65 #if (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV)) && (defined(VISP_HAVE_DC1394_2) || defined(VISP_HAVE_CMU1394) || (VISP_HAVE_OPENCV_VERSION >= 0x020100))
66  try { vpImage<unsigned char> I;
67 
68 #if defined(VISP_HAVE_DC1394_2)
70  g.open(I);
71 #elif defined(VISP_HAVE_CMU1394)
73  g.open(I);
74 #elif defined(VISP_HAVE_OPENCV)
75  cv::VideoCapture g(0); // open the default camera
76  if(!g.isOpened()) { // check if we succeeded
77  std::cout << "Failed to open the camera" << std::endl;
78  return -1;
79  }
80  cv::Mat frame;
81  g >> frame; // get a new frame from camera
82  vpImageConvert::convert(frame, I);
83 #endif
84 
85  // Parameters of our camera
86  vpCameraParameters cam(840, 840, I.getWidth()/2, I.getHeight()/2);
87 
88  // The pose container
90 
91  std::vector<vpDot2> dot(4);
92  std::vector<vpPoint> point(4);
93  double L = 0.06;
94  point[0].setWorldCoordinates(-L, -L, 0);
95  point[1].setWorldCoordinates( L, -L, 0);
96  point[2].setWorldCoordinates( L, L, 0);
97  point[3].setWorldCoordinates(-L, L, 0);
98 
99  bool init = true;
100 #if defined(VISP_HAVE_X11)
101  vpDisplayX d(I);
102 #elif defined(VISP_HAVE_GDI)
103  vpDisplayGDI d(I);
104 #elif defined(VISP_HAVE_OPENCV)
105  vpDisplayOpenCV d(I);
106 #endif
107 
108  while(1){
109  // Image Acquisition
110 #if defined(VISP_HAVE_DC1394_2) || defined(VISP_HAVE_CMU1394)
111  g.acquire(I);
112 #elif defined(VISP_HAVE_OPENCV)
113  g >> frame;
114  vpImageConvert::convert(frame, I);
115 #endif
116 
118  track(I, dot, init);
119  computePose(point, dot, cam, init, cMo);
120  vpDisplay::displayFrame(I, cMo, cam, 0.05, vpColor::none, 3);
121  vpDisplay::flush(I);
122  if (init) init = false; // turn off the initialisation specific stuff
123 
124  if (vpDisplay::getClick(I, false))
125  break;
126  }
127  }
128  catch(vpException e) {
129  std::cout << "Catch an exception: " << e << std::endl;
130  }
131 #endif
132 }
unsigned int getWidth() const
Definition: vpImage.h:161
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
void open(vpImage< unsigned char > &I)
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 void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2232
void open(vpImage< unsigned char > &I)
Firewire cameras video capture based on CMU 1394 Digital Camera SDK.
void acquire(vpImage< unsigned char > &I)
bool computePose(vpPoseMethodType methode, vpHomogeneousMatrix &cMo, bool(*func)(vpHomogeneousMatrix *)=NULL)
compute the pose for a given method
Definition: vpPose.cpp:386
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:210
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: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=vpColor::none, unsigned int thickness=1)
Definition: vpDisplay.cpp:375
unsigned int getHeight() const
Definition: vpImage.h:152
Class for firewire ieee1394 video devices using libdc1394-2.x api.
virtual bool getClick(bool blocking=true)=0
void addPoint(const vpPoint &P)
Add a new point in this array.
Definition: vpPose.cpp:155
double computeResidual(const vpHomogeneousMatrix &cMo) const
Compute and return the residual expressed in meter for the pose matrix 'cMo'.
Definition: vpPose.cpp:344