Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
tutorial-me-ellipse-tracker.cpp
1
2#include <iostream>
3
4#include <visp3/core/vpConfig.h>
6// If openCV available, priority to OpenCV capture, otherwise the user has to modify the code uncommenting/commenting
7// one of the following lines
8#if ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || \
9 ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
10#undef VISP_HAVE_V4L2
11#undef VISP_HAVE_DC1394
12#undef VISP_HAVE_CMU1394
13#undef VISP_HAVE_FLYCAPTURE
14#undef VISP_HAVE_REALSENSE2
15// #undef HAVE_OPENCV_HIGHGUI
16// #undef HAVE_OPENCV_VIDEOIO
17#else
18// Use the first grabber that is available. Uncomment/comment the following lines to disable usage of a grabber
19// #undef VISP_HAVE_V4L2
20// #undef VISP_HAVE_DC1394
21// #undef VISP_HAVE_CMU1394
22// #undef VISP_HAVE_FLYCAPTURE
23// #undef VISP_HAVE_REALSENSE2
24#undef HAVE_OPENCV_HIGHGUI
25#undef HAVE_OPENCV_VIDEOIO
26#endif
28
29#if (defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394) || defined(VISP_HAVE_CMU1394) || \
30 defined(VISP_HAVE_FLYCAPTURE) || defined(VISP_HAVE_REALSENSE2) || defined(VISP_HAVE_OPENCV) && \
31 (((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || \
32 ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))))
33
34#ifdef VISP_HAVE_MODULE_SENSOR
35#include <visp3/sensor/vp1394CMUGrabber.h>
36#include <visp3/sensor/vp1394TwoGrabber.h>
37#include <visp3/sensor/vpFlyCaptureGrabber.h>
38#include <visp3/sensor/vpRealSense2.h>
39#include <visp3/sensor/vpV4l2Grabber.h>
40#endif
41
42#include <visp3/core/vpIoTools.h>
43#include <visp3/gui/vpDisplayFactory.h>
44#include <visp3/io/vpVideoWriter.h>
46#include <visp3/me/vpMeEllipse.h>
48
49#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)
50#include <opencv2/highgui/highgui.hpp> // for cv::VideoCapture
51#elif defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)
52#include <opencv2/videoio/videoio.hpp> // for cv::VideoCapture
53#endif
54
55int main(int argc, char **argv)
56{
57#ifdef ENABLE_VISP_NAMESPACE
58 using namespace VISP_NAMESPACE_NAME;
59#endif
60
61#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
62 std::shared_ptr<vpDisplay> display;
63#else
64 vpDisplay *display = nullptr;
65#endif
66
67 int opt_me_range = 10;
68 int opt_me_sample_step = 5;
69 int opt_me_threshold = 20; // Value in [0 ; 255]
70 int opt_track_circle = false; // By default we will track an ellipse
71 int opt_track_arc = false; // By default we will track a full circle or ellipse
72 std::string opt_save_filename; // Saved images filename
73
74 for (int i = 1; i < argc; i++) {
75 if (std::string(argv[i]) == "--me-range" && i + 1 < argc) {
76 opt_me_range = std::atoi(argv[++i]);
77 }
78 else if (std::string(argv[i]) == "--me-sample-step" && i + 1 < argc) {
79 opt_me_sample_step = std::atoi(argv[++i]);
80 }
81 else if (std::string(argv[i]) == "--me-threshold" && i + 1 < argc) {
82 opt_me_threshold = std::atoi(argv[++i]);
83 }
84 else if (std::string(argv[i]) == "--track-circle") {
85 opt_track_circle = true;
86 }
87 else if (std::string(argv[i]) == "--track-arc") {
88 opt_track_arc = true;
89 }
90 else if (std::string(argv[i]) == "--save" && i + 1 < argc) {
91 opt_save_filename = std::string(argv[++i]);
92 }
93 else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
94 std::cout << "\nUsage: " << argv[0]
95 << " [--me-range <range>]"
96 << " [--me-sample-step <sample step>]"
97 << " [--me-threshold <threshold>]"
98 << " [--track-arc]"
99 << " [--track-circle]"
100 << " [--save <filename>]"
101 << " [--help] [-h]\n"
102 << std::endl;
103 return EXIT_SUCCESS;
104 }
105 else {
106 std::cout << "\nError: wrong parameter " << argv[i] << std::endl;
107 return EXIT_FAILURE;
108 }
109 }
110
111 try {
113 int opt_device = 0; // For OpenCV and V4l2 grabber to set the camera device
114
116#if defined(VISP_HAVE_V4L2)
118 std::ostringstream device;
119 device << "/dev/video" << opt_device;
120 std::cout << "Use Video 4 Linux grabber on device " << device.str() << std::endl;
121 g.setDevice(device.str());
122 g.setScale(1);
123 g.open(I);
124#elif defined(VISP_HAVE_DC1394)
125 (void)opt_device; // To avoid non used warning
126 std::cout << "Use DC1394 grabber" << std::endl;
128 g.open(I);
129#elif defined(VISP_HAVE_CMU1394)
130 (void)opt_device; // To avoid non used warning
131 std::cout << "Use CMU1394 grabber" << std::endl;
133 g.open(I);
134#elif defined(VISP_HAVE_FLYCAPTURE)
135 (void)opt_device; // To avoid non used warning
136 std::cout << "Use FlyCapture grabber" << std::endl;
138 g.open(I);
139#elif defined(VISP_HAVE_REALSENSE2)
140 (void)opt_device; // To avoid non used warning
141 std::cout << "Use Realsense 2 grabber" << std::endl;
142 vpRealSense2 g;
143 rs2::config config;
144 config.disable_stream(RS2_STREAM_DEPTH);
145 config.disable_stream(RS2_STREAM_INFRARED);
146 config.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_RGBA8, 30);
147 g.open(config);
148 g.acquire(I);
149#elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI))|| ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
150 std::cout << "Use OpenCV grabber on device " << opt_device << std::endl;
151 cv::VideoCapture g(opt_device); // Open the default camera
152 if (!g.isOpened()) { // Check if we succeeded
153 std::cout << "Failed to open the camera" << std::endl;
154 return EXIT_FAILURE;
155 }
156 int i = 0;
157 cv::Mat frame;
158 while ((i++ < 20) && !g.read(frame)) {
159 } // warm up camera by skiping unread frames
160 g >> frame; // get a new frame from camera
161 vpImageConvert::convert(frame, I);
162#endif
164
165#if defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394) || defined(VISP_HAVE_CMU1394) || defined(VISP_HAVE_FLYCAPTURE) || defined(VISP_HAVE_REALSENSE2)
166 g.acquire(I);
167#elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI))|| ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
168 g >> frame; // get a new frame from camera
169 vpImageConvert::convert(frame, I);
170#endif
171
172#if defined(VISP_HAVE_DISPLAY)
173#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
174 display = vpDisplayFactory::createDisplay(I, 0, 0, "Camera view");
175#else
176 display = vpDisplayFactory::allocateDisplay(I, 0, 0, "Camera view");
177#endif
178#else
179 std::cout << "No image viewer is available..." << std::endl;
180#endif
181
184
185 vpVideoWriter *writer = nullptr;
187 if (!opt_save_filename.empty()) {
188 std::string parent = vpIoTools::getParent(opt_save_filename);
189 if (!parent.empty()) {
190 std::cout << "Create output directory: " << parent << std::endl;
192 }
193
194 writer = new vpVideoWriter();
195 writer->setFileName(opt_save_filename);
196 writer->open(O);
197 }
198
199 vpMe me;
200 me.setRange(opt_me_range);
202 me.setThreshold(opt_me_threshold);
203 me.setSampleStep(opt_me_sample_step);
204
205 std::cout << "Moving-edges settings" << std::endl;
206 me.print();
207
208 std::cout << "\nTracker settings" << std::endl;
209 std::cout << " Tracker type....................."
210 << (opt_track_arc ? std::string("arc of ") : std::string())
211 << (opt_track_circle ? std::string("circle") : std::string("ellipse")) << std::endl;
212 std::cout << " Save results....................." << (opt_save_filename.empty() ? std::string("n/a") : opt_save_filename) << std::endl << std::endl;
214 vpMeEllipse ellipse;
215 ellipse.setMe(&me);
216 ellipse.setDisplay(vpMeSite::RANGE_RESULT);
217 ellipse.initTracking(I, opt_track_circle, opt_track_arc);
219
220 bool quit = false;
221 while (!quit) {
222#if defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394) || defined(VISP_HAVE_CMU1394) || defined(VISP_HAVE_FLYCAPTURE) || defined(VISP_HAVE_REALSENSE2)
223 g.acquire(I);
224#elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI))|| ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
225 g >> frame;
226 vpImageConvert::convert(frame, I);
227#endif
229 vpDisplay::displayText(I, 20, 20, "Click to quit", vpColor::red);
230 ellipse.track(I);
231 ellipse.display(I, vpColor::red);
232 if (vpDisplay::getClick(I, false)) {
233 quit = true;
234 }
236
237 if (!opt_save_filename.empty()) {
239 writer->saveFrame(O);
240 }
241 }
242 }
243 catch (const vpException &e) {
244 std::cout << "Catch an exception: " << e << std::endl;
245 }
246#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
247 if (display != nullptr) {
248 delete display;
249 }
250#endif
251}
252
253#else
254
255int main()
256{
257#if defined(VISP_HAVE_OPENCV)
258 std::cout << "Install a 3rd party dedicated to frame grabbing (dc1394, cmu1394, v4l2, OpenCV, FlyCapture, "
259 << "Realsense2), configure and build ViSP again to use this tutorial."
260 << std::endl;
261#else
262 std::cout << "Install OpenCV 3rd party, configure and build ViSP again to use this tutorial." << std::endl;
263#endif
264 return EXIT_SUCCESS;
265}
266#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 getImage(const vpImage< unsigned char > &Is, vpImage< vpRGBa > &Id)
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
static void makeDirectory(const std::string &dirname)
static std::string getParent(const std::string &pathname)
Class that tracks an ellipse or a circle using moving edges.
@ RANGE_RESULT
Definition vpMeSite.h:85
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)
Class that enables to write easily a video file or a sequence of images.
void saveFrame(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
void open(vpImage< vpRGBa > &I)
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.