Visual Servoing Platform  version 3.6.1 under development (2024-04-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 #if defined(HAVE_OPENCV_VIDEOIO)
13 #include <opencv2/videoio.hpp>
14 #endif
15 
16 int main(int argc, char **argv)
17 {
18 #if ((defined(VISP_HAVE_V4L2) || defined(HAVE_OPENCV_VIDEOIO)) && \
19  (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV) || defined(VISP_HAVE_GTK)))
20 
21  int device = 0;
22  if (argc == 2) {
23  device = std::atoi(argv[1]);
24  }
25  vpImage<unsigned char> I; // Create a gray level image container
26 
27  std::stringstream ss;
28  ss << "/dev/video" << device;
29 
30  std::cout << "Connect to: " << ss.str() << std::endl;
31 #if defined(VISP_HAVE_V4L2)
32  vpV4l2Grabber g;
33  g.setDevice(ss.str());
34  g.open(I);
35 #elif defined(HAVE_OPENCV_VIDEOIO)
36  cv::VideoCapture g(device); // open the default camera
37  if (!g.isOpened()) { // check if we succeeded
38  std::cout << "Failed to open the camera" << std::endl;
39  return EXIT_FAILURE;
40  }
41  cv::Mat frame;
42  g >> frame; // get a new frame from camera
43  vpImageConvert::convert(frame, I);
44 #endif
45 
46 #if defined(VISP_HAVE_X11)
47  vpDisplayX d(I, 0, 0, "Camera view");
48 #elif defined(VISP_HAVE_GDI)
49  vpDisplayGDI d(I, 0, 0, "Camera view");
50 #elif defined(HAVE_OPENCV_HIGHGUI)
51  vpDisplayOpenCV d(I, 0, 0, "Camera view");
52 #elif defined(VISP_HAVE_GTK)
53  vpDisplayGTK d(I, 0, 0, "Camera view");
54 #endif
55 
56  vpDot2 blob;
57  blob.setGraphics(true);
58  blob.setGraphicsThickness(2);
59 
60  vpImagePoint germ;
61  bool init_done = false;
62  std::cout << "Click!!!" << std::endl;
63  while (1) {
64  try {
65 #if defined(VISP_HAVE_V4L2)
66  g.acquire(I);
67 #elif defined(HAVE_OPENCV_VIDEOIO)
68  g >> frame;
69  vpImageConvert::convert(frame, I);
70 #endif
72 
73  if (!init_done) {
74  vpDisplay::displayText(I, vpImagePoint(10, 10), "Click in the blob to initialize the tracker", vpColor::red);
75  if (vpDisplay::getClick(I, germ, false)) {
76  blob.initTracking(I, germ);
77  init_done = true;
78  }
79  }
80  else {
81  blob.track(I);
82  }
84  }
85  catch (...) {
86  init_done = false;
87  }
88  }
89 #else
90  (void)argc;
91  (void)argv;
92 #endif
93 }
static const vpColor red
Definition: vpColor.h:211
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:128
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:128
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:124
void track(const vpImage< unsigned char > &I, bool canMakeTheWindowGrow=true)
Definition: vpDot2.cpp:433
void setGraphics(bool activate)
Definition: vpDot2.h:310
void setGraphicsThickness(unsigned int t)
Definition: vpDot2.h:318
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:82
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)