46 #include <visp3/core/vpConfig.h>
48 #if defined(VISP_HAVE_MODULE_MBT) && defined(VISP_HAVE_MODULE_KLT) && defined(VISP_HAVE_OPENCV) && defined(VISP_HAVE_DISPLAY) && (VISP_HAVE_OPENCV_VERSION >= 0x020100)
50 #include <visp3/core/vpDebug.h>
51 #include <visp3/gui/vpDisplayD3D.h>
52 #include <visp3/gui/vpDisplayGTK.h>
53 #include <visp3/gui/vpDisplayGDI.h>
54 #include <visp3/gui/vpDisplayOpenCV.h>
55 #include <visp3/gui/vpDisplayX.h>
56 #include <visp3/core/vpHomogeneousMatrix.h>
57 #include <visp3/io/vpImageIo.h>
58 #include <visp3/core/vpIoTools.h>
59 #include <visp3/core/vpMath.h>
60 #include <visp3/io/vpVideoReader.h>
61 #include <visp3/io/vpParseArgv.h>
62 #include <visp3/mbt/vpMbKltMultiTracker.h>
64 #define GETOPTARGS "x:m:i:n:dchtfolwv"
67 void usage(
const char *name,
const char *badparam)
70 Example of tracking based on the 3D model.\n\
73 %s [-i <test image path>] [-x <config file>]\n\
74 [-m <model name>] [-n <initialisation file base name>]\n\
75 [-t] [-c] [-d] [-h] [-f] [-o] [-w] [-l] [-v]",
80 -i <input image path> \n\
81 Set image input path.\n\
82 From this path read images \n\
83 \"ViSP-images/mbt/cube/image%%04d.ppm\". These \n\
84 images come from ViSP-images-x.y.z.tar.gz available \n\
85 on the ViSP website.\n\
86 Setting the VISP_INPUT_IMAGE_PATH environment\n\
87 variable produces the same behavior than using\n\
91 Set the config file (the xml file) to use.\n\
92 The config file is used to specify the parameters of the tracker.\n\
95 Specify the name of the file of the model\n\
96 The model can either be a vrml model (.wrl) or a .cao file.\n\
99 Do not use the vrml model, use the .cao one. These two models are \n\
100 equivalent and comes from ViSP-images-x.y.z.tar.gz available on the ViSP\n\
101 website. However, the .cao model allows to use the 3d model based tracker \n\
104 -n <initialisation file base name> \n\
105 Base name of the initialisation file. The file will be 'base_name'.init .\n\
106 This base name is also used for the optional picture specifying where to \n\
107 click (a .ppm picture).\
110 Turn off the display of the the klt points. \n\
113 Turn off the display.\n\
116 Disable the mouse click. Useful to automate the \n\
117 execution of this program without human intervention.\n\
120 Use Ogre3D for visibility tests\n\
123 When Ogre3D is enable [-o] show Ogre3D configuration dialog that allows to set the renderer.\n\
126 Use the scanline for visibility tests.\n\
129 Compute covariance matrix.\n\
132 Print the help.\n\n");
135 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
139 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &configFile, std::string &modelFile,
140 std::string &initFile,
bool &displayKltPoints,
bool &click_allowed,
bool &display,
141 bool& cao3DModel,
bool &useOgre,
bool &showOgreConfigDialog,
bool &useScanline,
bool &computeCovariance)
148 case 'i': ipath = optarg_;
break;
149 case 'x': configFile = optarg_;
break;
150 case 'm': modelFile = optarg_;
break;
151 case 'n': initFile = optarg_;
break;
152 case 't': displayKltPoints =
false;
break;
153 case 'f': cao3DModel =
true;
break;
154 case 'c': click_allowed =
false;
break;
155 case 'd': display =
false;
break;
156 case 'o': useOgre =
true;
break;
157 case 'l': useScanline =
true;
break;
158 case 'w': showOgreConfigDialog =
true;
break;
159 case 'v': computeCovariance =
true;
break;
160 case 'h': usage(argv[0], NULL);
return false;
break;
163 usage(argv[0], optarg_);
168 if ((c == 1) || (c == -1)) {
170 usage(argv[0], NULL);
171 std::cerr <<
"ERROR: " << std::endl;
172 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
180 main(
int argc,
const char ** argv)
183 std::string env_ipath;
184 std::string opt_ipath;
186 std::string opt_configFile;
187 std::string configFile;
188 std::string opt_modelFile;
189 std::string modelFile;
190 std::string opt_initFile;
191 std::string initFile;
192 bool displayKltPoints =
true;
193 bool opt_click_allowed =
true;
194 bool opt_display =
true;
195 bool cao3DModel =
false;
196 bool useOgre =
false;
197 bool showOgreConfigDialog =
false;
198 bool useScanline =
false;
199 bool computeCovariance =
false;
206 if (! env_ipath.empty())
210 if (!getOptions(argc, argv, opt_ipath, opt_configFile, opt_modelFile, opt_initFile, displayKltPoints,
211 opt_click_allowed, opt_display, cao3DModel, useOgre, showOgreConfigDialog, useScanline,
212 computeCovariance)) {
217 if (opt_ipath.empty() && env_ipath.empty() ){
218 usage(argv[0], NULL);
219 std::cerr << std::endl
220 <<
"ERROR:" << std::endl;
221 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
223 <<
" environment variable to specify the location of the " << std::endl
224 <<
" image path where test images are located." << std::endl
231 if (!opt_ipath.empty())
236 if (!opt_configFile.empty())
237 configFile = opt_configFile;
238 else if (!opt_ipath.empty())
243 if (!opt_modelFile.empty()){
244 modelFile = opt_modelFile;
246 std::string modelFileCao =
"ViSP-images/mbt/cube.cao";
247 std::string modelFileWrl =
"ViSP-images/mbt/cube.wrl";
249 if(!opt_ipath.empty()){
254 #ifdef VISP_HAVE_COIN3D
257 std::cerr <<
"Coin is not detected in ViSP. Use the .cao model instead." << std::endl;
267 #ifdef VISP_HAVE_COIN3D
270 std::cerr <<
"Coin is not detected in ViSP. Use the .cao model instead." << std::endl;
277 if (!opt_initFile.empty())
278 initFile = opt_initFile;
279 else if (!opt_ipath.empty())
292 std::cout <<
"Cannot open sequence: " << ipath << std::endl;
300 #if defined VISP_HAVE_X11
302 #elif defined VISP_HAVE_GDI
304 #elif defined VISP_HAVE_OPENCV
306 #elif defined VISP_HAVE_D3D9
308 #elif defined VISP_HAVE_GTK
315 #if (defined VISP_HAVE_DISPLAY)
318 display1.
init(I1, 100, 100,
"Test tracking (Left)");
332 #if defined (VISP_HAVE_XML2)
334 tracker.loadConfigFile(configFile, configFile);
349 tracker.setCameraParameters(cam1, cam2);
350 tracker.setKltOpencv(klt);
353 tracker.setMaskBorder(5);
356 tracker.setNearClippingDistance(0.01);
357 tracker.setFarClippingDistance(0.90);
363 tracker.setDisplayFeatures(displayKltPoints);
366 tracker.setOgreVisibilityTest(useOgre);
368 tracker.setOgreShowConfigDialog(showOgreConfigDialog);
371 tracker.setScanLineVisibilityTest(useScanline);
374 tracker.setCovarianceComputation(computeCovariance);
377 tracker.getCameraParameters(cam1, cam2);
380 if (opt_display && opt_click_allowed)
391 tracker.loadModel(modelFile);
397 if (opt_display && opt_click_allowed)
399 tracker.initClick(I1, I2, initFile, initFile,
true);
400 tracker.getPose(c1Mo, c2Mo);
402 tracker.display(I1, I2,c1Mo, c2Mo, cam1, cam2,
vpColor::red);
406 vpHomogeneousMatrix c1Moi(0.02044769891, 0.1101505452, 0.5078963719, 2.063603907, 1.110231561, -0.4392789872);
407 vpHomogeneousMatrix c2Moi(0.02044769891, 0.1101505452, 0.5078963719, 2.063603907, 1.110231561, -0.4392789872);
408 tracker.initFromPose(I1, I2, c1Moi, c2Moi);
412 tracker.track(I1, I2);
413 tracker.getPose(c1Mo, c2Mo);
420 while (!reader.
end())
433 std::cout <<
"----------Test reset tracker----------" << std::endl;
438 tracker.resetTracker();
439 #if defined (VISP_HAVE_XML2)
440 tracker.loadConfigFile(configFile, configFile);
455 tracker.setCameraParameters(cam1, cam2);
456 tracker.setKltOpencv(klt);
459 tracker.setMaskBorder(5);
462 tracker.setNearClippingDistance(0.01);
463 tracker.setFarClippingDistance(0.90);
467 tracker.loadModel(modelFile);
468 tracker.setCameraParameters(cam1, cam2);
469 tracker.setOgreVisibilityTest(useOgre);
470 tracker.setScanLineVisibilityTest(useScanline);
471 tracker.setCovarianceComputation(computeCovariance);
472 tracker.initFromPose(I1, I2, c1Mo, c2Mo);
477 c1Mo.
buildFrom(0.0439540832, 0.0845870108, 0.5477322481, 2.179498458, 0.8611798108, -0.3491961946);
478 c2Mo.
buildFrom(0.0439540832, 0.0845870108, 0.5477322481, 2.179498458, 0.8611798108, -0.3491961946);
479 std::cout <<
"Test set pose" << std::endl;
480 tracker.setPose(I1, I2, c1Mo, c2Mo);
485 tracker.track(I1, I2);
486 tracker.getPose(c1Mo, c2Mo);
496 if (opt_click_allowed) {
504 if(computeCovariance) {
505 std::cout <<
"Covariance matrix: \n" << tracker.getCovarianceMatrix() << std::endl << std::endl;
511 if (opt_click_allowed && !quit) {
517 #if defined (VISP_HAVE_XML2)
522 #if defined(VISP_HAVE_COIN3D) && (COIN_MAJOR_VERSION == 2 || COIN_MAJOR_VERSION == 3)
532 std::cout <<
"Catch an exception: " << e << std::endl;
541 std::cout <<
"visp_mbt, visp_gui modules and OpenCV are required to run this example." << std::endl;
VISP_EXPORT int wait(double t0, double t)
long getFrameIndex() const
long getFirstFrameIndex() const
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
void setHarrisFreeParameter(double harris_k)
unsigned int getWidth() const
Implementation of an homogeneous matrix and operations on such kind of matrices.
virtual void setDownScalingFactor(unsigned int scale)
static const vpColor darkRed
Display for windows using GDI (available on any windows 32 platform).
void setMaxFeatures(const int maxCount)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void setMinDistance(double minDistance)
error that can be emited by ViSP classes.
static void flush(const vpImage< unsigned char > &I)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
void setQuality(double qualityLevel)
void initPersProjWithoutDistortion(const double px, const double py, const double u0, const double v0)
void open(vpImage< vpRGBa > &I)
Display for windows using Direct3D 3rd party. Thus to enable this class Direct3D should be installed...
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Generic class defining intrinsic camera parameters.
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
void acquire(vpImage< vpRGBa > &I)
void setFileName(const char *filename)
void setPyramidLevels(const int pyrMaxLevel)
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
static double rad(double deg)
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
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, vpImagePoint offset=vpImagePoint(0, 0))
void setWindowSize(const int winSize)
unsigned int getDownScalingFactor()
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
void setBlockSize(const int blockSize)
Model based stereo (or more) tracker using only KLT.