Visual Servoing Platform
version 3.7.0
Toggle main menu visibility
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
13
int
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
23
vpImage<unsigned char>
I;
24
reader.
acquire
(I);
25
26
cv::Mat cvI;
27
28
vpImageConvert::convert
(I, cvI);
29
30
// Display initialisation
31
vpDisplayOpenCV
d
(I, 0, 0,
"Klt tracking"
);
32
vpDisplay::display
(I);
33
vpDisplay::flush
(I);
34
35
vpKltOpencv
tracker
;
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;
52
vpDisplay::display
(I);
53
54
vpImageConvert::convert
(I, cvI);
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
);
97
vpDisplay::flush
(I);
98
}
99
100
vpDisplay::getClick
(I);
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
112
int
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
vpColor::red
static const vpColor red
Definition
vpColor.h:198
vpDisplayOpenCV
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Definition
vpDisplayOpenCV.h:139
vpDisplay::getClick
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
Definition
vpDisplay_uchar.cpp:894
vpDisplay::display
static void display(const vpImage< unsigned char > &I)
Definition
vpDisplay_uchar.cpp:869
vpDisplay::flush
static void flush(const vpImage< unsigned char > &I)
Definition
vpDisplay_uchar.cpp:845
vpException
error that can be emitted by ViSP classes.
Definition
vpException.h:60
vpImageConvert::convert
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Definition
vpImageConvert.cpp:73
vpImage
Definition of the vpImage class member functions.
Definition
vpImage.h:131
vpKltOpencv
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
Definition
vpKltOpencv.h:83
vpMath::sqr
static double sqr(double x)
Definition
vpMath.h:203
vpVideoReader
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
Definition
vpVideoReader.h:181
vpVideoReader::setFileName
void setFileName(const std::string &filename)
Definition
vpVideoReader.cpp:123
vpVideoReader::end
bool end()
Definition
vpVideoReader.h:255
vpVideoReader::getFrameIndex
long getFrameIndex() const
Definition
vpVideoReader.h:292
vpVideoReader::acquire
void acquire(vpImage< vpRGBa > &I) VP_OVERRIDE
Definition
vpVideoReader.cpp:286
VISP_NAMESPACE_NAME
Definition
vpEigenConversion.h:44
ibvs-four-points.e
e
Definition
ibvs-four-points.py:272
realsense-mbt.tracker
tracker
Definition
realsense-mbt.py:151
yolo-centering-task-afma6.d
d
Definition
yolo-centering-task-afma6.py:273
yolo-centering-task-afma6.j
j
Definition
yolo-centering-task-afma6.py:329
yolo-centering-task-afma6.i
i
Definition
yolo-centering-task-afma6.py:329
tutorial
tracking
keypoint
tutorial-klt-tracker-with-reinit.cpp
Generated by
1.17.0