1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
|
//! \example tutorial-mb-generic-tracker-full.cpp
#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]
#include <visp3/mbt/vpMbGenericTracker.h>
//! [Include]
#include <visp3/io/vpVideoReader.h>
#include <visp3/io/vpVideoWriter.h>
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;
unsigned int thickness = 2;
vpImage<unsigned char> I;
vpDisplay *display = NULL;
vpPlot *plot = NULL;
vpVideoWriter *writer = NULL;
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]);
}
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 <video>]"
<< " [--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 <video>" << 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
<< " --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;
vpIoTools::makeDirectory(parent);
}
}
//! [Image]
vpImage<unsigned char> Ivideo;
//! [Image]
//! [cMo]
vpHomogeneousMatrix cMo;
//! [cMo]
vpVideoReader g;
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);
}
vpImage<vpRGBa> O;
if (!opt_save.empty()) {
writer = new vpVideoWriter();
writer->setFileName(opt_save);
writer->open(O);
}
#if defined(VISP_HAVE_X11)
display = new vpDisplayX;
#elif defined(VISP_HAVE_GDI)
display = new vpDisplayGDI;
#elif defined(HAVE_OPENCV_HIGHGUI)
display = new vpDisplayOpenCV;
#endif
if (opt_display_scale_auto) {
display->setDownScalingFactor(vpDisplay::SCALE_AUTO);
}
display->init(I, 100, 100, "Model-based tracker");
if (opt_plot) {
plot = new 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);
}
//! [Constructor]
vpMbGenericTracker tracker;
if (opt_tracker == 0)
tracker.setTrackerType(vpMbGenericTracker::EDGE_TRACKER);
#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
//! [Constructor]
bool usexml = false;
//! [Load xml]
if (vpIoTools::checkFilename(objectname + ".xml")) {
std::cout << "Tracker config file : " << objectname + ".xml" << std::endl;
tracker.loadConfigFile(objectname + ".xml");
usexml = true;
}
//! [Load xml]
if (!usexml) {
//! [Set parameters]
if (opt_tracker == 0 || opt_tracker == 2) {
//! [Set moving-edges parameters]
vpMe me;
me.setMaskSize(5);
me.setMaskNumber(180);
me.setRange(8);
me.setLikelihoodThresholdType(vpMe::NORMALIZED_THRESHOLD);
me.setThreshold(20);
me.setMu1(0.5);
me.setMu2(0.5);
me.setSampleStep(4);
tracker.setMovingEdge(me);
//! [Set moving-edges parameters]
}
#if defined(VISP_HAVE_MODULE_KLT) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
if (opt_tracker == 1 || opt_tracker == 2) {
//! [Set klt parameters]
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);
//! [Set klt parameters]
}
#endif
//! [Set angles]
tracker.setAngleAppear(vpMath::rad(70));
tracker.setAngleDisappear(vpMath::rad(80));
//! [Set angles]
//! [Set clipping distance]
tracker.setNearClippingDistance(0.1);
tracker.setFarClippingDistance(100.0);
//! [Set clipping distance]
//! [Set clipping fov]
tracker.setClipping(tracker.getClipping() | vpMbtPolygon::FOV_CLIPPING);
//! [Set clipping fov]
//! [Set camera parameters]
vpCameraParameters cam;
cam.initPersProjWithoutDistortion(839.21470, 839.44555, 325.66776, 243.69727);
tracker.setCameraParameters(cam);
//! [Set camera parameters]
//! [Set parameters]
}
//! [Set visibility parameters]
//! [Set ogre visibility]
tracker.setOgreVisibilityTest(false);
tracker.setOgreShowConfigDialog(false);
//! [Set ogre visibility]
//! [Set scanline visibility]
tracker.setScanLineVisibilityTest(true);
//! [Set scanline visibility]
//! [Set visibility parameters]
//! [Load cao]
if (vpIoTools::checkFilename(objectname + ".cao"))
tracker.loadModel(objectname + ".cao");
//! [Load cao]
//! [Load wrl]
else if (vpIoTools::checkFilename(objectname + ".wrl"))
tracker.loadModel(objectname + ".wrl");
//! [Load wrl]
//! [Set display]
tracker.setDisplayFeatures(true);
//! [Set display]
tracker.setGoodMovingEdgesRatioThreshold(0.2);
if (opt_dof_to_estimate != 1.) {
tracker.setEstimatedDoF(opt_dof_to_estimate);
}
//! [Get camera parameters]
vpCameraParameters cam;
tracker.getCameraParameters(cam);
std::cout << "Camera parameters: \n" << cam << std::endl;
//! [Get camera parameters]
std::cout << "Initialize tracker on image size: " << I.getWidth() << " x " << I.getHeight() << std::endl;
//! [Init]
tracker.initClick(I, objectname + ".init", true);
//! [Init]
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;
}
vpDisplay::display(I);
//! [Track]
tracker.track(I);
//! [Track]
//! [Get pose]
tracker.getPose(cMo);
//! [Get pose]
//! [Display]
tracker.display(I, cMo, cam, vpColor::red, thickness);
//! [Display]
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";
if (tracker.getTrackerType() & vpMbGenericTracker::EDGE_TRACKER) {
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;
}
}
vpDisplay::flush(I);
if (opt_plot) {
vpTranslationVector c_t_o = cMo.getTranslationVector();
vpThetaUVector c_tu_o = vpThetaUVector(cMo.getRotationMatrix());
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()) {
vpDisplay::getImage(I, O);
writer->saveFrame(O);
}
if (vpDisplay::getClick(I, false))
break;
}
vpDisplay::getClick(I);
}
catch (const vpException &e) {
std::cout << "Catch a ViSP exception: " << e << std::endl;
vpDisplay::getClick(I);
}
#ifdef VISP_HAVE_OGRE
catch (Ogre::Exception &e) {
std::cout << "Catch an Ogre exception: " << e.getDescription() << std::endl;
vpDisplay::getClick(I);
}
#endif
//! [Cleanup]
delete display;
if (opt_plot) {
delete plot;
}
if (writer) {
delete writer;
}
//! [Cleanup]
#else
(void)argc;
(void)argv;
std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
#endif
return EXIT_SUCCESS;
}
|