Visual Servoing Platform  version 3.1.0
tutorial-pose-from-qrcode-image.cpp
1 
2 #include <visp3/core/vpPixelMeterConversion.h>
3 #include <visp3/detection/vpDetectorQRCode.h>
4 #include <visp3/gui/vpDisplayGDI.h>
5 #include <visp3/gui/vpDisplayOpenCV.h>
6 #include <visp3/gui/vpDisplayX.h>
7 #include <visp3/io/vpImageIo.h>
8 #include <visp3/vision/vpPose.h>
9 
10 void computePose(std::vector<vpPoint> &point, const std::vector<vpImagePoint> &ip, const vpCameraParameters &cam,
11  bool init, vpHomogeneousMatrix &cMo)
12 {
13  vpPose pose;
14  double x = 0, y = 0;
15  for (unsigned int i = 0; i < point.size(); i++) {
16  vpPixelMeterConversion::convertPoint(cam, ip[i], x, y);
17  point[i].set_x(x);
18  point[i].set_y(y);
19  pose.addPoint(point[i]);
20  }
21 
22  if (init == true) {
23  vpHomogeneousMatrix cMo_dem;
24  vpHomogeneousMatrix cMo_lag;
25  pose.computePose(vpPose::DEMENTHON, cMo_dem);
26  pose.computePose(vpPose::LAGRANGE, cMo_lag);
27  double residual_dem = pose.computeResidual(cMo_dem);
28  double residual_lag = pose.computeResidual(cMo_lag);
29  if (residual_dem < residual_lag)
30  cMo = cMo_dem;
31  else
32  cMo = cMo_lag;
33  }
35 }
36 
37 int main()
38 {
39 #if defined(VISP_HAVE_ZBAR)
40  try {
42  vpImageIo::read(I, "bar-code.pgm");
43 
44 #if defined(VISP_HAVE_X11)
45  vpDisplayX d(I);
46 #elif defined(VISP_HAVE_GDI)
47  vpDisplayGDI d(I);
48 #elif defined(VISP_HAVE_OPENCV)
49  vpDisplayOpenCV d(I);
50 #endif
51 
52  // Camera parameters should be adapted to your camera
53  vpCameraParameters cam(840, 840, I.getWidth() / 2, I.getHeight() / 2);
54 
55  // 3D model of the QRcode: here we consider a 12cm by 12cm QRcode
56  std::vector<vpPoint> point;
57  point.push_back(vpPoint(-0.06, -0.06,
58  0)); // QCcode point 0 3D coordinates in plane Z=0
59  point.push_back(vpPoint(0.06, -0.06,
60  0)); // QCcode point 1 3D coordinates in plane Z=0
61  point.push_back(vpPoint(0.06, 0.06, 0)); // QCcode point 2 3D coordinates in plane Z=0
62  point.push_back(vpPoint(-0.06, 0.06,
63  0)); // QCcode point 3 3D coordinates in plane Z=0
64 
66  bool init = true;
67 
68  vpDetectorQRCode detector;
69 
70  while (1) {
71  vpImageIo::read(I, "bar-code.pgm");
73 
74  bool status = detector.detect(I);
75 
76  std::ostringstream legend;
77  legend << detector.getNbObjects() << " bar code detected";
78  vpDisplay::displayText(I, (int)I.getHeight() - 30, 10, legend.str(), vpColor::red);
79 
80  if (status) { // true if at least one QRcode is detected
81  for (size_t i = 0; i < detector.getNbObjects(); i++) {
82 
83  std::vector<vpImagePoint> p = detector.getPolygon(i); // get the four corners location in the image
84 
85  for (size_t j = 0; j < p.size(); j++) {
86  vpDisplay::displayCross(I, p[j], 14, vpColor::red, 3);
87  std::ostringstream number;
88  number << j;
89  vpDisplay::displayText(I, p[j] + vpImagePoint(15, 5), number.str(), vpColor::blue);
90  }
91 
92  computePose(point, p, cam, init,
93  cMo); // resulting pose is available in cMo var
94  std::cout << "Pose translation (meter): " << cMo.getTranslationVector().t() << std::endl
95  << "Pose rotation (quaternion): " << vpQuaternionVector(cMo.getRotationMatrix()).t() << std::endl;
96  vpDisplay::displayFrame(I, cMo, cam, 0.05, vpColor::none, 3);
97  }
98  }
99  vpDisplay::displayText(I, (int)I.getHeight() - 15, 10, "A click to quit...", vpColor::red);
100  vpDisplay::flush(I);
101 
102  if (vpDisplay::getClick(I, false))
103  break;
104 
105  vpTime::wait(40);
106  }
107  } catch (const vpException &e) {
108  std::cout << "Catch an exception: " << e.getMessage() << std::endl;
109  }
110 #else
111  std::cout << "ViSP is not build with zbar 3rd party." << std::endl;
112 #endif
113 }
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:150
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
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:129
vpRowVector t() const
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:151
static const vpColor none
Definition: vpColor.h:192
error that can be emited by ViSP classes.
Definition: vpException.h:71
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:180
Class that defines what is a point.
Definition: vpPoint.h:58
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
Implementation of a rotation vector as quaternion angle minimal representation.
const char * getMessage(void) const
Definition: vpException.cpp:90
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
vpTranslationVector getTranslationVector() const
unsigned int getHeight() const
Definition: vpImage.h:178
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:207
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 that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
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
vpRotationMatrix getRotationMatrix() const
static const vpColor blue
Definition: vpColor.h:186
std::vector< std::vector< vpImagePoint > > & getPolygon()