1 #include <visp3/core/vpConfig.h>
3 #include <visp3/core/vpIoTools.h>
4 #include <visp3/gui/vpDisplayGDI.h>
5 #include <visp3/gui/vpDisplayOpenCV.h>
6 #include <visp3/gui/vpDisplayX.h>
7 #include <visp3/gui/vpPlot.h>
9 #include <visp3/mbt/vpMbGenericTracker.h>
11 #include <visp3/io/vpVideoReader.h>
12 #include <visp3/io/vpVideoWriter.h>
14 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_VIDEOIO) && defined(HAVE_OPENCV_HIGHGUI)
15 #ifdef ENABLE_VISP_NAMESPACE
25 std::vector<double> vec { t[0], t[1], t[2], tu[0], tu[1], tu[2] };
32 int main(
int argc,
char **argv)
34 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_VIDEOIO) && defined(HAVE_OPENCV_HIGHGUI)
35 std::string opt_videoname =
"model/teabox/teabox.mp4";
36 std::string opt_modelname =
"model/teabox/teabox.cao";
38 int opt_video_first_frame = -1;
39 int opt_downscale_img = 1;
40 bool opt_verbose =
false;
42 bool opt_display_scale_auto =
false;
45 #if defined(VISP_HAVE_MINIZ) && defined(VISP_HAVE_WORKING_REGEX)
46 std::string opt_save_results;
48 unsigned int thickness = 2;
51 std::shared_ptr<vpDisplay> display;
52 std::shared_ptr<vpPlot> plot;
53 std::shared_ptr<vpVideoWriter> writer;
56 for (
int i = 0; i < argc; i++) {
57 if (std::string(argv[i]) ==
"--video") {
58 opt_videoname = std::string(argv[++i]);
60 else if (std::string(argv[i]) ==
"--video-first-frame") {
61 opt_video_first_frame = std::atoi(argv[++i]);
63 else if (std::string(argv[i]) ==
"--model") {
64 opt_modelname = std::string(argv[++i]);
66 else if (std::string(argv[i]) ==
"--tracker") {
67 opt_tracker = atoi(argv[++i]);
69 else if (std::string(argv[i]) ==
"--downscale-img") {
70 opt_downscale_img = std::atoi(argv[++i]);
72 else if (std::string(argv[i]) ==
"--save") {
73 opt_save = std::string(argv[++i]);
75 #if defined(VISP_HAVE_MINIZ) && defined(VISP_HAVE_WORKING_REGEX)
76 else if (std::string(argv[i]) ==
"--save-results") {
77 opt_save_results = std::string(argv[++i]);
80 else if (std::string(argv[i]) ==
"--plot") {
83 else if (std::string(argv[i]) ==
"--dof") {
84 for (
int j = 0; j < 6; j++) {
85 int val = std::atoi(argv[++i]);
86 if (val == 0 || val == 1) {
87 opt_dof_to_estimate[j] = val;
90 std::cout <<
"Error: wrong value after --dof option. Authorized values are 0 or 1 for each 6 dof to estimate." << std::endl;
95 else if (std::string(argv[i]) ==
"--display-scale-auto") {
96 opt_display_scale_auto =
true;
98 else if (std::string(argv[i]) ==
"--verbose" || std::string(argv[i]) ==
"-v") {
101 else if (std::string(argv[i]) ==
"--help" || std::string(argv[i]) ==
"-h") {
102 std::cout <<
"\nSYNOPSIS " << std::endl
104 <<
" [--video <video name>]"
105 <<
" [--video-first-frame <image index>"
106 <<
" [--model <model name>"
107 <<
" [--tracker <0=egde|1=keypoint|2=hybrid>]"
108 <<
" [--downscale-img <scale factor>]"
109 <<
" [--dof <0/1 0/1 0/1 0/1 0/1 0/1>]"
110 <<
" [--save <e.g. results-%04d.png>]"
111 #if defined(VISP_HAVE_MINIZ) && defined(VISP_HAVE_WORKING_REGEX)
112 <<
" [--save-results <e.g. tracking_poses.npz>]"
114 <<
" [--display-scale-auto]"
119 std::cout <<
"\nOPTIONS " << std::endl
120 <<
" --video <video name>" << std::endl
121 <<
" Input video name." << std::endl
122 <<
" Default: model/teabox/teabox.mp4" << std::endl
124 <<
" --video-first-frame <image index>" << std::endl
125 <<
" Index of the first image to process." << std::endl
126 <<
" Set to -1 to process the first image of the video." << std::endl
127 <<
" Default: -1" << std::endl
129 <<
" --model <model name>" << std::endl
130 <<
" CAD model filename. Supported formats are .cao and .wrl." << std::endl
131 <<
" To use wrl format, ViSP need to be built with Coin3D third-party." << std::endl
132 <<
" Default: model/teabox/teabox.cao" << std::endl
134 <<
" --tracker <0=egde|1=keypoint|2=hybrid>" << std::endl
135 <<
" Tracker type:" << std::endl
136 <<
" - when 0: use only moving-edges" << std::endl
137 <<
" - when 1: use only KLT keypoints" << std::endl
138 <<
" - when 2: use hybrid scheme, moving-edges and KLT keypoints." << std::endl
139 <<
" Default: 0" << std::endl
141 <<
" --downscale-img <scale factor>" << std::endl
142 <<
" Downscale input image width and height by this factor." << std::endl
143 <<
" When set to 1, image not down scaled. When set to 2, image width" << std::endl
144 <<
" and height is divided by 2." << std::endl
145 <<
" Default: 1" << std::endl
147 <<
" --dof <0/1 0/1 0/1 0/1 0/1 0/1>" << std::endl
148 <<
" 6-dim vector of 0 and 1 to indicate which dof [tx ty tz rx ry rz]" << std::endl
149 <<
" has to be estimated." << std::endl
150 <<
" When set to 1 the dof is estimated. When rather set to 0 the dof" << std::endl
151 <<
" is not estimated. It's value is the one from the initialisation." << std::endl
152 <<
" Default: 1 1 1 1 1 1 (to estimate all 6 dof)" << std::endl
154 <<
" --save <e.g. results-%04d.png>" << std::endl
155 <<
" Name of the saved image sequence that contains tracking results in overlay." << std::endl
156 <<
" When the name contains a folder like in the next example, the folder" << std::endl
157 <<
" is created if it doesn't exist."
158 <<
" Example: \"result/image-%04d.png\"." << std::endl
160 #if defined(VISP_HAVE_MINIZ) && defined(VISP_HAVE_WORKING_REGEX)
161 <<
" --save-results <e.g. tracking_results.npz>" << std::endl
162 <<
" Name of the npz file containing cMo data estimated from MBT." << std::endl
163 <<
" When the name contains a folder like in the next example, the folder" << std::endl
164 <<
" is created if it doesn't exist."
165 <<
" Example: \"result/tracking_results.npz\"." << std::endl
168 <<
" --display-scale-auto" << std::endl
169 <<
" Enable display window auto scaling to ensure that the image is fully" << std::endl
170 <<
" visible on the screen. Useful for large images." << std::endl
171 <<
" Note that this option doesn't affect the size of the processed images." << std::endl
173 <<
" --plot" << std::endl
174 <<
" Open a window that plots the estimated pose evolution." << std::endl
176 <<
" --verbose, -v" << std::endl
177 <<
" Enable verbose mode." << std::endl
179 <<
" --help, -h" << std::endl
180 <<
" Display this helper message." << std::endl
188 if (!parentname.empty())
189 objectname = parentname +
"/" + objectname;
191 std::cout <<
" *********** Tracker config ************ " << std::endl;
192 std::cout <<
"Video name : " << opt_videoname << std::endl;
193 std::cout <<
"Tracker cad model file : " << objectname <<
".[cao or wrl]" << std::endl;
194 std::cout <<
"Tracker init file : " << objectname <<
".init" << std::endl;
195 std::cout <<
"Tracker optional init image: " << objectname <<
".[png,ppm,jpg]" << std::endl;
196 if (opt_downscale_img > 1) {
197 std::cout <<
"Downscale image factor : " << opt_downscale_img << std::endl;
199 std::cout <<
"Dof to estimate : " << opt_dof_to_estimate.t() << std::endl;
202 if (!opt_save.empty()) {
204 if (!parent.empty()) {
205 std::cout <<
"Create output directory: " << parent << std::endl;
209 #if defined(VISP_HAVE_MINIZ) && defined(VISP_HAVE_WORKING_REGEX)
210 if (!opt_save_results.empty()) {
212 if (!parent.empty()) {
213 std::cout <<
"Create output directory for the npz file: " << parent << std::endl;
228 if (opt_video_first_frame > 0) {
231 if (opt_downscale_img > 1) {
233 Ivideo.
subsample(opt_downscale_img, opt_downscale_img, I);
240 if (!opt_save.empty()) {
241 writer = std::make_shared<vpVideoWriter>();
242 writer->setFileName(opt_save);
246 #if defined(VISP_HAVE_X11)
247 display = std::make_shared<vpDisplayX>();
248 #elif defined(VISP_HAVE_GDI)
249 display = std::make_shared<vpDisplayGDI>();
250 #elif defined(HAVE_OPENCV_HIGHGUI)
251 display = std::make_shared<vpDisplayOpenCV>();
253 if (opt_display_scale_auto) {
256 display->init(I, 100, 100,
"Model-based tracker");
259 plot = std::make_shared<vpPlot>(2, 700, 700, display->getWindowXPosition() + I.
getWidth() / display->getDownScalingFactor() + 30,
260 display->getWindowYPosition(),
"Estimated pose");
261 plot->initGraph(0, 3);
262 plot->setTitle(0,
"Translation [m]");
266 plot->initGraph(1, 3);
267 plot->setTitle(1,
"Attitude thetaU [deg]");
275 if (opt_tracker == 0)
277 #if defined(VISP_HAVE_MODULE_KLT) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
278 else if (opt_tracker == 1)
284 std::cout <<
"klt and hybrid model-based tracker are not available since visp_klt module is not available. "
285 "In CMakeGUI turn visp_klt module ON, configure and build ViSP again."
294 #if defined(VISP_HAVE_PUGIXML)
296 std::cout <<
"Tracker config file : " << objectname +
".xml" << std::endl;
305 if (opt_tracker == 0 || opt_tracker == 2) {
320 #if defined(VISP_HAVE_MODULE_KLT) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
321 if (opt_tracker == 1 || opt_tracker == 2) {
331 tracker.setKltOpencv(klt_settings);
332 tracker.setKltMaskBorder(5);
381 if (opt_dof_to_estimate != 1.) {
388 std::cout <<
"Camera parameters: \n" << cam << std::endl;
391 std::cout <<
"Initialize tracker on image size: " << I.
getWidth() <<
" x " << I.
getHeight() << std::endl;
393 std::vector<double> vec_poses;
394 #if defined(VISP_HAVE_MINIZ) && defined(VISP_HAVE_WORKING_REGEX)
395 if (!opt_save_results.empty()) {
409 tracker.initClick(I, objectname +
".init",
true);
413 if (opt_downscale_img > 1) {
415 Ivideo.
subsample(opt_downscale_img, opt_downscale_img, I);
420 std::stringstream ss;
423 std::cout <<
"-- " << ss.str() << std::endl;
439 std::stringstream ss;
444 #if defined(VISP_HAVE_MODULE_KLT) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO)
451 std::cout << ss.str() << std::endl;
452 std::cout <<
"cMo:\n" << cMo << std::endl;
457 std::stringstream ss;
458 ss <<
"Projection error: " << std::setprecision(2) << proj_error <<
" deg";
461 std::cout << ss.str() << std::endl;
474 if (!opt_save.empty()) {
476 writer->saveFrame(O);
479 #if defined(VISP_HAVE_MINIZ) && defined(VISP_HAVE_WORKING_REGEX)
480 if (!opt_save_results.empty()) {
481 std::vector<double> vec_pose = poseToVec(cMo);
482 vec_poses.insert(vec_poses.end(), vec_pose.begin(), vec_pose.end());
491 #if defined(VISP_HAVE_MINIZ) && defined(VISP_HAVE_WORKING_REGEX)
492 if (!opt_save_results.empty()) {
493 visp::cnpy::npz_save(opt_save_results,
"vec_poses", vec_poses.data(), { static_cast<size_t>(vec_poses.size()/6), 6 },
"a");
498 std::cout <<
"Catch a ViSP exception: " << e << std::endl;
501 #ifdef VISP_HAVE_OGRE
502 catch (Ogre::Exception &e) {
503 std::cout <<
"Catch an Ogre exception: " << e.getDescription() << std::endl;
510 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
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.
vpThetaUVector getThetaUVector() const
vpRotationMatrix getRotationMatrix() const
vpTranslationVector getTranslationVector() const
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) VP_OVERRIDE
virtual int getTrackerType() const
virtual void setOgreVisibilityTest(const bool &v) VP_OVERRIDE
virtual void setDisplayFeatures(bool displayF) VP_OVERRIDE
virtual unsigned int getNbFeaturesEdge() const
virtual double computeCurrentProjectionError(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &_cMo, const vpCameraParameters &_cam) VP_OVERRIDE
virtual void getCameraParameters(vpCameraParameters &camera) const VP_OVERRIDE
virtual void setGoodMovingEdgesRatioThreshold(double threshold)
virtual void getPose(vpHomogeneousMatrix &cMo) const VP_OVERRIDE
virtual unsigned int getNbFeaturesKlt() const
virtual void setMovingEdge(const vpMe &me)
virtual void setAngleDisappear(const double &a) VP_OVERRIDE
virtual void track(const vpImage< unsigned char > &I) VP_OVERRIDE
virtual void loadModel(const std::string &modelFile, bool verbose=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix()) VP_OVERRIDE
virtual void getClipping(unsigned int &clippingFlag1, unsigned int &clippingFlag2) const
virtual void setClipping(const unsigned int &flags) VP_OVERRIDE
virtual void setTrackerType(int type)
virtual void setOgreShowConfigDialog(bool showConfigDialog) VP_OVERRIDE
virtual void setScanLineVisibilityTest(const bool &v) VP_OVERRIDE
virtual void setNearClippingDistance(const double &dist) VP_OVERRIDE
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &col, unsigned int thickness=1, bool displayFullModel=false) VP_OVERRIDE
virtual void loadConfigFile(const std::string &configFile, bool verbose=true) VP_OVERRIDE
virtual void setAngleAppear(const double &a) VP_OVERRIDE
virtual void setFarClippingDistance(const double &dist) VP_OVERRIDE
virtual void setEstimatedDoF(const vpColVector &v)
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)
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
void npz_save(std::string zipname, std::string fname, const T *data, const std::vector< size_t > &shape, std::string mode="w")