Visual Servoing Platform  version 3.6.1 under development (2025-02-19)
tutorial-apriltag-detector-live.cpp
1 #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 
18 #if defined(VISP_HAVE_APRILTAG) && \
19  (defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394) || defined(VISP_HAVE_CMU1394) || \
20  defined(VISP_HAVE_FLYCAPTURE) || defined(VISP_HAVE_REALSENSE2) || \
21  ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || \
22  ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)))
23 
25 
26 #ifdef VISP_HAVE_MODULE_SENSOR
27 #include <visp3/sensor/vp1394CMUGrabber.h>
28 #include <visp3/sensor/vp1394TwoGrabber.h>
29 #include <visp3/sensor/vpFlyCaptureGrabber.h>
30 #include <visp3/sensor/vpRealSense2.h>
31 #include <visp3/sensor/vpV4l2Grabber.h>
32 #endif
34 #include <visp3/detection/vpDetectorAprilTag.h>
36 #include <visp3/core/vpXmlParserCamera.h>
37 #include <visp3/gui/vpDisplayFactory.h>
38 
39 #if (VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)
40 #include <opencv2/highgui/highgui.hpp> // for cv::VideoCapture
41 #elif (VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)
42 #include <opencv2/videoio/videoio.hpp>
43 #endif
44 
45 int main(int argc, const char **argv)
46 {
47 #ifdef ENABLE_VISP_NAMESPACE
48  using namespace VISP_NAMESPACE_NAME;
49 #endif
50 
51  int opt_device = 0; // For OpenCV and V4l2 grabber to set the camera device
54  double tagSize = 0.053;
55  float quad_decimate = 1.0;
56  int nThreads = 1;
57  std::string intrinsic_file = "";
58  std::string camera_name = "";
59  bool display_tag = false;
60  int color_id = -1;
61  unsigned int thickness = 2;
62  bool align_frame = false;
63 
64 #if !(defined(VISP_HAVE_DISPLAY))
65  bool display_off = true;
66  std::cout << "Warning: There is no 3rd party (X11, GDI or openCV) to dislay images..." << std::endl;
67 #else
68  bool display_off = false;
69 #endif
70 
72 
73  for (int i = 1; i < argc; i++) {
74  if (std::string(argv[i]) == "--pose-method" && i + 1 < argc) {
75  poseEstimationMethod = (vpDetectorAprilTag::vpPoseEstimationMethod)atoi(argv[++i]);
76  }
77  else if (std::string(argv[i]) == "--tag-size" && i + 1 < argc) {
78  tagSize = atof(argv[++i]);
79  }
80  else if (std::string(argv[i]) == "--camera-device" && i + 1 < argc) {
81  opt_device = atoi(argv[++i]);
82  }
83  else if (std::string(argv[i]) == "--quad-decimate" && i + 1 < argc) {
84  quad_decimate = (float)atof(argv[++i]);
85  }
86  else if (std::string(argv[i]) == "--nthreads" && i + 1 < argc) {
87  nThreads = atoi(argv[++i]);
88  }
89  else if (std::string(argv[i]) == "--intrinsic" && i + 1 < argc) {
90  intrinsic_file = std::string(argv[++i]);
91  }
92  else if (std::string(argv[i]) == "--camera-name" && i + 1 < argc) {
93  camera_name = std::string(argv[++i]);
94  }
95  else if (std::string(argv[i]) == "--display-tag") {
96  display_tag = true;
97  }
98  else if (std::string(argv[i]) == "--display-off") {
99  display_off = true;
100  }
101  else if (std::string(argv[i]) == "--color" && i + 1 < argc) {
102  color_id = atoi(argv[++i]);
103  }
104  else if (std::string(argv[i]) == "--thickness" && i + 1 < argc) {
105  thickness = (unsigned int)atoi(argv[++i]);
106  }
107  else if (std::string(argv[i]) == "--tag-family" && i + 1 < argc) {
108  tagFamily = (vpDetectorAprilTag::vpAprilTagFamily)atoi(argv[++i]);
109  }
110  else if (std::string(argv[i]) == "--z-aligned") {
111  align_frame = true;
112  }
113  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
114  std::cout << "Usage: " << argv[0] << " [--camera-device <camera device> (default: 0)]"
115  << " [--tag-size <tag_size in m> (default: 0.053)]"
116  << " [--quad-decimate <quad_decimate> (default: 1)]"
117  << " [--nthreads <nb> (default: 1)]"
118  << " [--intrinsic <intrinsic file> (default: empty)]"
119  << " [--camera-name <camera name> (default: empty)]"
120  << " [--pose-method <method> (0: HOMOGRAPHY, 1: HOMOGRAPHY_VIRTUAL_VS, "
121  << " 2: DEMENTHON_VIRTUAL_VS, 3: LAGRANGE_VIRTUAL_VS, "
122  << " 4: BEST_RESIDUAL_VIRTUAL_VS, 5: HOMOGRAPHY_ORTHOGONAL_ITERATION) (default: 0)]"
123  << " [--tag-family <family> (0: TAG_36h11, 1: TAG_36h10 (DEPRECATED), 2: TAG_36ARTOOLKIT (DEPRECATED),"
124  << " 3: TAG_25h9, 4: TAG_25h7 (DEPRECATED), 5: TAG_16h5, 6: TAG_CIRCLE21h7, 7: TAG_CIRCLE49h12,"
125  << " 8: TAG_CUSTOM48h12, 9: TAG_STANDARD41h12, 10: TAG_STANDARD52h13) (default: 0)]"
126  << " [--display-tag] [--z-aligned]";
127 #if (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
128  std::cout << " [--display-off] [--color <color id>] [--thickness <line thickness>]";
129 #endif
130  std::cout << " [--help,-h]" << std::endl;
131  return EXIT_SUCCESS;
132  }
133  }
134 
135 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
136  std::shared_ptr<vpDisplay> display;
137 #else
138  vpDisplay *display = nullptr;
139 #endif
140 
141  try {
142  vpCameraParameters cam;
143  cam.initPersProjWithoutDistortion(615.1674805, 615.1675415, 312.1889954, 243.4373779);
144 #if defined(VISP_HAVE_PUGIXML)
145  vpXmlParserCamera parser;
146  if (!intrinsic_file.empty() && !camera_name.empty())
147  parser.parse(cam, intrinsic_file, camera_name, vpCameraParameters::perspectiveProjWithoutDistortion);
148 #endif
149 
151 #if defined(VISP_HAVE_V4L2)
152  vpV4l2Grabber g;
153  std::ostringstream device;
154  device << "/dev/video" << opt_device;
155  std::cout << "Use Video 4 Linux grabber on device " << device.str() << std::endl;
156  g.setDevice(device.str());
157  g.setScale(1);
158  g.open(I);
159 #elif defined(VISP_HAVE_DC1394)
160  (void)opt_device; // To avoid non used warning
161  std::cout << "Use DC1394 grabber" << std::endl;
163  g.open(I);
164 #elif defined(VISP_HAVE_CMU1394)
165  (void)opt_device; // To avoid non used warning
166  std::cout << "Use CMU1394 grabber" << std::endl;
168  g.open(I);
169 #elif defined(VISP_HAVE_FLYCAPTURE)
170  (void)opt_device; // To avoid non used warning
171  std::cout << "Use FlyCapture grabber" << std::endl;
173  g.open(I);
174 #elif defined(VISP_HAVE_REALSENSE2)
175  (void)opt_device; // To avoid non used warning
176  std::cout << "Use Realsense 2 grabber" << std::endl;
177  vpRealSense2 g;
178  rs2::config config;
179  config.disable_stream(RS2_STREAM_DEPTH);
180  config.disable_stream(RS2_STREAM_INFRARED);
181  config.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_RGBA8, 30);
182  g.open(config);
183  g.acquire(I);
184 
185  std::cout << "Read camera parameters from Realsense device" << std::endl;
187 #elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
188  std::cout << "Use OpenCV grabber on device " << opt_device << std::endl;
189  cv::VideoCapture g(opt_device); // Open the default camera
190  if (!g.isOpened()) { // Check if we succeeded
191  std::cout << "Failed to open the camera" << std::endl;
192  return EXIT_FAILURE;
193  }
194  cv::Mat frame;
195  g >> frame; // get a new frame from camera
196  vpImageConvert::convert(frame, I);
197 #endif
199 
200  std::cout << cam << std::endl;
201  std::cout << "poseEstimationMethod: " << poseEstimationMethod << std::endl;
202  std::cout << "tagFamily: " << tagFamily << std::endl;
203  std::cout << "nThreads : " << nThreads << std::endl;
204  std::cout << "Z aligned: " << align_frame << std::endl;
205 
206  if (!display_off) {
207 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
208  display = vpDisplayFactory::createDisplay(I);
209 #else
211 #endif
212  }
213 
215  vpDetectorAprilTag detector(tagFamily);
217 
219  detector.setAprilTagQuadDecimate(quad_decimate);
220  detector.setAprilTagPoseEstimationMethod(poseEstimationMethod);
221  detector.setAprilTagNbThreads(nThreads);
222  detector.setDisplayTag(display_tag, color_id < 0 ? vpColor::none : vpColor::getColor(color_id), thickness);
223  detector.setZAlignedWithCameraAxis(align_frame);
225 
226  std::vector<double> time_vec;
227  for (;;) {
229 #if defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394) || defined(VISP_HAVE_CMU1394) || \
230  defined(VISP_HAVE_FLYCAPTURE) || defined(VISP_HAVE_REALSENSE2)
231  g.acquire(I);
232 #elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
233  g >> frame;
234  vpImageConvert::convert(frame, I);
235 #endif
237 
239 
240  double t = vpTime::measureTimeMs();
242  std::vector<vpHomogeneousMatrix> cMo_vec;
243  detector.detect(I, tagSize, cam, cMo_vec);
245  t = vpTime::measureTimeMs() - t;
246  time_vec.push_back(t);
247 
248  std::stringstream ss;
249  ss << "Detection time: " << t << " ms for " << detector.getNbObjects() << " tags";
250  vpDisplay::displayText(I, 40, 20, ss.str(), vpColor::red);
251 
253  for (size_t i = 0; i < cMo_vec.size(); i++) {
254  vpDisplay::displayFrame(I, cMo_vec[i], cam, tagSize / 2, vpColor::none, 3);
255  }
257 
258  vpDisplay::displayText(I, 20, 20, "Click to quit.", vpColor::red);
259  vpDisplay::flush(I);
260  if (vpDisplay::getClick(I, false))
261  break;
262  }
263 
264  std::cout << "Benchmark computation time" << std::endl;
265  std::cout << "Mean / Median / Std: " << vpMath::getMean(time_vec) << " ms"
266  << " ; " << vpMath::getMedian(time_vec) << " ms"
267  << " ; " << vpMath::getStdev(time_vec) << " ms" << std::endl;
268  }
269  catch (const vpException &e) {
270  std::cerr << "Catch an exception: " << e.getMessage() << std::endl;
271  }
272 
273 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
274  if (display != nullptr) {
275  delete display;
276  }
277 #endif
278 
279  return EXIT_SUCCESS;
280 }
281 
282 #else
283 
284 int main()
285 {
286 #ifndef VISP_HAVE_APRILTAG
287  std::cout << "Enable Apriltag support, configure and build ViSP to run this tutorial" << std::endl;
288 #else
289  std::cout << "Install a 3rd party dedicated to frame grabbing (dc1394, cmu1394, v4l2, OpenCV, FlyCapture, "
290  << "Realsense2), configure and build ViSP again to use this example"
291  << std::endl;
292 #endif
293  return EXIT_SUCCESS;
294 }
295 
296 #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)
Generic class defining intrinsic camera parameters.
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
@ perspectiveProjWithoutDistortion
Perspective projection without distortion model.
static vpColor getColor(const unsigned int &i)
Definition: vpColor.h:292
static const vpColor red
Definition: vpColor.h:198
static const vpColor none
Definition: vpColor.h:210
@ TAG_36h11
AprilTag 36h11 pattern (recommended)
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
void open(vpImage< unsigned char > &I)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static double getMedian(const std::vector< double > &v)
Definition: vpMath.cpp:322
static double getStdev(const std::vector< double > &v, bool useBesselCorrection=false)
Definition: vpMath.cpp:353
static double getMean(const std::vector< double > &v)
Definition: vpMath.cpp:302
vpCameraParameters getCameraParameters(const rs2_stream &stream, vpCameraParameters::vpCameraParametersProjType type=vpCameraParameters::perspectiveProjWithDistortion, int index=-1) const
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)
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)
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.
VISP_EXPORT double measureTimeMs()