Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
tutorial-pose-from-qrcode-image.cpp
1 
2 #include <visp3/gui/vpDisplayGDI.h>
3 #include <visp3/gui/vpDisplayOpenCV.h>
4 #include <visp3/gui/vpDisplayX.h>
5 #include <visp3/io/vpImageIo.h>
6 #include <visp3/core/vpPixelMeterConversion.h>
7 #include <visp3/vision/vpPose.h>
8 #include <visp3/detection/vpDetectorQRCode.h>
9 
10 void computePose(std::vector<vpPoint> &point, const std::vector<vpImagePoint> &ip,
11  const vpCameraParameters &cam, bool init, vpHomogeneousMatrix &cMo)
12 {
13  vpPose pose; double x=0, y=0;
14  for (unsigned int i=0; i < point.size(); i ++) {
15  vpPixelMeterConversion::convertPoint(cam, ip[i], x, y);
16  point[i].set_x(x);
17  point[i].set_y(y);
18  pose.addPoint(point[i]);
19  }
20 
21  if (init == true) {
22  vpHomogeneousMatrix cMo_dem;
23  vpHomogeneousMatrix cMo_lag;
24  pose.computePose(vpPose::DEMENTHON, cMo_dem);
25  pose.computePose(vpPose::LAGRANGE, cMo_lag);
26  double residual_dem = pose.computeResidual(cMo_dem);
27  double residual_lag = pose.computeResidual(cMo_lag);
28  if (residual_dem < residual_lag)
29  cMo = cMo_dem;
30  else
31  cMo = cMo_lag;
32  }
34 }
35 
36 int main()
37 {
38 #if defined(VISP_HAVE_ZBAR)
39  try {
41  vpImageIo::read(I, "bar-code.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  // Camera parameters should be adapted to your camera
52  vpCameraParameters cam(840, 840, I.getWidth()/2, I.getHeight()/2);
53 
54  // 3D model of the QRcode: here we consider a 12cm by 12cm QRcode
55  std::vector<vpPoint> point;
56  point.push_back( vpPoint(-0.06, -0.06, 0) ); // QCcode point 0 3D coordinates in plane Z=0
57  point.push_back( vpPoint( 0.06, -0.06, 0) ); // QCcode point 1 3D coordinates in plane Z=0
58  point.push_back( vpPoint( 0.06, 0.06, 0) ); // QCcode point 2 3D coordinates in plane Z=0
59  point.push_back( vpPoint(-0.06, 0.06, 0) ); // QCcode point 3 3D coordinates in plane Z=0
60 
62  bool init = true;
63 
64  vpDetectorQRCode detector;
65 
66  while(1) {
67  vpImageIo::read(I, "bar-code.pgm");
69 
70  bool status = detector.detect(I);
71 
72  std::ostringstream legend;
73  legend << detector.getNbObjects() << " bar code detected";
74  vpDisplay::displayText(I, (int)I.getHeight()-30, 10, legend.str(), vpColor::red);
75 
76  if (status) { // true if at least one QRcode is detected
77  for(size_t i=0; i < detector.getNbObjects(); i++) {
78 
79  std::vector<vpImagePoint> p = detector.getPolygon(i); // get the four corners location in the image
80 
81  for(size_t j=0; j < p.size(); j++) {
82  vpDisplay::displayCross(I, p[j], 14, vpColor::red, 3);
83  std::ostringstream number;
84  number << j;
85  vpDisplay::displayText(I, p[j]+vpImagePoint(15,5), number.str(), vpColor::blue);
86  }
87 
88  computePose(point, p, cam, init, cMo); // resulting pose is available in cMo var
89  std::cout << "Pose translation (meter): " << cMo.getTranslationVector().t() << std::endl
90  << "Pose rotation (quaternion): " << vpQuaternionVector(cMo.getRotationMatrix()).t() << std::endl;
91  vpDisplay::displayFrame(I, cMo, cam, 0.05, vpColor::none, 3);
92  }
93  }
94  vpDisplay::displayText(I, (int)I.getHeight()-15, 10, "A click to quit...", vpColor::red);
96 
97  if (vpDisplay::getClick(I, false)) break;
98 
99  vpTime::wait(40);
100  }
101  }
102  catch(const vpException &e) {
103  std::cout << "Catch an exception: " << e.getMessage() << std::endl;
104  }
105 #else
106  std::cout << "ViSP is not build with zbar 3rd party." << std::endl;
107 #endif
108 }
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:157
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
unsigned int getWidth() const
Definition: vpImage.h:226
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
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:153
static const vpColor none
Definition: vpColor.h:175
error that can be emited by ViSP classes.
Definition: vpException.h:73
bool detect(const vpImage< unsigned char > &I)
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...
size_t getNbObjects() const
static void flush(const vpImage< unsigned char > &I)
static const vpColor red
Definition: vpColor.h:163
Class that defines what is a point.
Definition: vpPoint.h:59
vpRotationMatrix getRotationMatrix() const
vpTranslationVector getTranslationVector() const
std::vector< std::vector< vpImagePoint > > & getPolygon()
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:76
Generic class defining intrinsic camera parameters.
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, bool(*func)(vpHomogeneousMatrix *)=NULL)
Definition: vpPose.cpp:372
Implementation of a rotation vector as quaternion angle minimal representation.
const char * getMessage(void) const
Definition: vpException.cpp:97
vpRowVector t() const
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))
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:205
unsigned int getHeight() const
Definition: vpImage.h:175
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
void addPoint(const vpPoint &P)
Definition: vpPose.cpp:145
double computeResidual(const vpHomogeneousMatrix &cMo) const
Compute and return the residual expressed in meter for the pose matrix 'cMo'.
Definition: vpPose.cpp:337
static const vpColor blue
Definition: vpColor.h:169