Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
poseVirtualVS.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 *
30 * Description:
31 * Pose computation on an object made of dots.
32 * reading of PGM image
33 * Display image using either the X11 or GTK or GDI display
34 * track 4 dots (vpDots) in the image
35 * compute the pose
36 */
37
49
50#include <iomanip>
51#include <sstream>
52#include <stdio.h>
53#include <stdlib.h>
54#include <visp3/core/vpConfig.h>
55#include <visp3/core/vpDebug.h>
56
57#if defined(VISP_HAVE_DISPLAY) && \
58 (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
59
60#include <visp3/core/vpImage.h>
61#include <visp3/core/vpImagePoint.h>
62#include <visp3/core/vpIoTools.h>
63#include <visp3/io/vpImageIo.h>
64
65#include <visp3/gui/vpDisplayFactory.h>
66
67#include <visp3/blob/vpDot.h>
68#include <visp3/core/vpIoTools.h>
69#include <visp3/core/vpPixelMeterConversion.h>
70#include <visp3/io/vpParseArgv.h>
71#include <visp3/vision/vpPose.h>
72
73// List of allowed command line options
74#define GETOPTARGS "cdi:p:hf:l:s:"
75
76#ifdef ENABLE_VISP_NAMESPACE
77using namespace VISP_NAMESPACE_NAME;
78#endif
79
93void usage(const char *name, const char *badparam, const std::string &ipath, const std::string &ppath, unsigned first,
94 unsigned last, unsigned step)
95{
96#if defined(VISP_HAVE_DATASET)
97#if VISP_HAVE_DATASET_VERSION >= 0x030600
98 std::string ext("png");
99#else
100 std::string ext("pgm");
101#endif
102#else
103 // We suppose that the user will download a recent dataset
104 std::string ext("png");
105#endif
106
107 fprintf(stdout, "\n\
108Test dot tracking.\n\
109\n\
110SYNOPSIS\n\
111 %s [-i <input image path>] [-p <personal image path>]\n\
112 [-f <first image>] [-l <last image>] [-s <step>][-c] [-d] [-h]\n",
113 name);
114
115 fprintf(stdout, "\n\
116OPTIONS: Default\n\
117 -i <input image path> %s\n\
118 Set image input path.\n\
119 From this path read images \n\
120 \"cube/image.%%04d.%s\"\n\
121 Setting the VISP_INPUT_IMAGE_PATH environment\n\
122 variable produces the same behaviour than using\n\
123 this option.\n\
124 \n\
125 -p <personal image path> %s\n\
126 Specify a personal sequence containing images \n\
127 to process.\n\
128 By image sequence, we mean one file per image.\n\
129 The format is selected by analyzing the filename extension.\n\
130 Example : \"/Temp/visp-images/cube/image.%%04d.%s\"\n\
131 %%04d is for the image numbering.\n\
132\n\
133 -f <first image> %u\n\
134 First image number of the sequence.\n\
135\n\
136 -l <last image> %u\n\
137 Last image number of the sequence.\n\
138\n\
139 -s <step> %u\n\
140 Step between two images.\n\
141\n\
142 -c\n\
143 Disable the mouse click. Useful to automate the \n\
144 execution of this program without human intervention.\n\
145\n\
146 -d \n\
147 Turn off the display.\n\
148\n\
149 -h\n\
150 Print the help.\n",
151 ipath.c_str(), ext.c_str(), ppath.c_str(), ext.c_str(), first, last, step);
152
153 if (badparam)
154 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
155}
177bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath, unsigned &first, unsigned &last,
178 unsigned &step, bool &click_allowed, bool &display)
179{
180 const char *optarg_;
181 int c;
182 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
183
184 switch (c) {
185 case 'c':
186 click_allowed = false;
187 break;
188 case 'd':
189 display = false;
190 break;
191 case 'i':
192 ipath = optarg_;
193 break;
194 case 'p':
195 ppath = optarg_;
196 break;
197 case 'f':
198 first = static_cast<unsigned int>(atoi(optarg_));
199 break;
200 case 'n':
201 last = static_cast<unsigned int>(atoi(optarg_));
202 break;
203 case 's':
204 step = static_cast<unsigned int>(atoi(optarg_));
205 break;
206 case 'h':
207 usage(argv[0], nullptr, ipath, ppath, first, last, step);
208 return false;
209
210 default:
211 usage(argv[0], optarg_, ipath, ppath, first, last, step);
212 return false;
213 }
214 }
215
216 if ((c == 1) || (c == -1)) {
217 // standalone param or error
218 usage(argv[0], nullptr, ipath, ppath, first, last, step);
219 std::cerr << "ERROR: " << std::endl;
220 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
221 return false;
222 }
223
224 return true;
225}
226
227int main(int argc, const char **argv)
228{
229 // We declare the display variable here to be able to free it in the catch section too
230#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
231 std::shared_ptr<vpDisplay> display;
232#else
233 vpDisplay *display = nullptr;
234#endif
235
236 try {
237 std::string env_ipath;
238 std::string opt_ipath;
239 std::string ipath;
240 std::string opt_ppath;
241 std::string dirname;
242 std::string filename;
243 unsigned opt_first = 0;
244 unsigned opt_last = 80;
245 unsigned opt_step = 1;
246 bool opt_click_allowed = true;
247 bool opt_display = true;
248 int i;
249
250#if defined(VISP_HAVE_DATASET)
251#if VISP_HAVE_DATASET_VERSION >= 0x030600
252 std::string ext("png");
253#else
254 std::string ext("pgm");
255#endif
256#else
257 // We suppose that the user will download a recent dataset
258 std::string ext("png");
259#endif
260
261 std::cout << "-------------------------------------------------------" << std::endl;
262 std::cout << " poseVirtualVS.cpp" << std::endl << std::endl;
263
264 std::cout << " Example of dots tracking in an image sequence and pose "
265 "computation"
266 << std::endl;
267 std::cout << "-------------------------------------------------------" << std::endl;
268 std::cout << std::endl;
269
270 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
271 // environment variable value
273
274 // Set the default input path
275 if (!env_ipath.empty())
276 ipath = env_ipath;
277
278 // Read the command line options
279 if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_first, opt_last, opt_step, opt_click_allowed,
280 opt_display) == false) {
281 return EXIT_FAILURE;
282 }
283
284 // Get the option values
285 if (!opt_ipath.empty())
286 ipath = opt_ipath;
287
288 // Compare ipath and env_ipath. If they differ, we take into account
289 // the input path coming from the command line option
290 if (opt_ipath.empty() && opt_ppath.empty()) {
291 if (ipath != env_ipath) {
292 std::cout << std::endl << "WARNING: " << std::endl;
293 std::cout << " Since -i <visp image path=" << ipath << "> "
294 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
295 << " we skip the environment variable." << std::endl;
296 }
297 }
298 // Test if an input path is set
299 if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty()) {
300 usage(argv[0], nullptr, ipath, opt_ppath, opt_first, opt_last, opt_step);
301 std::cerr << std::endl << "ERROR:" << std::endl;
302 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
303 << " environment variable to specify the location of the " << std::endl
304 << " image path where test images are located." << std::endl
305 << " Use -p <personal image path> option if you want to " << std::endl
306 << " use personal images" << std::endl
307 << std::endl;
308 return EXIT_FAILURE;
309 }
310
311 // Declare an image, this is a gray level image (unsigned char)
312 // it size is not defined yet, it will be defined when the image will
313 // read on the disk
315
316 unsigned iter = opt_first;
317
318 if (opt_ppath.empty()) {
319
320 // Warning : the datset is available on https://visp.inria.fr/download/
321 dirname = vpIoTools::createFilePath(ipath, "cube");
322
323 // Build the name of the image file
324 std::string name = vpIoTools::formatString("image.%04d." + ext, iter);
325 filename = vpIoTools::createFilePath(dirname, name);
326 }
327 else {
328 filename = vpIoTools::formatString(opt_ppath, iter);
329 }
330
331 // define the vpDot structure, here 4 dots will tracked
332 vpDot d[4];
333
334 for (i = 0; i < 4; ++i) {
335 // by using setGraphics, we request to see the all the pixel of the dot
336 // in green on the screen.
337 // It uses the overlay image plane.
338 // The default of this setting is that it is time consuming
339
340 if (opt_display) {
341 d[i].setGraphics(true);
342 }
343 else {
344 d[i].setGraphics(false);
345 }
346 }
347
348 // Read image named filename and put the bitmap into in I.
349 try {
350 vpImageIo::read(I, filename);
351 }
352 catch (...) {
353 if (opt_ppath.empty()) {
354 std::cerr << std::endl << "ERROR:" << std::endl;
355 std::cerr << " Cannot read " << filename << std::endl;
356 std::cerr << " Check your -i " << ipath << " option, " << std::endl
357 << " or VISP_INPUT_IMAGE_PATH environment variable" << std::endl;
358 }
359 else {
360 std::cerr << std::endl << "ERROR:" << std::endl;
361 std::cerr << " Cannot read " << filename << std::endl;
362 std::cerr << " or your -p " << opt_ppath << " option " << std::endl << std::endl;
363 }
364 return EXIT_FAILURE;
365 }
366
367 // We open a window using either of the window manager
368 // it will be located in 100,100 and titled "tracking using vpDot"
369 // its size is automatically defined by the image (I) size
370 if (opt_display) {
371 // Display size is automatically defined by the image (I) size
372#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
373 display = vpDisplayFactory::createDisplay(I, 100, 100, "tracking using vpDot");
374#else
375 display = vpDisplayFactory::allocateDisplay(I, 100, 100, "tracking using vpDot");
376#endif
377
378 // display the image
379 // The image class has a member that specify a pointer toward
380 // the display that has been initialized in the display declaration
381 // therefore is is no longer necessary to make a reference to the
382 // display variable.
384 // Flush the display
386 }
387
388 vpImagePoint cog[4]; // Center of gravity of the dot
389 if (opt_display && opt_click_allowed) {
390 // dot coordinates (u,v) = (column,row)
391 std::cout << "Click the four white dots on the object corner clockwise" << std::endl;
392 for (i = 0; i < 4; ++i) {
393 // tracking is initalized if no other parameters are given
394 // to the iniTracking(..) method a right mouse click on the
395 // dot is expected dot location can also be specified
396 // explicitly in the initTracking method :
397 // d.initTracking(I,ip) where ip is the image point from
398 // where the dot need to be searched.
399
400 d[i].initTracking(I);
401 // track the dot and returns its coordinates in the image
402 // results are given in float since many many are usually considered
403 //
404 // an exception is thrown by the track method if
405 // - dot is lost
406 // - the number of pixel is too small
407 // - too many pixels are detected (this is usual when a "big" specularity
408 // occurs. The threshold can be modified using the
409 // setMaxDotSize() method
410 d[i].track(I, cog[i]);
412 }
413 }
414 else {
415 cog[0].set_u(192.9);
416 cog[0].set_v(86.7);
417 d[0].initTracking(I, cog[0]);
418 d[0].track(I, cog[0]);
420
421 cog[1].set_u(239.6);
422 cog[1].set_v(83.4);
423 d[1].initTracking(I, cog[1]);
424 d[1].track(I, cog[1]);
426
427 cog[2].set_u(243.5);
428 cog[2].set_v(128.5);
429 d[2].initTracking(I, cog[2]);
430 d[2].track(I, cog[2]);
432
433 cog[3].set_u(196.4);
434 cog[3].set_v(131.7);
435 d[3].initTracking(I, cog[3]);
436 d[3].track(I, cog[3]);
438 }
439
440 if (opt_display) {
441 // display a red cross (size 10) in the image at the dot center
442 // of gravity location
443 //
444 // WARNING
445 // in the vpDisplay class member's when pixel coordinates
446 // are considered the first element is the row index and the second
447 // is the column index:
448 // vpDisplay::displayCross(Image, row index, column index, size,
449 // color) therefore u and v are inverted wrt to the vpDot
450 // specification
451 // Alternatively, to avoid this problem another set of member have
452 // been defined in the vpDisplay class.
453 // If the method name is postfixe with _uv the specification is :
454 // vpDisplay::displayCross_uv(Image, column index, row index, size,
455 // color)
456
457 for (i = 0; i < 4; ++i) {
459 }
460
461 // flush the X11 buffer
463 }
464
465 // --------------------------------------------------------
466 // Now wil compute the pose
467 //
468
469 // The pose will be contained in an homogeneous matrix cMo
471
472 // We need a structure that content both the 3D coordinates of the point
473 // in the object frame and the 2D coordinates of the point expressed in
474 // meter the vpPoint class is ok for that
475 vpPoint P[4];
476
477 // The vpPose class mainly contents a list of vpPoint (that is (X,Y,Z, x,
478 // y) )
479 vpPose pose;
480 // the list of point is cleared (if that's not done before)
481 pose.clearPoint();
482
483 // we set the 3D points coordinates (in meter !) in the object/world frame
484 double L = 0.04;
485 P[0].setWorldCoordinates(-L, -L, 0); // (X,Y,Z)
486 P[1].setWorldCoordinates(+L, -L, 0);
487 P[2].setWorldCoordinates(+L, +L, 0);
488 P[3].setWorldCoordinates(-L, +L, 0);
489
490 // set the camera intrinsic parameters
491 // see more details about the model in vpCameraParameters
492 double px = 600;
493 double py = 600;
494 double u0 = 192;
495 double v0 = 144;
496 vpCameraParameters cam(px, py, u0, v0);
497
498 // pixel-> meter conversion
499 for (i = 0; i < 4; ++i) {
500 // u[i]. v[i] are expressed in pixel
501 // conversion in meter is achieved using
502 // x = (u-u0)/px
503 // y = (v-v0)/py
504 // where px, py, u0, v0 are the intrinsic camera parameters
505 double x = 0, y = 0;
506 vpPixelMeterConversion::convertPoint(cam, cog[i], x, y);
507 P[i].set_x(x);
508 P[i].set_y(y);
509 }
510
511 // The pose structure is build, we put in the point list the set of point
512 // here both 2D and 3D world coordinates are known
513 for (i = 0; i < 4; ++i) {
514 pose.addPoint(P[i]); // and added to the pose computation point list
515 }
516
517 // compute the initial pose using Dementhon method followed by a non
518 // linear minimization method
519
520 // Pose by Dementhon or Lagrange provides an initialization of the non linear virtual visual-servoing pose estimation
522 if (opt_display) {
523 // display the compute pose
524 pose.display(I, cMo, cam, 0.05, vpColor::red);
526 }
527
528 // Covariance Matrix Computation
529 // Uncomment if you want to compute the covariance matrix.
530 // pose.setCovarianceComputation(true); //Important if you want
531 // tracker.getCovarianceMatrix() to work.
532
533 // this is the loop over the image sequence
534 while (iter < opt_last) {
535 // set the new image name
536 if (opt_ppath.empty()) {
537 std::string name = vpIoTools::formatString("image.%04d." + ext, iter);
538 filename = vpIoTools::createFilePath(dirname, name);
539 }
540 else {
541 filename = vpIoTools::formatString(opt_ppath, iter);
542 }
543
544 std::cout << "Read image: " << filename << std::endl;
545 // read the image
546 vpImageIo::read(I, filename);
547 if (opt_display) {
548 // Display the image
550 // Flush the display
552 }
553 // kill the point list
554 pose.clearPoint();
555
556 // track the dot
557 for (i = 0; i < 4; ++i) {
558 // track the point
559 d[i].track(I, cog[i]);
560 if (opt_display) {
561 // display point location
563 }
564 // pixel->meter conversion
565 {
566 double x = 0, y = 0;
567 vpPixelMeterConversion::convertPoint(cam, cog[i], x, y);
568 P[i].set_x(x);
569 P[i].set_y(y);
570 }
571
572 // and added to the pose computation point list
573 pose.addPoint(P[i]);
574 }
575 // the pose structure has been updated
576
577 // the pose is now updated using the virtual visual servoing approach
578 // Dementhon or lagrange is no longer necessary, pose at the
579 // previous iteration is sufficient
581 if (opt_display) {
582 // display the compute pose
583 pose.display(I, cMo, cam, 0.05, vpColor::none);
584
586 if (opt_click_allowed) {
587 vpTime::wait(40);
588 }
589 }
590
591 // Covariance Matrix Display
592 // Uncomment if you want to print the covariance matrix.
593 // Make sure pose.setCovarianceComputation(true) has been called
594 // (uncomment below). std::cout << pose.getCovarianceMatrix() <<
595 // std::endl << std::endl;
596
597 iter += opt_step;
598 }
599 if (opt_display) {
600 vpDisplay::displayText(I, 20, 20, "Click to quit", vpColor::red);
602 if (opt_click_allowed) {
604 }
605 }
606#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
607 if (display != nullptr) {
608 delete display;
609 }
610#endif
611 return EXIT_SUCCESS;
612 }
613 catch (const vpException &e) {
614 std::cout << "Catch a ViSP exception: " << e << std::endl;
615#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
616 if (display != nullptr) {
617 delete display;
618 }
619#endif
620 return EXIT_FAILURE;
621 }
622}
623#elif !(defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
624int main()
625{
626 std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
627 return EXIT_SUCCESS;
628}
629#else
630int main()
631{
632 std::cout << "You do not have X11, or GTK, or GDI (Graphical Device Interface) functionalities to display images..."
633 << std::endl;
634 std::cout << "Tip if you are on a unix-like system:" << std::endl;
635 std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
636 std::cout << "Tip if you are on a windows-like system:" << std::endl;
637 std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
638 return EXIT_SUCCESS;
639}
640#endif
Generic class defining intrinsic camera parameters.
static const vpColor red
Definition vpColor.h:198
static const vpColor none
Definition vpColor.h:210
Class that defines generic functionalities for display.
Definition vpDisplay.h:171
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
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)
This tracker is meant to track a dot (connected pixels with same gray level) on a vpImage.
Definition vpDot.h:123
error that can be emitted by ViSP classes.
Definition vpException.h:60
Implementation of an homogeneous matrix and operations on such kind of matrices.
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
void set_u(double u)
void set_v(double v)
Definition of the vpImage class member functions.
Definition vpImage.h:131
static std::string getViSPImagesDataPath()
static std::string formatString(const std::string &name, unsigned int val)
static std::string createFilePath(const std::string &parent, const std::string &child)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition vpPoint.h:79
void set_x(double x)
Set the point x coordinate in the image plane.
Definition vpPoint.cpp:471
void setWorldCoordinates(double oX, double oY, double oZ)
Definition vpPoint.cpp:116
void set_y(double y)
Set the point y coordinate in the image plane.
Definition vpPoint.cpp:473
Class used for pose computation from N points (pose from point only). Some of the algorithms implemen...
Definition vpPose.h:82
void addPoint(const vpPoint &P)
Definition vpPose.cpp:96
@ DEMENTHON_LAGRANGE_VIRTUAL_VS
Definition vpPose.h:103
@ VIRTUAL_VS
Definition vpPose.h:97
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, FuncCheckValidityPose func=nullptr)
Definition vpPose.cpp:385
void clearPoint()
Definition vpPose.cpp:89
static void display(vpImage< unsigned char > &I, vpHomogeneousMatrix &cMo, vpCameraParameters &cam, double size, vpColor col=vpColor::none)
Definition vpPose.cpp:567
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.
VISP_EXPORT int wait(double t0, double t)