Visual Servoing Platform version 3.5.0
tutorial-blob-tracker-live-v4l2.cpp
1
2#include <visp3/core/vpConfig.h>
3#ifdef VISP_HAVE_MODULE_SENSOR
4#include <visp3/sensor/vpV4l2Grabber.h>
5#endif
6#include <visp3/blob/vpDot2.h>
7#include <visp3/gui/vpDisplayGDI.h>
8#include <visp3/gui/vpDisplayGTK.h>
9#include <visp3/gui/vpDisplayOpenCV.h>
10#include <visp3/gui/vpDisplayX.h>
11
12int main(int argc, char **argv)
13{
14#if ((defined(VISP_HAVE_V4L2) || (VISP_HAVE_OPENCV_VERSION >= 0x020100)) && \
15 (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV) || defined(VISP_HAVE_GTK)))
16
17 int device = 0;
18 if (argc == 2) {
19 device = std::atoi(argv[1]);
20 }
21 vpImage<unsigned char> I; // Create a gray level image container
22
23 std::stringstream ss;
24 ss << "/dev/video" << device;
25
26 std::cout << "Connect to: " << ss.str() << std::endl;
27#if defined(VISP_HAVE_V4L2)
29 g.setDevice(ss.str());
30 g.open(I);
31#elif defined(VISP_HAVE_OPENCV)
32 cv::VideoCapture g(device); // open the default camera
33 if (!g.isOpened()) { // check if we succeeded
34 std::cout << "Failed to open the camera" << std::endl;
35 return -1;
36 }
37 cv::Mat frame;
38 g >> frame; // get a new frame from camera
40#endif
41
42#if defined(VISP_HAVE_X11)
43 vpDisplayX d(I, 0, 0, "Camera view");
44#elif defined(VISP_HAVE_GDI)
45 vpDisplayGDI d(I, 0, 0, "Camera view");
46#elif defined(VISP_HAVE_OPENCV)
47 vpDisplayOpenCV d(I, 0, 0, "Camera view");
48#elif defined(VISP_HAVE_GTK)
49 vpDisplayGTK d(I, 0, 0, "Camera view");
50#endif
51
52 vpDot2 blob;
53 blob.setGraphics(true);
55
56 vpImagePoint germ;
57 bool init_done = false;
58 std::cout << "Click!!!" << std::endl;
59 while (1) {
60 try {
61#if defined(VISP_HAVE_V4L2)
62 g.acquire(I);
63#elif defined(VISP_HAVE_OPENCV)
64 g >> frame;
66#endif
68
69 if (!init_done) {
70 vpDisplay::displayText(I, vpImagePoint(10, 10), "Click in the blob to initialize the tracker", vpColor::red);
71 if (vpDisplay::getClick(I, germ, false)) {
72 blob.initTracking(I, germ);
73 init_done = true;
74 }
75 } else {
76 blob.track(I);
77 }
79 } catch (...) {
80 init_done = false;
81 }
82 }
83#else
84 (void)argc;
85 (void)argv;
86#endif
87}
static const vpColor red
Definition: vpColor.h:217
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:135
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:135
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition: vpDot2.h:127
void track(const vpImage< unsigned char > &I, bool canMakeTheWindowGrow=true)
Definition: vpDot2.cpp:441
void setGraphics(bool activate)
Definition: vpDot2.h:314
void setGraphicsThickness(unsigned int t)
Definition: vpDot2.h:321
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition: vpDot2.cpp:253
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:88
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
void open(vpImage< unsigned char > &I)
void setDevice(const std::string &devname)
void acquire(vpImage< unsigned char > &I)