Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
trackDot2.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2024 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 * Example of dot tracking.
32 */
33
40
41#include <visp3/core/vpConfig.h>
42
43#if defined(VISP_HAVE_MODULE_BLOB) && defined(VISP_HAVE_DISPLAY)
44
45#include <visp3/blob/vpDot2.h>
46#include <visp3/core/vpImage.h>
47#include <visp3/core/vpImagePoint.h>
48#include <visp3/core/vpIoTools.h>
49#include <visp3/gui/vpDisplayFactory.h>
50#include <visp3/io/vpImageIo.h>
51#include <visp3/io/vpParseArgv.h>
52
53// List of allowed command line options
54#define GETOPTARGS "cdf:i:l:p:s:h"
55
56#ifdef ENABLE_VISP_NAMESPACE
57using namespace VISP_NAMESPACE_NAME;
58#endif
59
60void usage(const char *name, const char *badparam, const std::string &ipath, const std::string &ppath, unsigned first,
61 unsigned last, unsigned step);
62bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath, unsigned &first, unsigned &last,
63 unsigned &step, bool &click_allowed, bool &display);
64
76void usage(const char *name, const char *badparam, const std::string &ipath, const std::string &ppath, unsigned first,
77 unsigned last, unsigned step)
78{
79#if defined(VISP_HAVE_DATASET)
80#if VISP_HAVE_DATASET_VERSION >= 0x030600
81 std::string ext("png");
82#else
83 std::string ext("pgm");
84#endif
85#else
86 // We suppose that the user will download a recent dataset
87 std::string ext("png");
88#endif
89 fprintf(stdout, "\n\
90Test dot tracking using vpDot2 class.\n\
91\n\
92SYNOPSIS\n\
93 %s [-i <test image path>] [-p <personal image path>]\n\
94 [-f <first image>] [-l <last image>] [-s <step>]\n\
95 [-c] [-d] [-h]\n",
96 name);
97
98 fprintf(stdout, "\n\
99OPTIONS: Default\n\
100 -i <input image path> %s\n\
101 Set image input path.\n\
102 From this path read images \n\
103 \"mire-2/image.%%04d.%s\". These \n\
104 images come from visp-images-x.y.z.tar.gz available \n\
105 on the ViSP website.\n\
106 Setting the VISP_INPUT_IMAGE_PATH environment\n\
107 variable produces the same behaviour than using\n\
108 this option.\n\
109 \n\
110 -p <personal image path> %s\n\
111 Specify a personal sequence containing images \n\
112 to process.\n\
113 By image sequence, we mean one file per image.\n\
114 Example : \"C:/Temp/visp-images/cube/image.%%04d.%s\"\n\
115 %%04d is for the image numbering.\n\
116 \n\
117 -f <first image> %u\n\
118 First image number of the sequence.\n\
119 \n\
120 -l <last image> %u\n\
121 Last image number of the sequence.\n\
122 \n\
123 -s <step> %u\n\
124 Step between two images.\n\
125\n\
126 -c\n\
127 Disable the mouse click. Useful to automate the \n\
128 execution of this program without human intervention.\n\
129\n\
130 -d \n\
131 Turn off the display.\n\
132\n\
133 -h\n\
134 Print the help.\n",
135 ipath.c_str(), ext.c_str(), ppath.c_str(), ext.c_str(), first, last, step);
136
137 if (badparam)
138 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
139}
140
156bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath, unsigned &first, unsigned &last,
157 unsigned &step, bool &click_allowed, bool &display)
158{
159 const char *optarg_;
160 int c;
161 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
162
163 switch (c) {
164 case 'c':
165 click_allowed = false;
166 break;
167 case 'd':
168 display = false;
169 break;
170 case 'i':
171 ipath = optarg_;
172 break;
173 case 'p':
174 ppath = optarg_;
175 break;
176 case 'f':
177 first = static_cast<unsigned int>(atoi(optarg_));
178 break;
179 case 'l':
180 last = static_cast<unsigned int>(atoi(optarg_));
181 break;
182 case 's':
183 step = static_cast<unsigned int>(atoi(optarg_));
184 break;
185 case 'h':
186 usage(argv[0], nullptr, ipath, ppath, first, last, step);
187 return false;
188
189 default:
190 usage(argv[0], optarg_, ipath, ppath, first, last, step);
191 return false;
192 }
193 }
194
195 if ((c == 1) || (c == -1)) {
196 // standalone param or error
197 usage(argv[0], nullptr, ipath, ppath, first, last, step);
198 std::cerr << "ERROR: " << std::endl;
199 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
200 return false;
201 }
202
203 return true;
204}
205
206int main(int argc, const char **argv)
207{
208 try {
209 std::string env_ipath;
210 std::string opt_ipath;
211 std::string ipath;
212 std::string opt_ppath;
213 std::string dirname;
214 std::string filename;
215 unsigned opt_first = 1;
216 unsigned opt_last = 500;
217 unsigned opt_step = 1;
218 bool opt_click_allowed = true;
219 bool opt_display = true;
220
221#if defined(VISP_HAVE_DATASET)
222#if VISP_HAVE_DATASET_VERSION >= 0x030600
223 std::string ext("png");
224#else
225 std::string ext("pgm");
226#endif
227#else
228 // We suppose that the user will download a recent dataset
229 std::string ext("png");
230#endif
231
232 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
233 // environment variable value
235
236 // Set the default input path
237 if (!env_ipath.empty())
238 ipath = env_ipath;
239
240 // Read the command line options
241 if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_first, opt_last, opt_step, opt_click_allowed,
242 opt_display) == false) {
243 return EXIT_FAILURE;
244 }
245
246 // Get the option values
247 if (!opt_ipath.empty())
248 ipath = opt_ipath;
249
250 // Compare ipath and env_ipath. If they differ, we take into account
251 // the input path coming from the command line option
252 if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
253 if (ipath != env_ipath) {
254 std::cout << std::endl << "WARNING: " << std::endl;
255 std::cout << " Since -i <visp image path=" << ipath << "> "
256 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
257 << " we skip the environment variable." << std::endl;
258 }
259 }
260
261 // Test if an input path is set
262 if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty()) {
263 usage(argv[0], nullptr, ipath, opt_ppath, opt_first, opt_last, opt_step);
264 std::cerr << std::endl << "ERROR:" << std::endl;
265 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
266 << " environment variable to specify the location of the " << std::endl
267 << " image path where test images are located." << std::endl
268 << " Use -p <personal image path> option if you want to " << std::endl
269 << " use personal images." << std::endl
270 << std::endl;
271
272 return EXIT_FAILURE;
273 }
274
275 // Declare an image, this is a gray level image (unsigned char)
276 // it size is not defined yet, it will be defined when the image will
277 // read on the disk
279 vpDisplay *display = nullptr;
280
281 unsigned iter = opt_first;
282
283 if (opt_ppath.empty()) {
284
285 // Warning :
286 // The image sequence is not provided with the ViSP package
287 // therefore the program will return an error :
288 // !! couldn't read file visp-images/mire-2/image.0001.png
289 //
290 // ViSP dataset is available on the visp www site
291 // https://visp.inria.fr/download/.
292
293 // Set the path location of the image sequence
294 dirname = vpIoTools::createFilePath(ipath, "mire-2");
295
296 // Build the name of the image file
297 std::string name = vpIoTools::formatString("image.%04d." + ext, iter);
298 filename = vpIoTools::createFilePath(dirname, name);
299 }
300 else {
301 filename = vpIoTools::formatString(opt_ppath, iter);
302 }
303
304 // Read the image named "filename", and put the bitmap into the image structure I.
305 // I is initialized to the correct size
306 //
307 // vpImageIo::read() may throw various exception if, for example,
308 // the file does not exist, or if the memory cannot be allocated
309 try {
310 std::cout << "Load: " << filename << std::endl;
311
312 vpImageIo::read(I, filename);
313 }
314 catch (...) {
315 // If an exception is thrown by vpImageIo::read() it will result in the end of the program.
316 std::cerr << std::endl << "ERROR:" << std::endl;
317 std::cerr << " Cannot read " << filename << std::endl;
318 if (opt_ppath.empty()) {
319 std::cerr << " Check your -i " << ipath << " option " << std::endl
320 << " or VISP_INPUT_IMAGE_PATH environment variable." << std::endl;
321 }
322 else {
323 std::cerr << " Check your -p " << opt_ppath << " option " << std::endl;
324 }
325 return EXIT_FAILURE;
326 }
327
328 if (opt_display) {
329 // We open a window using either X11, GTK, OpenCV or GDI
331 // Display size is automatically defined by the image (I) size
332 display->init(I, 100, 100, "Display...");
333 // Display the image
334 // The image class has a member that specify a pointer toward
335 // the display that has been initialized in the display declaration
336 // therefore is is no longer necessary to make a reference to the
337 // display variable.
340 }
341 // define the vpDot structure.
342
343 // vpDot and vpDot2 correspond to two different algorithms designed to
344 // track a dot. vpDot is based on recurse connex componants (all the
345 // pixels of the dot are parsed), while vpDot2 is based on freeman chain
346 // code (only the contour of the dot is parsed)
347
348 vpDot2 d;
349 vpImagePoint cog;
350
351 if (opt_display) {
352 // by using setGraphics, we request to see the all the pixel of the dot
353 // in green on the screen.
354 // It uses the overlay image plane.
355 // The default of this setting is that it is time consuming
356
357 d.setGraphics(true);
358 }
359 else {
360
361 d.setGraphics(false);
362 }
363 // We want to track an ellipsoid shape. If you want to track a non
364 // ellipsoid object, use d.setEllipsoidShape(0); we also request to
365 // compute the dot moment m00, m10, m01, m11, m20, m02
366 d.setComputeMoments(true);
367 d.setGrayLevelPrecision(0.90);
368
369 // tracking is initalized if no other parameters are given to the
370 // iniTracking(..) method a right mouse click on the dot is expected
371 // dot location can also be specified explicitly in the
372 // initTracking method : d.initTracking(I,ip) where ip is the image
373 // point from which the dot is searched
374
375 if (opt_display && opt_click_allowed) {
376 std::cout << "Click on a dot to track it." << std::endl;
377 d.initTracking(I);
378 }
379 else {
380 vpImagePoint ip;
381 ip.set_u(160);
382 ip.set_v(212);
383 d.initTracking(I, ip);
384 }
385 if (1) {
386 std::cout << "COG: " << std::endl;
387 cog = d.getCog();
388 std::cout << " u: " << cog.get_u() << " v: " << cog.get_v() << std::endl;
389 std::cout << "Size:" << std::endl;
390 std::cout << " w: " << d.getWidth() << " h: " << d.getHeight() << std::endl;
391 std::cout << "Area: " << d.getArea() << std::endl;
392 std::cout << "Centered normalized moments nij:" << std::endl;
393 std::cout << " n20: " << d.get_nij()[0] << std::endl;
394 std::cout << " n11: " << d.get_nij()[1] << std::endl;
395 std::cout << " n02: " << d.get_nij()[2] << std::endl;
396 std::cout << "Settings:" << std::endl;
397 std::cout << " gray level min: " << d.getGrayLevelMin() << std::endl;
398 std::cout << " gray level max: " << d.getGrayLevelMax() << std::endl;
399 std::cout << " size precision: " << d.getSizePrecision() << std::endl;
400 std::cout << " gray level precision: " << d.getGrayLevelPrecision() << std::endl;
401 }
402
403 bool quit = false;
404 while ((iter < opt_last) && (!quit)) {
405 // set the new image name
406 if (opt_ppath.empty()) {
407 std::string name = vpIoTools::formatString("image.%04d." + ext, iter);
408 filename = vpIoTools::createFilePath(dirname, name);
409 }
410 else {
411 filename = vpIoTools::formatString(opt_ppath, iter);
412 }
413 // read the image
414 std::cout << "read : " << filename << std::endl;
415 vpImageIo::read(I, filename);
416
417 // track the dot and returns its coordinates in the image
418 // results are given in float since many many are usually considered
419 //
420 // an exception is thrown by the track method if
421 // - dot is lost
422
423 if (opt_display) {
424 // Display the image
426 }
427
428 std::cout << "Tracking on image: " << filename << std::endl;
429 double time = vpTime::measureTimeMs();
430 d.track(I);
431
432 std::cout << "COG (" << vpTime::measureTimeMs() - time << " ms): " << std::endl;
433 cog = d.getCog();
434 std::cout << " u: " << cog.get_u() << " v: " << cog.get_v() << std::endl;
435 std::cout << "Size:" << std::endl;
436 std::cout << " w: " << d.getWidth() << " h: " << d.getHeight() << std::endl;
437 std::cout << "Area: " << d.getArea() << std::endl;
438 std::cout << "Centered normalized moments nij:" << std::endl;
439 std::cout << " n20: " << d.get_nij()[0] << std::endl;
440 std::cout << " n11: " << d.get_nij()[1] << std::endl;
441 std::cout << " n02: " << d.get_nij()[2] << std::endl;
442 std::cout << "Settings:" << std::endl;
443 std::cout << " gray level min: " << d.getGrayLevelMin() << std::endl;
444 std::cout << " gray level max: " << d.getGrayLevelMax() << std::endl;
445 std::cout << " size precision: " << d.getSizePrecision() << std::endl;
446 std::cout << " gray level precision: " << d.getGrayLevelPrecision() << std::endl;
447
448 if (opt_display) {
449 if (0) {
450 std::list<vpImagePoint> edges;
451 d.getEdges(edges);
452 std::list<vpImagePoint>::const_iterator it;
453 for (it = edges.begin(); it != edges.end(); ++it) {
455 }
456 }
457
458 // display a green cross (size 10) in the image at the dot center
459 // of gravity location
461 vpDisplay::displayText(I, 20, 20, "Click to quit...", vpColor::red);
462 if (vpDisplay::getClick(I, false)) {
463 quit = true;
464 }
466 }
467 iter++;
468 }
469
470 if (opt_display && opt_click_allowed && !quit) {
471 std::cout << "\nA click to exit..." << std::endl;
472 // Wait for a blocking mouse click
474 }
475 if (display) {
476 delete display;
477 }
478 return EXIT_SUCCESS;
479 }
480 catch (const vpException &e) {
481 std::cout << "Catch an exception: " << e << std::endl;
482 return EXIT_FAILURE;
483 }
484}
485#else
486#include <iostream>
487
488int main()
489{
490 std::cout << "visp_me module or X11, GTK, GDI or OpenCV display "
491 "functionalities are required..."
492 << std::endl;
493 return EXIT_SUCCESS;
494}
495
496#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 displayPoint(const vpImage< unsigned char > &I, const vpImagePoint &ip, const vpColor &color, unsigned int thickness=1)
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
void set_u(double u)
void set_v(double v)
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)
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.
VISP_EXPORT double measureTimeMs()