Visual Servoing Platform  version 3.0.0
tutorial-barcode-detector-live.cpp
1 #include <visp3/core/vpConfig.h>
3 #include <visp3/gui/vpDisplayGDI.h>
4 #include <visp3/gui/vpDisplayOpenCV.h>
5 #include <visp3/gui/vpDisplayX.h>
6 #include <visp3/core/vpImageConvert.h>
7 #include <visp3/detection/vpDetectorDataMatrixCode.h>
8 #include <visp3/detection/vpDetectorQRCode.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") {
25  std::cout << "Usage: " << argv[0]
26  << " [--device <camera number>] [--code-type <0 for QRcode | 1 for DataMatrix>] [--help]"
27  << std::endl;
28  return 0;
29  }
30  }
31  std::cout << "Use device: " << opt_device << std::endl;
32 
33  try {
34  vpImage<unsigned char> I; // for gray images
35 
37 #if defined(VISP_HAVE_V4L2)
38  vpV4l2Grabber g;
39  std::ostringstream device;
40  device << "/dev/video" << opt_device;
41  g.setDevice(device.str());
42  g.setScale(1);
43  g.acquire(I);
44 #elif defined(VISP_HAVE_OPENCV)
45  cv::VideoCapture cap(opt_device); // open the default camera
46  if(!cap.isOpened()) { // check if we succeeded
47  std::cout << "Failed to open the camera" << std::endl;
48  return -1;
49  }
50  cv::Mat frame;
51  cap >> frame; // get a new frame from camera
52  vpImageConvert::convert(frame, I);
53 #endif
54 
56 #if defined(VISP_HAVE_X11)
57  vpDisplayX d(I);
58 #elif defined(VISP_HAVE_GDI)
59  vpDisplayGDI d(I);
60 #elif defined(VISP_HAVE_OPENCV)
61  vpDisplayOpenCV d(I);
62 #endif
63  vpDisplay::setTitle(I, "ViSP viewer");
64 
65  vpDetectorBase *detector = NULL;
66 #if (defined(VISP_HAVE_ZBAR) && defined(VISP_HAVE_DMTX))
67  if (opt_barcode == 0)
68  detector = new vpDetectorQRCode;
69  else
70  detector = new vpDetectorDataMatrixCode;
71 #elif defined(VISP_HAVE_ZBAR)
72  detector = new vpDetectorQRCode;
73  (void)opt_barcode;
74 #elif defined(VISP_HAVE_DMTX)
75  detector = new vpDetectorDataMatrixCode;
76  (void)opt_barcode;
77 #endif
78 
79  for(;;) {
81 #if defined(VISP_HAVE_V4L2)
82  g.acquire(I);
83 #else
84  cap >> frame; // get a new frame from camera
85  vpImageConvert::convert(frame, I);
86 #endif
89 
90  bool status = detector->detect(I);
91  std::ostringstream legend;
92  legend << detector->getNbObjects() << " bar code detected";
93  vpDisplay::displayText(I, 10, 10, legend.str(), vpColor::red);
94 
95  if (status) {
96  for(size_t i=0; i < detector->getNbObjects(); i++) {
97  std::vector<vpImagePoint> p = detector->getPolygon(i);
98  vpRect bbox = detector->getBBox(i);
100  vpDisplay::displayText(I, bbox.getTop()-20, bbox.getLeft(), "Message: \"" + detector->getMessage(i) + "\"", vpColor::red);
101  for(size_t j=0; j < p.size(); j++) {
102  vpDisplay::displayCross(I, p[j], 14, vpColor::red, 3);
103  std::ostringstream number;
104  number << j;
105  vpDisplay::displayText(I, p[j]+vpImagePoint(10,0), number.str(), vpColor::blue);
106  }
107  }
108  }
109 
110  vpDisplay::displayText(I, (int)I.getHeight()-25, 10, "Click to quit...", vpColor::red);
111  vpDisplay::flush(I);
112  if (vpDisplay::getClick(I, false)) // a click to exit
113  break;
114  }
115  delete detector;
116  }
117  catch(vpException e) {
118  std::cout << "Catch an exception: " << e << std::endl;
119  }
120 #else
121  (void)argc;
122  (void)argv;
123 #endif
124 }
double getTop() const
Definition: vpRect.h:176
void acquire(vpImage< unsigned char > &I)
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)
Definition: vpDisplay.cpp:888
Define the X11 console to display images.
Definition: vpDisplayX.h:148
void setDevice(const std::string &devname)
error that can be emited by ViSP classes.
Definition: vpException.h:73
virtual bool detect(const vpImage< unsigned char > &I)=0
size_t getNbObjects() const
static const vpColor green
Definition: vpColor.h:166
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2233
static const vpColor red
Definition: vpColor.h:163
std::vector< std::vector< vpImagePoint > > & getPolygon()
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
The vpDisplayOpenCV allows to display image using the opencv library.
virtual void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)=0
virtual void setTitle(const char *title)=0
virtual void displayRectangle(const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)=0
void setScale(unsigned scale=vpV4l2Grabber::DEFAULT_SCALE)
Class for the Video4Linux2 video device.
std::string & getMessage(size_t i)
unsigned int getHeight() const
Definition: vpImage.h:152
Defines a rectangle in the plane.
Definition: vpRect.h:81
virtual bool getClick(bool blocking=true)=0
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
vpRect getBBox(size_t i) const
double getLeft() const
Definition: vpRect.h:157
static const vpColor blue
Definition: vpColor.h:169