Visual Servoing Platform  version 3.5.1 under development (2022-07-06)
tutorial-pose-from-qrcode-image.cpp
1 
2 #include <visp3/core/vpIoTools.h>
3 #include <visp3/core/vpPixelMeterConversion.h>
4 #include <visp3/detection/vpDetectorQRCode.h>
5 #include <visp3/gui/vpDisplayGDI.h>
6 #include <visp3/gui/vpDisplayOpenCV.h>
7 #include <visp3/gui/vpDisplayX.h>
8 #include <visp3/io/vpImageIo.h>
9 #include <visp3/vision/vpPose.h>
10 
11 #include "pose_helper.h"
12 
13 int main(int, char *argv[])
14 {
15 #if defined(VISP_HAVE_ZBAR)
16  try {
18  vpImageIo::read(I, vpIoTools::getParent(argv[0]) + "/data/bar-code.pgm");
19 
20 #if defined(VISP_HAVE_X11)
21  vpDisplayX d(I);
22 #elif defined(VISP_HAVE_GDI)
23  vpDisplayGDI d(I);
24 #elif defined(VISP_HAVE_OPENCV)
25  vpDisplayOpenCV d(I);
26 #endif
27 
28  // Camera parameters should be adapted to your camera
29  vpCameraParameters cam(840, 840, I.getWidth() / 2, I.getHeight() / 2);
30 
31  // 3D model of the QRcode: here we consider a 12cm by 12cm QRcode
32  std::vector<vpPoint> point;
33  point.push_back(vpPoint(-0.06, -0.06, 0)); // QRcode point 0 3D coordinates in plane Z=0
34  point.push_back(vpPoint(0.06, -0.06, 0)); // QRcode point 1 3D coordinates in plane Z=0
35  point.push_back(vpPoint(0.06, 0.06, 0)); // QRcode point 2 3D coordinates in plane Z=0
36  point.push_back(vpPoint(-0.06, 0.06, 0)); // QRcode point 3 3D coordinates in plane Z=0
37 
39  bool init = true;
40 
41  vpDetectorQRCode detector;
42 
43  while (1) {
44  vpImageIo::read(I, vpIoTools::getParent(argv[0]) + "/data/bar-code.pgm");
46 
47  bool status = detector.detect(I);
48 
49  std::ostringstream legend;
50  legend << detector.getNbObjects() << " bar code detected";
51  vpDisplay::displayText(I, (int)I.getHeight() - 30, 10, legend.str(), vpColor::red);
52 
53  if (status) { // true if at least one QRcode is detected
54  for (size_t i = 0; i < detector.getNbObjects(); i++) {
55 
56  std::vector<vpImagePoint> p = detector.getPolygon(i); // get the four corners location in the image
57 
58  for (size_t j = 0; j < p.size(); j++) {
59  vpDisplay::displayCross(I, p[j], 14, vpColor::red, 3);
60  std::ostringstream number;
61  number << j;
62  vpDisplay::displayText(I, p[j] + vpImagePoint(15, 5), number.str(), vpColor::blue);
63  }
64 
65  computePose(point, p, cam, init, cMo); // resulting pose is available in cMo var
66  std::cout << "Pose translation (meter): " << cMo.getTranslationVector().t() << std::endl
67  << "Pose rotation (quaternion): " << vpQuaternionVector(cMo.getRotationMatrix()).t() << std::endl;
68  vpDisplay::displayFrame(I, cMo, cam, 0.05, vpColor::none, 3);
69  }
70  }
71  vpDisplay::displayText(I, (int)I.getHeight() - 15, 10, "A click to quit...", vpColor::red);
73 
74  if (vpDisplay::getClick(I, false))
75  break;
76 
77  vpTime::wait(40);
78  }
79  } catch (const vpException &e) {
80  std::cout << "Catch an exception: " << e.getMessage() << std::endl;
81  }
82 #else
83  (void)argv;
84  std::cout << "ViSP is not build with zbar 3rd party." << std::endl;
85 #endif
86 }
Generic class defining intrinsic camera parameters.
static const vpColor red
Definition: vpColor.h:217
static const vpColor none
Definition: vpColor.h:229
static const vpColor blue
Definition: vpColor.h:223
std::vector< std::vector< vpImagePoint > > & getPolygon()
size_t getNbObjects() const
bool detect(const vpImage< unsigned char > &I)
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:135
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
static void flush(const vpImage< unsigned char > &I)
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))
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emited by ViSP classes.
Definition: vpException.h:72
const char * getMessage() const
Definition: vpException.cpp:87
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpRotationMatrix getRotationMatrix() const
vpTranslationVector getTranslationVector() const
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:148
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:89
unsigned int getWidth() const
Definition: vpImage.h:246
unsigned int getHeight() const
Definition: vpImage.h:188
void init(unsigned int h, unsigned int w, Type value)
Definition: vpImage.h:631
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1620
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:82
Implementation of a rotation vector as quaternion angle minimal representation.
vpRowVector t() const
vpRowVector t() const
VISP_EXPORT int wait(double t0, double t)