Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
testTrackDot.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 * Test auto detection of dots.
32 */
33
34#include <iomanip>
35#include <sstream>
36#include <stdio.h>
37#include <stdlib.h>
38#include <visp3/core/vpConfig.h>
39#include <visp3/core/vpDebug.h>
40#if (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
41
42#include <visp3/blob/vpDot2.h>
43#include <visp3/core/vpCameraParameters.h>
44#include <visp3/core/vpImage.h>
45#include <visp3/core/vpIoTools.h>
46#include <visp3/gui/vpDisplayGDI.h>
47#include <visp3/gui/vpDisplayGTK.h>
48#include <visp3/gui/vpDisplayX.h>
49#include <visp3/io/vpImageIo.h>
50#include <visp3/io/vpParseArgv.h>
51#ifdef VISP_HAVE_MODULE_FEATURES
52#include <visp3/visual_features/vpFeatureBuilder.h>
53#include <visp3/visual_features/vpFeatureEllipse.h>
54#endif
55
61
62// List of allowed command line options
63#define GETOPTARGS "cdi:h"
64
65#ifdef ENABLE_VISP_NAMESPACE
66using namespace VISP_NAMESPACE_NAME;
67#endif
68
69bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display);
70
71void usage(const char *name, const char *badparam, std::string ipath);
81void usage(const char *name, const char *badparam, std::string ipath)
82{
83#if defined(VISP_HAVE_DATASET)
84#if VISP_HAVE_DATASET_VERSION >= 0x030600
85 std::string ext("png");
86#else
87 std::string ext("pgm");
88#endif
89#else
90 // We suppose that the user will download a recent dataset
91 std::string ext("png");
92#endif
93 fprintf(stdout, "\n\
94Test dot tracking.\n\
95\n\
96SYNOPSIS\n\
97 %s [-i <input image path>] [-c] [-d] [-h]\n",
98 name);
99
100 fprintf(stdout, "\n\
101OPTIONS: Default\n\
102 -i <input image path> %s\n\
103 Set image input path.\n\
104 From this path read image \n\
105 \"ellipse/ellipse.%s\"\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 -c\n\
111 Disable the mouse click. Useful to automate the \n\
112 execution of this program without human intervention.\n\
113\n\
114 -d \n\
115 Turn off the display.\n\
116\n\
117 -h\n\
118 Print the help.\n",
119 ipath.c_str(), ext.c_str());
120
121 if (badparam)
122 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
123}
136bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display)
137{
138 const char *optarg_;
139 int c;
140 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
141
142 switch (c) {
143 case 'c':
144 click_allowed = false;
145 break;
146 case 'd':
147 display = false;
148 break;
149 case 'i':
150 ipath = optarg_;
151 break;
152 case 'h':
153 usage(argv[0], nullptr, ipath);
154 return false;
155
156 default:
157 usage(argv[0], optarg_, ipath);
158 return false;
159 }
160 }
161
162 if ((c == 1) || (c == -1)) {
163 // standalone param or error
164 usage(argv[0], nullptr, ipath);
165 std::cerr << "ERROR: " << std::endl;
166 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
167 return false;
168 }
169
170 return true;
171}
172
173int main(int argc, const char **argv)
174{
175 try {
176 std::string env_ipath;
177 std::string opt_ipath;
178 std::string ipath;
179 std::string dirname;
180 std::string filename;
181 bool opt_click_allowed = true;
182 bool opt_display = true;
183
184#if defined(VISP_HAVE_DATASET)
185#if VISP_HAVE_DATASET_VERSION >= 0x030600
186 std::string ext("png");
187#else
188 std::string ext("pgm");
189#endif
190#else
191 // We suppose that the user will download a recent dataset
192 std::string ext("png");
193#endif
194
195 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
196 // environment variable value
198
199 // Set the default input path
200 if (!env_ipath.empty())
201 ipath = env_ipath;
202
203 // Read the command line options
204 if (getOptions(argc, argv, opt_ipath, opt_click_allowed, opt_display) == false) {
205 return EXIT_FAILURE;
206 }
207
208 // Get the option values
209 if (!opt_ipath.empty())
210 ipath = opt_ipath;
211
212 // Compare ipath and env_ipath. If they differ, we take into account
213 // the input path coming from the command line option
214 if (!opt_ipath.empty() && !env_ipath.empty()) {
215 if (ipath != env_ipath) {
216 std::cout << std::endl << "WARNING: " << std::endl;
217 std::cout << " Since -i <visp image path=" << ipath << "> "
218 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
219 << " we skip the environment variable." << std::endl;
220 }
221 }
222
223 // Test if an input path is set
224 if (opt_ipath.empty() && env_ipath.empty()) {
225 usage(argv[0], nullptr, ipath);
226 std::cerr << std::endl << "ERROR:" << std::endl;
227 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
228 << " environment variable to specify the location of the " << std::endl
229 << " image path where test images are located." << std::endl
230 << std::endl;
231 return EXIT_FAILURE;
232 }
233
234 // Declare an image, this is a gray level image (unsigned char)
235 // it size is not defined yet, it will be defined when the image will
236 // read on the disk
238
239 // Set the path location of the image sequence
240 dirname = vpIoTools::createFilePath(ipath, "ellipse");
241
242 // Build the name of the image file
243 filename = vpIoTools::createFilePath(dirname, "ellipse." + ext);
244
245 // Read image named "filename" and put the bitmap in I
246 try {
247 vpCTRACE << "Load: " << filename << std::endl;
248
249 vpImageIo::read(I, filename);
250 }
251 catch (...) {
252 std::cerr << std::endl << "ERROR:" << std::endl;
253 std::cerr << " Cannot read " << filename << std::endl;
254 std::cerr << " Check your -i " << ipath << " option " << std::endl
255 << " or VISP_INPUT_IMAGE_PATH environment variable." << std::endl;
256 return EXIT_FAILURE;
257 }
258
259// We open a window using either X11, GTK or GDI.
260#if defined(VISP_HAVE_X11)
261 vpDisplayX display;
262#elif defined(VISP_HAVE_GTK)
263 vpDisplayGTK display;
264#elif defined(VISP_HAVE_GDI)
265 vpDisplayGDI display;
266#endif
267
268 if (opt_display) {
269 // Display size is automatically defined by the image (I) size
270 display.init(I, 100, 100, "Display...");
271 // Display the image
272 // The image class has a member that specify a pointer toward
273 // the display that has been initialized in the display declaration
274 // therefore is is no longer necessary to make a reference to the
275 // display variable.
277 // Flush the display
279 }
280
281 vpDot2 dot;
282 std::cout << "debut 1\n";
283 // dot.setMaxDotSize(0.50); // dot max size = 50% of the image size
284 vpImagePoint ip;
285 ip.set_i(140);
286 ip.set_j(140);
287 dot.initTracking(I, ip);
288 if (opt_display) {
289 dot.setGraphics(true);
290 }
291 else {
292 dot.setGraphics(false);
293 }
294 dot.setComputeMoments(true);
295 dot.track(I);
296
298
299#ifdef VISP_HAVE_MODULE_FEATURES
301 vpFeatureBuilder::create(e, cam, dot);
302#endif
303 if (opt_display) {
304#ifdef VISP_HAVE_MODULE_FEATURES
305 e.display(cam, I, vpColor::red);
306#endif
308 if (opt_click_allowed) {
309 std::cout << "A click to exit..." << std::endl;
311 }
312 }
313 return EXIT_SUCCESS;
314 }
315 catch (const vpException &e) {
316 std::cout << "Catch an exception: " << e.getMessage() << std::endl;
317 return EXIT_FAILURE;
318 }
319}
320#else
321int main() { vpERROR_TRACE("You do not have X11, GTK or GDI display functionalities..."); }
322
323#endif
Generic class defining intrinsic camera parameters.
static const vpColor red
Definition vpColor.h:198
Display for windows using GDI (available on any windows 32 platform).
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition vpDisplayX.h:135
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition vpDot2.h:127
void track(const vpImage< unsigned char > &I, bool canMakeTheWindowGrow=true)
Definition vpDot2.cpp:441
void setGraphics(bool activate)
Definition vpDot2.h:320
void setComputeMoments(bool activate)
Definition vpDot2.h:278
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition vpDot2.cpp:263
error that can be emitted by ViSP classes.
Definition vpException.h:60
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
Class that defines 2D ellipse visual feature.
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_j(double jj)
void set_i(double ii)
Definition of the vpImage class member functions.
Definition vpImage.h:131
static std::string getViSPImagesDataPath()
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
#define vpERROR_TRACE
Definition vpDebug.h:423