Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
tutorial-apriltag-detector.cpp
#include <visp3/core/vpConfig.h>
#include <visp3/detection/vpDetectorAprilTag.h>
#include <visp3/core/vpXmlParserCamera.h>
#include <visp3/gui/vpDisplayFactory.h>
#include <visp3/io/vpImageIo.h>
void usage(const char **argv, int error);
void usage(const char **argv, int error)
{
std::cout << "Synopsis" << std::endl
<< " " << argv[0]
<< " [--input <filename>]"
<< " [--tag-size <size>]"
<< " [--tag-family <family>]"
<< " [--tag-decision-margin-threshold <threshold>]"
<< " [--tag-hamming-distance-threshold <threshold>]"
<< " [--tag-quad-decimate <factor>]"
<< " [--tag-n-threads <number>]"
<< " [--tag-z-aligned]"
<< " [--tag-pose-method <method>]"
#if defined(VISP_HAVE_PUGIXML)
<< " [--intrinsic <xmlfile>]"
<< " [--camera <name>]"
#endif
#if defined(VISP_HAVE_DISPLAY)
<< " [--display-tag]"
<< " [--color <id>]"
<< " [--thickness <thickness>"
#endif
<< " [--help, -h]" << std::endl
<< std::endl;
std::cout << "Description" << std::endl
<< " Detect AprilTags in an image and compute their corresponding pose." << std::endl
<< std::endl
<< " --input <filename>" << std::endl
<< " Image filename to process." << std::endl
<< " Default: AprilTag.jpg" << std::endl
<< std::endl
<< " --tag-size <size>" << std::endl
<< " Apriltag size in [m]." << std::endl
<< " Default: 0.03" << std::endl
<< std::endl
<< " --tag-family <family>" << std::endl
<< " Apriltag family. Supported values are:" << std::endl
<< " 0: TAG_36h11" << std::endl
<< " 1: TAG_36h10 (DEPRECATED)" << std::endl
<< " 2: TAG_36ARTOOLKIT (DEPRECATED)" << std::endl
<< " 3: TAG_25h9" << std::endl
<< " 4: TAG_25h7 (DEPRECATED)" << std::endl
<< " 5: TAG_16h5" << std::endl
<< " 6: TAG_CIRCLE21h7" << std::endl
<< " 7: TAG_CIRCLE49h12" << std::endl
<< " 8: TAG_CUSTOM48h12" << std::endl
<< " 9: TAG_STANDARD41h12" << std::endl
<< " 10: TAG_STANDARD52h13" << std::endl
<< " 11: TAG_ARUCO_4x4_50" << std::endl
<< " 12: TAG_ARUCO_4x4_100" << std::endl
<< " 13: TAG_ARUCO_4x4_250" << std::endl
<< " 14: TAG_ARUCO_4x4_1000" << std::endl
<< " 15: TAG_ARUCO_5x5_50" << std::endl
<< " 16: TAG_ARUCO_5x5_100" << std::endl
<< " 17: TAG_ARUCO_5x5_250" << std::endl
<< " 18: TAG_ARUCO_5x5_1000" << std::endl
<< " 19: TAG_ARUCO_6x6_50" << std::endl
<< " 20: TAG_ARUCO_6x6_100" << std::endl
<< " 21: TAG_ARUCO_6x6_250" << std::endl
<< " 22: TAG_ARUCO_6x6_1000" << std::endl
<< " 23: TAG_ARUCO_7x7_50" << std::endl
<< " 24: TAG_ARUCO_7x7_100" << std::endl
<< " 25: TAG_ARUCO_7x7_250" << std::endl
<< " 26: TAG_ARUCO_7x7_1000" << std::endl
<< " 27: TAG_ARUCO_MIP_36h12" << std::endl
<< " Default: 0 (36h11)" << std::endl
<< std::endl
<< " --tag-decision-margin-threshold <threshold>" << std::endl
<< " Threshold used to discard low-confident detections. A typical value is " << std::endl
<< " around 100. The higher this value, the more false positives will be filtered" << std::endl
<< " out. When this value is set to -1, false positives are not filtered out." << std::endl
<< " Default: 50" << std::endl
<< std::endl
<< " --tag-hamming-distance-threshold <threshold>" << std::endl
<< " Threshold used to discard low-confident detections with corrected bits." << std::endl
<< " A typical value is between 0 and 3. The lower this value, the more false" << std::endl
<< " positives will be filtered out." << std::endl
<< " Default: 0" << std::endl
<< std::endl
<< " --tag-quad-decimate <factor>" << std::endl
<< " Decimation factor used to detect a tag. " << std::endl
<< " Default: 1" << std::endl
<< std::endl
<< " --tag-n-threads <number>" << std::endl
<< " Number of threads used to detect a tag." << std::endl
<< " Default: 1" << std::endl
<< std::endl
<< " --tag-z-aligned" << std::endl
<< " When enabled, tag z-axis and camera z-axis are aligned." << std::endl
<< " Default: false" << std::endl
<< std::endl
<< " --tag-pose-method <method>" << std::endl
<< " Algorithm used to compute the tag pose from its 4 corners." << std::endl
<< " Possible values are:" << std::endl
<< " 0: HOMOGRAPHY" << std::endl
<< " 1: HOMOGRAPHY_VIRTUAL_VS" << std::endl
<< " 2: DEMENTHON_VIRTUAL_VS" << std::endl
<< " 3: LAGRANGE_VIRTUAL_VS" << std::endl
<< " 4: BEST_RESIDUAL_VIRTUAL_VS" << std::endl
<< " 5: HOMOGRAPHY_ORTHOGONAL_ITERATION" << std::endl
<< " Default: 1 (HOMOGRAPHY_VIRTUAL_VS)" << std::endl
<< std::endl
#if defined(VISP_HAVE_PUGIXML)
<< " --intrinsic <xmlfile>" << std::endl
<< " Camera intrinsic parameters file in xml format." << std::endl
<< " Default: empty" << std::endl
<< std::endl
<< " --camera <name>" << std::endl
<< " Camera name in the intrinsic parameters file in xml format." << std::endl
<< " Default: empty" << std::endl
<< std::endl
#endif
#if defined(VISP_HAVE_DISPLAY)
<< " --display-tag" << std::endl
<< " Flag used to enable displaying the edges of a tag." << std::endl
<< " Default: disabled" << std::endl
<< std::endl
<< " --color <id>" << std::endl
<< " Color id used to display the frame over each tag." << std::endl
<< " Possible values are:" << std::endl
<< " -1: R-G-B colors for X, Y, Z axis respectively" << std::endl
<< " 0: all axis in black" << std::endl
<< " 1: all axis in white" << std::endl
<< " ..." << std::endl
<< " Default: -1" << std::endl
<< std::endl
<< " --thickness <thickness>" << std::endl
<< " Thickness of the drawings in overlay." << std::endl
<< " Default: 2" << std::endl
#endif
<< std::endl
<< " --help, -h" << std::endl
<< " Print this helper message." << std::endl
<< std::endl;
if (error) {
std::cout << "Error" << std::endl
<< " "
<< "Unsupported parameter " << argv[error] << std::endl;
}
}
int main(int argc, const char **argv)
{
#if defined(VISP_HAVE_APRILTAG) && defined(VISP_HAVE_DISPLAY)
#ifdef ENABLE_VISP_NAMESPACE
using namespace VISP_NAMESPACE_NAME;
#endif
std::string opt_input_filename = "AprilTag.jpg";
double opt_tag_size = 0.053;
float opt_tag_quad_decimate = 1.0;
float opt_tag_decision_margin_threshold = 50;
int opt_tag_hamming_distance_threshold = 2;
int opt_tag_nThreads = 1;
std::string opt_intrinsic_file = "";
std::string opt_camera_name = "";
bool opt_display_tag = false;
int opt_color_id = -1;
unsigned int opt_thickness = 2;
bool opt_tag_z_align_frame = false;
for (int i = 1; i < argc; ++i) {
if (std::string(argv[i]) == "--input" && i + 1 < argc) {
opt_input_filename = std::string(argv[++i]);
}
else if (std::string(argv[i]) == "--tag-size" && i + 1 < argc) {
opt_tag_size = atof(argv[++i]);
}
else if (std::string(argv[i]) == "--tag-family" && i + 1 < argc) {
opt_tag_family = static_cast<vpDetectorAprilTag::vpAprilTagFamily>(atoi(argv[++i]));
}
else if (std::string(argv[i]) == "--tag-quad-decimate" && i + 1 < argc) {
opt_tag_quad_decimate = static_cast<float>(atof(argv[++i]));
}
else if (std::string(argv[i]) == "--tag-n-threads" && i + 1 < argc) {
opt_tag_nThreads = atoi(argv[++i]);
}
else if (std::string(argv[i]) == "--tag-z-aligned") {
opt_tag_z_align_frame = true;
}
else if (std::string(argv[i]) == "--tag-pose-method" && i + 1 < argc) {
opt_tag_pose_estimation_method = static_cast<vpDetectorAprilTag::vpPoseEstimationMethod>(atoi(argv[++i]));
}
else if (std::string(argv[i]) == "--tag-decision-margin-threshold" && i + 1 < argc) {
opt_tag_decision_margin_threshold = static_cast<float>(atof(argv[++i]));
}
else if (std::string(argv[i]) == "--tag-hamming-distance-threshold" && i + 1 < argc) {
opt_tag_hamming_distance_threshold = atoi(argv[++i]);
}
#if defined(VISP_HAVE_PUGIXML)
else if (std::string(argv[i]) == "--intrinsic" && i + 1 < argc) {
opt_intrinsic_file = std::string(argv[++i]);
}
else if (std::string(argv[i]) == "--camera-name" && i + 1 < argc) {
opt_camera_name = std::string(argv[++i]);
}
#endif
#if defined(VISP_HAVE_DISPLAY)
else if (std::string(argv[i]) == "--display-tag") {
opt_display_tag = true;
}
else if (std::string(argv[i]) == "--color" && i + 1 < argc) {
opt_color_id = atoi(argv[++i]);
}
else if (std::string(argv[i]) == "--thickness" && i + 1 < argc) {
opt_thickness = static_cast<unsigned int>(atoi(argv[++i]));
}
#endif
else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
usage(argv, 0);
return EXIT_SUCCESS;
}
else {
usage(argv, i);
return EXIT_FAILURE;
}
}
std::cout << "Input data" << std::endl;
std::cout << " Image : " << opt_input_filename << std::endl;
cam.initPersProjWithoutDistortion(615.1674805, 615.1675415, 312.1889954, 243.4373779);
#if defined(VISP_HAVE_PUGIXML)
if (!opt_intrinsic_file.empty() && !opt_camera_name.empty()) {
std::cout << " Intrinsics : " << opt_intrinsic_file << std::endl << std::endl;
parser.parse(cam, opt_intrinsic_file, opt_camera_name, vpCameraParameters::perspectiveProjWithoutDistortion);
}
else {
std::cout << " Intrinsics : default" << std::endl << std::endl;
}
#else
std::cout << " Intrinsics : default" << std::endl << std::endl;
#endif
std::cout << cam << std::endl;
std::cout << "Tag detector settings" << std::endl;
std::cout << " Tag size [m] : " << opt_tag_size << std::endl;
std::cout << " Tag family : " << opt_tag_family << std::endl;
std::cout << " Quad decimate : " << opt_tag_quad_decimate << std::endl;
std::cout << " Decision margin threshold : " << opt_tag_decision_margin_threshold << std::endl;
std::cout << " Hamming distance threshold: " << opt_tag_hamming_distance_threshold << std::endl;
std::cout << " Num threads : " << opt_tag_nThreads << std::endl;
std::cout << " Z aligned : " << opt_tag_z_align_frame << std::endl;
std::cout << " Pose estimation : " << opt_tag_pose_estimation_method << std::endl;
#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
std::shared_ptr<vpDisplay> display, display2;
#else
vpDisplay *display = nullptr;
vpDisplay *display2 = nullptr;
#endif
try {
vpImage<vpRGBa> I_color;
vpImageIo::read(I_color, opt_input_filename);
#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
display = vpDisplayFactory::createDisplay(I, -1, -1, "AprilTag detection (for loop display)");
#else
display = vpDisplayFactory::allocateDisplay(I, -1, -1, "AprilTag detection (for loop display)");
#endif
vpDetectorAprilTag detector(opt_tag_family);
detector.setAprilTagQuadDecimate(opt_tag_quad_decimate);
detector.setAprilTagPoseEstimationMethod(opt_tag_pose_estimation_method);
detector.setAprilTagNbThreads(opt_tag_nThreads);
detector.setDisplayTag(opt_display_tag, opt_color_id < 0 ? vpColor::none : vpColor::getColor(opt_color_id), opt_thickness);
detector.setZAlignedWithCameraAxis(opt_tag_z_align_frame);
detector.setAprilTagDecisionMarginThreshold(opt_tag_decision_margin_threshold);
detector.setAprilTagHammingDistanceThreshold(opt_tag_hamming_distance_threshold);
std::vector<vpHomogeneousMatrix> cMo_vec;
detector.detect(I, opt_tag_size, cam, cMo_vec);
std::stringstream ss;
ss << "Detection time: " << t << " ms for " << detector.getNbObjects() << " tags";
vpDisplay::displayText(I, 40, 20, ss.str(), vpColor::red);
std::vector<int> tag_ids = detector.getTagsId();
std::vector<float> tag_decision_margins = detector.getTagsDecisionMargin();
std::vector<int> tag_hamming_distances = detector.getTagsHammingDistance();
std::cout << "\nDetected tags" << std::endl;
for (size_t i = 0; i < detector.getNbObjects(); i++) {
std::vector<vpImagePoint> p = detector.getPolygon(i);
vpRect bbox = detector.getBBox(i);
std::string message = detector.getMessage(i);
ss.str("");
ss << message << " with decision margin: " << tag_decision_margins[i] << " and hamming distance: " << tag_hamming_distances[i];
std::cout << " " << ss.str() << std::endl;
ss.str("");
ss << "Tag id: " << tag_ids[i] << " - " << tag_decision_margins[i];
vpDisplay::displayText(I, static_cast<int>(bbox.getTop()), static_cast<int>(bbox.getLeft()), ss.str(), vpColor::red);
for (size_t j = 0; j < p.size(); j++) {
std::ostringstream number;
number << j;
vpDisplay::displayText(I, p[j] + vpImagePoint(15, 5), number.str(), vpColor::blue);
}
}
vpDisplay::displayText(I, 20, 20, "Click to display tag poses", vpColor::red);
for (size_t i = 0; i < cMo_vec.size(); i++) {
vpDisplay::displayFrame(I, cMo_vec[i], cam, opt_tag_size / 2, vpColor::none, 3);
}
vpDisplay::displayText(I, 20, 20, "Click.", vpColor::red);
#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
display2 = vpDisplayFactory::createDisplay(I_color, 50, 50, "AprilTag detection (class display)");
#else
display2 = vpDisplayFactory::allocateDisplay(I_color, 50, 50, "AprilTag detection (class display)");
#endif
// To test the displays on a vpRGBa image
// Display frames and tags
std::vector<std::vector<vpImagePoint> > tagsCorners = detector.getTagsCorners();
detector.displayTags(I_color, tagsCorners, vpColor::none, 3);
detector.displayFrames(I_color, cMo_vec, cam, opt_tag_size / 2, vpColor::none, 3);
vpDisplay::displayText(I_color, 20, 20, "Click to quit.", vpColor::red);
vpDisplay::flush(I_color);
}
catch (const vpException &e) {
std::cerr << "Catch an exception: " << e.getMessage() << std::endl;
}
#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
if (display != nullptr) {
delete display;
}
if (display2 != nullptr) {
delete display2;
}
#endif
#else
(void)argc;
(void)argv;
#endif
return EXIT_SUCCESS;
}
Generic class defining intrinsic camera parameters.
@ perspectiveProjWithoutDistortion
Perspective projection without distortion model.
static vpColor getColor(const unsigned int &i)
Definition vpColor.h:300
static const vpColor red
Definition vpColor.h:198
static const vpColor none
Definition vpColor.h:210
static const vpColor blue
Definition vpColor.h:204
static const vpColor green
Definition vpColor.h:201
@ TAG_36h11
AprilTag 36h11 pattern (recommended).
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 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 displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
static void close(vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
static void displayRectangle(const vpImage< unsigned char > &I, const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)
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
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
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
Defines a rectangle in the plane.
Definition vpRect.h:79
double getLeft() const
Definition vpRect.h:173
double getTop() const
Definition vpRect.h:192
XML parser to load and save intrinsic camera parameters.
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()