Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
tutorial-klt-tracker-with-reinit.cpp
1
2#include <iostream>
3
4#include <visp3/core/vpConfig.h>
5
6#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO) && defined(HAVE_OPENCV_VIDEOIO)
7
8#include <visp3/core/vpImageConvert.h>
9#include <visp3/gui/vpDisplayOpenCV.h>
10#include <visp3/io/vpVideoReader.h>
11#include <visp3/klt/vpKltOpencv.h>
12
13int main()
14{
15#ifdef ENABLE_VISP_NAMESPACE
16 using namespace VISP_NAMESPACE_NAME;
17#endif
18
19 try {
20 vpVideoReader reader;
21 reader.setFileName("video-postcard.mp4");
22
24 reader.acquire(I);
25
26 cv::Mat cvI;
27
29
30 // Display initialisation
31 vpDisplayOpenCV d(I, 0, 0, "Klt tracking");
34
36 // Set tracker parameters
37 tracker.setMaxFeatures(200);
38 tracker.setWindowSize(10);
39 tracker.setQuality(0.01);
40 tracker.setMinDistance(15);
41 tracker.setHarrisFreeParameter(0.04);
42 tracker.setBlockSize(9);
43 tracker.setUseHarris(1);
44 tracker.setPyramidLevels(3);
45
46 // Initialise the tracking
47 tracker.initTracking(cvI);
48
49 while (!reader.end()) {
50 reader.acquire(I);
51 std::cout << "Process image " << reader.getFrameIndex() << std::endl;
53
55
57 // Restart the initialization to detect new keypoints
58 if (reader.getFrameIndex() == 25) {
59 std::cout << "Re initialize the tracker" << std::endl;
60
61 // Save of previous features
62 std::vector<cv::Point2f> prev_features = tracker.getFeatures();
63
64 // Start a new feature detection
65 tracker.initTracking(cvI);
66 std::vector<cv::Point2f> new_features = tracker.getFeatures();
67
68 // Add previous features if they are not to close to detected one
69 double distance, minDistance_ = tracker.getMinDistance();
70 for (size_t i = 0; i < prev_features.size(); i++) {
71 // Test if a previous feature is not redundant with one of the newly
72 // detected
73 bool is_redundant = false;
74 for (size_t j = 0; j < new_features.size(); j++) {
75 distance = sqrt(vpMath::sqr(new_features[j].x - prev_features[i].x) +
76 vpMath::sqr(new_features[j].y - prev_features[i].y));
77 if (distance < minDistance_) {
78 is_redundant = true;
79 break;
80 }
81 }
82 if (is_redundant) {
83 continue;
84 }
85 // std::cout << "Add previous feature with index " << i <<
86 // std::endl;
87 tracker.addFeature(prev_features[i]);
88 }
89 }
90 // Track the features
91 tracker.track(cvI);
93
94 std::cout << "tracking of " << tracker.getNbFeatures() << " features" << std::endl;
95
96 tracker.display(I, vpColor::red);
98 }
99
101
102 }
103 catch (const vpException &e) {
104 std::cout << "Catch an exception: " << e << std::endl;
105 return EXIT_FAILURE;
106 }
107 return EXIT_SUCCESS;
108}
109
110#else
111
112int main()
113{
114#if !defined(HAVE_OPENCV_HIGHGUI)
115 std::cout << "This tutorial needs OpenCV highgui module that is missing." << std::endl;
116#endif
117#if !defined(HAVE_OPENCV_IMGPROC)
118 std::cout << "This tutorial needs OpenCV imgproc module that is missing." << std::endl;
119#endif
120#if !defined(HAVE_OPENCV_VIDEO)
121 std::cout << "This tutorial needs OpenCV video module that is missing." << std::endl;
122#endif
123#if !defined(HAVE_OPENCV_VIDEOIO)
124 std::cout << "This tutorial needs OpenCV videoio module that is missing." << std::endl;
125#endif
126}
127
128#endif
static const vpColor red
Definition vpColor.h:198
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 flush(const vpImage< unsigned char > &I)
error that can be emitted by ViSP classes.
Definition vpException.h:60
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
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
static double sqr(double x)
Definition vpMath.h:203
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void setFileName(const std::string &filename)
long getFrameIndex() const
void acquire(vpImage< vpRGBa > &I) VP_OVERRIDE