Visual Servoing Platform  version 3.6.1 under development (2025-01-21)
tutorial-blob-tracker-live.cpp
1 #include <iostream>
3 
4 #include <visp3/core/vpConfig.h>
5 
7 // Comment / uncomment following lines to use the specific 3rd party compatible with your camera
8 // #undef VISP_HAVE_V4L2
9 // #undef VISP_HAVE_DC1394
10 // #undef VISP_HAVE_CMU1394
11 // #undef VISP_HAVE_FLYCAPTURE
12 // #undef VISP_HAVE_REALSENSE2
13 // #undef HAVE_OPENCV_HIGHGUI
14 // #undef HAVE_OPENCV_VIDEOIO
16 
17 #if (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV)) && \
18  (defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394) || defined(VISP_HAVE_CMU1394) || \
19  defined(VISP_HAVE_FLYCAPTURE) || defined(VISP_HAVE_REALSENSE2) || \
20  ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)))
21 
22 #ifdef VISP_HAVE_MODULE_SENSOR
23 #include <visp3/sensor/vp1394CMUGrabber.h>
24 #include <visp3/sensor/vp1394TwoGrabber.h>
25 #include <visp3/sensor/vpFlyCaptureGrabber.h>
26 #include <visp3/sensor/vpRealSense2.h>
27 #include <visp3/sensor/vpV4l2Grabber.h>
28 #endif
29 #include <visp3/blob/vpDot2.h>
30 #include <visp3/gui/vpDisplayGDI.h>
31 #include <visp3/gui/vpDisplayOpenCV.h>
32 #include <visp3/gui/vpDisplayX.h>
33 
34 #if (VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)
35 #include <opencv2/highgui/highgui.hpp> // for cv::VideoCapture
36 #elif (VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)
37 #include <opencv2/videoio/videoio.hpp>
38 #endif
39 
40 int main()
41 {
42 #ifdef ENABLE_VISP_NAMESPACE
43  using namespace VISP_NAMESPACE_NAME;
44 #endif
45 
46  vpImage<unsigned char> I; // Create a gray level image container
47  int opt_device = 0; // For OpenCV and V4l2 grabber to set the camera device
48 
50 #if defined(VISP_HAVE_V4L2)
51  vpV4l2Grabber g;
52  std::ostringstream device;
53  device << "/dev/video" << opt_device;
54  std::cout << "Use Video 4 Linux grabber on device " << device.str() << std::endl;
55  g.setDevice(device.str());
56  g.setScale(1);
57  g.open(I);
58 #elif defined(VISP_HAVE_DC1394)
59  (void)opt_device; // To avoid non used warning
60  std::cout << "Use DC1394 grabber" << std::endl;
62  g.open(I);
63 #elif defined(VISP_HAVE_CMU1394)
64  (void)opt_device; // To avoid non used warning
65  std::cout << "Use CMU1394 grabber" << std::endl;
67  g.open(I);
68 #elif defined(VISP_HAVE_FLYCAPTURE)
69  (void)opt_device; // To avoid non used warning
70  std::cout << "Use FlyCapture grabber" << std::endl;
72  g.open(I);
73 #elif defined(VISP_HAVE_REALSENSE2)
74  (void)opt_device; // To avoid non used warning
75  std::cout << "Use Realsense 2 grabber" << std::endl;
76  vpRealSense2 g;
77  rs2::config config;
78  config.disable_stream(RS2_STREAM_DEPTH);
79  config.disable_stream(RS2_STREAM_INFRARED);
80  config.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_RGBA8, 30);
81  g.open(config);
82  g.acquire(I);
83 #elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI))|| ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
84  cv::VideoCapture g(opt_device); // open the default camera
85  if (!g.isOpened()) { // check if we succeeded
86  std::cout << "Failed to open the camera" << std::endl;
87  return EXIT_FAILURE;
88  }
89  cv::Mat frame;
90  g >> frame; // get a new frame from camera
91  vpImageConvert::convert(frame, I);
92 #endif
93 
94 #if defined(VISP_HAVE_X11)
95  vpDisplayX d(I, 0, 0, "Camera view");
96 #elif defined(VISP_HAVE_GDI)
97  vpDisplayGDI d(I, 0, 0, "Camera view");
98 #elif defined(HAVE_OPENCV_HIGHGUI)
99  vpDisplayOpenCV d(I, 0, 0, "Camera view");
100 #endif
101 
103  vpDot2 blob;
106  blob.setGraphics(true);
107  blob.setGraphicsThickness(2);
109 
110  vpImagePoint germ;
111  bool init_done = false;
112  bool quit = false;
113  bool germ_selected = false;
115 
116  while (!quit) {
117  try {
118 #if defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394) || defined(VISP_HAVE_CMU1394) || defined(VISP_HAVE_FLYCAPTURE) || defined(VISP_HAVE_REALSENSE2)
119  g.acquire(I);
120 #elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI))|| ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
121  g >> frame;
122  vpImageConvert::convert(frame, I);
123 #endif
125  vpDisplay::displayText(I, 20, 20, "Left click in the blob to initialize the tracker", vpColor::red);
126  vpDisplay::displayText(I, 40, 20, "Right click to quit", vpColor::red);
127 
128  if (vpDisplay::getClick(I, germ, button, false)) {
129  if (button == vpMouseButton::button3) {
130  quit = true;
131  }
132  else {
133  germ_selected = true;
134  }
135  }
136  if (germ_selected && !init_done) {
138  std::cout << "Tracking initialized" << std::endl;
139  blob.initTracking(I, germ);
141  init_done = true;
142  germ_selected = false;
143  }
144  else if (init_done) {
146  blob.track(I);
148  }
149 
150  vpDisplay::flush(I);
151  }
152  catch (const vpException &e) {
153  std::cout << "Tracking failed: " << e.getMessage() << std::endl;
154  init_done = false;
155  }
156  }
157 }
158 
159 #else
160 int main()
161 {
162  std::cout << "There are missing 3rd parties to run this tutorial" << std::endl;
163 }
164 #endif
Firewire cameras video capture based on CMU 1394 Digital Camera SDK.
void open(vpImage< unsigned char > &I)
Class for firewire ieee1394 video devices using libdc1394-2.x api.
void open(vpImage< unsigned char > &I)
static const vpColor red
Definition: vpColor.h:198
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
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
error that can be emitted by ViSP classes.
Definition: vpException.h:60
const char * getMessage() const
Definition: vpException.cpp:65
void open(vpImage< unsigned char > &I)
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
void acquire(vpImage< unsigned char > &grey, double *ts=nullptr)
bool open(const rs2::config &cfg=rs2::config())
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
void open(vpImage< unsigned char > &I)
void setScale(unsigned scale=vpV4l2Grabber::DEFAULT_SCALE)
void setDevice(const std::string &devname)