Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
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()
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  vpImage<unsigned char> I; // Create a gray level image container
17 
18 #if defined(VISP_HAVE_V4L2)
19  vpV4l2Grabber g;
20  g.open(I);
21 #elif defined(VISP_HAVE_OPENCV)
22  cv::VideoCapture g(0); // open the default camera
23  if (!g.isOpened()) { // check if we succeeded
24  std::cout << "Failed to open the camera" << std::endl;
25  return -1;
26  }
27  cv::Mat frame;
28  g >> frame; // get a new frame from camera
29  vpImageConvert::convert(frame, I);
30 #endif
31 
32 #if defined(VISP_HAVE_X11)
33  vpDisplayX d(I, 0, 0, "Camera view");
34 #elif defined(VISP_HAVE_GDI)
35  vpDisplayGDI d(I, 0, 0, "Camera view");
36 #elif defined(VISP_HAVE_OPENCV)
37  vpDisplayOpenCV d(I, 0, 0, "Camera view");
38 #elif defined(VISP_HAVE_GTK)
39  vpDisplayGTK d(I, 0, 0, "Camera view");
40 #endif
41 
42  vpDot2 blob;
43  blob.setGraphics(true);
44  blob.setGraphicsThickness(2);
45 
46  vpImagePoint germ;
47  bool init_done = false;
48  std::cout << "Click!!!" << std::endl;
49  while (1) {
50  try {
51 #if defined(VISP_HAVE_V4L2)
52  g.acquire(I);
53 #elif defined(VISP_HAVE_OPENCV)
54  g >> frame;
55  vpImageConvert::convert(frame, I);
56 #endif
58 
59  if (!init_done) {
60  vpDisplay::displayText(I, vpImagePoint(10, 10), "Click in the blob to initialize the tracker", vpColor::red);
61  if (vpDisplay::getClick(I, germ, false)) {
62  blob.initTracking(I, germ);
63  init_done = true;
64  }
65  } else {
66  blob.track(I);
67  }
69  } catch (...) {
70  init_done = false;
71  }
72  }
73 #endif
74 }
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:294
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
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:179
void setGraphicsThickness(unsigned int t)
Definition: vpDot2.h:301
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:137
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:88