Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
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 #include "pose_helper.h"
11 
12 int main()
13 {
14 #if defined(VISP_HAVE_ZBAR)
15  try {
17  vpImageIo::read(I, "bar-code.pgm");
18 
19 #if defined(VISP_HAVE_X11)
20  vpDisplayX d(I);
21 #elif defined(VISP_HAVE_GDI)
22  vpDisplayGDI d(I);
23 #elif defined(VISP_HAVE_OPENCV)
24  vpDisplayOpenCV d(I);
25 #endif
26 
27  // Camera parameters should be adapted to your camera
28  vpCameraParameters cam(840, 840, I.getWidth() / 2, I.getHeight() / 2);
29 
30  // 3D model of the QRcode: here we consider a 12cm by 12cm QRcode
31  std::vector<vpPoint> point;
32  point.push_back(vpPoint(-0.06, -0.06, 0)); // QRcode point 0 3D coordinates in plane Z=0
33  point.push_back(vpPoint( 0.06, -0.06, 0)); // QRcode point 1 3D coordinates in plane Z=0
34  point.push_back(vpPoint( 0.06, 0.06, 0)); // QRcode point 2 3D coordinates in plane Z=0
35  point.push_back(vpPoint(-0.06, 0.06, 0)); // QRcode point 3 3D coordinates in plane Z=0
36 
38  bool init = true;
39 
40  vpDetectorQRCode detector;
41 
42  while (1) {
43  vpImageIo::read(I, "bar-code.pgm");
45 
46  bool status = detector.detect(I);
47 
48  std::ostringstream legend;
49  legend << detector.getNbObjects() << " bar code detected";
50  vpDisplay::displayText(I, (int)I.getHeight() - 30, 10, legend.str(), vpColor::red);
51 
52  if (status) { // true if at least one QRcode is detected
53  for (size_t i = 0; i < detector.getNbObjects(); i++) {
54 
55  std::vector<vpImagePoint> p = detector.getPolygon(i); // get the four corners location in the image
56 
57  for (size_t j = 0; j < p.size(); j++) {
58  vpDisplay::displayCross(I, p[j], 14, vpColor::red, 3);
59  std::ostringstream number;
60  number << j;
61  vpDisplay::displayText(I, p[j] + vpImagePoint(15, 5), number.str(), vpColor::blue);
62  }
63 
64  computePose(point, p, cam, init, cMo); // resulting pose is available in cMo var
65  std::cout << "Pose translation (meter): " << cMo.getTranslationVector().t() << std::endl
66  << "Pose rotation (quaternion): " << vpQuaternionVector(cMo.getRotationMatrix()).t() << std::endl;
67  vpDisplay::displayFrame(I, cMo, cam, 0.05, vpColor::none, 3);
68  }
69  }
70  vpDisplay::displayText(I, (int)I.getHeight() - 15, 10, "A click to quit...", vpColor::red);
72 
73  if (vpDisplay::getClick(I, false))
74  break;
75 
76  vpTime::wait(40);
77  }
78  } catch (const vpException &e) {
79  std::cout << "Catch an exception: " << e.getMessage() << std::endl;
80  }
81 #else
82  std::cout << "ViSP is not build with zbar 3rd party." << std::endl;
83 #endif
84 }
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:150
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
unsigned int getWidth() const
Definition: vpImage.h:239
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
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)
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
vpRotationMatrix getRotationMatrix() const
vpTranslationVector getTranslationVector() const
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Generic class defining intrinsic camera parameters.
Implementation of a rotation vector as quaternion angle minimal representation.
const char * getMessage(void) const
Definition: vpException.cpp:90
vpRowVector t() const
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: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))
unsigned int getHeight() const
Definition: vpImage.h:178
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
static const vpColor blue
Definition: vpColor.h:186
std::vector< std::vector< vpImagePoint > > & getPolygon()