Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
trackMeCircle.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 * Tracking of an ellipse.
32 */
33
39
45
46#include <visp3/core/vpConfig.h>
47
48#if defined(VISP_HAVE_MODULE_ME) && defined(VISP_HAVE_DISPLAY)
49
50#include <visp3/core/vpColor.h>
51#include <visp3/core/vpImage.h>
52#include <visp3/core/vpIoTools.h>
53#include <visp3/gui/vpDisplayFactory.h>
54#include <visp3/io/vpImageIo.h>
55#include <visp3/io/vpParseArgv.h>
56#include <visp3/me/vpMeEllipse.h>
57
58// List of allowed command line options
59#define GETOPTARGS "cdi:h"
60
61#ifdef ENABLE_VISP_NAMESPACE
62using namespace VISP_NAMESPACE_NAME;
63#endif
64
65void usage(const char *name, const char *badparam, std::string ipath);
66bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display);
67
77void usage(const char *name, const char *badparam, std::string ipath)
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 auto detection of dots using vpDot2.\n\
91\n\
92SYNOPSIS\n\
93 %s [-i <input image path>] [-c] [-d] [-h]\n",
94 name);
95
96 fprintf(stdout, "\n\
97OPTIONS: Default\n\
98 -i <input image path> %s\n\
99 Set image input path.\n\
100 From this path read \"circle/circle.%s\"\n\
101 image. \n\
102 Setting the VISP_INPUT_IMAGE_PATH environment\n\
103 variable produces the same behaviour than using\n\
104 this option.\n\
105\n\
106 -c\n\
107 Disable the mouse click. Useful to automate the \n\
108 execution of this program without human intervention.\n\
109\n\
110 -d \n\
111 Turn off the display.\n\
112\n\
113 -h\n\
114 Print the help.\n",
115 ipath.c_str(), ext.c_str());
116
117 if (badparam)
118 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
119}
133bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display)
134{
135 const char *optarg_;
136 int c;
137 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
138
139 switch (c) {
140 case 'c':
141 click_allowed = false;
142 break;
143 case 'd':
144 display = false;
145 break;
146 case 'i':
147 ipath = optarg_;
148 break;
149 case 'h':
150 usage(argv[0], nullptr, ipath);
151 return false;
152
153 default:
154 usage(argv[0], optarg_, ipath);
155 return false;
156 }
157 }
158
159 if ((c == 1) || (c == -1)) {
160 // standalone param or error
161 usage(argv[0], nullptr, ipath);
162 std::cerr << "ERROR: " << std::endl;
163 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
164 return false;
165 }
166
167 return true;
168}
169
170int main(int argc, const char **argv)
171{
172#if defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV)
173 try {
174 std::string env_ipath;
175 std::string opt_ipath;
176 std::string ipath;
177 std::string dirname;
178 std::string filename;
179 bool opt_click_allowed = true;
180 bool opt_display = true;
181
182#if defined(VISP_HAVE_DATASET)
183#if VISP_HAVE_DATASET_VERSION >= 0x030600
184 std::string ext("png");
185#else
186 std::string ext("pgm");
187#endif
188#else
189 // We suppose that the user will download a recent dataset
190 std::string ext("png");
191#endif
192
193 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
194 // environment variable value
196
197 // Set the default input path
198 if (!env_ipath.empty())
199 ipath = env_ipath;
200
201 // Read the command line options
202 if (getOptions(argc, argv, opt_ipath, opt_click_allowed, opt_display) == false) {
203 return EXIT_FAILURE;
204 }
205
206 // Get the option values
207 if (!opt_ipath.empty())
208 ipath = opt_ipath;
209
210 // Compare ipath and env_ipath. If they differ, we take into account
211 // the input path coming from the command line option
212 if (!opt_ipath.empty() && !env_ipath.empty()) {
213 if (ipath != env_ipath) {
214 std::cout << std::endl << "WARNING: " << std::endl;
215 std::cout << " Since -i <visp image path=" << ipath << "> "
216 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
217 << " we skip the environment variable." << std::endl;
218 }
219 }
220
221 // Test if an input path is set
222 if (opt_ipath.empty() && env_ipath.empty()) {
223 usage(argv[0], nullptr, ipath);
224 std::cerr << std::endl << "ERROR:" << std::endl;
225 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
226 << " environment variable to specify the location of the " << std::endl
227 << " image path where test images are located." << std::endl
228 << std::endl;
229 return EXIT_FAILURE;
230 }
231
232 // Declare an image, this is a gray level image (unsigned char)
233 // it size is not defined yet, it will be defined when the image will
234 // read on the disk
236 vpDisplay *display = nullptr;
237
238 // Set the path location of the image sequence
239 dirname = vpIoTools::createFilePath(ipath, "circle");
240
241 // Build the name of the image file
242 filename = vpIoTools::createFilePath(dirname, "circle." + ext);
243
244 // Read the image into the image structure I. I is initialized to the correct size
245 // vpImageIo::read() may throw various exception if, for example,
246 // the file does not exist, or if the memory cannot be allocated
247 try {
248 std::cout << "Load: " << filename << std::endl;
249
250 vpImageIo::read(I, filename);
251 }
252 catch (...) {
253 // If an exception is throwned it is catched here and will result in the end of the program.
254 // Note that another error message can be printed from vpImageIo::read() to give more
255 // information about the error
256 std::cerr << std::endl << "ERROR:" << std::endl;
257 std::cerr << " Cannot read " << filename << std::endl;
258 std::cerr << " Check your -i " << ipath << " option " << std::endl
259 << " or VISP_INPUT_IMAGE_PATH environment variable." << std::endl;
260 return EXIT_FAILURE;
261 }
262
263 // We open a window using either X11, GTK or GDI
264 if (opt_display) {
266 // Display size is automatically defined by the image (I) size
267 display->init(I, 100, 100, "Display...");
268 // Display the image
269 // The image class has a member that specify a pointer toward
270 // the display that has been initialized in the display declaration
271 // therefore is is no longer necessary to make a reference to the
272 // display variable.
275 }
276
277 vpMeEllipse E1;
278
279 vpMe me;
280 me.setRange(20);
281 me.setSampleStep(10);
283 me.setThreshold(20);
284
285 E1.setMe(&me);
287 // If click is allowed, wait for a mouse click to select the points
288 // on the ellipse
289 if (opt_display && opt_click_allowed) {
290 E1.initTracking(I);
291 }
292 else {
293 // Create a list of points to automate the test
294 std::vector<vpImagePoint> ip;
295 ip.push_back(vpImagePoint(78, 203));
296 ip.push_back(vpImagePoint(62, 125));
297 ip.push_back(vpImagePoint(128, 101));
298 ip.push_back(vpImagePoint(167, 147));
299 ip.push_back(vpImagePoint(147, 200));
300
301 E1.initTracking(I, ip);
302 }
303
304 if (opt_display) {
305 E1.display(I, vpColor::green);
307 }
308
309 std::cout << "Sample step: " << E1.getMe()->getSampleStep() << std::endl;
310 std::cout << "Tracking on image: " << filename << std::endl;
311 E1.track(I);
312 if (opt_display) {
314 }
315
316 if (opt_display && opt_click_allowed) {
317 std::cout << "A click to exit..." << std::endl;
319 }
320
321 if (display) {
322 delete display;
323 }
324 return EXIT_SUCCESS;
325 }
326 catch (const vpException &e) {
327 std::cout << "Catch an exception: " << e << std::endl;
328 return EXIT_FAILURE;
329 }
330#else
331 (void)argc;
332 (void)argv;
333 std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
334#endif
335}
336#else
337#include <iostream>
338
339int main()
340{
341 std::cout << "visp_me module or X11, GTK, GDI or OpenCV display "
342 "functionalities are required..."
343 << std::endl;
344 return EXIT_SUCCESS;
345}
346
347#endif
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 flush(const vpImage< unsigned char > &I)
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 ...
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)
Class that tracks an ellipse or a circle using moving edges.
void display(const vpImage< unsigned char > &I, const vpColor &col, unsigned int thickness=1)
void initTracking(const vpImage< unsigned char > &I, bool trackCircle=false, bool trackArc=false)
void track(const vpImage< unsigned char > &I)
@ RANGE_RESULT
Definition vpMeSite.h:85
void setDisplay(vpMeSite::vpMeSiteDisplayType select)
void setMe(vpMe *me)
vpMe * getMe()
Definition vpMe.h:143
void setRange(const unsigned int &range)
Definition vpMe.h:438
void setLikelihoodThresholdType(const vpLikelihoodThresholdType likelihood_threshold_type)
Definition vpMe.h:531
void setThreshold(const double &threshold)
Definition vpMe.h:489
void setSampleStep(const double &sample_step)
Definition vpMe.h:445
double getSampleStep() const
Definition vpMe.h:290
@ NORMALIZED_THRESHOLD
Definition vpMe.h:154
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.