Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
tutorial-blob-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 VISP_HAVE_DC1394
10// #undef VISP_HAVE_CMU1394
11// #undef VISP_HAVE_FLYCAPTURE
12// #undef VISP_HAVE_REALSENSE2
13// #undef HAVE_OPENCV_HIGHGUI
14// #undef HAVE_OPENCV_VIDEOIO
16
17#if defined(VISP_HAVE_DISPLAY) && \
18 (defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394) || defined(VISP_HAVE_CMU1394) || \
19 defined(VISP_HAVE_FLYCAPTURE) || defined(VISP_HAVE_REALSENSE2) || defined(VISP_HAVE_OPENCV) && \
20 (((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || \
21 ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))))
22
23#ifdef VISP_HAVE_MODULE_SENSOR
24#include <visp3/sensor/vp1394CMUGrabber.h>
25#include <visp3/sensor/vp1394TwoGrabber.h>
26#include <visp3/sensor/vpFlyCaptureGrabber.h>
27#include <visp3/sensor/vpRealSense2.h>
28#include <visp3/sensor/vpV4l2Grabber.h>
29#endif
30#include <visp3/blob/vpDot2.h>
31#include <visp3/gui/vpDisplayFactory.h>
32
33#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)
34#include <opencv2/highgui/highgui.hpp> // for cv::VideoCapture
35#elif defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)
36#include <opencv2/videoio/videoio.hpp>
37#endif
38
39int main()
40{
41#ifdef ENABLE_VISP_NAMESPACE
42 using namespace VISP_NAMESPACE_NAME;
43#endif
44
45 vpImage<unsigned char> I; // Create a gray level image container
46 int opt_device = 0; // For OpenCV and V4l2 grabber to set the camera device
47
49#if defined(VISP_HAVE_V4L2)
51 std::ostringstream device;
52 device << "/dev/video" << opt_device;
53 std::cout << "Use Video 4 Linux grabber on device " << device.str() << std::endl;
54 g.setDevice(device.str());
55 g.setScale(1);
56 g.open(I);
57#elif defined(VISP_HAVE_DC1394)
58 (void)opt_device; // To avoid non used warning
59 std::cout << "Use DC1394 grabber" << std::endl;
61 g.open(I);
62#elif defined(VISP_HAVE_CMU1394)
63 (void)opt_device; // To avoid non used warning
64 std::cout << "Use CMU1394 grabber" << std::endl;
66 g.open(I);
67#elif defined(VISP_HAVE_FLYCAPTURE)
68 (void)opt_device; // To avoid non used warning
69 std::cout << "Use FlyCapture grabber" << std::endl;
71 g.open(I);
72#elif defined(VISP_HAVE_REALSENSE2)
73 (void)opt_device; // To avoid non used warning
74 std::cout << "Use Realsense 2 grabber" << std::endl;
76 rs2::config config;
77 config.disable_stream(RS2_STREAM_DEPTH);
78 config.disable_stream(RS2_STREAM_INFRARED);
79 config.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_RGBA8, 30);
80 g.open(config);
81 g.acquire(I);
82#elif defined(VISP_HAVE_OPENCV) && \
83 (((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || \
84 ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)))
85 cv::VideoCapture g(opt_device); // open the default camera
86 if (!g.isOpened()) { // check if we succeeded
87 std::cout << "Failed to open the camera" << std::endl;
88 return EXIT_FAILURE;
89 }
90 cv::Mat frame;
91 g >> frame; // get a new frame from camera
93#endif
94
95#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
96 std::shared_ptr<vpDisplay> display = vpDisplayFactory::createDisplay(I, 0, 0, "Camera view");
97#else
98 vpDisplay *display = vpDisplayFactory::allocateDisplay(I, 0, 0, "Camera view");
99#endif
100
102 vpDot2 blob;
105 blob.setGraphics(true);
106 blob.setGraphicsThickness(2);
108
109 vpImagePoint germ;
110 bool init_done = false;
111 bool quit = false;
112 bool germ_selected = false;
114
115 while (!quit) {
116 try {
117#if defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394) || defined(VISP_HAVE_CMU1394) || defined(VISP_HAVE_FLYCAPTURE) || defined(VISP_HAVE_REALSENSE2)
118 g.acquire(I);
119#elif defined(VISP_HAVE_OPENCV) && \
120 (((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || \
121 ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)))
122 g >> frame;
123 vpImageConvert::convert(frame, I);
124#endif
126 vpDisplay::displayText(I, 20, 20, "Left click in the blob to initialize the tracker", vpColor::red);
127 vpDisplay::displayText(I, 40, 20, "Right click to quit", vpColor::red);
128
129 if (vpDisplay::getClick(I, germ, button, false)) {
130 if (button == vpMouseButton::button3) {
131 quit = true;
132 }
133 else {
134 germ_selected = true;
135 }
136 }
137 if (germ_selected && !init_done) {
139 std::cout << "Tracking initialized" << std::endl;
140 blob.initTracking(I, germ);
142 init_done = true;
143 germ_selected = false;
144 }
145 else if (init_done) {
147 blob.track(I);
149 }
150
152 }
153 catch (const vpException &e) {
154 std::cout << "Tracking failed: " << e.getMessage() << std::endl;
155 init_done = false;
156 }
157 }
158
159#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
160 if (display != nullptr) {
161 delete display;
162 }
163#endif
164}
165
166#else
167int main()
168{
169 std::cout << "There are missing 3rd parties to run this tutorial" << std::endl;
170}
171#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 flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition vpDot2.h:127
void track(const vpImage< unsigned char > &I, bool canMakeTheWindowGrow=true)
Definition vpDot2.cpp:441
void setGraphics(bool activate)
Definition vpDot2.h:320
void setGraphicsThickness(unsigned int thickness)
Definition vpDot2.h:328
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition vpDot2.cpp:263
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)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition of the vpImage class member functions.
Definition vpImage.h:131
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)
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.