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