Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
trackDot2WithAutoDetection.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 * Test auto detection of dots.
32 */
33
40
41#include <visp3/core/vpConfig.h>
42#include <visp3/core/vpDebug.h>
43
44
45#if defined(VISP_HAVE_MODULE_BLOB) && defined(VISP_HAVE_DISPLAY)
46
47#include <visp3/blob/vpDot2.h>
48#include <visp3/core/vpImage.h>
49#include <visp3/core/vpImagePoint.h>
50#include <visp3/core/vpIoTools.h>
51#include <visp3/gui/vpDisplayFactory.h>
52#include <visp3/io/vpImageIo.h>
53#include <visp3/io/vpParseArgv.h>
54
55// List of allowed command line options
56#define GETOPTARGS "cdi:p:f:l:s:S:G:E:h"
57
58#ifdef ENABLE_VISP_NAMESPACE
59using namespace VISP_NAMESPACE_NAME;
60#endif
61
62void usage(const char *name, const char *badparam, const std::string &ipath, const std::string &ppath, unsigned first,
63 unsigned last, unsigned step, double sizePrecision, double grayLevelPrecision,
64 double ellipsoidShapePrecision);
65bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath, unsigned &first, unsigned &last,
66 unsigned &step, double &sizePrecision, double &grayLevelPrecision, double &ellipsoidShapePrecision,
67 bool &click_allowed, bool &display);
68
83void usage(const char *name, const char *badparam, const std::string &ipath, const std::string &ppath, unsigned first,
84 unsigned last, unsigned step, double sizePrecision, double grayLevelPrecision,
85 double ellipsoidShapePrecision)
86{
87#if defined(VISP_HAVE_DATASET)
88#if VISP_HAVE_DATASET_VERSION >= 0x030600
89 std::string ext("png");
90#else
91 std::string ext("pgm");
92#endif
93#else
94 // We suppose that the user will download a recent dataset
95 std::string ext("png");
96#endif
97 fprintf(stdout, "\n\
98Test auto detection of dots using vpDot2.\n\
99 \n\
100SYNOPSIS\n\
101 %s [-i <input image path>] [-p <personal image path>]\n\
102 [-f <first image>] [-l <last image>] [-s <step>] \n\
103 [-S <size precision>] [-G <gray level precision>]\n\
104 [-E <ellipsoid shape precision>] [-c] [-d] [-h]\n",
105 name);
106
107 fprintf(stdout, "\n\
108OPTIONS: Default\n\
109 -i <input image path> %s\n\
110 Set image input path.\n\
111 From this path read images \n\
112 \"mire-2/image.%%04d.%s\"\n\
113 Setting the VISP_INPUT_IMAGE_PATH environment\n\
114 variable produces the same behaviour than using\n\
115 this option.\n\
116 \n\
117 -p <personal image path> %s\n\
118 Specify a personal sequence containing images \n\
119 to process.\n\
120 The format is selected by analyzing the filename extension.\n\
121 Example : \"/Temp/visp-images/mire-2/image.%%04d.%s\"\n\
122 %%04d is for the image numbering.\n\
123 \n\
124 -f <first image> %u\n\
125 First image number of the sequence.\n\
126 \n\
127 -l <last image> %u\n\
128 Last image number of the sequence.\n\
129 \n\
130 -s <step> %u\n\
131 Step between two images.\n\
132 \n\
133 -S <size precision> %f\n\
134 Precision of the size of the dot. \n\
135 It is a double precision float witch value is in ]0,1].\n\
136 1 means full precision, the sizes (width, heigth, surface) \n\
137 of the dots must the same, whereas values close to 0 \n\
138 show a very bad precision.\n\
139\n\
140 -G <gray level precision> %f\n\
141 Precision of the gray level of the dot. \n\
142 It is a double precision float witch value is in ]0,1].\n\
143 1 means full precision, the gray level must the same in \n\
144 the wall dot, whereas values close to 0 \n\
145 show a very bad precision.\n\
146\n\
147 -E <ellipsoid shape precision> %f\n\
148 Precision of the ellipsoid shape of the dot. \n\
149 It is a double precision float witch value is in [0,1].\n\
150 1 means full precision, the shape should be a perfect ellipsoid,\n\
151 whereas values close to 0 show a very bad precision.\n\
152 0 means the shape of dots is not tested \n\
153\n",
154ipath.c_str(), ext.c_str(), ppath.c_str(), ext.c_str(), first, last, step, sizePrecision, grayLevelPrecision,
155ellipsoidShapePrecision);
156
157 fprintf(stdout, "\
158 -c\n\
159 Disable the mouse click. Useful to automate the \n\
160 execution of this program without human intervention.\n\
161 \n\
162 -d \n\
163 Turn off the display.\n\
164 \n\
165 -h\n\
166 Print the help.\n");
167
168 if (badparam)
169 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
170}
171
190bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath, unsigned &first, unsigned &last,
191 unsigned &step, double &sizePrecision, double &grayLevelPrecision, double &ellipsoidShapePrecision,
192 bool &click_allowed, bool &display)
193{
194 const char *optarg_;
195 int c;
196 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
197
198 switch (c) {
199 case 'c':
200 click_allowed = false;
201 break;
202 case 'd':
203 display = false;
204 break;
205 case 'i':
206 ipath = optarg_;
207 break;
208 case 'p':
209 ppath = optarg_;
210 break;
211 case 'f':
212 first = static_cast<unsigned int>(atoi(optarg_));
213 break;
214 case 'l':
215 last = static_cast<unsigned int>(atoi(optarg_));
216 break;
217 case 's':
218 step = static_cast<unsigned int>(atoi(optarg_));
219 break;
220 case 'S':
221 sizePrecision = atof(optarg_);
222 break;
223 case 'G':
224 grayLevelPrecision = atof(optarg_);
225 break;
226 case 'E':
227 ellipsoidShapePrecision = atof(optarg_);
228 break;
229 case 'h':
230 usage(argv[0], nullptr, ipath, ppath, first, last, step, sizePrecision, grayLevelPrecision,
231 ellipsoidShapePrecision);
232 return false;
233
234 default:
235 usage(argv[0], optarg_, ipath, ppath, first, last, step, sizePrecision, grayLevelPrecision,
236 ellipsoidShapePrecision);
237 return false;
238 }
239 }
240
241 if ((c == 1) || (c == -1)) {
242 // standalone param or error
243 usage(argv[0], nullptr, ipath, ppath, first, last, step, sizePrecision, grayLevelPrecision,
244 ellipsoidShapePrecision);
245 std::cerr << "ERROR: " << std::endl;
246 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
247 return false;
248 }
249
250 return true;
251}
252
253int main(int argc, const char **argv)
254{
255 try {
256 std::string env_ipath;
257 std::string opt_ipath;
258 std::string ipath;
259 std::string opt_ppath;
260 std::string dirname;
261 std::string filename;
262 unsigned opt_first = 1;
263 unsigned opt_last = 10;
264 unsigned opt_step = 1;
265 double opt_sizePrecision = 0.65;
266 double opt_grayLevelPrecision = 0.85;
267 double opt_ellipsoidShapePrecision = 0.8;
268 bool opt_click_allowed = true;
269 bool opt_display = true;
270
271#if defined(VISP_HAVE_DATASET)
272#if VISP_HAVE_DATASET_VERSION >= 0x030600
273 std::string ext("png");
274#else
275 std::string ext("pgm");
276#endif
277#else
278 // We suppose that the user will download a recent dataset
279 std::string ext("png");
280#endif
281
282 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
283 // environment variable value
285
286 // Set the default input path
287 if (!env_ipath.empty())
288 ipath = env_ipath;
289
290 // Read the command line options
291 if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_first, opt_last, opt_step, opt_sizePrecision,
292 opt_grayLevelPrecision, opt_ellipsoidShapePrecision, opt_click_allowed, opt_display) == false) {
293 return EXIT_FAILURE;
294 }
295
296 // Get the option values
297 if (!opt_ipath.empty())
298 ipath = opt_ipath;
299
300 // Compare ipath and env_ipath. If they differ, we take into account
301 // the input path coming from the command line option
302 if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
303 if (ipath != env_ipath) {
304 std::cout << std::endl << "WARNING: " << std::endl;
305 std::cout << " Since -i <visp image path=" << ipath << "> "
306 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
307 << " we skip the environment variable." << std::endl;
308 }
309 }
310
311 // Test if an input path is set
312 if (opt_ipath.empty() && env_ipath.empty()) {
313 usage(argv[0], nullptr, ipath, opt_ppath, opt_first, opt_last, opt_step, opt_sizePrecision,
314 opt_grayLevelPrecision, opt_ellipsoidShapePrecision);
315 std::cerr << std::endl << "ERROR:" << std::endl;
316 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
317 << " environment variable to specify the location of the " << std::endl
318 << " image path where test images are located." << std::endl
319 << std::endl
320 << " Use -p <personal image path> option if you want to " << std::endl
321 << " use personal images." << std::endl;
322 return EXIT_FAILURE;
323 }
324
325 // Declare an image, this is a gray level image (unsigned char)
326 // it size is not defined yet, it will be defined when the image will
327 // read on the disk
329 vpDisplay *display = nullptr;
330 unsigned iter = opt_first; // Image number
331
332 if (opt_ppath.empty()) {
333
334 // Warning :
335 // The image sequence is not provided with the ViSP package
336 // therefore the program will return an error :
337 // !! couldn't read file visp-images/mire-2/image.0001.png
338 //
339 // ViSP dataset is available on the visp www site
340 // https://visp.inria.fr/download/.
341
342 // Set the path location of the image sequence
343 dirname = vpIoTools::createFilePath(ipath, "mire-2");
344
345 // Build the name of the image file
346 std::string name = vpIoTools::formatString("image.%04d." + ext, iter);
347 filename = vpIoTools::createFilePath(dirname, name);
348 }
349 else {
350 filename = vpIoTools::formatString(opt_ppath, iter);
351 }
352 // Read the image named "filename", and put the bitmap into the image structure I.
353 // I is initialized to the correct size
354 //
355 // vpImageIo::read() may throw various exception if, for example,
356 // the file does not exist, or if the memory cannot be allocated
357 try {
358 vpCTRACE << "Load: " << filename << std::endl;
359
360 vpImageIo::read(I, filename);
361 }
362 catch (...) {
363 // If an exception is thrown by vpImageIo::read() it will result in the end of the program.
364 std::cerr << std::endl << "ERROR:" << std::endl;
365 std::cerr << " Cannot read " << filename << std::endl;
366 if (opt_ppath.empty()) {
367 std::cerr << " Check your -i " << ipath << " option " << std::endl
368 << " or VISP_INPUT_IMAGE_PATH environment variable." << std::endl;
369 }
370 else {
371 std::cerr << " Check your -p " << opt_ppath << " option " << std::endl;
372 }
373 return EXIT_FAILURE;
374 }
375
376 if (opt_display) {
377 // We open a window using either X11, GTK, OpenCV or GDI
379 // Display size is automatically defined by the image (I) size
380 display->init(I, 100, 100, "Display...");
381 // Display the image
382 // The image class has a member that specify a pointer toward
383 // the display that has been initialized in the display declaration
384 // therefore is is no longer necessary to make a reference to the
385 // display variable.
388 }
389
390 // Dot declaration
391 vpDot2 d;
392
393 d.setGraphics(true);
394 if (opt_click_allowed & opt_display) {
395 d.setGrayLevelPrecision(opt_grayLevelPrecision);
396
397 std::cout << "Please click on a dot to initialize detection" << std::endl;
398
399 d.initTracking(I);
400 if (opt_display) {
401 vpImagePoint cog;
402 cog = d.getCog();
405 }
406 d.setSizePrecision(opt_sizePrecision);
407 d.setEllipsoidShapePrecision(opt_ellipsoidShapePrecision);
408 printf("Dot characteristics: \n");
409 printf(" width : %lf\n", d.getWidth());
410 printf(" height: %lf\n", d.getHeight());
411 printf(" area: %lf\n", d.getArea());
412 printf(" gray level min: %u\n", d.getGrayLevelMin());
413 printf(" gray level max: %u\n", d.getGrayLevelMax());
414 printf(" grayLevelPrecision: %lf\n", d.getGrayLevelPrecision());
415 printf(" sizePrecision: %lf\n", d.getSizePrecision());
416 printf(" ellipsoidShapePrecision: %lf\n", d.getEllipsoidShapePrecision());
417 }
418 else {
419 // Set dot characteristics for the auto detection
420 d.setGraphics(true);
421 d.setWidth(15.0);
422 d.setHeight(12.0);
423 d.setArea(124);
424 d.setGrayLevelMin(164);
425 d.setGrayLevelMax(255);
426 d.setGrayLevelPrecision(opt_grayLevelPrecision);
427 d.setSizePrecision(opt_sizePrecision);
428 d.setEllipsoidShapePrecision(opt_ellipsoidShapePrecision);
429 }
430
431 bool quit = false;
432 while ((iter < opt_last) && (!quit)) {
433 // set the new image name
434 if (opt_ppath.empty()) {
435 std::string name = vpIoTools::formatString("image.%04d." + ext, iter);
436 filename = vpIoTools::createFilePath(dirname, name);
437 }
438 else {
439 filename = vpIoTools::formatString(opt_ppath, iter);
440 }
441 // read the image
442 vpImageIo::read(I, filename);
443
444 if (opt_display) {
445 // Display the image
447 }
448
449 std::cout << "Search dots in image" << filename << std::endl;
450 std::list<vpDot2> list_d;
451 d.searchDotsInArea(I, 0, 0, I.getWidth(), I.getHeight(), list_d);
452
453 if (list_d.empty()) {
454 std::cout << "Dot auto detection did not work." << std::endl;
455 return EXIT_FAILURE;
456 }
457 else {
458 std::cout << std::endl << list_d.size() << " dots are detected" << std::endl;
459
460 if (opt_display) {
461 int i = 0;
462 // Parse all founded dots for display
463 for (std::list<vpDot2>::const_iterator it = list_d.begin(); it != list_d.end(); ++it) {
464 vpImagePoint cog = (*it).getCog();
465
466 std::cout << "Dot " << i++ << " : " << cog.get_u() << " " << cog.get_v() << std::endl;
467
469 }
470 if (opt_click_allowed) {
471 vpDisplay::displayText(I, 20, 20, "Left click to continue on next image", vpColor::red);
472 vpDisplay::displayText(I, 40, 20, "Right click to quit", vpColor::red);
474 if (vpDisplay::getClick(I, button, true)) {
475 if (button == vpMouseButton::button3) {
476 quit = true;
477 }
478 }
479 }
481 }
482 }
483
484 iter += opt_step;
485 }
486 if (opt_display && opt_click_allowed && !quit) {
487 std::cout << "\nA click to exit..." << std::endl;
488 // Wait for a blocking mouse click
490 }
491
492 if (display) {
493 delete display;
494 }
495
496 return EXIT_SUCCESS;
497 }
498 catch (const vpException &e) {
499 std::cout << "Catch an exception: " << e << std::endl;
500 return EXIT_FAILURE;
501 }
502}
503#else
504#include <iostream>
505
506int main()
507{
508 std::cout << "visp_me module or X11, GTK, GDI or OpenCV display "
509 "functionalities are required..."
510 << std::endl;
511 return EXIT_SUCCESS;
512}
513
514#endif
static const vpColor red
Definition vpColor.h:198
static const vpColor blue
Definition vpColor.h:204
static const vpColor green
Definition vpColor.h:201
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 blob (connex pixels with same gray level) on a vpImage.
Definition vpDot2.h:127
error that can be emitted by ViSP classes.
Definition vpException.h:60
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 ...
double get_u() const
double get_v() const
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)
#define vpCTRACE
Definition vpDebug.h:362
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.