ViSP  2.10.0
tutorial-barcode-detector-live.cpp
1 #include <visp/vpDisplayGDI.h>
3 #include <visp/vpDisplayOpenCV.h>
4 #include <visp/vpDisplayX.h>
5 #include <visp/vpImageConvert.h>
6 #include <visp/vpDetectorDataMatrixCode.h>
7 #include <visp/vpDetectorQRCode.h>
8 #include <visp/vpV4l2Grabber.h>
9 
10 int main(int argc, const char** argv)
11 {
12 #if (VISP_HAVE_OPENCV_VERSION >= 0x020100) && (defined(VISP_HAVE_ZBAR) || defined(VISP_HAVE_DMTX))
13  int opt_device = 0;
14  int opt_barcode = 0; // 0=QRCode, 1=DataMatrix
15 
16  for (int i=0; i<argc; i++) {
17  if (std::string(argv[i]) == "--device")
18  opt_device = atoi(argv[i+1]);
19  else if (std::string(argv[i]) == "--code-type")
20  opt_barcode = atoi(argv[i+1]);
21  else if (std::string(argv[i]) == "--help") {
22  std::cout << "Usage: " << argv[0]
23  << " [--device <camera number>] [--code-type <0 for QRcode | 1 for DataMatrix>] [--help]"
24  << std::endl;
25  return 0;
26  }
27  }
28  std::cout << "Use device: " << opt_device << std::endl;
29 
30  try {
31  vpImage<unsigned char> I; // for gray images
32 
34 #if defined(VISP_HAVE_V4L2)
35  vpV4l2Grabber g;
36  std::ostringstream device;
37  device << "/dev/video" << opt_device;
38  g.setDevice(device.str());
39  g.setScale(1);
40  g.acquire(I);
41 #elif defined(VISP_HAVE_OPENCV)
42  cv::VideoCapture cap(opt_device); // open the default camera
43  if(!cap.isOpened()) { // check if we succeeded
44  std::cout << "Failed to open the camera" << std::endl;
45  return -1;
46  }
47  cv::Mat frame;
48  cap >> frame; // get a new frame from camera
49  vpImageConvert::convert(frame, I);
50 #endif
51 
53 #if defined(VISP_HAVE_X11)
54  vpDisplayX d(I);
55 #elif defined(VISP_HAVE_GDI)
56  vpDisplayGDI d(I);
57 #elif defined(VISP_HAVE_OPENCV)
58  vpDisplayOpenCV d(I);
59 #endif
60  vpDisplay::setTitle(I, "ViSP viewer");
61 
62  vpDetectorBase *detector = NULL;
63 #if (defined(VISP_HAVE_ZBAR) && defined(VISP_HAVE_DMTX))
64  if (opt_barcode == 0)
65  detector = new vpDetectorQRCode;
66  else
67  detector = new vpDetectorDataMatrixCode;
68 #elif defined(VISP_HAVE_ZBAR)
69  detector = new vpDetectorQRCode;
70  (void)opt_barcode;
71 #elif defined(VISP_HAVE_DMTX)
72  detector = new vpDetectorDataMatrixCode;
73  (void)opt_barcode;
74 #endif
75 
76  for(;;) {
78 #if defined(VISP_HAVE_V4L2)
79  g.acquire(I);
80 #else
81  cap >> frame; // get a new frame from camera
82  vpImageConvert::convert(frame, I);
83 #endif
86 
87  bool status = detector->detect(I);
88  std::ostringstream legend;
89  legend << detector->getNbObjects() << " bar code detected";
90  vpDisplay::displayText(I, 10, 10, legend.str(), vpColor::red);
91 
92  if (status) {
93  for(size_t i=0; i < detector->getNbObjects(); i++) {
94  std::vector<vpImagePoint> p = detector->getPolygon(i);
95  vpRect bbox = detector->getBBox(i);
97  vpDisplay::displayText(I, bbox.getTop()-20, bbox.getLeft(), "Message: \"" + detector->getMessage(i) + "\"", vpColor::red);
98  for(size_t j=0; j < p.size(); j++) {
99  vpDisplay::displayCross(I, p[j], 14, vpColor::red, 3);
100  std::ostringstream number;
101  number << j;
102  vpDisplay::displayText(I, p[j]+vpImagePoint(10,0), number.str(), vpColor::blue);
103  }
104  }
105  }
106 
107  vpDisplay::displayText(I, (int)I.getHeight()-25, 10, "Click to quit...", vpColor::red);
108  vpDisplay::flush(I);
109  if (vpDisplay::getClick(I, false)) // a click to exit
110  break;
111  }
112  delete detector;
113  }
114  catch(vpException e) {
115  std::cout << "Catch an exception: " << e << std::endl;
116  }
117 #else
118  (void)argc;
119  (void)argv;
120 #endif
121 }
double getTop() const
Definition: vpRect.h:180
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:132
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Definition: vpDisplay.cpp:887
Define the X11 console to display images.
Definition: vpDisplayX.h:152
void setDevice(const std::string &devname)
error that can be emited by ViSP classes.
Definition: vpException.h:76
virtual bool detect(const vpImage< unsigned char > &I)=0
size_t getNbObjects() const
static const vpColor green
Definition: vpColor.h:170
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2232
static const vpColor red
Definition: vpColor.h:167
std::vector< std::vector< vpImagePoint > > & getPolygon()
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:210
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:85
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:93
vpRect getBBox(size_t i) const
double getLeft() const
Definition: vpRect.h:161
static const vpColor blue
Definition: vpColor.h:173