Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
tutorial-barcode-detector-live.cpp
1
2#include <iostream>
3
4#include <visp3/core/vpConfig.h>
5
7// Comment / uncomment following lines to use the specific 3rd party compatible with your camera
8// #undef VISP_HAVE_V4L2
9// #undef HAVE_OPENCV_HIGHGUI
10// #undef HAVE_OPENCV_VIDEOIO
12
13#if (defined(VISP_HAVE_ZBAR) || defined(VISP_HAVE_DMTX)) && (defined(VISP_HAVE_V4L2) || \
14 ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI))|| ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)))
15
16#include <visp3/core/vpImageConvert.h>
17#include <visp3/detection/vpDetectorDataMatrixCode.h>
18#include <visp3/detection/vpDetectorQRCode.h>
19#include <visp3/gui/vpDisplayFactory.h>
20#ifdef VISP_HAVE_MODULE_SENSOR
21#include <visp3/sensor/vpV4l2Grabber.h>
22#endif
23
24#if (VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)
25#include <opencv2/highgui/highgui.hpp> // for cv::VideoCapture
26#elif (VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)
27#include <opencv2/videoio/videoio.hpp> // for cv::VideoCapture
28#endif
29
30int main(int argc, const char **argv)
31{
32#ifdef ENABLE_VISP_NAMESPACE
33 using namespace VISP_NAMESPACE_NAME;
34#endif
35 int opt_device = 0;
36 int opt_barcode = 0; // 0=QRCode, 1=DataMatrix
37
38 for (int i = 1; i < argc; i++) {
39 if (std::string(argv[i]) == "--device" && i + 1 < argc) {
40 opt_device = atoi(argv[++i]);
41 }
42 else if (std::string(argv[i]) == "--code-type" && i + 1 < argc) {
43 opt_barcode = atoi(argv[++i]);
44 }
45 else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
46 std::cout << "Usage: " << argv[0]
47 << " [--device <camera number>]"
48 << " [--code-type <0 for QR code | 1 for DataMatrix code>]"
49 << " [--help] [-h]"
50 << std::endl;
51 return EXIT_SUCCESS;
52 }
53 }
54 std::cout << "Use device: " << opt_device << std::endl;
55
56#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
57 std::shared_ptr<vpDisplay> display;
58#else
59 vpDisplay *display = nullptr;
60#endif
61 try {
62 vpImage<unsigned char> I; // for gray images
63
65#if defined(VISP_HAVE_V4L2)
67 std::ostringstream device;
68 device << "/dev/video" << opt_device;
69 g.setDevice(device.str());
70 g.setScale(1);
71 g.acquire(I);
72#elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI))|| ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
73 cv::VideoCapture cap(opt_device); // open the default camera
74 if (!cap.isOpened()) { // check if we succeeded
75 std::cout << "Failed to open the camera" << std::endl;
76 return EXIT_FAILURE;
77 }
78 cv::Mat frame;
79 cap >> frame; // get a new frame from camera
81#endif
83
84#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
86#else
88#endif
89 vpDisplay::setTitle(I, "ViSP viewer");
90
91 vpDetectorBase *detector = nullptr;
92#if (defined(VISP_HAVE_ZBAR) && defined(VISP_HAVE_DMTX))
93 if (opt_barcode == 0)
94 detector = new vpDetectorQRCode;
95 else
96 detector = new vpDetectorDataMatrixCode;
97#elif defined(VISP_HAVE_ZBAR)
98 detector = new vpDetectorQRCode;
99 (void)opt_barcode;
100#elif defined(VISP_HAVE_DMTX)
101 detector = new vpDetectorDataMatrixCode;
102 (void)opt_barcode;
103#endif
104
105 for (;;) {
107#if defined(VISP_HAVE_V4L2)
108 g.acquire(I);
109#elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI))|| ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
110 cap >> frame; // get a new frame from camera
111 vpImageConvert::convert(frame, I);
112#endif
115
116 bool status = detector->detect(I);
117 std::ostringstream legend;
118 legend << detector->getNbObjects() << " bar code detected";
119 vpDisplay::displayText(I, 10, 10, legend.str(), vpColor::red);
120
121 if (status) {
122 for (size_t i = 0; i < detector->getNbObjects(); i++) {
123 std::vector<vpImagePoint> p = detector->getPolygon(i);
124 vpRect bbox = detector->getBBox(i);
126 vpDisplay::displayText(I, static_cast<int>(bbox.getTop()) - 20, static_cast<int>(bbox.getLeft()),
127 "Message: \"" + detector->getMessage(i) + "\"", vpColor::red);
128 for (size_t j = 0; j < p.size(); j++) {
129 vpDisplay::displayCross(I, p[j], 14, vpColor::red, 3);
130 std::ostringstream number;
131 number << j;
132 vpDisplay::displayText(I, p[j] + vpImagePoint(10, 0), number.str(), vpColor::blue);
133 }
134 }
135 }
136
137 vpDisplay::displayText(I, static_cast<int>(I.getHeight()) - 25, 10, "Click to quit...", vpColor::red);
139 if (vpDisplay::getClick(I, false)) // a click to exit
140 break;
141 }
142 delete detector;
143 }
144 catch (const vpException &e) {
145 std::cout << "Catch an exception: " << e << std::endl;
146 }
147#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
148 if (display != nullptr) {
149 delete display;
150 }
151#endif
152}
153
154#else
155
156int main()
157{
158 std::cout << "There are missing 3rd parties to run this tutorial" << std::endl;
159}
160
161#endif
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
std::vector< std::string > & getMessage()
size_t getNbObjects() const
virtual bool detect(const vpImage< unsigned char > &I)=0
Class that defines generic functionalities for display.
Definition vpDisplay.h:171
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:60
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 of the vpImage class member functions.
Definition vpImage.h:131
Defines a rectangle in the plane.
Definition vpRect.h:79
double getLeft() const
Definition vpRect.h:173
double getTop() const
Definition vpRect.h:192
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)
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.