Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
tutorial-me-line-tracker.cpp
1
2#include <iostream>
3
4#include <visp3/core/vpConfig.h>
5
7// If openCV available, priority to OpenCV capture, otherwise the user has to modify the code uncommenting/commenting
8// one of the following lines
9#if defined(VISP_HAVE_OPENCV) && \
10 (((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || \
11 ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)))
12#undef VISP_HAVE_V4L2
13#undef VISP_HAVE_DC1394
14#undef VISP_HAVE_CMU1394
15#undef VISP_HAVE_FLYCAPTURE
16#undef VISP_HAVE_REALSENSE2
17// #undef HAVE_OPENCV_HIGHGUI
18// #undef HAVE_OPENCV_VIDEOIO
19#else
20// Use the first grabber that is available. Uncomment/comment the following lines to disable usage of a grabber
21// #undef VISP_HAVE_V4L2
22// #undef VISP_HAVE_DC1394
23// #undef VISP_HAVE_CMU1394
24// #undef VISP_HAVE_FLYCAPTURE
25// #undef VISP_HAVE_REALSENSE2
26#undef HAVE_OPENCV_HIGHGUI
27#undef HAVE_OPENCV_VIDEOIO
28#endif
30
32#if (defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394) || defined(VISP_HAVE_CMU1394) || \
33 defined(VISP_HAVE_FLYCAPTURE) || defined(VISP_HAVE_REALSENSE2) || defined(VISP_HAVE_OPENCV) && \
34 (((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || \
35 ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))) && \
36 ((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_CALIB3D) && defined(HAVE_OPENCV_FEATURES2D)) || \
37 ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_3D) && defined(HAVE_OPENCV_FEATURES)))
39
40#ifdef VISP_HAVE_MODULE_SENSOR
42#include <visp3/sensor/vp1394CMUGrabber.h>
43#include <visp3/sensor/vp1394TwoGrabber.h>
44#include <visp3/sensor/vpFlyCaptureGrabber.h>
45#include <visp3/sensor/vpRealSense2.h>
46#include <visp3/sensor/vpV4l2Grabber.h>
47#if (VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)
48#include <opencv2/highgui/highgui.hpp> // for cv::VideoCapture
49#elif (VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)
50#include <opencv2/videoio/videoio.hpp> // for cv::VideoCapture
51#endif
53#endif
55#include <visp3/gui/vpDisplayFactory.h>
58#include <visp3/me/vpMeLine.h>
60
61int main(int argc, char **argv)
62{
63#ifdef ENABLE_VISP_NAMESPACE
64 using namespace VISP_NAMESPACE_NAME;
65#endif
66
67#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
68 std::shared_ptr<vpDisplay> display;
69#else
70 vpDisplay *display = nullptr;
71#endif
73 int opt_me_range = 10;
74 int opt_me_sample_step = 5;
75 int opt_me_threshold = 20; // Value in [0 ; 255]
77
78 for (int i = 1; i < argc; i++) {
79 if (std::string(argv[i]) == "--me-range" && i + 1 < argc) {
80 opt_me_range = std::atoi(argv[++i]);
81 }
82 else if (std::string(argv[i]) == "--me-sample-step" && i + 1 < argc) {
83 opt_me_sample_step = std::atoi(argv[++i]);
84 }
85 else if (std::string(argv[i]) == "--me-threshold" && i + 1 < argc) {
86 opt_me_threshold = std::atoi(argv[++i]);
87 }
88 else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
89 std::cout << "\nUsage: " << argv[0]
90 << " [--me-range <range>]"
91 << " [--me-sample-step <sample step>]"
92 << " [--me-threshold <threshold>]"
93 << " [--help] [-h]\n"
94 << std::endl;
95 return EXIT_SUCCESS;
96 }
97 else {
98 std::cout << "\nError: wrong parameter " << argv[i] << std::endl;
99 return EXIT_FAILURE;
100 }
101 }
102
103 try {
107
109 int opt_device = 0; // For OpenCV and V4l2 grabber to set the camera device
110#if defined(VISP_HAVE_V4L2)
112 std::ostringstream device;
113 device << "/dev/video" << opt_device;
114 std::cout << "Use Video 4 Linux grabber on device " << device.str() << std::endl;
115 g.setDevice(device.str());
116 g.setScale(1);
117 g.open(I);
118#elif defined(VISP_HAVE_DC1394)
119 (void)opt_device; // To avoid non used warning
120 std::cout << "Use DC1394 grabber" << std::endl;
122 g.open(I);
123#elif defined(VISP_HAVE_CMU1394)
124 (void)opt_device; // To avoid non used warning
125 std::cout << "Use CMU1394 grabber" << std::endl;
127 g.open(I);
128#elif defined(VISP_HAVE_FLYCAPTURE)
129 (void)opt_device; // To avoid non used warning
130 std::cout << "Use FlyCapture grabber" << std::endl;
132 g.open(I);
133#elif defined(VISP_HAVE_REALSENSE2)
134 (void)opt_device; // To avoid non used warning
135 std::cout << "Use Realsense 2 grabber" << std::endl;
136 vpRealSense2 g;
137 rs2::config config;
138 config.disable_stream(RS2_STREAM_DEPTH);
139 config.disable_stream(RS2_STREAM_INFRARED);
140 config.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_RGBA8, 30);
141 g.open(config);
142 g.acquire(I);
143#elif defined(VISP_HAVE_OPENCV) && \
144 (((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || \
145 ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)))
146 std::cout << "Use OpenCV grabber on device " << opt_device << std::endl;
147 cv::VideoCapture g(opt_device); // Open the default camera
148 if (!g.isOpened()) { // Check if we succeeded
149 std::cout << "Failed to open the camera" << std::endl;
150 return EXIT_FAILURE;
151 }
152 cv::Mat frame;
153 int i = 0;
154 while ((i++ < 20) && !g.read(frame)) {
155 } // warm up camera by skiping unread frames
156 g >> frame; // get a new frame from camera
157 vpImageConvert::convert(frame, I);
158#endif
161#if defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394) || defined(VISP_HAVE_CMU1394) || defined(VISP_HAVE_FLYCAPTURE) || defined(VISP_HAVE_REALSENSE2)
162 g.acquire(I);
163#elif defined(VISP_HAVE_OPENCV) && \
164 (((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || \
165 ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)))
166 g >> frame; // get a new frame from camera
167 vpImageConvert::convert(frame, I);
168#endif
170
172#if defined(VISP_HAVE_DISPLAY)
173#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
174 display = vpDisplayFactory::createDisplay(I, -1, -1, "Camera view");
175#else
176 display = vpDisplayFactory::allocateDisplay(I, -1, -1, "Camera view");
177#endif
178#else
179 std::cout << "No image viewer is available..." << std::endl;
180#endif
186
188 vpMe me;
189 me.setRange(opt_me_range);
191 me.setThreshold(opt_me_threshold);
192 me.setSampleStep(opt_me_sample_step);
194
195 std::cout << "Moving-edges settings" << std::endl;
196 me.print();
197
199 vpMeLine line;
200 line.setMe(&me);
202 line.initTracking(I);
204
206 bool quit = false;
207 while (!quit) {
208#if defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394) || defined(VISP_HAVE_CMU1394) || defined(VISP_HAVE_FLYCAPTURE) || defined(VISP_HAVE_REALSENSE2)
209 g.acquire(I);
210#elif defined(VISP_HAVE_OPENCV) && \
211 (((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || \
212 ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)))
213 g >> frame;
214 vpImageConvert::convert(frame, I);
215#endif
217 vpDisplay::displayText(I, 20, 20, "Click to quit", vpColor::red);
218 line.track(I);
219 line.display(I, vpColor::red);
220 if (vpDisplay::getClick(I, false)) {
221 quit = true;
222 }
224 }
226 }
227 catch (const vpException &e) {
228 std::cout << "Catch an exception: " << e << std::endl;
229 }
230#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
231 if (display != nullptr) {
232 delete display;
233 }
234#endif
235}
236
237#else
238
239int main()
240{
241#if defined(VISP_HAVE_OPENCV)
242 std::cout << "Install a 3rd party dedicated to frame grabbing (dc1394, cmu1394, v4l2, OpenCV, FlyCapture, "
243 << "Realsense2), configure and build ViSP again to use this tutorial."
244 << std::endl;
245#else
246 std::cout << "Install OpenCV 3rd party, configure and build ViSP again to use this tutorial." << std::endl;
247#endif
248 return EXIT_SUCCESS;
249}
250#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:171
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)
Definition of the vpImage class member functions.
Definition vpImage.h:131
Class that tracks in an image a line moving edges.
Definition vpMeLine.h:157
void display(const vpImage< unsigned char > &I, const vpColor &color, unsigned int thickness=1)
Definition vpMeLine.cpp:177
void track(const vpImage< unsigned char > &I)
Definition vpMeLine.cpp:607
void initTracking(const vpImage< unsigned char > &I)
Definition vpMeLine.cpp:187
@ RANGE_RESULT
Definition vpMeSite.h:85
void setDisplay(vpMeSite::vpMeSiteDisplayType select)
void setMe(vpMe *me)
Definition vpMe.h:143
void print()
Definition vpMe.cpp:404
void setRange(const unsigned int &range)
Definition vpMe.h:438
void setLikelihoodThresholdType(const vpLikelihoodThresholdType likelihood_threshold_type)
Definition vpMe.h:531
void setThreshold(const double &threshold)
Definition vpMe.h:489
void setSampleStep(const double &sample_step)
Definition vpMe.h:445
@ NORMALIZED_THRESHOLD
Definition vpMe.h:154
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.