Visual Servoing Platform  version 3.6.1 under development (2025-02-18)
tutorial-pose-from-qrcode-image.cpp
1 
2 #include <visp3/core/vpConfig.h>
3 #include <visp3/core/vpIoTools.h>
4 #include <visp3/core/vpPixelMeterConversion.h>
5 #include <visp3/detection/vpDetectorQRCode.h>
6 #include <visp3/gui/vpDisplayFactory.h>
7 #include <visp3/io/vpImageIo.h>
8 #include <visp3/vision/vpPose.h>
9 
10 #include "pose_helper.h"
11 
12 int main(int, char *argv[])
13 {
14 #if defined(VISP_HAVE_ZBAR)
15 #ifdef ENABLE_VISP_NAMESPACE
16  using namespace VISP_NAMESPACE_NAME;
17 #endif
18 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
19  std::shared_ptr<vpDisplay> display;
20 #else
21  vpDisplay *display = nullptr;
22 #endif
23  try {
25  vpImageIo::read(I, vpIoTools::getParent(argv[0]) + "/data/bar-code.pgm");
26 
27 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
29 #else
31 #endif
32 
33  // Camera parameters should be adapted to your camera
34  vpCameraParameters cam(840, 840, I.getWidth() / 2, I.getHeight() / 2);
35 
36  // 3D model of the QRcode: here we consider a 12cm by 12cm QRcode
37  std::vector<vpPoint> point;
38  point.push_back(vpPoint(-0.06, -0.06, 0)); // QRcode point 0 3D coordinates in plane Z=0
39  point.push_back(vpPoint(0.06, -0.06, 0)); // QRcode point 1 3D coordinates in plane Z=0
40  point.push_back(vpPoint(0.06, 0.06, 0)); // QRcode point 2 3D coordinates in plane Z=0
41  point.push_back(vpPoint(-0.06, 0.06, 0)); // QRcode point 3 3D coordinates in plane Z=0
42 
44  bool init = true;
45 
46  vpDetectorQRCode detector;
47 
48  while (1) {
49  vpImageIo::read(I, vpIoTools::getParent(argv[0]) + "/data/bar-code.pgm");
51 
52  bool status = detector.detect(I);
53 
54  std::ostringstream legend;
55  legend << detector.getNbObjects() << " bar code detected";
56  vpDisplay::displayText(I, (int)I.getHeight() - 30, 10, legend.str(), vpColor::red);
57 
58  if (status) { // true if at least one QRcode is detected
59  for (size_t i = 0; i < detector.getNbObjects(); i++) {
60 
61  std::vector<vpImagePoint> p = detector.getPolygon(i); // get the four corners location in the image
62 
63  for (size_t j = 0; j < p.size(); j++) {
64  vpDisplay::displayCross(I, p[j], 14, vpColor::red, 3);
65  std::ostringstream number;
66  number << j;
67  vpDisplay::displayText(I, p[j] + vpImagePoint(15, 5), number.str(), vpColor::blue);
68  }
69 
70  computePose(point, p, cam, init, cMo); // resulting pose is available in cMo var
71  std::cout << "Pose translation (meter): " << cMo.getTranslationVector().t() << std::endl
72  << "Pose rotation (quaternion): " << vpQuaternionVector(cMo.getRotationMatrix()).t() << std::endl;
73  vpDisplay::displayFrame(I, cMo, cam, 0.05, vpColor::none, 3);
74  }
75  }
76  vpDisplay::displayText(I, (int)I.getHeight() - 15, 10, "A click to quit...", vpColor::red);
78 
79  if (vpDisplay::getClick(I, false))
80  break;
81 
82  vpTime::wait(40);
83  }
84  }
85  catch (const vpException &e) {
86  std::cout << "Catch an exception: " << e.getMessage() << std::endl;
87  }
88 
89 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
90  if (display != nullptr) {
91  delete display;
92 }
93 #endif
94 #else
95  (void)argv;
96  std::cout << "ViSP is not build with zbar 3rd party." << std::endl;
97 #endif
98 }
Generic class defining intrinsic camera parameters.
static const vpColor red
Definition: vpColor.h:198
static const vpColor none
Definition: vpColor.h:210
static const vpColor blue
Definition: vpColor.h:204
Class that defines generic functionalities for display.
Definition: vpDisplay.h:178
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(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), const std::string &frameName="", const vpColor &textColor=vpColor::black, const vpImagePoint &textOffset=vpImagePoint(15, 15))
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 displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
Definition: vpException.h:60
const char * getMessage() const
Definition: vpException.cpp:65
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:147
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1314
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:79
Implementation of a rotation vector as quaternion angle minimal representation.
vpRowVector t() const
vpRowVector t() const
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.
VISP_EXPORT int wait(double t0, double t)