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