Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
tutorial-mb-generic-tracker-full.cpp
#include <visp3/core/vpConfig.h>
#include <visp3/core/vpIoTools.h>
#include <visp3/gui/vpDisplayGDI.h>
#include <visp3/gui/vpDisplayOpenCV.h>
#include <visp3/gui/vpDisplayX.h>
#include <visp3/gui/vpPlot.h>
#include <visp3/mbt/vpMbGenericTracker.h>
#include <visp3/io/vpVideoReader.h>
#include <visp3/io/vpVideoWriter.h>
#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_VIDEOIO) && defined(HAVE_OPENCV_HIGHGUI)
#ifdef ENABLE_VISP_NAMESPACE
using namespace VISP_NAMESPACE_NAME;
#endif
namespace
{
std::vector<double> poseToVec(const vpHomogeneousMatrix &cMo)
{
std::vector<double> vec { t[0], t[1], t[2], tu[0], tu[1], tu[2] };
return vec;
}
}
#endif
int main(int argc, char **argv)
{
#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_VIDEOIO) && defined(HAVE_OPENCV_HIGHGUI)
std::string opt_videoname = "model/teabox/teabox.mp4";
std::string opt_modelname = "model/teabox/teabox.cao";
int opt_tracker = 0;
int opt_video_first_frame = -1;
int opt_downscale_img = 1;
bool opt_verbose = false;
bool opt_plot = true;
bool opt_display_scale_auto = false;
vpColVector opt_dof_to_estimate(6, 1.); // Here we consider 6 dof estimation
std::string opt_save;
#if defined(VISP_HAVE_MINIZ) && defined(VISP_HAVE_WORKING_REGEX)
std::string opt_save_results;
#endif
unsigned int thickness = 2;
std::shared_ptr<vpDisplay> display;
std::shared_ptr<vpPlot> plot;
std::shared_ptr<vpVideoWriter> writer;
try {
for (int i = 0; i < argc; i++) {
if (std::string(argv[i]) == "--video") {
opt_videoname = std::string(argv[++i]);
}
else if (std::string(argv[i]) == "--video-first-frame") {
opt_video_first_frame = std::atoi(argv[++i]);
}
else if (std::string(argv[i]) == "--model") {
opt_modelname = std::string(argv[++i]);
}
else if (std::string(argv[i]) == "--tracker") {
opt_tracker = atoi(argv[++i]);
}
else if (std::string(argv[i]) == "--downscale-img") {
opt_downscale_img = std::atoi(argv[++i]);
}
else if (std::string(argv[i]) == "--save") {
opt_save = std::string(argv[++i]);
}
#if defined(VISP_HAVE_MINIZ) && defined(VISP_HAVE_WORKING_REGEX)
else if (std::string(argv[i]) == "--save-results") {
opt_save_results = std::string(argv[++i]);
}
#endif
else if (std::string(argv[i]) == "--plot") {
opt_plot = true;
}
else if (std::string(argv[i]) == "--dof") {
for (int j = 0; j < 6; j++) {
int val = std::atoi(argv[++i]);
if (val == 0 || val == 1) {
opt_dof_to_estimate[j] = val;
}
else {
std::cout << "Error: wrong value after --dof option. Authorized values are 0 or 1 for each 6 dof to estimate." << std::endl;
return EXIT_FAILURE;
}
}
}
else if (std::string(argv[i]) == "--display-scale-auto") {
opt_display_scale_auto = true;
}
else if (std::string(argv[i]) == "--verbose" || std::string(argv[i]) == "-v") {
opt_verbose = true;
}
else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
std::cout << "\nSYNOPSIS " << std::endl
<< argv[0]
<< " [--video <video name>]"
<< " [--video-first-frame <image index>"
<< " [--model <model name>"
<< " [--tracker <0=egde|1=keypoint|2=hybrid>]"
<< " [--downscale-img <scale factor>]"
<< " [--dof <0/1 0/1 0/1 0/1 0/1 0/1>]"
<< " [--save <e.g. results-%04d.png>]"
#if defined(VISP_HAVE_MINIZ) && defined(VISP_HAVE_WORKING_REGEX)
<< " [--save-results <e.g. tracking_poses.npz>]"
#endif
<< " [--display-scale-auto]"
<< " [--plot]"
<< " [--verbose,-v]"
<< " [--help,-h]"
<< std::endl;
std::cout << "\nOPTIONS " << std::endl
<< " --video <video name>" << std::endl
<< " Input video name." << std::endl
<< " Default: model/teabox/teabox.mp4" << std::endl
<< std::endl
<< " --video-first-frame <image index>" << std::endl
<< " Index of the first image to process." << std::endl
<< " Set to -1 to process the first image of the video." << std::endl
<< " Default: -1" << std::endl
<< std::endl
<< " --model <model name>" << std::endl
<< " CAD model filename. Supported formats are .cao and .wrl." << std::endl
<< " To use wrl format, ViSP need to be built with Coin3D third-party." << std::endl
<< " Default: model/teabox/teabox.cao" << std::endl
<< std::endl
<< " --tracker <0=egde|1=keypoint|2=hybrid>" << std::endl
<< " Tracker type:" << std::endl
<< " - when 0: use only moving-edges" << std::endl
<< " - when 1: use only KLT keypoints" << std::endl
<< " - when 2: use hybrid scheme, moving-edges and KLT keypoints." << std::endl
<< " Default: 0" << std::endl
<< std::endl
<< " --downscale-img <scale factor>" << std::endl
<< " Downscale input image width and height by this factor." << std::endl
<< " When set to 1, image not down scaled. When set to 2, image width" << std::endl
<< " and height is divided by 2." << std::endl
<< " Default: 1" << std::endl
<< std::endl
<< " --dof <0/1 0/1 0/1 0/1 0/1 0/1>" << std::endl
<< " 6-dim vector of 0 and 1 to indicate which dof [tx ty tz rx ry rz]" << std::endl
<< " has to be estimated." << std::endl
<< " When set to 1 the dof is estimated. When rather set to 0 the dof" << std::endl
<< " is not estimated. It's value is the one from the initialisation." << std::endl
<< " Default: 1 1 1 1 1 1 (to estimate all 6 dof)" << std::endl
<< std::endl
<< " --save <e.g. results-%04d.png>" << std::endl
<< " Name of the saved image sequence that contains tracking results in overlay." << std::endl
<< " When the name contains a folder like in the next example, the folder" << std::endl
<< " is created if it doesn't exist."
<< " Example: \"result/image-%04d.png\"." << std::endl
<< std::endl
#if defined(VISP_HAVE_MINIZ) && defined(VISP_HAVE_WORKING_REGEX)
<< " --save-results <e.g. tracking_results.npz>" << std::endl
<< " Name of the npz file containing cMo data estimated from MBT." << std::endl
<< " When the name contains a folder like in the next example, the folder" << std::endl
<< " is created if it doesn't exist."
<< " Example: \"result/tracking_results.npz\"." << std::endl
<< std::endl
#endif
<< " --display-scale-auto" << std::endl
<< " Enable display window auto scaling to ensure that the image is fully" << std::endl
<< " visible on the screen. Useful for large images." << std::endl
<< " Note that this option doesn't affect the size of the processed images." << std::endl
<< std::endl
<< " --plot" << std::endl
<< " Open a window that plots the estimated pose evolution." << std::endl
<< std::endl
<< " --verbose, -v" << std::endl
<< " Enable verbose mode." << std::endl
<< std::endl
<< " --help, -h" << std::endl
<< " Display this helper message." << std::endl
<< std::endl;
return EXIT_SUCCESS;
}
}
std::string parentname = vpIoTools::getParent(opt_modelname);
std::string objectname = vpIoTools::getNameWE(opt_modelname);
if (!parentname.empty())
objectname = parentname + "/" + objectname;
std::cout << " *********** Tracker config ************ " << std::endl;
std::cout << "Video name : " << opt_videoname << std::endl;
std::cout << "Tracker cad model file : " << objectname << ".[cao or wrl]" << std::endl;
std::cout << "Tracker init file : " << objectname << ".init" << std::endl;
std::cout << "Tracker optional init image: " << objectname << ".[png,ppm,jpg]" << std::endl;
if (opt_downscale_img > 1) {
std::cout << "Downscale image factor : " << opt_downscale_img << std::endl;
}
std::cout << "Dof to estimate : " << opt_dof_to_estimate.t() << std::endl;
// Create output folder if needed
if (!opt_save.empty()) {
std::string parent = vpIoTools::getParent(opt_save);
if (!parent.empty()) {
std::cout << "Create output directory: " << parent << std::endl;
}
}
#if defined(VISP_HAVE_MINIZ) && defined(VISP_HAVE_WORKING_REGEX)
if (!opt_save_results.empty()) {
std::string parent = vpIoTools::getParent(opt_save_results);
if (!parent.empty()) {
std::cout << "Create output directory for the npz file: " << parent << std::endl;
}
}
#endif
g.setFileName(opt_videoname);
if (opt_video_first_frame > 0) {
g.setFirstFrameIndex(static_cast<unsigned int>(opt_video_first_frame));
}
if (opt_downscale_img > 1) {
g.open(Ivideo);
Ivideo.subsample(opt_downscale_img, opt_downscale_img, I);
}
else {
g.open(I);
}
if (!opt_save.empty()) {
writer = std::make_shared<vpVideoWriter>();
writer->setFileName(opt_save);
writer->open(O);
}
#if defined(VISP_HAVE_X11)
display = std::make_shared<vpDisplayX>();
#elif defined(VISP_HAVE_GDI)
display = std::make_shared<vpDisplayGDI>();
#elif defined(HAVE_OPENCV_HIGHGUI)
display = std::make_shared<vpDisplayOpenCV>();
#endif
if (opt_display_scale_auto) {
display->setDownScalingFactor(vpDisplay::SCALE_AUTO);
}
display->init(I, 100, 100, "Model-based tracker");
if (opt_plot) {
plot = std::make_shared<vpPlot>(2, 700, 700, display->getWindowXPosition() + I.getWidth() / display->getDownScalingFactor() + 30,
display->getWindowYPosition(), "Estimated pose");
plot->initGraph(0, 3); // Translation
plot->setTitle(0, "Translation [m]");
plot->setColor(0, 0, vpColor::red);
plot->setColor(0, 1, vpColor::green);
plot->setColor(0, 2, vpColor::blue);
plot->initGraph(1, 3); // Attitude
plot->setTitle(1, "Attitude thetaU [deg]");
plot->setColor(1, 0, vpColor::red);
plot->setColor(1, 1, vpColor::green);
plot->setColor(1, 2, vpColor::blue);
}
if (opt_tracker == 0)
#if defined(VISP_HAVE_MODULE_KLT) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
else if (opt_tracker == 1)
tracker.setTrackerType(vpMbGenericTracker::KLT_TRACKER);
else
tracker.setTrackerType(vpMbGenericTracker::EDGE_TRACKER | vpMbGenericTracker::KLT_TRACKER);
#else
else {
std::cout << "klt and hybrid model-based tracker are not available since visp_klt module is not available. "
"In CMakeGUI turn visp_klt module ON, configure and build ViSP again."
<< std::endl;
return EXIT_FAILURE;
}
#endif
bool usexml = false;
#if defined(VISP_HAVE_PUGIXML)
if (vpIoTools::checkFilename(objectname + ".xml")) {
std::cout << "Tracker config file : " << objectname + ".xml" << std::endl;
tracker.loadConfigFile(objectname + ".xml");
usexml = true;
}
#endif
if (!usexml) {
if (opt_tracker == 0 || opt_tracker == 2) {
vpMe me;
me.setMaskSize(5);
me.setMaskNumber(180);
me.setRange(8);
me.setThreshold(20);
me.setMu1(0.5);
me.setMu2(0.5);
tracker.setMovingEdge(me);
}
#if defined(VISP_HAVE_MODULE_KLT) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
if (opt_tracker == 1 || opt_tracker == 2) {
vpKltOpencv klt_settings;
klt_settings.setMaxFeatures(300);
klt_settings.setWindowSize(5);
klt_settings.setQuality(0.015);
klt_settings.setMinDistance(8);
klt_settings.setHarrisFreeParameter(0.01);
klt_settings.setBlockSize(3);
klt_settings.setPyramidLevels(3);
tracker.setKltOpencv(klt_settings);
tracker.setKltMaskBorder(5);
}
#endif
tracker.setFarClippingDistance(100.0);
cam.initPersProjWithoutDistortion(839.21470, 839.44555, 325.66776, 243.69727);
tracker.setCameraParameters(cam);
}
tracker.setOgreVisibilityTest(false);
tracker.setOgreShowConfigDialog(false);
if (vpIoTools::checkFilename(objectname + ".cao"))
tracker.loadModel(objectname + ".cao");
else if (vpIoTools::checkFilename(objectname + ".wrl"))
tracker.loadModel(objectname + ".wrl");
tracker.setDisplayFeatures(true);
if (opt_dof_to_estimate != 1.) {
tracker.setEstimatedDoF(opt_dof_to_estimate);
}
tracker.getCameraParameters(cam);
std::cout << "Camera parameters: \n" << cam << std::endl;
std::cout << "Initialize tracker on image size: " << I.getWidth() << " x " << I.getHeight() << std::endl;
std::vector<double> vec_poses;
#if defined(VISP_HAVE_MINIZ) && defined(VISP_HAVE_WORKING_REGEX)
if (!opt_save_results.empty()) {
const unsigned int height = I.getHeight(), width = I.getWidth();
visp::cnpy::npz_save(opt_save_results, "height", &height, { 1 }, "w");
visp::cnpy::npz_save(opt_save_results, "width", &width, { 1 }, "a");
const double cam_px = cam.get_px(), cam_py = cam.get_py(), cam_u0 = cam.get_u0(), cam_v0 = cam.get_v0();
visp::cnpy::npz_save(opt_save_results, "cam_px", &cam_px, { 1 }, "a");
visp::cnpy::npz_save(opt_save_results, "cam_py", &cam_py, { 1 }, "a");
visp::cnpy::npz_save(opt_save_results, "cam_u0", &cam_u0, { 1 }, "a");
visp::cnpy::npz_save(opt_save_results, "cam_v0", &cam_v0, { 1 }, "a");
}
#endif
tracker.initClick(I, objectname + ".init", true);
while (!g.end()) {
if (opt_downscale_img > 1) {
g.acquire(Ivideo);
Ivideo.subsample(opt_downscale_img, opt_downscale_img, I);
}
else {
g.acquire(I);
}
std::stringstream ss;
ss << "Process image " << g.getFrameIndex();
if (opt_verbose) {
std::cout << "-- " << ss.str() << std::endl;
}
tracker.track(I);
tracker.getPose(cMo);
tracker.display(I, cMo, cam, vpColor::red, thickness);
vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, thickness);
vpDisplay::displayText(I, 20 * display->getDownScalingFactor(), 10 * display->getDownScalingFactor(), "A click to exit...", vpColor::red);
vpDisplay::displayText(I, 40 * display->getDownScalingFactor(), 10 * display->getDownScalingFactor(), ss.str(), vpColor::red);
{
std::stringstream ss;
ss << "Features";
ss << " edge: " << tracker.getNbFeaturesEdge();
}
#if defined(VISP_HAVE_MODULE_KLT) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
if (tracker.getTrackerType() & vpMbGenericTracker::KLT_TRACKER) {
ss << " klt: " << tracker.getNbFeaturesKlt();
}
#endif
vpDisplay::displayText(I, 60 * display->getDownScalingFactor(), 10 * display->getDownScalingFactor(), ss.str(), vpColor::red);
if (opt_verbose) {
std::cout << ss.str() << std::endl;
std::cout << "cMo:\n" << cMo << std::endl;
}
}
{
double proj_error = tracker.computeCurrentProjectionError(I, cMo, cam);
std::stringstream ss;
ss << "Projection error: " << std::setprecision(2) << proj_error << " deg";
vpDisplay::displayText(I, 80 * display->getDownScalingFactor(), 10 * display->getDownScalingFactor(), ss.str(), vpColor::red);
if (opt_verbose) {
std::cout << ss.str() << std::endl;
}
}
if (opt_plot) {
vpColVector c_tu_o_deg = vpMath::deg(c_tu_o);
plot->plot(0, g.getFrameIndex(), c_t_o);
plot->plot(1, g.getFrameIndex(), c_tu_o_deg);
}
if (!opt_save.empty()) {
writer->saveFrame(O);
}
#if defined(VISP_HAVE_MINIZ) && defined(VISP_HAVE_WORKING_REGEX)
if (!opt_save_results.empty()) {
std::vector<double> vec_pose = poseToVec(cMo);
vec_poses.insert(vec_poses.end(), vec_pose.begin(), vec_pose.end());
}
#endif
if (vpDisplay::getClick(I, false))
break;
}
#if defined(VISP_HAVE_MINIZ) && defined(VISP_HAVE_WORKING_REGEX)
if (!opt_save_results.empty()) {
visp::cnpy::npz_save(opt_save_results, "vec_poses", vec_poses.data(), { static_cast<size_t>(vec_poses.size()/6), 6 }, "a");
}
#endif
}
catch (const vpException &e) {
std::cout << "Catch a ViSP exception: " << e << std::endl;
}
#ifdef VISP_HAVE_OGRE
catch (Ogre::Exception &e) {
std::cout << "Catch an Ogre exception: " << e.getDescription() << std::endl;
}
#endif
#else
(void)argc;
(void)argv;
std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
#endif
return EXIT_SUCCESS;
}
Generic class defining intrinsic camera parameters.
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
static const vpColor red
Definition: vpColor.h:217
static const vpColor none
Definition: vpColor.h:229
static const vpColor blue
Definition: vpColor.h:223
static const vpColor green
Definition: vpColor.h:220
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 getImage(const vpImage< unsigned char > &Is, vpImage< vpRGBa > &Id)
Definition: vpDisplay.cpp:140
static void flush(const vpImage< unsigned char > &I)
@ SCALE_AUTO
Definition: vpDisplay.h:184
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
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpThetaUVector getThetaUVector() const
vpRotationMatrix getRotationMatrix() const
vpTranslationVector getTranslationVector() const
void subsample(unsigned int v_scale, unsigned int h_scale, vpImage< Type > &sampled) const
Definition: vpImage.h:753
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181
static bool checkFilename(const std::string &filename)
Definition: vpIoTools.cpp:786
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:550
static std::string getNameWE(const std::string &pathname)
Definition: vpIoTools.cpp:1227
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1314
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
Definition: vpKltOpencv.h:74
void setBlockSize(int blockSize)
Definition: vpKltOpencv.h:267
void setQuality(double qualityLevel)
Definition: vpKltOpencv.h:356
void setHarrisFreeParameter(double harris_k)
Definition: vpKltOpencv.h:275
void setMaxFeatures(int maxCount)
Definition: vpKltOpencv.h:315
void setMinDistance(double minDistance)
Definition: vpKltOpencv.h:324
void setWindowSize(int winSize)
Definition: vpKltOpencv.h:377
void setPyramidLevels(int pyrMaxLevel)
Definition: vpKltOpencv.h:343
static double rad(double deg)
Definition: vpMath.h:129
static double deg(double rad)
Definition: vpMath.h:119
Real-time 6D object pose tracking using its CAD model.
virtual void setCameraParameters(const vpCameraParameters &camera) VP_OVERRIDE
virtual int getTrackerType() const
virtual void setOgreVisibilityTest(const bool &v) 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 setGoodMovingEdgesRatioThreshold(double threshold)
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 getClipping(unsigned int &clippingFlag1, unsigned int &clippingFlag2) const
virtual void setClipping(const unsigned int &flags) VP_OVERRIDE
virtual void setTrackerType(int type)
virtual void setOgreShowConfigDialog(bool showConfigDialog) VP_OVERRIDE
virtual void setScanLineVisibilityTest(const bool &v) VP_OVERRIDE
virtual void setNearClippingDistance(const double &dist) VP_OVERRIDE
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 loadConfigFile(const std::string &configFile, bool verbose=true) VP_OVERRIDE
virtual void setAngleAppear(const double &a) VP_OVERRIDE
virtual void setFarClippingDistance(const double &dist) VP_OVERRIDE
virtual void setEstimatedDoF(const vpColVector &v)
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
Implementation of a rotation vector as axis-angle minimal representation.
Class that consider the case of a translation vector.
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void acquire(vpImage< vpRGBa > &I)
void open(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
void setFirstFrameIndex(const long first_frame)
long getFrameIndex() const
void npz_save(std::string zipname, std::string fname, const T *data, const std::vector< size_t > &shape, std::string mode="w")
Definition: vpIoTools.h:235