Visual Servoing Platform  version 3.6.1 under development (2025-03-13)
tutorial-me-line-tracker.cpp
1 #include <iostream>
3 
4 #include <visp3/core/vpConfig.h>
5 
7 // #undef VISP_HAVE_V4L2
8 // #undef VISP_HAVE_DC1394
9 // #undef VISP_HAVE_CMU1394
10 // #undef VISP_HAVE_FLYCAPTURE
11 // #undef VISP_HAVE_REALSENSE2
12 // #undef HAVE_OPENCV_HIGHGUI
13 // #undef HAVE_OPENCV_VIDEOIO
15 
16 #if (defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394) || defined(VISP_HAVE_CMU1394) || \
17  defined(VISP_HAVE_FLYCAPTURE) || defined(VISP_HAVE_REALSENSE2) || \
18  ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || \
19  ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))) && \
20  ((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_CALIB3D) && defined(HAVE_OPENCV_FEATURES2D)) || \
21  ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_3D) && defined(HAVE_OPENCV_FEATURES))
22 
23 #ifdef VISP_HAVE_MODULE_SENSOR
25 #include <visp3/sensor/vp1394CMUGrabber.h>
26 #include <visp3/sensor/vp1394TwoGrabber.h>
27 #include <visp3/sensor/vpFlyCaptureGrabber.h>
28 #include <visp3/sensor/vpRealSense2.h>
29 #include <visp3/sensor/vpV4l2Grabber.h>
31 #endif
33 #include <visp3/gui/vpDisplayFactory.h>
36 #include <visp3/me/vpMeLine.h>
38 
39 #if (VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)
40 #include <opencv2/highgui/highgui.hpp> // for cv::VideoCapture
41 #elif (VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)
42 #include <opencv2/videoio/videoio.hpp> // for cv::VideoCapture
43 #endif
44 
45 int main()
46 {
47 #ifdef ENABLE_VISP_NAMESPACE
48  using namespace VISP_NAMESPACE_NAME;
49 #endif
50 
51 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
52  std::shared_ptr<vpDisplay> display;
53 #else
54  vpDisplay *display = nullptr;
55 #endif
56 
57  try {
61 
63  int opt_device = 0; // For OpenCV and V4l2 grabber to set the camera device
64 #if defined(VISP_HAVE_V4L2)
65  vpV4l2Grabber g;
66  std::ostringstream device;
67  device << "/dev/video" << opt_device;
68  std::cout << "Use Video 4 Linux grabber on device " << device.str() << std::endl;
69  g.setDevice(device.str());
70  g.setScale(1);
71  g.open(I);
72 #elif defined(VISP_HAVE_DC1394)
73  (void)opt_device; // To avoid non used warning
74  std::cout << "Use DC1394 grabber" << std::endl;
76  g.open(I);
77 #elif defined(VISP_HAVE_CMU1394)
78  (void)opt_device; // To avoid non used warning
79  std::cout << "Use CMU1394 grabber" << std::endl;
81  g.open(I);
82 #elif defined(VISP_HAVE_FLYCAPTURE)
83  (void)opt_device; // To avoid non used warning
84  std::cout << "Use FlyCapture grabber" << std::endl;
86  g.open(I);
87 #elif defined(VISP_HAVE_REALSENSE2)
88  (void)opt_device; // To avoid non used warning
89  std::cout << "Use Realsense 2 grabber" << std::endl;
90  vpRealSense2 g;
91  rs2::config config;
92  config.disable_stream(RS2_STREAM_DEPTH);
93  config.disable_stream(RS2_STREAM_INFRARED);
94  config.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_RGBA8, 30);
95  g.open(config);
96  g.acquire(I);
97 #elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI))|| ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
98  std::cout << "Use OpenCV grabber on device " << opt_device << std::endl;
99  cv::VideoCapture g(opt_device); // Open the default camera
100  if (!g.isOpened()) { // Check if we succeeded
101  std::cout << "Failed to open the camera" << std::endl;
102  return EXIT_FAILURE;
103  }
104  cv::Mat frame;
105  g >> frame; // get a new frame from camera
106  vpImageConvert::convert(frame, I);
107 #endif
110 #if defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394) || defined(VISP_HAVE_CMU1394) || defined(VISP_HAVE_FLYCAPTURE) || defined(VISP_HAVE_REALSENSE2)
111  g.acquire(I);
112 #elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI))|| ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
113  g >> frame; // get a new frame from camera
114  vpImageConvert::convert(frame, I);
115 #endif
117 
119 #if defined(VISP_HAVE_DISPLAY)
120 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
121  display = vpDisplayFactory::createDisplay(I, 0, 0, "Camera view");
122 #else
123  display = vpDisplayFactory::allocateDisplay(I, 0, 0, "Camera view");
124 #endif
125 #else
126  std::cout << "No image viewer is available..." << std::endl;
127 #endif
131  vpDisplay::flush(I);
133 
135  vpMe me;
136  me.setRange(25);
138  me.setThreshold(20);
139  me.setSampleStep(10);
141 
143  vpMeLine line;
144  line.setMe(&me);
146  line.initTracking(I);
148 
150  bool quit = false;
151  while (!quit) {
152 #if defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394) || defined(VISP_HAVE_CMU1394) || defined(VISP_HAVE_FLYCAPTURE) || defined(VISP_HAVE_REALSENSE2)
153  g.acquire(I);
154 #elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI))|| ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
155  g >> frame;
156  vpImageConvert::convert(frame, I);
157 #endif
159  vpDisplay::displayText(I, 20, 20, "Click to quit", vpColor::red);
160  line.track(I);
161  line.display(I, vpColor::red);
162  if (vpDisplay::getClick(I, false)) {
163  quit = true;
164  }
165  vpDisplay::flush(I);
166  }
168  }
169  catch (const vpException &e) {
170  std::cout << "Catch an exception: " << e << std::endl;
171  }
172 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
173  if (display != nullptr) {
174  delete display;
175  }
176 #endif
177 }
178 
179 #else
180 
181 int main()
182 {
183 #if defined(VISP_HAVE_OPENCV)
184  std::cout << "Install a 3rd party dedicated to frame grabbing (dc1394, cmu1394, v4l2, OpenCV, FlyCapture, "
185  << "Realsense2), configure and build ViSP again to use this tutorial."
186  << std::endl;
187 #else
188  std::cout << "Install OpenCV 3rd party, configure and build ViSP again to use this tutorial." << std::endl;
189 #endif
190  return EXIT_SUCCESS;
191  }
192 #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)
error that can be emitted by ViSP classes.
Definition: vpException.h:60
void open(vpImage< unsigned char > &I)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Class that tracks in an image a line moving edges.
Definition: vpMeLine.h:153
void display(const vpImage< unsigned char > &I, const vpColor &color, unsigned int thickness=1)
Definition: vpMeLine.cpp:169
void track(const vpImage< unsigned char > &I)
Definition: vpMeLine.cpp:599
void initTracking(const vpImage< unsigned char > &I)
Definition: vpMeLine.cpp:179
@ RANGE_RESULT
Definition: vpMeSite.h:78
void setDisplay(vpMeSite::vpMeSiteDisplayType select)
Definition: vpMeTracker.h:232
void setMe(vpMe *me)
Definition: vpMeTracker.h:260
Definition: vpMe.h:134
void setRange(const unsigned int &range)
Definition: vpMe.h:415
void setLikelihoodThresholdType(const vpLikelihoodThresholdType likelihood_threshold_type)
Definition: vpMe.h:505
void setThreshold(const double &threshold)
Definition: vpMe.h:466
void setSampleStep(const double &sample_step)
Definition: vpMe.h:422
@ NORMALIZED_THRESHOLD
Definition: vpMe.h:145
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.