2#include <visp3/core/vpIoTools.h>
3#include <visp3/gui/vpDisplayGDI.h>
4#include <visp3/gui/vpDisplayOpenCV.h>
5#include <visp3/gui/vpDisplayX.h>
6#include <visp3/gui/vpPlot.h>
8#include <visp3/mbt/vpMbGenericTracker.h>
10#include <visp3/io/vpVideoReader.h>
11#include <visp3/io/vpVideoWriter.h>
13int main(
int argc,
char **argv)
15#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_VIDEOIO) && defined(HAVE_OPENCV_HIGHGUI)
16 std::string opt_videoname =
"model/teabox/teabox.mp4";
17 std::string opt_modelname =
"model/teabox/teabox.cao";
19 int opt_video_first_frame = -1;
20 int opt_downscale_img = 1;
21 bool opt_verbose =
false;
23 bool opt_display_scale_auto =
false;
26 unsigned int thickness = 2;
34 for (
int i = 0; i < argc; i++) {
35 if (std::string(argv[i]) ==
"--video") {
36 opt_videoname = std::string(argv[++i]);
38 else if (std::string(argv[i]) ==
"--video-first-frame") {
39 opt_video_first_frame = std::atoi(argv[++i]);
41 else if (std::string(argv[i]) ==
"--model") {
42 opt_modelname = std::string(argv[++i]);
44 else if (std::string(argv[i]) ==
"--tracker") {
45 opt_tracker = atoi(argv[++i]);
47 else if (std::string(argv[i]) ==
"--downscale-img") {
48 opt_downscale_img = std::atoi(argv[++i]);
50 else if (std::string(argv[i]) ==
"--save") {
51 opt_save = std::string(argv[++i]);
53 else if (std::string(argv[i]) ==
"--plot") {
56 else if (std::string(argv[i]) ==
"--dof") {
57 for (
int j = 0; j < 6; j++) {
58 int val = std::atoi(argv[++i]);
59 if (val == 0 || val == 1) {
60 opt_dof_to_estimate[j] = val;
63 std::cout <<
"Error: wrong value after --dof option. Authorized values are 0 or 1 for each 6 dof to estimate." << std::endl;
68 else if (std::string(argv[i]) ==
"--display-scale-auto") {
69 opt_display_scale_auto =
true;
71 else if (std::string(argv[i]) ==
"--verbose" || std::string(argv[i]) ==
"-v") {
74 else if (std::string(argv[i]) ==
"--help" || std::string(argv[i]) ==
"-h") {
75 std::cout <<
"\nSYNOPSIS " << std::endl
77 <<
" [--video <video name>]"
78 <<
" [--video-first-frame <image index>"
79 <<
" [--model <model name>"
80 <<
" [--tracker <0=egde|1=keypoint|2=hybrid>]"
81 <<
" [--downscale-img <scale factor>]"
82 <<
" [--dof <0/1 0/1 0/1 0/1 0/1 0/1>]"
83 <<
" [--save <video>]"
84 <<
" [--display-scale-auto]"
89 std::cout <<
"\nOPTIONS " << std::endl
90 <<
" --video <video name>" << std::endl
91 <<
" Input video name." << std::endl
92 <<
" Default: model/teabox/teabox.mp4" << std::endl
94 <<
" --video-first-frame <image index>" << std::endl
95 <<
" Index of the first image to process." << std::endl
96 <<
" Set to -1 to process the first image of the video." << std::endl
97 <<
" Default: -1" << std::endl
99 <<
" --model <model name>" << std::endl
100 <<
" CAD model filename. Supported formats are .cao and .wrl." << std::endl
101 <<
" To use wrl format, ViSP need to be built with Coin3D third-party." << std::endl
102 <<
" Default: model/teabox/teabox.cao" << std::endl
104 <<
" --tracker <0=egde|1=keypoint|2=hybrid>" << std::endl
105 <<
" Tracker type:" << std::endl
106 <<
" - when 0: use only moving-edges" << std::endl
107 <<
" - when 1: use only KLT keypoints" << std::endl
108 <<
" - when 2: use hybrid scheme, moving-edges and KLT keypoints." << std::endl
109 <<
" Default: 0" << std::endl
111 <<
" --downscale-img <scale factor>" << std::endl
112 <<
" Downscale input image width and height by this factor." << std::endl
113 <<
" When set to 1, image not down scaled. When set to 2, image width" << std::endl
114 <<
" and height is divided by 2." << std::endl
115 <<
" Default: 1" << std::endl
117 <<
" --dof <0/1 0/1 0/1 0/1 0/1 0/1>" << std::endl
118 <<
" 6-dim vector of 0 and 1 to indicate which dof [tx ty tz rx ry rz]" << std::endl
119 <<
" has to be estimated." << std::endl
120 <<
" When set to 1 the dof is estimated. When rather set to 0 the dof" << std::endl
121 <<
" is not estimated. It's value is the one from the initialisation." << std::endl
122 <<
" Default: 1 1 1 1 1 1 (to estimate all 6 dof)" << std::endl
124 <<
" --save <video>" << std::endl
125 <<
" Name of the saved image sequence that contains tracking results in overlay." << std::endl
126 <<
" When the name contains a folder like in the next example, the folder" << std::endl
127 <<
" is created if it doesn't exist."
128 <<
" Example: \"result/image-%04d.png\"." << std::endl
130 <<
" --display-scale-auto" << std::endl
131 <<
" Enable display window auto scaling to ensure that the image is fully" << std::endl
132 <<
" visible on the screen. Useful for large images." << std::endl
133 <<
" Note that this option doesn't affect the size of the processed images." << std::endl
135 <<
" --plot" << std::endl
136 <<
" Open a window that plots the estimated pose evolution." << std::endl
138 <<
" --verbose, -v" << std::endl
139 <<
" Enable verbose mode." << std::endl
141 <<
" --help, -h" << std::endl
142 <<
" Display this helper message." << std::endl
150 if (!parentname.empty())
151 objectname = parentname +
"/" + objectname;
153 std::cout <<
" *********** Tracker config ************ " << std::endl;
154 std::cout <<
"Video name : " << opt_videoname << std::endl;
155 std::cout <<
"Tracker cad model file : " << objectname <<
".[cao or wrl]" << std::endl;
156 std::cout <<
"Tracker init file : " << objectname <<
".init" << std::endl;
157 std::cout <<
"Tracker optional init image: " << objectname <<
".[png,ppm,jpg]" << std::endl;
158 if (opt_downscale_img > 1) {
159 std::cout <<
"Downscale image factor : " << opt_downscale_img << std::endl;
161 std::cout <<
"Dof to estimate : " << opt_dof_to_estimate.t() << std::endl;
164 if (!opt_save.empty()) {
166 if (!parent.empty()) {
167 std::cout <<
"Create output directory: " << parent << std::endl;
181 if (opt_video_first_frame > 0) {
184 if (opt_downscale_img > 1) {
186 Ivideo.
subsample(opt_downscale_img, opt_downscale_img, I);
193 if (!opt_save.empty()) {
199#if defined(VISP_HAVE_X11)
201#elif defined(VISP_HAVE_GDI)
203#elif defined(HAVE_OPENCV_HIGHGUI)
206 if (opt_display_scale_auto) {
209 display->init(I, 100, 100,
"Model-based tracker");
212 plot =
new vpPlot(2, 700, 700, display->getWindowXPosition() + I.
getWidth() / display->getDownScalingFactor() + 30, display->getWindowYPosition(),
"Estimated pose");
214 plot->
setTitle(0,
"Translation [m]");
219 plot->
setTitle(1,
"Attitude thetaU [deg]");
227 if (opt_tracker == 0)
229#if defined(VISP_HAVE_MODULE_KLT) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
230 else if (opt_tracker == 1)
236 std::cout <<
"klt and hybrid model-based tracker are not available since visp_klt module is not available. "
237 "In CMakeGUI turn visp_klt module ON, configure and build ViSP again."
247 std::cout <<
"Tracker config file : " << objectname +
".xml" << std::endl;
255 if (opt_tracker == 0 || opt_tracker == 2) {
270#if defined(VISP_HAVE_MODULE_KLT) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
271 if (opt_tracker == 1 || opt_tracker == 2) {
331 if (opt_dof_to_estimate != 1.) {
338 std::cout <<
"Camera parameters: \n" << cam << std::endl;
341 std::cout <<
"Initialize tracker on image size: " << I.
getWidth() <<
" x " << I.
getHeight() << std::endl;
344 tracker.
initClick(I, objectname +
".init",
true);
348 if (opt_downscale_img > 1) {
350 Ivideo.
subsample(opt_downscale_img, opt_downscale_img, I);
355 std::stringstream ss;
358 std::cout <<
"-- " << ss.str() << std::endl;
374 std::stringstream ss;
379#if defined(VISP_HAVE_MODULE_KLT) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
386 std::cout << ss.str() << std::endl;
387 std::cout <<
"cMo:\n" << cMo << std::endl;
392 std::stringstream ss;
393 ss <<
"Projection error: " << std::setprecision(2) << proj_error <<
" deg";
396 std::cout << ss.str() << std::endl;
409 if (!opt_save.empty()) {
420 std::cout <<
"Catch a ViSP exception: " << e << std::endl;
424 catch (Ogre::Exception &e) {
425 std::cout <<
"Catch an Ogre exception: " << e.getDescription() << std::endl;
441 std::cout <<
"Install OpenCV and rebuild ViSP to use this example." << std::endl;
Generic class defining intrinsic camera parameters.
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
Implementation of column vector and the associated operations.
static const vpColor none
static const vpColor blue
static const vpColor green
Display for windows using GDI (available on any windows 32 platform).
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...
Class that defines generic functionalities for display.
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 getImage(const vpImage< unsigned char > &Is, vpImage< vpRGBa > &Id)
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.
vpRotationMatrix getRotationMatrix() const
vpTranslationVector getTranslationVector() const
Definition of the vpImage class member functions.
void subsample(unsigned int v_scale, unsigned int h_scale, vpImage< Type > &sampled) const
unsigned int getWidth() const
unsigned int getHeight() const
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)
static double deg(double rad)
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 int getTrackerType() const
virtual void setKltMaskBorder(const unsigned int &e)
virtual unsigned int getNbFeaturesEdge() const
virtual void setOgreShowConfigDialog(bool showConfigDialog)
virtual void setGoodMovingEdgesRatioThreshold(double threshold)
virtual void setAngleAppear(const double &a)
virtual void setNearClippingDistance(const double &dist)
virtual unsigned int getNbFeaturesKlt() const
virtual void getCameraParameters(vpCameraParameters &camera) const
virtual void setAngleDisappear(const double &a)
virtual void setMovingEdge(const vpMe &me)
virtual void setScanLineVisibilityTest(const bool &v)
virtual void setKltOpencv(const vpKltOpencv &t)
virtual void getClipping(unsigned int &clippingFlag1, unsigned int &clippingFlag2) const
virtual void setTrackerType(int type)
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 double computeCurrentProjectionError(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &_cMo, const vpCameraParameters &_cam)
virtual void setClipping(const unsigned int &flags)
virtual void loadConfigFile(const std::string &configFile, bool verbose=true)
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)
virtual void setEstimatedDoF(const vpColVector &v)
void setMu1(const double &mu_1)
void setSampleStep(const double &s)
void setRange(const unsigned int &r)
void setLikelihoodThresholdType(const vpLikelihoodThresholdType likelihood_threshold_type)
void setMaskSize(const unsigned int &a)
void setMu2(const double &mu_2)
@ NORMALIZED_THRESHOLD
Easy-to-use normalized likelihood threshold corresponding to the minimal luminance contrast to consid...
void setMaskNumber(const unsigned int &a)
void setThreshold(const double &t)
This class enables real time drawing of 2D or 3D graphics. An instance of the class open a window whi...
void initGraph(unsigned int graphNum, unsigned int curveNbr)
void plot(unsigned int graphNum, unsigned int curveNum, double x, double y)
void setColor(unsigned int graphNum, unsigned int curveNum, vpColor color)
void setTitle(unsigned int graphNum, const std::string &title)
Implementation of a rotation vector as axis-angle minimal representation.
Class that consider the case of a translation vector.
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)
void setFirstFrameIndex(const long first_frame)
long getFrameIndex() const
Class that enables to write easily a video file or a sequence of images.
void saveFrame(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
void open(vpImage< vpRGBa > &I)