Visual Servoing Platform  version 3.4.0
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 int main(int argc, const char **argv)
14 {
15 #if (VISP_HAVE_OPENCV_VERSION >= 0x020100) && (defined(VISP_HAVE_ZBAR) || defined(VISP_HAVE_DMTX))
16  int opt_device = 0;
17  int opt_barcode = 0; // 0=QRCode, 1=DataMatrix
18 
19  for (int i = 0; i < argc; i++) {
20  if (std::string(argv[i]) == "--device")
21  opt_device = atoi(argv[i + 1]);
22  else if (std::string(argv[i]) == "--code-type")
23  opt_barcode = atoi(argv[i + 1]);
24  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
25  std::cout << "Usage: " << argv[0]
26  << " [--device <camera number>] [--code-type <0 for QR code | "
27  "1 for DataMatrix code>] [--help] [-h]"
28  << std::endl;
29  return 0;
30  }
31  }
32  std::cout << "Use device: " << opt_device << std::endl;
33 
34  try {
35  vpImage<unsigned char> I; // for gray images
36 
38 #if defined(VISP_HAVE_V4L2)
39  vpV4l2Grabber g;
40  std::ostringstream device;
41  device << "/dev/video" << opt_device;
42  g.setDevice(device.str());
43  g.setScale(1);
44  g.acquire(I);
45 #elif defined(VISP_HAVE_OPENCV)
46  cv::VideoCapture cap(opt_device); // open the default camera
47  if (!cap.isOpened()) { // check if we succeeded
48  std::cout << "Failed to open the camera" << std::endl;
49  return -1;
50  }
51  cv::Mat frame;
52  cap >> frame; // get a new frame from camera
53  vpImageConvert::convert(frame, I);
54 #endif
55 
57 #if defined(VISP_HAVE_X11)
58  vpDisplayX d(I);
59 #elif defined(VISP_HAVE_GDI)
60  vpDisplayGDI d(I);
61 #elif defined(VISP_HAVE_OPENCV)
62  vpDisplayOpenCV d(I);
63 #endif
64  vpDisplay::setTitle(I, "ViSP viewer");
65 
66  vpDetectorBase *detector = NULL;
67 #if (defined(VISP_HAVE_ZBAR) && defined(VISP_HAVE_DMTX))
68  if (opt_barcode == 0)
69  detector = new vpDetectorQRCode;
70  else
71  detector = new vpDetectorDataMatrixCode;
72 #elif defined(VISP_HAVE_ZBAR)
73  detector = new vpDetectorQRCode;
74  (void)opt_barcode;
75 #elif defined(VISP_HAVE_DMTX)
76  detector = new vpDetectorDataMatrixCode;
77  (void)opt_barcode;
78 #endif
79 
80  for (;;) {
82 #if defined(VISP_HAVE_V4L2)
83  g.acquire(I);
84 #else
85  cap >> frame; // get a new frame from camera
86  vpImageConvert::convert(frame, I);
87 #endif
90 
91  bool status = detector->detect(I);
92  std::ostringstream legend;
93  legend << detector->getNbObjects() << " bar code detected";
94  vpDisplay::displayText(I, 10, 10, legend.str(), vpColor::red);
95 
96  if (status) {
97  for (size_t i = 0; i < detector->getNbObjects(); i++) {
98  std::vector<vpImagePoint> p = detector->getPolygon(i);
99  vpRect bbox = detector->getBBox(i);
101  vpDisplay::displayText(I, (int)bbox.getTop() - 20, (int)bbox.getLeft(),
102  "Message: \"" + detector->getMessage(i) + "\"", vpColor::red);
103  for (size_t j = 0; j < p.size(); j++) {
104  vpDisplay::displayCross(I, p[j], 14, vpColor::red, 3);
105  std::ostringstream number;
106  number << j;
107  vpDisplay::displayText(I, p[j] + vpImagePoint(10, 0), number.str(), vpColor::blue);
108  }
109  }
110  }
111 
112  vpDisplay::displayText(I, (int)I.getHeight() - 25, 10, "Click to quit...", vpColor::red);
113  vpDisplay::flush(I);
114  if (vpDisplay::getClick(I, false)) // a click to exit
115  break;
116  }
117  delete detector;
118  } catch (const vpException &e) {
119  std::cout << "Catch an exception: " << e << std::endl;
120  }
121 #else
122  (void)argc;
123  (void)argv;
124 #endif
125 }
double getTop() const
Definition: vpRect.h:193
void acquire(vpImage< unsigned char > &I)
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
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:150
void setDevice(const std::string &devname)
error that can be emited by ViSP classes.
Definition: vpException.h:71
virtual bool detect(const vpImage< unsigned char > &I)=0
size_t getNbObjects() const
static const vpColor green
Definition: vpColor.h:220
static void flush(const vpImage< unsigned char > &I)
static const vpColor red
Definition: vpColor.h:217
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
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)
void setScale(unsigned scale=vpV4l2Grabber::DEFAULT_SCALE)
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
unsigned int getHeight() const
Definition: vpImage.h:188
Defines a rectangle in the plane.
Definition: vpRect.h:79
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:87
std::vector< std::string > & getMessage()
vpRect getBBox(size_t i) const
static void setTitle(const vpImage< unsigned char > &I, const std::string &windowtitle)
double getLeft() const
Definition: vpRect.h:174
static const vpColor blue
Definition: vpColor.h:223
std::vector< std::vector< vpImagePoint > > & getPolygon()