Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
tutorial-klt-tracker-live.cpp
1
2#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 HAVE_OPENCV_HIGHGUI
10// #undef HAVE_OPENCV_VIDEOIO
12
13#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO) && \
14 (defined(VISP_HAVE_V4L2) || ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || \
15 ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)))
16
17#ifdef VISP_HAVE_MODULE_SENSOR
18#include <visp3/sensor/vpV4l2Grabber.h>
19#endif
20#include <visp3/core/vpImageConvert.h>
21#include <visp3/gui/vpDisplayOpenCV.h>
22#include <visp3/io/vpVideoReader.h>
23#include <visp3/klt/vpKltOpencv.h>
24
25#if (VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)
26#include <opencv2/highgui/highgui.hpp> // for cv::VideoCapture
27#elif (VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)
28#include <opencv2/videoio/videoio.hpp>
29#endif
30
31int main(int argc, const char *argv[])
32{
33#ifdef ENABLE_VISP_NAMESPACE
34 using namespace VISP_NAMESPACE_NAME;
35#endif
36
37 try {
38 bool opt_init_by_click = false;
39 int opt_device = 0;
40
41 for (int i = 1; i < argc; i++) {
42 if (std::string(argv[i]) == "--init-by-click") {
43 opt_init_by_click = true;
44 }
45 else if (std::string(argv[i]) == "--device" && i + 1 < argc) {
46 opt_device = atoi(argv[++i]);
47 }
48 else if (std::string(argv[i]) == "--help") {
49 std::cout << "Usage: " << argv[0] << " [--init-by-click] [--device <camera device>] [--help]" << std::endl;
50 return EXIT_SUCCESS;
51 }
52 }
53
55
56#if defined(VISP_HAVE_V4L2)
57 std::cout << "Use v4l2 grabber..." << std::endl;
59 std::ostringstream device;
60 device << "/dev/video" << opt_device;
61 g.setDevice(device.str());
62 g.open(I);
63 g.acquire(I);
64#elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
65 std::cout << "Use OpenCV grabber..." << std::endl;
66 cv::VideoCapture g(opt_device);
67 if (!g.isOpened()) { // check if we succeeded
68 std::cout << "Failed to open the camera" << std::endl;
69 return EXIT_FAILURE;
70 }
71 cv::Mat frame;
72 g >> frame; // get a new frame from camera
74#endif
75
76 cv::Mat cvI;
78
79 // Display initialisation
80 vpDisplayOpenCV d(I, 0, 0, "Klt tracking");
83
85 // Set tracker parameters
86 tracker.setMaxFeatures(200);
87 tracker.setWindowSize(10);
88 tracker.setQuality(0.01);
89 tracker.setMinDistance(15);
90 tracker.setHarrisFreeParameter(0.04);
91 tracker.setBlockSize(9);
92 tracker.setUseHarris(1);
93 tracker.setPyramidLevels(3);
94
95 // Initialise the tracking
96 if (opt_init_by_click) {
98 std::vector<cv::Point2f> guess;
99 vpImagePoint ip;
100 do {
101 vpDisplay::displayText(I, 10, 10, "Left click to select a point, right to start tracking", vpColor::red);
102 if (vpDisplay::getClick(I, ip, button, false)) {
103 if (button == vpMouseButton::button1) {
104 guess.push_back(cv::Point2f(static_cast<float>(ip.get_u()), static_cast<float>(ip.get_v())));
105 vpDisplay::displayText(I, 10, 10, "Left click to select a point, right to start tracking", vpColor::red);
107 }
108 }
110 vpTime::wait(20);
111 } while (button != vpMouseButton::button3);
112 tracker.initTracking(cvI, guess);
113 }
114 else {
115 tracker.initTracking(cvI);
116 }
117
118 while (1) {
119#if defined(VISP_HAVE_V4L2)
120 g.acquire(I);
121#elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
122 g >> frame;
123 vpImageConvert::convert(frame, I);
124#endif
126
128 tracker.track(cvI);
129
130 tracker.display(I, vpColor::red);
131 vpDisplay::displayText(I, 10, 10, "Click to quit", vpColor::red);
133 if (vpDisplay::getClick(I, false))
134 break;
135 }
136 }
137 catch (const vpException &e) {
138 std::cout << "Catch an exception: " << e << std::endl;
139 return EXIT_FAILURE;
140 }
141 return EXIT_SUCCESS;
142}
143
144#else
145
146int main()
147{
148#if !defined(HAVE_OPENCV_HIGHGUI)
149 std::cout << "This tutorial needs OpenCV highgui module that is missing." << std::endl;
150#endif
151#if !defined(HAVE_OPENCV_IMGPROC)
152 std::cout << "This tutorial needs OpenCV imgproc module that is missing." << std::endl;
153#endif
154#if !defined(HAVE_OPENCV_VIDEO)
155 std::cout << "This tutorial needs OpenCV video module that is missing." << std::endl;
156#endif
157#if !(defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_OPENCV) && \
158 (((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || \
159 ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))))
160 std::cout << "This tutorial needs V4l2 or OpenCV grabber capabilities." << std::endl;
161#endif
162}
163
164#endif
static const vpColor red
Definition vpColor.h:198
static const vpColor green
Definition vpColor.h:201
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 displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
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
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 ...
double get_u() const
double get_v() const
Definition of the vpImage class member functions.
Definition vpImage.h:131
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
Definition vpKltOpencv.h:83
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
void open(vpImage< unsigned char > &I)
void setDevice(const std::string &devname)
void acquire(vpImage< unsigned char > &I)
VISP_EXPORT int wait(double t0, double t)