44 #include <visp3/core/vpConfig.h>
46 #if defined(VISP_HAVE_MODULE_MBT) && defined(VISP_HAVE_MODULE_KLT) && defined(VISP_HAVE_OPENCV) && \
47 defined(VISP_HAVE_DISPLAY) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
49 #include <visp3/core/vpDebug.h>
50 #include <visp3/core/vpHomogeneousMatrix.h>
51 #include <visp3/core/vpIoTools.h>
52 #include <visp3/core/vpMath.h>
53 #include <visp3/gui/vpDisplayD3D.h>
54 #include <visp3/gui/vpDisplayGDI.h>
55 #include <visp3/gui/vpDisplayGTK.h>
56 #include <visp3/gui/vpDisplayOpenCV.h>
57 #include <visp3/gui/vpDisplayX.h>
58 #include <visp3/io/vpImageIo.h>
59 #include <visp3/io/vpParseArgv.h>
60 #include <visp3/io/vpVideoReader.h>
61 #include <visp3/mbt/vpMbEdgeKltTracker.h>
63 #define GETOPTARGS "x:m:i:n:de:chtfColwvp"
65 #ifdef ENABLE_VISP_NAMESPACE
69 void usage(
const char *name,
const char *badparam)
71 #if VISP_HAVE_DATASET_VERSION >= 0x030600
72 std::string ext(
"png");
74 std::string ext(
"pgm");
77 Example of tracking based on the 3D model.\n\
80 %s [-i <test image path>] [-x <config file>]\n\
81 [-m <model name>] [-n <initialisation file base name>] [-e <last frame index>]\n\
82 [-t] [-c] [-d] [-h] [-f] [-C] [-o] [-w] [-l] [-v] [-p]\n",
87 -i <input image path> \n\
88 Set image input path.\n\
89 From this path read images \n\
90 \"mbt/cube/image%%04d.%s\". These \n\
91 images come from visp-images-x.y.z.tar.gz available \n\
92 on the ViSP website.\n\
93 Setting the VISP_INPUT_IMAGE_PATH environment\n\
94 variable produces the same behaviour than using\n\
98 Set the config file (the xml file) to use.\n\
99 The config file is used to specify the parameters of the tracker.\n\
102 Specify the name of the file of the model\n\
103 The model can either be a vrml model (.wrl) or a .cao file.\n\
105 -e <last frame index> \n\
106 Specify the index of the last frame. Once reached, the tracking is stopped\n\
109 Do not use the vrml model, use the .cao one. These two models are \n\
110 equivalent and comes from ViSP-images-x.y.z.tar.gz available on the ViSP\n\
111 website. However, the .cao model allows to use the 3d model based tracker \n\
115 Track only the cube (not the cylinder). In this case the models files are\n\
116 cube.cao or cube.wrl instead of cube_and_cylinder.cao and \n\
117 cube_and_cylinder.wrl.\n\
119 -n <initialisation file base name> \n\
120 Base name of the initialisation file. The file will be 'base_name'.init .\n\
121 This base name is also used for the Optional picture specifying where to \n\
122 click (a .ppm picture).\n\
125 Turn off the display of the the moving edges and Klt points. \n\
128 Turn off the display.\n\
131 Disable the mouse click. Useful to automate the \n\
132 execution of this program without human intervention.\n\
135 Use Ogre3D for visibility tests\n\
138 When Ogre3D is enable [-o] show Ogre3D configuration dialog thatallows to set the renderer.\n\
141 Use the scanline for visibility tests.\n\
144 Compute covariance matrix.\n\
147 Compute gradient projection error.\n\
150 Print the help.\n\n",
154 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
157 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &configFile, std::string &modelFile,
158 std::string &initFile,
long &lastFrame,
bool &displayFeatures,
bool &click_allowed,
bool &display,
159 bool &cao3DModel,
bool &trackCylinder,
bool &useOgre,
bool &showOgreConfigDialog,
bool &useScanline,
160 bool &computeCovariance,
bool &projectionError)
168 lastFrame = atol(optarg_);
174 configFile = optarg_;
183 displayFeatures =
false;
189 click_allowed =
false;
195 trackCylinder =
false;
204 showOgreConfigDialog =
true;
207 computeCovariance =
true;
210 projectionError =
true;
213 usage(argv[0],
nullptr);
218 usage(argv[0], optarg_);
224 if ((c == 1) || (c == -1)) {
226 usage(argv[0],
nullptr);
227 std::cerr <<
"ERROR: " << std::endl;
228 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
235 int main(
int argc,
const char **argv)
238 std::string env_ipath;
239 std::string opt_ipath;
241 std::string opt_configFile;
242 std::string configFile;
243 std::string opt_modelFile;
244 std::string modelFile;
245 std::string opt_initFile;
246 std::string initFile;
247 long opt_lastFrame = -1;
248 bool displayFeatures =
true;
249 bool opt_click_allowed =
true;
250 bool opt_display =
true;
251 bool cao3DModel =
false;
252 bool trackCylinder =
true;
253 bool useOgre =
false;
254 bool showOgreConfigDialog =
false;
255 bool useScanline =
false;
256 bool computeCovariance =
false;
257 bool projectionError =
false;
260 #if VISP_HAVE_DATASET_VERSION >= 0x030600
261 std::string ext(
"png");
263 std::string ext(
"pgm");
271 if (!env_ipath.empty())
275 if (!getOptions(argc, argv, opt_ipath, opt_configFile, opt_modelFile, opt_initFile, opt_lastFrame, displayFeatures,
276 opt_click_allowed, opt_display, cao3DModel, trackCylinder, useOgre, showOgreConfigDialog,
277 useScanline, computeCovariance, projectionError)) {
282 if (opt_ipath.empty() && env_ipath.empty()) {
283 usage(argv[0],
nullptr);
284 std::cerr << std::endl <<
"ERROR:" << std::endl;
285 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
286 <<
" environment variable to specify the location of the " << std::endl
287 <<
" image path where test images are located." << std::endl
294 if (!opt_ipath.empty())
299 if (!opt_configFile.empty())
300 configFile = opt_configFile;
301 else if (!opt_ipath.empty())
306 if (!opt_modelFile.empty()) {
307 modelFile = opt_modelFile;
310 std::string modelFileCao;
311 std::string modelFileWrl;
313 modelFileCao =
"mbt/cube_and_cylinder.cao";
314 modelFileWrl =
"mbt/cube_and_cylinder.wrl";
317 modelFileCao =
"mbt/cube.cao";
318 modelFileWrl =
"mbt/cube.wrl";
321 if (!opt_ipath.empty()) {
326 #ifdef VISP_HAVE_COIN3D
329 std::cerr <<
"Coin is not detected in ViSP. Use the .cao model instead." << std::endl;
339 #ifdef VISP_HAVE_COIN3D
342 std::cerr <<
"Coin is not detected in ViSP. Use the .cao model instead." << std::endl;
349 if (!opt_initFile.empty())
350 initFile = opt_initFile;
351 else if (!opt_ipath.empty())
364 std::cout <<
"Cannot open sequence: " << ipath << std::endl;
374 #if defined(VISP_HAVE_X11)
376 #elif defined(VISP_HAVE_GDI)
378 #elif defined(HAVE_OPENCV_HIGHGUI)
380 #elif defined(VISP_HAVE_D3D9)
382 #elif defined(VISP_HAVE_GTK)
388 #if defined(VISP_HAVE_DISPLAY)
389 display.init(I, 100, 100,
"Test tracking");
395 vpMbEdgeKltTracker tracker;
401 #if defined(VISP_HAVE_PUGIXML)
403 tracker.loadConfigFile(configFile);
428 tracker.setCameraParameters(cam);
429 tracker.setMovingEdge(me);
430 tracker.setKltOpencv(klt);
431 tracker.setKltMaskBorder(5);
436 tracker.setNearClippingDistance(0.01);
437 tracker.setFarClippingDistance(0.90);
445 tracker.setDisplayFeatures(displayFeatures);
448 tracker.setOgreVisibilityTest(useOgre);
450 tracker.setOgreShowConfigDialog(showOgreConfigDialog);
453 tracker.setScanLineVisibilityTest(useScanline);
456 tracker.setCovarianceComputation(computeCovariance);
459 tracker.setProjectionErrorComputation(projectionError);
462 tracker.getCameraParameters(cam);
465 if (opt_display && opt_click_allowed) {
475 tracker.loadModel(modelFile);
483 if (opt_display && opt_click_allowed) {
484 tracker.initClick(I, initFile,
true);
485 tracker.getPose(cMo);
490 vpHomogeneousMatrix cMoi(0.02044769891, 0.1101505452, 0.5078963719, 2.063603907, 1.110231561, -0.4392789872);
491 tracker.initFromPose(I, cMoi);
496 tracker.getPose(cMo);
501 while (!reader.
end()) {
510 vpTRACE(
"Test reset tracker");
513 tracker.resetTracker();
514 #if defined(VISP_HAVE_PUGIXML)
515 tracker.loadConfigFile(configFile);
540 tracker.setCameraParameters(cam);
541 tracker.setMovingEdge(me);
542 tracker.setKltOpencv(klt);
543 tracker.setKltMaskBorder(5);
548 tracker.setNearClippingDistance(0.01);
549 tracker.setFarClippingDistance(0.90);
555 tracker.loadModel(modelFile);
556 tracker.setCameraParameters(cam);
557 tracker.setOgreVisibilityTest(useOgre);
558 tracker.setScanLineVisibilityTest(useScanline);
559 tracker.setCovarianceComputation(computeCovariance);
560 tracker.setProjectionErrorComputation(projectionError);
561 tracker.initFromPose(I, cMo);
566 cMo.
buildFrom(0.0439540832, 0.0845870108, 0.5477322481, 2.179498458, 0.8611798108, -0.3491961946);
567 vpTRACE(
"Test set pose");
568 tracker.setPose(I, cMo);
585 tracker.getPose(cMo);
594 if (opt_click_allowed) {
602 if (computeCovariance) {
603 std::cout <<
"Covariance matrix: \n" << tracker.getCovarianceMatrix() << std::endl << std::endl;
606 if (projectionError) {
607 std::cout <<
"Projection error: " << tracker.getProjectionError() << std::endl << std::endl;
614 std::cout <<
"Reached last frame: " << reader.
getFrameIndex() << std::endl;
616 if (opt_click_allowed && !quit) {
624 std::cout <<
"Catch an exception: " << e << std::endl;
633 std::cout <<
"visp_mbt, visp_gui modules and OpenCV are required to run "
Generic class defining intrinsic camera parameters.
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
static const vpColor darkRed
Display for windows using Direct3D 3rd party. Thus to enable this class Direct3D should be installed....
Display for windows using GDI (available on any windows 32 platform).
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
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 flush(const vpImage< unsigned char > &I)
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.
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix & buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
Wrapper for the KLT (Kanade-Lucas-Tomasi) feature tracker implemented in OpenCV. Thus to enable this ...
void setBlockSize(int blockSize)
void setQuality(double qualityLevel)
void setHarrisFreeParameter(double harris_k)
void setMaxFeatures(int maxCount)
void setMinDistance(double minDistance)
void setWindowSize(int winSize)
void setPyramidLevels(int pyrMaxLevel)
static double rad(double deg)
void setMu1(const double &mu_1)
void setRange(const unsigned int &range)
void setLikelihoodThresholdType(const vpLikelihoodThresholdType likelihood_threshold_type)
void setMaskNumber(const unsigned int &mask_number)
void setThreshold(const double &threshold)
void setSampleStep(const double &sample_step)
void setMaskSize(const unsigned int &mask_size)
void setMu2(const double &mu_2)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
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 setLastFrameIndex(const long last_frame)
void open(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
long getFirstFrameIndex()
long getFrameIndex() const
VISP_EXPORT int wait(double t0, double t)