Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
displaySequence.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 * Read an image sequence from the disk and display it.
32 */
42
43#include <iomanip>
44#include <sstream>
45#include <stdio.h>
46#include <stdlib.h>
47#include <visp3/core/vpConfig.h>
48#include <visp3/core/vpDebug.h>
49#include <visp3/core/vpIoTools.h>
50#include <visp3/io/vpParseArgv.h>
51#if defined(VISP_HAVE_DISPLAY)
52
53#include <visp3/core/vpImage.h>
54#include <visp3/io/vpImageIo.h>
55#include <visp3/gui/vpDisplayFactory.h>
56#include <visp3/core/vpTime.h>
57
67
68// List of allowed command line options
69#define GETOPTARGS "di:p:hf:l:s:w"
70
71#ifdef ENABLE_VISP_NAMESPACE
72using namespace VISP_NAMESPACE_NAME;
73#endif
74
88void usage(const char *name, const char *badparam, const std::string &ipath, const std::string &ppath, unsigned first,
89 unsigned last, unsigned step)
90{
91#if defined(VISP_HAVE_DATASET)
92#if VISP_HAVE_DATASET_VERSION >= 0x030600
93 std::string ext("png");
94#else
95 std::string ext("pgm");
96#endif
97#else
98 // We suppose that the user will download a recent dataset
99 std::string ext("png");
100#endif
101 fprintf(stdout, "\n\
102Read an image sequence from the disk and display it.\n\
103The sequence is made of separate images. Each image corresponds\n\
104to a PGM file.\n\
105\n\
106SYNOPSIS\n\
107 %s [-i <test image path>] [-p <personal image path>]\n\
108 [-f <first image>] [-l <last image>] [-s <step>] \n\
109 [-w] [-d] [-h]\n", name);
110
111 fprintf(stdout, "\n\
112 OPTIONS: Default\n\
113 -i <test image path> %s\n\
114 Set image input path.\n\
115 From this path read \"cube/image.%%04d.%s\"\n\
116 images. These images come from ViSP-images-x.y.z.tar.gz\n\
117 available on the ViSP website.\n\
118 Setting the VISP_INPUT_IMAGE_PATH environment\n\
119 variable produces the same behaviour than using\n\
120 this option.\n\
121 \n\
122 -p <personal image path> %s\n\
123 Specify a personal sequence containing images \n\
124 to process.\n\
125 By image sequence, we mean one file per image.\n\
126 The format is selected by analyzing the filename extension.\n\
127 Example : \"/Temp/visp-images/cube/image.%%04d.%s\"\n\
128 %%04d is for the image numbering.\n\
129 \n\
130 -f <first image> %u\n\
131 First image number of the sequence.\n\
132 \n\
133 -l <last image> %u\n\
134 last image number of the sequence.\n\
135 \n\
136 -s <step> %u\n\
137 Step between two images.\n\
138\n\
139 -d \n\
140 Disable the image display. This can be useful \n\
141 for automatic tests using crontab under Unix or \n\
142 using the task manager under Windows.\n\
143\n\
144 -w\n\
145 Wait for a mouse click between two images.\n\
146 If the image display is disabled (using -d)\n\
147 this option is without effect.\n\
148\n\
149 -h\n\
150 Print the help.\n\n",
151 ipath.c_str(), ext.c_str(), ppath.c_str(), ext.c_str(), first, last, step);
152
153 if (badparam)
154 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
155}
177bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath, unsigned &first, unsigned &last,
178 unsigned &step, bool &display, bool &wait)
179{
180 const char *optarg_;
181 int c;
182 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
183
184 switch (c) {
185 case 'd':
186 display = false;
187 break;
188 case 'i':
189 ipath = optarg_;
190 break;
191 case 'p':
192 ppath = optarg_;
193 break;
194 case 'f':
195 first = static_cast<unsigned int>(atoi(optarg_));
196 break;
197 case 'l':
198 last = static_cast<unsigned int>(atoi(optarg_));
199 break;
200 case 's':
201 step = static_cast<unsigned int>(atoi(optarg_));
202 break;
203 case 'w':
204 wait = true;
205 break;
206 case 'h':
207 usage(argv[0], nullptr, ipath, ppath, first, last, step);
208 return false;
209
210 default:
211 usage(argv[0], optarg_, ipath, ppath, first, last, step);
212 return false;
213 }
214 }
215
216 if ((c == 1) || (c == -1)) {
217 // standalone param or error
218 usage(argv[0], nullptr, ipath, ppath, first, last, step);
219 std::cerr << "ERROR: " << std::endl;
220 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
221 return false;
222 }
223
224 return true;
225}
226
227int main(int argc, const char **argv)
228{
229#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
230 vpDisplay *display = nullptr;
231#endif
232 try {
233 std::string env_ipath;
234 std::string opt_ipath;
235 std::string ipath;
236 std::string opt_ppath;
237 std::string dirname;
238 std::string filename;
239 unsigned opt_first = 0;
240 unsigned opt_last = 80;
241 unsigned opt_step = 1;
242 bool opt_display = true;
243 bool opt_wait = false;
244
245#if defined(VISP_HAVE_DATASET)
246#if VISP_HAVE_DATASET_VERSION >= 0x030600
247 std::string ext("png");
248#else
249 std::string ext("pgm");
250#endif
251#else
252 // We suppose that the user will download a recent dataset
253 std::string ext("png");
254#endif
255
256 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
257 // environment variable value
259
260 // Set the default input path
261 if (!env_ipath.empty())
262 ipath = env_ipath;
263
264 // Read the command line options
265 if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_first, opt_last, opt_step, opt_display, opt_wait) ==
266 false) {
267 return EXIT_FAILURE;
268 }
269
270 if (!opt_display)
271 opt_wait = false; // turn off the waiting
272
273 // Get the option values
274 if (!opt_ipath.empty())
275 ipath = opt_ipath;
276
277 // Compare ipath and env_ipath. If they differ, we take into account
278 // the input path coming from the command line option
279 if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
280 if (ipath != env_ipath) {
281 std::cout << std::endl << "WARNING: " << std::endl;
282 std::cout << " Since -i <visp image path=" << ipath << "> "
283 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
284 << " we skip the environment variable." << std::endl;
285 }
286 }
287
288 // Test if an input path is set
289 if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty()) {
290 usage(argv[0], nullptr, ipath, opt_ppath, opt_first, opt_last, opt_step);
291 std::cerr << std::endl << "ERROR:" << std::endl;
292 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
293 << " environment variable to specify the location of the " << std::endl
294 << " image path where test images are located." << std::endl
295 << " Use -p <personal image path> option if you want to " << std::endl
296 << " use personal images." << std::endl
297 << std::endl;
298
299 return EXIT_FAILURE;
300 }
301
302 // Declare an image, this is a gray level image (unsigned char)
303 // it size is not defined yet, it will be defined when the image will
304 // read on the disk
306
307 unsigned iter = opt_first;
308 std::string imagename("image.%04d." + ext);
309
310 if (opt_ppath.empty()) {
311 // Warning : the dataset is available https://visp.inria.fr/download/
312 dirname = vpIoTools::createFilePath(ipath, "cube");
313
314 // Build the name of the image file
316 }
317 else {
318 filename = vpIoTools::formatString(opt_ppath, iter);
319 }
320 // Read image named "filename" and put the bitmap in I
321 try {
322 vpImageIo::read(I, filename);
323 }
324 catch (...) {
325 std::cerr << std::endl << "ERROR:" << std::endl;
326 std::cerr << " Cannot read " << filename << std::endl;
327 std::cerr << " Check your -i " << ipath << " option, " << std::endl
328 << " or your -p " << opt_ppath << " option " << std::endl
329 << " or VISP_INPUT_IMAGE_PATH environment variable" << std::endl;
330 return EXIT_FAILURE;
331 }
332
333#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
334 std::shared_ptr<vpDisplay> display = vpDisplayFactory::createDisplay();
335#else
337#endif
338 if (opt_display) {
339
340 // We open a window using either X11 or GTK or GDI.
341 // Its size is automatically defined by the image (I) size
342 display->init(I, 100, 100, "Display...");
343
344 // Display the image
345 // The image class has a member that specify a pointer toward
346 // the display that has been initialized in the display declaration
347 // therefore is is no longer necessary to make a reference to the
348 // display variable.
351 }
352
353 // this is the loop over the image sequence
354 while (iter < opt_last) {
355 double tms = vpTime::measureTimeMs();
356
357 // set the new image name
358 if (opt_ppath.empty()) {
360 }
361 else {
362 filename = vpIoTools::formatString(opt_ppath, iter);
363 }
364
365 std::cout << "read : " << filename << std::endl;
366 // read the image
367 vpImageIo::read(I, filename);
368 if (opt_display) {
369 // Display the image
371 // Flush the display
373 }
374 if (opt_wait) {
375 std::cout << "A click in the image to continue..." << std::endl;
376 // Wait for a blocking mouse click
378 }
379 else {
380 // Synchronise the loop to 40 ms
381 vpTime::wait(tms, 40);
382 }
383
384 iter += opt_step;
385 }
386 // double tms_2 = vpTime::measureTimeMs() ;
387 // double tms_total = tms_2 - tms_1 ;
388 // std::cout << "Total Time : "<< tms_total<<std::endl;
389#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
390 if (display != nullptr) {
391 delete display;
392 }
393#endif
394 return EXIT_SUCCESS;
395 }
396 catch (const vpException &e) {
397 std::cout << "Catch an exception: " << e << std::endl;
398#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
399 if (display != nullptr) {
400 delete display;
401 }
402#endif
403 return EXIT_FAILURE;
404 }
405}
406#else
407int main()
408{
409 std::cout << "You do not have X11, or GDI (Graphical Device Interface), or GTK functionalities to display images..."
410 << std::endl;
411 std::cout << "Tip if you are on a unix-like system:" << std::endl;
412 std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
413 std::cout << "Tip if you are on a windows-like system:" << std::endl;
414 std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
415 return EXIT_SUCCESS;
416}
417#endif
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)
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)
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.
VISP_EXPORT double measureTimeMs()
VISP_EXPORT int wait(double t0, double t)