Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
tutorial-mb-hybrid-tracker.cpp
#include <visp3/core/vpIoTools.h>
#include <visp3/gui/vpDisplayGDI.h>
#include <visp3/gui/vpDisplayOpenCV.h>
#include <visp3/gui/vpDisplayX.h>
#include <visp3/io/vpImageIo.h>
#include <visp3/io/vpVideoReader.h>
#include <visp3/mbt/vpMbEdgeKltTracker.h>
int main(int argc, char **argv)
{
#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100)
try {
std::string videoname = "teabox.mpg";
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::cout << "\nUsage: " << argv[0] << " [--name <video name>] [--help]\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,"
#ifdef VISP_HAVE_PUGIXML
<< "xml,"
#endif
<< "cao or wrl]" << std::endl;
std::cout << "Tracker optional config files: " << objectname << ".[ppm]" << std::endl;
g.setFileName(videoname);
g.open(I);
#if defined(VISP_HAVE_X11)
vpDisplayX display(I, 100, 100, "Model-based hybrid tracker");
;
#elif defined(VISP_HAVE_GDI)
vpDisplayGDI display(I, 100, 100, "Model-based hybrid tracker");
;
#elif defined(VISP_HAVE_OPENCV)
vpDisplayOpenCV display(I, 100, 100, "Model-based hybrid tracker");
;
#else
std::cout << "No image viewer is available..." << std::endl;
#endif
bool usexml = false;
#ifdef VISP_HAVE_PUGIXML
if (vpIoTools::checkFilename(objectname + ".xml")) {
tracker.loadConfigFile(objectname + ".xml");
usexml = true;
}
#endif
if (!usexml) {
vpMe me;
me.setMaskSize(5);
me.setMaskNumber(180);
me.setRange(8);
me.setThreshold(10000);
me.setMu1(0.5);
me.setMu2(0.5);
tracker.setMovingEdge(me);
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.setMaskBorder(5);
cam.initPersProjWithoutDistortion(839, 839, 325, 243);
tracker.setCameraParameters(cam);
tracker.setFarClippingDistance(100.0);
}
tracker.setOgreVisibilityTest(true);
tracker.setOgreShowConfigDialog(false);
tracker.loadModel(objectname + ".cao");
tracker.setDisplayFeatures(true);
tracker.initClick(I, objectname + ".init", true);
while (!g.end()) {
g.acquire(I);
tracker.track(I);
tracker.getPose(cMo);
tracker.getCameraParameters(cam);
tracker.display(I, cMo, cam, vpColor::red, 2);
vpDisplay::displayFrame(I, cMo, cam, 0.025, vpColor::none, 3);
vpDisplay::displayText(I, 10, 10, "A click to exit...", vpColor::red);
if (vpDisplay::getClick(I, false))
break;
}
#if defined(VISP_HAVE_COIN3D) && (COIN_MAJOR_VERSION >= 2)
SoDB::finish();
#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
}