Visual Servoing Platform  version 3.6.1 under development (2025-01-11)
tutorial-mb-generic-tracker-apriltag-webcam.cpp
1 #include <iostream>
3 
4 #include <visp3/core/vpConfig.h>
5 
7 // #undef VISP_HAVE_V4L2
8 // #undef HAVE_OPENCV_HIGHGUI
9 // #undef HAVE_OPENCV_VIDEOIO
11 
13 #if defined(VISP_HAVE_APRILTAG) && defined(VISP_HAVE_MODULE_MBT) && \
14  (defined(VISP_HAVE_V4L2) || \
15  ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || \
16  ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)))
18 
19 #include <fstream>
20 #include <ios>
21 
22 #ifdef VISP_HAVE_MODULE_SENSOR
23 #include <visp3/sensor/vpV4l2Grabber.h>
24 #endif
25 #include <visp3/core/vpXmlParserCamera.h>
26 #include <visp3/detection/vpDetectorAprilTag.h>
27 #include <visp3/gui/vpDisplayGDI.h>
28 #include <visp3/gui/vpDisplayOpenCV.h>
29 #include <visp3/gui/vpDisplayX.h>
30 #include <visp3/mbt/vpMbGenericTracker.h>
31 
32 #if (VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)
33 #include <opencv2/highgui/highgui.hpp> // for cv::VideoCapture
34 #elif (VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)
35 #include <opencv2/videoio/videoio.hpp> // for cv::VideoCapture
36 #endif
37 
38 #ifdef ENABLE_VISP_NAMESPACE
39 using namespace VISP_NAMESPACE_NAME;
40 #endif
41 
42 typedef enum { state_detection, state_tracking, state_quit } state_t;
43 
44 // Creates a cube.cao file in your current directory
45 // cubeEdgeSize : size of cube edges in meters
46 void createCaoFile(double cubeEdgeSize)
47 {
48  std::ofstream fileStream;
49  fileStream.open("cube.cao", std::ofstream::out | std::ofstream::trunc);
50  fileStream << "V1\n";
51  fileStream << "# 3D Points\n";
52  fileStream << "8 # Number of points\n";
53  fileStream << cubeEdgeSize / 2 << " " << cubeEdgeSize / 2 << " " << 0 << " # Point 0: (X, Y, Z)\n";
54  fileStream << cubeEdgeSize / 2 << " " << -cubeEdgeSize / 2 << " " << 0 << " # Point 1\n";
55  fileStream << -cubeEdgeSize / 2 << " " << -cubeEdgeSize / 2 << " " << 0 << " # Point 2\n";
56  fileStream << -cubeEdgeSize / 2 << " " << cubeEdgeSize / 2 << " " << 0 << " # Point 3\n";
57  fileStream << -cubeEdgeSize / 2 << " " << cubeEdgeSize / 2 << " " << -cubeEdgeSize << " # Point 4\n";
58  fileStream << -cubeEdgeSize / 2 << " " << -cubeEdgeSize / 2 << " " << -cubeEdgeSize << " # Point 5\n";
59  fileStream << cubeEdgeSize / 2 << " " << -cubeEdgeSize / 2 << " " << -cubeEdgeSize << " # Point 6\n";
60  fileStream << cubeEdgeSize / 2 << " " << cubeEdgeSize / 2 << " " << -cubeEdgeSize << " # Point 7\n";
61  fileStream << "# 3D Lines\n";
62  fileStream << "0 # Number of lines\n";
63  fileStream << "# Faces from 3D lines\n";
64  fileStream << "0 # Number of faces\n";
65  fileStream << "# Faces from 3D points\n";
66  fileStream << "6 # Number of faces\n";
67  fileStream << "4 0 3 2 1 # Face 0: [number of points] [index of the 3D points]...\n";
68  fileStream << "4 1 2 5 6\n";
69  fileStream << "4 4 7 6 5\n";
70  fileStream << "4 0 7 4 3\n";
71  fileStream << "4 5 2 3 4\n";
72  fileStream << "4 0 1 6 7 # Face 5\n";
73  fileStream << "# 3D cylinders\n";
74  fileStream << "0 # Number of cylinders\n";
75  fileStream << "# 3D circles\n";
76  fileStream << "0 # Number of circles\n";
77  fileStream.close();
78 }
79 
80 state_t detectAprilTag(const vpImage<unsigned char> &I, vpDetectorAprilTag &detector, double tagSize,
81  const vpCameraParameters &cam, vpHomogeneousMatrix &cMo)
82 {
83  std::vector<vpHomogeneousMatrix> cMo_vec;
84 
85  // Detection
86  bool ret = detector.detect(I, tagSize, cam, cMo_vec);
87 
88  // Display camera pose
89  for (size_t i = 0; i < cMo_vec.size(); i++) {
90  vpDisplay::displayFrame(I, cMo_vec[i], cam, tagSize / 2, vpColor::none, 3);
91  }
92 
93  vpDisplay::displayText(I, 40, 20, "State: waiting tag detection", vpColor::red);
94 
95  if (ret && detector.getNbObjects() > 0) { // if tag detected, we pick the first one
96  cMo = cMo_vec[0];
97  return state_tracking;
98  }
99 
100  return state_detection;
101 }
102 
103 state_t track(const vpImage<unsigned char> &I, vpMbGenericTracker &tracker, double projection_error_threshold,
104  vpHomogeneousMatrix &cMo)
105 {
106  vpCameraParameters cam;
107  tracker.getCameraParameters(cam);
108 
109  // Track the object
110  try {
111  tracker.track(I);
112  }
113  catch (...) {
114  return state_detection;
115  }
116 
117  tracker.getPose(cMo);
118 
119  // Detect tracking error
120  double projection_error = tracker.computeCurrentProjectionError(I, cMo, cam);
121  if (projection_error > projection_error_threshold) {
122  return state_detection;
123  }
124 
125  // Display
126  tracker.display(I, cMo, cam, vpColor::red, 2);
127  vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
128  vpDisplay::displayText(I, 40, 20, "State: tracking in progress", vpColor::red);
129  {
130  std::stringstream ss;
131  ss << "Features: edges " << tracker.getNbFeaturesEdge() << ", klt " << tracker.getNbFeaturesKlt();
132  vpDisplay::displayText(I, 60, 20, ss.str(), vpColor::red);
133  }
134 
135  return state_tracking;
136 }
137 
138 int main(int argc, const char **argv)
139 {
140  int opt_device = 0;
142  double opt_tag_size = 0.08;
143  float opt_quad_decimate = 1.0;
144  int opt_nthreads = 1;
145  std::string opt_intrinsic_file = "";
146  std::string opt_camera_name = "";
147  double opt_cube_size = 0.125; // 12.5cm by default
148 #ifdef VISP_HAVE_OPENCV
149  bool opt_use_texture = false;
150 #endif
151  double opt_projection_error_threshold = 40.;
152 
153 #if !(defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
154  bool display_off = true;
155 #else
156  bool display_off = false;
157 #endif
158 
159  for (int i = 1; i < argc; i++) {
160  if (std::string(argv[i]) == "--tag_size" && i + 1 < argc) {
161  opt_tag_size = atof(argv[i + 1]);
162  }
163  else if (std::string(argv[i]) == "--input" && i + 1 < argc) {
164  opt_device = atoi(argv[i + 1]);
165  }
166  else if (std::string(argv[i]) == "--quad-decimate" && i + 1 < argc) {
167  opt_quad_decimate = (float)atof(argv[i + 1]);
168  }
169  else if (std::string(argv[i]) == "--nthreads" && i + 1 < argc) {
170  opt_nthreads = atoi(argv[i + 1]);
171  }
172  else if (std::string(argv[i]) == "--intrinsic" && i + 1 < argc) {
173  opt_intrinsic_file = std::string(argv[i + 1]);
174  }
175  else if (std::string(argv[i]) == "--camera-name" && i + 1 < argc) {
176  opt_camera_name = std::string(argv[i + 1]);
177  }
178  else if (std::string(argv[i]) == "--display-off") {
179  display_off = true;
180  }
181  else if (std::string(argv[i]) == "--tag-family" && i + 1 < argc) {
182  opt_tag_family = (vpDetectorAprilTag::vpAprilTagFamily)atoi(argv[i + 1]);
183  }
184  else if (std::string(argv[i]) == "--cube-size" && i + 1 < argc) {
185  opt_cube_size = atof(argv[i + 1]);
186 #ifdef VISP_HAVE_OPENCV
187  }
188  else if (std::string(argv[i]) == "--texture") {
189  opt_use_texture = true;
190 #endif
191  }
192  else if (std::string(argv[i]) == "--projection-error" && i + 1 < argc) {
193  opt_projection_error_threshold = atof(argv[i + 1]);
194  }
195  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
196  std::cout << "Usage: " << argv[0]
197  << " [--input <camera id>]"
198  << " [--cube-size <size in m>]"
199  << " [--tag-size <size in m>]"
200  << " [--quad-decimate <decimation>]"
201  << " [--nthreads <nb>]"
202  << " [--intrinsic <xml intrinsic file>]"
203  << " [--camera-name <camera name in xml file>]"
204  << " [--tag-family <0: TAG_36h11, 1: TAG_36h10, 2: TAG_36ARTOOLKIT, 3: TAG_25h9, 4: TAG_25h7, 5: TAG_16h5>]";
205 #if (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
206  std::cout << " [--display-off]";
207 #endif
208  std::cout << " [--texture]"
209  << " [--projection-error <30 - 100>]"
210  << " [--help,-h]" << std::endl;
211  return EXIT_SUCCESS;
212  }
213  }
214 
215  createCaoFile(opt_cube_size);
216 
217  vpCameraParameters cam;
218  bool camIsInit = false;
219 #if defined(VISP_HAVE_PUGIXML)
220  vpXmlParserCamera parser;
221  if (!opt_intrinsic_file.empty() && !opt_camera_name.empty()) {
222  parser.parse(cam, opt_intrinsic_file, opt_camera_name, vpCameraParameters::perspectiveProjWithoutDistortion);
223  camIsInit = true;
224  }
225 #endif
226 
227  try {
229 
231 #if defined(VISP_HAVE_V4L2)
232  vpV4l2Grabber g;
233  std::ostringstream device;
234  device << "/dev/video" << opt_device;
235  std::cout << "Use Video 4 Linux grabber on device " << device.str() << std::endl;
236  g.setDevice(device.str());
237  g.setScale(1);
238  g.open(I);
239 #elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI))|| ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
240  std::cout << "Use OpenCV grabber on device " << opt_device << std::endl;
241  cv::VideoCapture g(opt_device); // Open the default camera
242  if (!g.isOpened()) { // Check if we succeeded
243  std::cout << "Failed to open the camera" << std::endl;
244  return EXIT_FAILURE;
245  }
246  cv::Mat frame;
247  g >> frame; // get a new frame from camera
248  vpImageConvert::convert(frame, I);
249 #endif
251  if (!camIsInit) {
252  cam.initPersProjWithoutDistortion(600, 600, I.getWidth() / 2., I.getHeight() / 2.);
253  }
254 
255  std::cout << "Cube size: " << opt_cube_size << std::endl;
256  std::cout << "AprilTag size: " << opt_tag_size << std::endl;
257  std::cout << "AprilTag family: " << opt_tag_family << std::endl;
258  std::cout << "Camera parameters:\n" << cam << std::endl;
259  std::cout << "Detection: " << std::endl;
260  std::cout << " Quad decimate: " << opt_quad_decimate << std::endl;
261  std::cout << " Threads number: " << opt_nthreads << std::endl;
262  std::cout << "Tracker: " << std::endl;
263  std::cout << " Use edges : 1" << std::endl;
264  std::cout << " Use texture: "
265 #ifdef VISP_HAVE_OPENCV
266  << opt_use_texture << std::endl;
267 #else
268  << " na" << std::endl;
269 #endif
270  std::cout << " Projection error: " << opt_projection_error_threshold << std::endl;
271 
272  // Construct display
273  vpDisplay *d = nullptr;
274  if (!display_off) {
275 #ifdef VISP_HAVE_X11
276  d = new vpDisplayX(I);
277 #elif defined(VISP_HAVE_GDI)
278  d = new vpDisplayGDI(I);
279 #elif defined(HAVE_OPENCV_HIGHGUI)
280  d = new vpDisplayOpenCV(I);
281 #endif
282  }
283 
284  // Initialize AprilTag detector
285  vpDetectorAprilTag detector(opt_tag_family);
286  detector.setAprilTagQuadDecimate(opt_quad_decimate);
287  detector.setAprilTagNbThreads(opt_nthreads);
288 
289  // Prepare MBT
290  vpMbGenericTracker tracker;
291 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
292  if (opt_use_texture)
293  tracker.setTrackerType(vpMbGenericTracker::EDGE_TRACKER | vpMbGenericTracker::KLT_TRACKER);
294  else
295 #endif
297  // edges
298  vpMe me;
299  me.setMaskSize(5);
300  me.setMaskNumber(180);
301  me.setRange(12);
303  me.setThreshold(20);
304  me.setMu1(0.5);
305  me.setMu2(0.5);
306  me.setSampleStep(4);
307  tracker.setMovingEdge(me);
308 
309 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
310  if (opt_use_texture) {
311  vpKltOpencv klt_settings;
312  klt_settings.setMaxFeatures(300);
313  klt_settings.setWindowSize(5);
314  klt_settings.setQuality(0.015);
315  klt_settings.setMinDistance(8);
316  klt_settings.setHarrisFreeParameter(0.01);
317  klt_settings.setBlockSize(3);
318  klt_settings.setPyramidLevels(3);
319  tracker.setKltOpencv(klt_settings);
320  tracker.setKltMaskBorder(5);
321  }
322 #endif
323 
324  // camera calibration params
325  tracker.setCameraParameters(cam);
326  // model definition
327  tracker.loadModel("cube.cao");
328  tracker.setDisplayFeatures(true);
329  tracker.setAngleAppear(vpMath::rad(70));
330  tracker.setAngleDisappear(vpMath::rad(80));
331 
333  state_t state = state_detection;
334 
335  // wait for a tag detection
336  while (state != state_quit) {
337 
338 #if defined(VISP_HAVE_V4L2)
339  g.acquire(I);
340 #elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI))|| ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
341  g >> frame;
342  vpImageConvert::convert(frame, I);
343 #endif
344 
346 
347  if (state == state_detection) {
348  state = detectAprilTag(I, detector, opt_tag_size, cam, cMo);
349 
350  // Initialize the tracker with the result of the detection
351  if (state == state_tracking) {
353  tracker.initFromPose(I, cMo);
355  }
356  }
357 
358  if (state == state_tracking) {
359  state = track(I, tracker, opt_projection_error_threshold, cMo);
360  }
361 
362  vpDisplay::displayText(I, 20, 20, "Click to quit...", vpColor::red);
363  if (vpDisplay::getClick(I, false)) { // exit
364  state = state_quit;
365  }
366 
367  vpDisplay::flush(I);
368  }
369 
370  if (!display_off)
371  delete d;
372  }
373  catch (const vpException &e) {
374  std::cerr << "Catch an exception: " << e.getMessage() << std::endl;
375  }
376 
377  return EXIT_SUCCESS;
378 }
379 
380 #else
381 
382 int main()
383 {
384 #if !defined(VISP_HAVE_APRILTAG)
385  std::cout << "ViSP is not build with Apriltag support" << std::endl;
386 #endif
387 #if !(defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_OPENCV))
388  std::cout << "ViSP is not build with v4l2 or OpenCV support" << std::endl;
389 #else
390  std::cout << "Install missing 3rd parties, configure and build ViSP to run this tutorial" << std::endl;
391 #endif
392 
393  return EXIT_SUCCESS;
394 }
395 
396 #endif
Generic class defining intrinsic camera parameters.
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
@ perspectiveProjWithoutDistortion
Perspective projection without distortion model.
static const vpColor red
Definition: vpColor.h:217
static const vpColor none
Definition: vpColor.h:229
void setAprilTagQuadDecimate(float quadDecimate)
bool detect(const vpImage< unsigned char > &I) VP_OVERRIDE
@ TAG_36h11
AprilTag 36h11 pattern (recommended)
void setAprilTagNbThreads(int nThreads)
size_t getNbObjects() const
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Class that defines generic functionalities for display.
Definition: vpDisplay.h:178
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void displayFrame(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double size, const vpColor &color=vpColor::none, unsigned int thickness=1, const vpImagePoint &offset=vpImagePoint(0, 0), const std::string &frameName="", const vpColor &textColor=vpColor::black, const vpImagePoint &textOffset=vpImagePoint(15, 15))
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
const char * getMessage() const
Definition: vpException.cpp:65
Implementation of an homogeneous matrix and operations on such kind of matrices.
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
Definition: vpKltOpencv.h:79
void setBlockSize(int blockSize)
Definition: vpKltOpencv.h:272
void setQuality(double qualityLevel)
Definition: vpKltOpencv.h:361
void setHarrisFreeParameter(double harris_k)
Definition: vpKltOpencv.h:280
void setMaxFeatures(int maxCount)
Definition: vpKltOpencv.h:320
void setMinDistance(double minDistance)
Definition: vpKltOpencv.h:329
void setWindowSize(int winSize)
Definition: vpKltOpencv.h:382
void setPyramidLevels(int pyrMaxLevel)
Definition: vpKltOpencv.h:348
static double rad(double deg)
Definition: vpMath.h:129
Real-time 6D object pose tracking using its CAD model.
virtual void setCameraParameters(const vpCameraParameters &camera) VP_OVERRIDE
virtual void setDisplayFeatures(bool displayF) VP_OVERRIDE
virtual unsigned int getNbFeaturesEdge() const
virtual double computeCurrentProjectionError(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &_cMo, const vpCameraParameters &_cam) VP_OVERRIDE
virtual void getCameraParameters(vpCameraParameters &camera) const VP_OVERRIDE
virtual void initFromPose(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo) VP_OVERRIDE
virtual void getPose(vpHomogeneousMatrix &cMo) const VP_OVERRIDE
virtual unsigned int getNbFeaturesKlt() const
virtual void setMovingEdge(const vpMe &me)
virtual void setAngleDisappear(const double &a) VP_OVERRIDE
virtual void track(const vpImage< unsigned char > &I) VP_OVERRIDE
virtual void loadModel(const std::string &modelFile, bool verbose=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix()) VP_OVERRIDE
virtual void setTrackerType(int type)
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &col, unsigned int thickness=1, bool displayFullModel=false) VP_OVERRIDE
virtual void setAngleAppear(const double &a) VP_OVERRIDE
Definition: vpMe.h:134
void setMu1(const double &mu_1)
Definition: vpMe.h:385
void setRange(const unsigned int &range)
Definition: vpMe.h:415
void setLikelihoodThresholdType(const vpLikelihoodThresholdType likelihood_threshold_type)
Definition: vpMe.h:505
void setMaskNumber(const unsigned int &mask_number)
Definition: vpMe.cpp:552
void setThreshold(const double &threshold)
Definition: vpMe.h:466
void setSampleStep(const double &sample_step)
Definition: vpMe.h:422
void setMaskSize(const unsigned int &mask_size)
Definition: vpMe.cpp:560
void setMu2(const double &mu_2)
Definition: vpMe.h:392
@ NORMALIZED_THRESHOLD
Definition: vpMe.h:145
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)
void acquire(vpImage< unsigned char > &I)
XML parser to load and save intrinsic camera parameters.
int parse(vpCameraParameters &cam, const std::string &filename, const std::string &camera_name, const vpCameraParameters::vpCameraParametersProjType &projModel, unsigned int image_width=0, unsigned int image_height=0, bool verbose=true)