Visual Servoing Platform  version 3.6.1 under development (2024-04-16)
tutorial-barcode-detector-live.cpp
1 #include <visp3/core/vpConfig.h>
3 #include <visp3/core/vpImageConvert.h>
4 #include <visp3/detection/vpDetectorDataMatrixCode.h>
5 #include <visp3/detection/vpDetectorQRCode.h>
6 #include <visp3/gui/vpDisplayGDI.h>
7 #include <visp3/gui/vpDisplayOpenCV.h>
8 #include <visp3/gui/vpDisplayX.h>
9 #ifdef VISP_HAVE_MODULE_SENSOR
10 #include <visp3/sensor/vpV4l2Grabber.h>
11 #endif
12 
13 #if defined(HAVE_OPENCV_VIDEOIO)
14 #include <opencv2/videoio.hpp>
15 #endif
16 
17 int main(int argc, const char **argv)
18 {
19 #if (defined(VISP_HAVE_V4L2) || defined(HAVE_OPENCV_VIDEOIO)) && (defined(VISP_HAVE_ZBAR) || defined(VISP_HAVE_DMTX))
20  int opt_device = 0;
21  int opt_barcode = 0; // 0=QRCode, 1=DataMatrix
22 
23  for (int i = 0; i < argc; i++) {
24  if (std::string(argv[i]) == "--device")
25  opt_device = atoi(argv[i + 1]);
26  else if (std::string(argv[i]) == "--code-type")
27  opt_barcode = atoi(argv[i + 1]);
28  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
29  std::cout << "Usage: " << argv[0]
30  << " [--device <camera number>] [--code-type <0 for QR code | "
31  "1 for DataMatrix code>] [--help] [-h]"
32  << std::endl;
33  return EXIT_SUCCESS;
34  }
35  }
36  std::cout << "Use device: " << opt_device << std::endl;
37 
38  try {
39  vpImage<unsigned char> I; // for gray images
40 
42 #if defined(VISP_HAVE_V4L2)
43  vpV4l2Grabber g;
44  std::ostringstream device;
45  device << "/dev/video" << opt_device;
46  g.setDevice(device.str());
47  g.setScale(1);
48  g.acquire(I);
49 #elif defined(HAVE_OPENCV_VIDEOIO)
50  cv::VideoCapture cap(opt_device); // open the default camera
51  if (!cap.isOpened()) { // check if we succeeded
52  std::cout << "Failed to open the camera" << std::endl;
53  return EXIT_FAILURE;
54  }
55  cv::Mat frame;
56  cap >> frame; // get a new frame from camera
57  vpImageConvert::convert(frame, I);
58 #endif
60 
61 #if defined(VISP_HAVE_X11)
62  vpDisplayX d(I);
63 #elif defined(VISP_HAVE_GDI)
64  vpDisplayGDI d(I);
65 #elif defined(HAVE_OPENCV_HIGHGUI)
66  vpDisplayOpenCV d(I);
67 #endif
68  vpDisplay::setTitle(I, "ViSP viewer");
69 
70  vpDetectorBase *detector = nullptr;
71 #if (defined(VISP_HAVE_ZBAR) && defined(VISP_HAVE_DMTX))
72  if (opt_barcode == 0)
73  detector = new vpDetectorQRCode;
74  else
75  detector = new vpDetectorDataMatrixCode;
76 #elif defined(VISP_HAVE_ZBAR)
77  detector = new vpDetectorQRCode;
78  (void)opt_barcode;
79 #elif defined(VISP_HAVE_DMTX)
80  detector = new vpDetectorDataMatrixCode;
81  (void)opt_barcode;
82 #endif
83 
84  for (;;) {
86 #if defined(VISP_HAVE_V4L2)
87  g.acquire(I);
88 #elif defined(HAVE_OPENCV_VIDEOIO)
89  cap >> frame; // get a new frame from camera
90  vpImageConvert::convert(frame, I);
91 #endif
94 
95  bool status = detector->detect(I);
96  std::ostringstream legend;
97  legend << detector->getNbObjects() << " bar code detected";
98  vpDisplay::displayText(I, 10, 10, legend.str(), vpColor::red);
99 
100  if (status) {
101  for (size_t i = 0; i < detector->getNbObjects(); i++) {
102  std::vector<vpImagePoint> p = detector->getPolygon(i);
103  vpRect bbox = detector->getBBox(i);
105  vpDisplay::displayText(I, (int)bbox.getTop() - 20, (int)bbox.getLeft(),
106  "Message: \"" + detector->getMessage(i) + "\"", vpColor::red);
107  for (size_t j = 0; j < p.size(); j++) {
108  vpDisplay::displayCross(I, p[j], 14, vpColor::red, 3);
109  std::ostringstream number;
110  number << j;
111  vpDisplay::displayText(I, p[j] + vpImagePoint(10, 0), number.str(), vpColor::blue);
112  }
113  }
114  }
115 
116  vpDisplay::displayText(I, (int)I.getHeight() - 25, 10, "Click to quit...", vpColor::red);
117  vpDisplay::flush(I);
118  if (vpDisplay::getClick(I, false)) // a click to exit
119  break;
120  }
121  delete detector;
122  }
123  catch (const vpException &e) {
124  std::cout << "Catch an exception: " << e << std::endl;
125  }
126 #else
127  (void)argc;
128  (void)argv;
129 #endif
130 }
static const vpColor red
Definition: vpColor.h:211
static const vpColor blue
Definition: vpColor.h:217
static const vpColor green
Definition: vpColor.h:214
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
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
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:128
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 setTitle(const vpImage< unsigned char > &I, const std::string &windowtitle)
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:59
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
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:184
Defines a rectangle in the plane.
Definition: vpRect.h:76
double getLeft() const
Definition: vpRect.h:170
double getTop() const
Definition: vpRect.h:189
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
void setScale(unsigned scale=vpV4l2Grabber::DEFAULT_SCALE)
void setDevice(const std::string &devname)
void acquire(vpImage< unsigned char > &I)