Visual Servoing Platform
version 3.7.0
Toggle main menu visibility
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
31
int
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
54
vpImage<unsigned char>
I;
55
56
#if defined(VISP_HAVE_V4L2)
57
std::cout <<
"Use v4l2 grabber..."
<< std::endl;
58
vpV4l2Grabber
g;
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
73
vpImageConvert::convert
(frame, I);
74
#endif
75
76
cv::Mat cvI;
77
vpImageConvert::convert
(I, cvI);
78
79
// Display initialisation
80
vpDisplayOpenCV
d
(I, 0, 0,
"Klt tracking"
);
81
vpDisplay::display
(I);
82
vpDisplay::flush
(I);
83
84
vpKltOpencv
tracker
;
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) {
97
vpMouseButton::vpMouseButtonType
button;
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
);
106
vpDisplay::displayCross
(I, ip, 12,
vpColor::green
);
107
}
108
}
109
vpDisplay::flush
(I);
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
125
vpDisplay::display
(I);
126
127
vpImageConvert::convert
(I, cvI);
128
tracker
.track(cvI);
129
130
tracker
.display(I,
vpColor::red
);
131
vpDisplay::displayText
(I, 10, 10,
"Click to quit"
,
vpColor::red
);
132
vpDisplay::flush
(I);
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
146
int
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
vpColor::red
static const vpColor red
Definition
vpColor.h:198
vpColor::green
static const vpColor green
Definition
vpColor.h:201
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::displayCross
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
Definition
vpDisplay_uchar.cpp:205
vpDisplay::flush
static void flush(const vpImage< unsigned char > &I)
Definition
vpDisplay_uchar.cpp:845
vpDisplay::displayText
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Definition
vpDisplay_uchar.cpp:788
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
vpImagePoint
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition
vpImagePoint.h:82
vpImagePoint::get_u
double get_u() const
Definition
vpImagePoint.h:136
vpImagePoint::get_v
double get_v() const
Definition
vpImagePoint.h:147
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
vpMouseButton::vpMouseButtonType
vpMouseButtonType
Definition
vpMouseButton.h:49
vpMouseButton::button1
@ button1
Definition
vpMouseButton.h:50
vpMouseButton::button3
@ button3
Definition
vpMouseButton.h:52
vpV4l2Grabber
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
Definition
vpV4l2Grabber.h:130
vpV4l2Grabber::open
void open(vpImage< unsigned char > &I)
Definition
vpV4l2Grabber.cpp:405
vpV4l2Grabber::setDevice
void setDevice(const std::string &devname)
Definition
vpV4l2Grabber.h:290
vpV4l2Grabber::acquire
void acquire(vpImage< unsigned char > &I)
Definition
vpV4l2Grabber.cpp:525
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
vpTime::wait
VISP_EXPORT int wait(double t0, double t)
yolo-centering-task-afma6.d
d
Definition
yolo-centering-task-afma6.py:273
yolo-centering-task-afma6.i
i
Definition
yolo-centering-task-afma6.py:329
tutorial
tracking
keypoint
tutorial-klt-tracker-live.cpp
Generated by
1.17.0