Visual Servoing Platform  version 3.6.1 under development (2025-02-19)
tutorial-barcode-detector.cpp
1 #include <visp3/detection/vpDetectorDataMatrixCode.h>
4 #include <visp3/detection/vpDetectorQRCode.h>
6 #include <visp3/gui/vpDisplayFactory.h>
7 #include <visp3/io/vpImageIo.h>
8 
9 int main(int argc, const char **argv)
10 {
12 #if (defined(VISP_HAVE_ZBAR) || defined(VISP_HAVE_DMTX)) && \
13  defined(VISP_HAVE_DISPLAY)
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, "bar-code.pgm");
26 
27 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
29 #else
31 #endif
32 
34  vpDetectorBase *detector = nullptr;
36 
37 #if (defined(VISP_HAVE_ZBAR) && defined(VISP_HAVE_DMTX))
38  int opt_barcode = 0; // 0=QRCode, 1=DataMatrix
39 
40  for (int i = 1; i < argc; i++) {
41  if (std::string(argv[i]) == "--code-type" && i + 1 < argc) {
42  opt_barcode = atoi(argv[++i]);
43  }
44  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
45  std::cout << "Usage: " << argv[0] << " [--code-type <0 for QR code | 1 for DataMatrix code>] [--help] [-h]"
46  << std::endl;
47  return EXIT_SUCCESS;
48  }
49  }
51  if (opt_barcode == 0)
52  detector = new vpDetectorQRCode;
53  else
54  detector = new vpDetectorDataMatrixCode;
56 #elif defined(VISP_HAVE_ZBAR)
57  detector = new vpDetectorQRCode;
58  (void)argc;
59  (void)argv;
60 #elif defined(VISP_HAVE_DMTX)
61  detector = new vpDetectorDataMatrixCode;
62  (void)argc;
63  (void)argv;
64 #endif
65 
67 
69  bool status = detector->detect(I);
71  std::ostringstream legend;
72  legend << detector->getNbObjects() << " bar code detected";
73  vpDisplay::displayText(I, (int)I.getHeight() - 30, 10, legend.str(), vpColor::red);
74 
76  if (status) {
77  for (size_t i = 0; i < detector->getNbObjects(); i++) {
80  std::vector<vpImagePoint> p = detector->getPolygon(i);
81  vpRect bbox = detector->getBBox(i);
85  vpDisplay::displayText(I, (int)(bbox.getTop() - 10), (int)bbox.getLeft(),
86  "Message: \"" + detector->getMessage(i) + "\"", vpColor::red);
88  for (size_t j = 0; j < p.size(); j++) {
89  vpDisplay::displayCross(I, p[j], 14, vpColor::red, 3);
90  std::ostringstream number;
91  number << j;
92  vpDisplay::displayText(I, p[j] + vpImagePoint(15, 5), number.str(), vpColor::blue);
93  }
94  }
95 
96  vpDisplay::displayText(I, (int)I.getHeight() - 15, 10, "A click to quit...", vpColor::red);
99  }
100  delete detector;
101  }
102  catch (const vpException &e) {
103  std::cout << "Catch an exception: " << e.getMessage() << std::endl;
104  }
105 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
106  if (display != nullptr) {
107  delete display;
108 }
109 #endif
110 #else
111  (void)argc;
112  (void)argv;
113 #endif
114  }
static const vpColor red
Definition: vpColor.h:198
static const vpColor blue
Definition: vpColor.h:204
static const vpColor green
Definition: vpColor.h:201
std::vector< std::vector< vpImagePoint > > & getPolygon()
vpRect getBBox(size_t i) const
size_t getNbObjects() const
std::vector< std::string > & getMessage()
virtual bool detect(const vpImage< unsigned char > &I)=0
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 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 displayRectangle(const vpImage< unsigned char > &I, const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)
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
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 getHeight() const
Definition: vpImage.h:181
Defines a rectangle in the plane.
Definition: vpRect.h:79
double getLeft() const
Definition: vpRect.h:173
double getTop() const
Definition: vpRect.h:192
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.