Visual Servoing Platform  version 3.6.1 under development (2025-03-14)
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_DISPLAY) && \
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/vpDisplayFactory.h>
31 
32 #if (VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)
33 #include <opencv2/highgui/highgui.hpp> // for cv::VideoCapture
34 #elif (VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)
35 #include <opencv2/videoio/videoio.hpp>
36 #endif
37 
38 int main()
39 {
40 #ifdef ENABLE_VISP_NAMESPACE
41  using namespace VISP_NAMESPACE_NAME;
42 #endif
43 
44  vpImage<unsigned char> I; // Create a gray level image container
45  int opt_device = 0; // For OpenCV and V4l2 grabber to set the camera device
46 
48 #if defined(VISP_HAVE_V4L2)
49  vpV4l2Grabber g;
50  std::ostringstream device;
51  device << "/dev/video" << opt_device;
52  std::cout << "Use Video 4 Linux grabber on device " << device.str() << std::endl;
53  g.setDevice(device.str());
54  g.setScale(1);
55  g.open(I);
56 #elif defined(VISP_HAVE_DC1394)
57  (void)opt_device; // To avoid non used warning
58  std::cout << "Use DC1394 grabber" << std::endl;
60  g.open(I);
61 #elif defined(VISP_HAVE_CMU1394)
62  (void)opt_device; // To avoid non used warning
63  std::cout << "Use CMU1394 grabber" << std::endl;
65  g.open(I);
66 #elif defined(VISP_HAVE_FLYCAPTURE)
67  (void)opt_device; // To avoid non used warning
68  std::cout << "Use FlyCapture grabber" << std::endl;
70  g.open(I);
71 #elif defined(VISP_HAVE_REALSENSE2)
72  (void)opt_device; // To avoid non used warning
73  std::cout << "Use Realsense 2 grabber" << std::endl;
74  vpRealSense2 g;
75  rs2::config config;
76  config.disable_stream(RS2_STREAM_DEPTH);
77  config.disable_stream(RS2_STREAM_INFRARED);
78  config.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_RGBA8, 30);
79  g.open(config);
80  g.acquire(I);
81 #elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI))|| ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
82  cv::VideoCapture g(opt_device); // open the default camera
83  if (!g.isOpened()) { // check if we succeeded
84  std::cout << "Failed to open the camera" << std::endl;
85  return EXIT_FAILURE;
86  }
87  cv::Mat frame;
88  g >> frame; // get a new frame from camera
89  vpImageConvert::convert(frame, I);
90 #endif
91 
92 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
93  std::shared_ptr<vpDisplay> display = vpDisplayFactory::createDisplay(I, 0, 0, "Camera view");
94 #else
95  vpDisplay *display = vpDisplayFactory::allocateDisplay(I, 0, 0, "Camera view");
96 #endif
97 
99  vpDot2 blob;
102  blob.setGraphics(true);
103  blob.setGraphicsThickness(2);
105 
106  vpImagePoint germ;
107  bool init_done = false;
108  bool quit = false;
109  bool germ_selected = false;
111 
112  while (!quit) {
113  try {
114 #if defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394) || defined(VISP_HAVE_CMU1394) || defined(VISP_HAVE_FLYCAPTURE) || defined(VISP_HAVE_REALSENSE2)
115  g.acquire(I);
116 #elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI))|| ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
117  g >> frame;
118  vpImageConvert::convert(frame, I);
119 #endif
121  vpDisplay::displayText(I, 20, 20, "Left click in the blob to initialize the tracker", vpColor::red);
122  vpDisplay::displayText(I, 40, 20, "Right click to quit", vpColor::red);
123 
124  if (vpDisplay::getClick(I, germ, button, false)) {
125  if (button == vpMouseButton::button3) {
126  quit = true;
127  }
128  else {
129  germ_selected = true;
130  }
131  }
132  if (germ_selected && !init_done) {
134  std::cout << "Tracking initialized" << std::endl;
135  blob.initTracking(I, germ);
137  init_done = true;
138  germ_selected = false;
139  }
140  else if (init_done) {
142  blob.track(I);
144  }
145 
146  vpDisplay::flush(I);
147  }
148  catch (const vpException &e) {
149  std::cout << "Tracking failed: " << e.getMessage() << std::endl;
150  init_done = false;
151  }
152  }
153 
154 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
155  if (display != nullptr) {
156  delete display;
157  }
158 #endif
159 }
160 
161 #else
162 int main()
163 {
164  std::cout << "There are missing 3rd parties to run this tutorial" << std::endl;
165 }
166 #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
Class that defines generic functionalities for display.
Definition: vpDisplay.h:178
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)
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.