Visual Servoing Platform  version 3.5.1 under development (2022-07-05)
#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/io/vpVideoReader.h>
#include <visp3/mbt/vpMbGenericTracker.h>
#include <visp3/vision/vpKeyPoint.h>
int main(int argc, char **argv)
#if (VISP_HAVE_OPENCV_VERSION >= 0x020400)
try {
std::string videoname = "teabox.mp4";
for (int i = 0; i < argc; i++) {
if (std::string(argv[i]) == "--name")
videoname = std::string(argv[i + 1]);
else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
std::cout << "\nUsage: " << argv[0] << " [--name <video name>] [--help] [-h]\n" << std::endl;
return 0;
std::string parentname = vpIoTools::getParent(videoname);
std::string objectname = vpIoTools::getNameWE(videoname);
if (!parentname.empty())
objectname = parentname + "/" + objectname;
std::cout << "Video name: " << videoname << std::endl;
std::cout << "Tracker requested config files: " << objectname << ".[init,"
<< "xml,"
<< "cao or wrl]" << std::endl;
std::cout << "Tracker optional config files: " << objectname << ".[ppm]" << std::endl;
#if defined(VISP_HAVE_X11)
vpDisplayX display;
#elif defined(VISP_HAVE_GDI)
vpDisplayGDI display;
#elif defined(VISP_HAVE_OPENCV)
vpDisplayOpenCV display;
std::cout << "No image viewer is available..." << std::endl;
return 0;
display.init(I, 100, 100, "Model-based edge tracker");
bool usexml = false;
if (vpIoTools::checkFilename(objectname + ".xml")) {
tracker.loadConfigFile(objectname + ".xml");
usexml = true;
if (!usexml) {
vpMe me;
cam.initPersProjWithoutDistortion(839, 839, 325, 243);
if (vpIoTools::checkFilename(objectname + ".cao"))
tracker.loadModel(objectname + ".cao");
else if (vpIoTools::checkFilename(objectname + ".wrl"))
tracker.loadModel(objectname + ".wrl");
tracker.initClick(I, objectname + ".init", true);
std::string detectorName = "SIFT";
std::string extractorName = "SIFT";
std::string matcherName = "BruteForce";
std::string configurationFile = "detection-config-SIFT.xml";
std::string detectorName = "FAST";
std::string extractorName = "ORB";
std::string matcherName = "BruteForce-Hamming";
std::string configurationFile = "detection-config.xml";
vpKeyPoint keypoint_learning;
if (usexml) {
} else {
std::vector<cv::KeyPoint> trainKeyPoints;
double elapsedTime;
keypoint_learning.detect(I, trainKeyPoints, elapsedTime);
std::vector<vpPolygon> polygons;
std::vector<std::vector<vpPoint> > roisPt;
std::pair<std::vector<vpPolygon>, std::vector<std::vector<vpPoint> > > pair = tracker.getPolygonFaces(false);
polygons = pair.first;
roisPt = pair.second;
std::vector<cv::Point3f> points3f;
vpKeyPoint::compute3DForPointsInPolygons(cMo, cam, trainKeyPoints, polygons, roisPt, points3f);
keypoint_learning.buildReference(I, trainKeyPoints, points3f);
keypoint_learning.saveLearningData("teabox_learning_data.bin", true);
for (std::vector<cv::KeyPoint>::const_iterator it = trainKeyPoints.begin(); it != trainKeyPoints.end(); ++it) {
vpDisplay::displayCross(I, (int)it->pt.y, (int)it->pt.x, 4, vpColor::red);
vpDisplay::displayText(I, 10, 10, "Learning step: keypoints are detected on visible teabox faces", vpColor::red);
vpDisplay::displayText(I, 30, 10, "Click to continue with detection...", vpColor::red);
vpKeyPoint keypoint_detection;
if (usexml) {
} else {
keypoint_detection.loadLearningData("teabox_learning_data.bin", true);
double error;
bool click_done = false;
while (!g.end()) {
vpDisplay::displayText(I, 10, 10, "Detection and localization in process...", vpColor::red);
if (keypoint_detection.matchPoint(I, cam, cMo, error, elapsedTime)) {
tracker.setPose(I, cMo);
tracker.display(I, cMo, cam, vpColor::red, 2);
vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
vpDisplay::displayText(I, 30, 10, "A click to exit.", vpColor::red);
if (vpDisplay::getClick(I, false)) {
click_done = true;
if (!click_done)
} catch (const vpException &e) {
std::cout << "Catch an exception: " << e << std::endl;
std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
return 0;
Generic class defining intrinsic camera parameters.
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
static const vpColor red
Definition: vpColor.h:217
static const vpColor none
Definition: vpColor.h:229
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:135
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
static void flush(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))
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emited by ViSP classes.
Definition: vpException.h:72
Implementation of an homogeneous matrix and operations on such kind of matrices.
static bool checkFilename(const std::string &filename)
Definition: vpIoTools.cpp:800
static std::string getNameWE(const std::string &pathname)
Definition: vpIoTools.cpp:1536
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1620
Class that allows keypoints detection (and descriptors extraction) and matching thanks to OpenCV libr...
Definition: vpKeyPoint.h:223
unsigned int matchPoint(const vpImage< unsigned char > &I)
void setRansacConsensusPercentage(double percentage)
Definition: vpKeyPoint.h:1002
void setFilterMatchingType(const vpFilterMatchingType &filterType)
Definition: vpKeyPoint.h:938
void setUseRansacVVS(bool ransacVVS)
Definition: vpKeyPoint.h:1148
void setExtractor(const vpFeatureDescriptorType &extractorType)
Definition: vpKeyPoint.h:834
void loadLearningData(const std::string &filename, bool binaryMode=false, bool append=false)
void setRansacThreshold(double threshold)
Definition: vpKeyPoint.h:1085
void detect(const vpImage< unsigned char > &I, std::vector< cv::KeyPoint > &keyPoints, const vpRect &rectangle=vpRect())
void setMatcher(const std::string &matcherName)
Definition: vpKeyPoint.h:910
static void compute3DForPointsInPolygons(const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, std::vector< cv::KeyPoint > &candidates, const std::vector< vpPolygon > &polygons, const std::vector< std::vector< vpPoint > > &roisPt, std::vector< cv::Point3f > &points, cv::Mat *descriptors=NULL)
Definition: vpKeyPoint.cpp:634
void saveLearningData(const std::string &filename, bool binaryMode=false, bool saveTrainingImages=true)
void setUseRansacConsensusPercentage(bool usePercentage)
Definition: vpKeyPoint.h:1139
void setMatchingRatioThreshold(double ratio)
Definition: vpKeyPoint.h:987
@ ratioDistanceThreshold
Definition: vpKeyPoint.h:232
void setDetector(const vpFeatureDetectorType &detectorType)
Definition: vpKeyPoint.h:776
unsigned int buildReference(const vpImage< unsigned char > &I)
Definition: vpKeyPoint.cpp:237
void loadConfigFile(const std::string &configFile)
void setRansacIteration(int nbIter)
Definition: vpKeyPoint.h:1023
static double rad(double deg)
Definition: vpMath.h:117
Real-time 6D object pose tracking using its CAD model.
virtual void setCameraParameters(const vpCameraParameters &camera)
virtual void getPose(vpHomogeneousMatrix &cMo) const
virtual void setDisplayFeatures(bool displayF)
virtual void setAngleAppear(const double &a)
virtual void setNearClippingDistance(const double &dist)
virtual void getCameraParameters(vpCameraParameters &camera) const
virtual void setAngleDisappear(const double &a)
virtual void setMovingEdge(const vpMe &me)
virtual std::pair< std::vector< vpPolygon >, std::vector< std::vector< vpPoint > > > getPolygonFaces(bool orderPolygons=true, bool useVisibility=true, bool clipPolygon=false)
virtual void getClipping(unsigned int &clippingFlag1, unsigned int &clippingFlag2) const
virtual void initClick(const vpImage< unsigned char > &I1, const vpImage< unsigned char > &I2, const std::string &initFile1, const std::string &initFile2, bool displayHelp=false, const vpHomogeneousMatrix &T1=vpHomogeneousMatrix(), const vpHomogeneousMatrix &T2=vpHomogeneousMatrix())
virtual void setFarClippingDistance(const double &dist)
virtual void setClipping(const unsigned int &flags)
virtual void loadConfigFile(const std::string &configFile, bool verbose=true)
virtual void setPose(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cdMo)
virtual void setOgreVisibilityTest(const bool &v)
virtual void loadModel(const std::string &modelFile, bool verbose=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &col, unsigned int thickness=1, bool displayFullModel=false)
virtual void track(const vpImage< unsigned char > &I)
Definition: vpMe.h:61
void setMu1(const double &mu_1)
Definition: vpMe.h:241
void setSampleStep(const double &s)
Definition: vpMe.h:278
void setRange(const unsigned int &r)
Definition: vpMe.h:271
void setMaskSize(const unsigned int &a)
Definition: vpMe.cpp:459
void setNbTotalSample(const int &nb)
Definition: vpMe.h:255
void setMu2(const double &mu_2)
Definition: vpMe.h:248
void setMaskNumber(const unsigned int &a)
Definition: vpMe.cpp:452
void setThreshold(const double &t)
Definition: vpMe.h:300
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)