Visual Servoing Platform  version 3.4.0
tutorial-blob-tracker-live-v4l2.cpp
1 #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 
12 int 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)
28  vpV4l2Grabber g;
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
39  vpImageConvert::convert(frame, I);
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);
54  blob.setGraphicsThickness(2);
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;
65  vpImageConvert::convert(frame, I);
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 }
void acquire(vpImage< unsigned char > &I)
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
void open(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
void setGraphics(bool activate)
Definition: vpDot2.h:314
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)
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition: vpDot2.h:126
static void flush(const vpImage< unsigned char > &I)
static const vpColor red
Definition: vpColor.h:217
void setGraphicsThickness(unsigned int t)
Definition: vpDot2.h:321
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:134
void track(const vpImage< unsigned char > &I, bool canMakeTheWindowGrow=true)
Definition: vpDot2.cpp:441
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition: vpDot2.cpp:253
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:87