Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
displaySequence.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5 *
6 * This software is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 * See the file LICENSE.txt at the root directory of this source
11 * distribution for additional information about the GNU GPL.
12 *
13 * For using ViSP with software that can not be combined with the GNU
14 * GPL, please contact Inria about acquiring a ViSP Professional
15 * Edition License.
16 *
17 * See https://visp.inria.fr for more information.
18 *
19 * This software was developed at:
20 * Inria Rennes - Bretagne Atlantique
21 * Campus Universitaire de Beaulieu
22 * 35042 Rennes Cedex
23 * France
24 *
25 * If you have questions regarding the use of this file, please contact
26 * Inria at visp@inria.fr
27 *
28 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30 *
31 * Description:
32 * Read an image sequence from the disk and display it.
33 *
34*****************************************************************************/
45#include <iomanip>
46#include <sstream>
47#include <stdio.h>
48#include <stdlib.h>
49#include <visp3/core/vpConfig.h>
50#include <visp3/core/vpDebug.h>
51#include <visp3/core/vpIoTools.h>
52#include <visp3/io/vpParseArgv.h>
53#if (defined(VISP_HAVE_GTK) || defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI))
54
55#include <visp3/core/vpImage.h>
56#include <visp3/io/vpImageIo.h>
57#include <visp3/gui/vpDisplayGDI.h>
58#include <visp3/gui/vpDisplayGTK.h>
59#include <visp3/gui/vpDisplayX.h>
60#include <visp3/core/vpTime.h>
61
72// List of allowed command line options
73#define GETOPTARGS "di:p:hf:l:s:w"
74
88void usage(const char *name, const char *badparam, std::string ipath, std::string ppath, unsigned first,
89 unsigned last, unsigned step)
90{
91#if VISP_HAVE_DATASET_VERSION >= 0x030600
92 std::string ext("png");
93#else
94 std::string ext("pgm");
95#endif
96 fprintf(stdout, "\n\
97Read an image sequence from the disk and display it.\n\
98The sequence is made of separate images. Each image corresponds\n\
99to a PGM file.\n\
100\n\
101SYNOPSIS\n\
102 %s [-i <test image path>] [-p <personal image path>]\n\
103 [-f <first image>] [-l <last image>] [-s <step>] \n\
104 [-w] [-d] [-h]\n", name);
105
106 fprintf(stdout, "\n\
107 OPTIONS: Default\n\
108 -i <test image path> %s\n\
109 Set image input path.\n\
110 From this path read \"cube/image.%%04d.%s\"\n\
111 images. These images come from ViSP-images-x.y.z.tar.gz\n\
112 available on the ViSP website.\n\
113 Setting the VISP_INPUT_IMAGE_PATH environment\n\
114 variable produces the same behaviour than using\n\
115 this option.\n\
116 \n\
117 -p <personal image path> %s\n\
118 Specify a personal sequence containing images \n\
119 to process.\n\
120 By image sequence, we mean one file per image.\n\
121 The format is selected by analysing the filename extension.\n\
122 Example : \"/Temp/visp-images/cube/image.%%04d.%s\"\n\
123 %%04d is for the image numbering.\n\
124 \n\
125 -f <first image> %u\n\
126 First image number of the sequence.\n\
127 \n\
128 -l <last image> %u\n\
129 last image number of the sequence.\n\
130 \n\
131 -s <step> %u\n\
132 Step between two images.\n\
133\n\
134 -d \n\
135 Disable the image display. This can be useful \n\
136 for automatic tests using crontab under Unix or \n\
137 using the task manager under Windows.\n\
138\n\
139 -w\n\
140 Wait for a mouse click between two images.\n\
141 If the image display is disabled (using -d)\n\
142 this option is without effect.\n\
143\n\
144 -h\n\
145 Print the help.\n\n",
146 ipath.c_str(), ext.c_str(), ppath.c_str(), ext.c_str(), first, last, step);
147
148 if (badparam)
149 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
150}
172bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath, unsigned &first, unsigned &last,
173 unsigned &step, bool &display, bool &wait)
174{
175 const char *optarg_;
176 int c;
177 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
178
179 switch (c) {
180 case 'd':
181 display = false;
182 break;
183 case 'i':
184 ipath = optarg_;
185 break;
186 case 'p':
187 ppath = optarg_;
188 break;
189 case 'f':
190 first = (unsigned)atoi(optarg_);
191 break;
192 case 'l':
193 last = (unsigned)atoi(optarg_);
194 break;
195 case 's':
196 step = (unsigned)atoi(optarg_);
197 break;
198 case 'w':
199 wait = true;
200 break;
201 case 'h':
202 usage(argv[0], NULL, ipath, ppath, first, last, step);
203 return false;
204 break;
205
206 default:
207 usage(argv[0], optarg_, ipath, ppath, first, last, step);
208 return false;
209 break;
210 }
211 }
212
213 if ((c == 1) || (c == -1)) {
214 // standalone param or error
215 usage(argv[0], NULL, ipath, ppath, first, last, step);
216 std::cerr << "ERROR: " << std::endl;
217 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
218 return false;
219 }
220
221 return true;
222}
223
224int main(int argc, const char **argv)
225{
226 try {
227 std::string env_ipath;
228 std::string opt_ipath;
229 std::string ipath;
230 std::string opt_ppath;
231 std::string dirname;
232 std::string filename;
233 unsigned opt_first = 0;
234 unsigned opt_last = 80;
235 unsigned opt_step = 1;
236 bool opt_display = true;
237 bool opt_wait = false;
238
239#if VISP_HAVE_DATASET_VERSION >= 0x030600
240 std::string ext("png");
241#else
242 std::string ext("pgm");
243#endif
244
245 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
246 // environment variable value
248
249 // Set the default input path
250 if (!env_ipath.empty())
251 ipath = env_ipath;
252
253 // Read the command line options
254 if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_first, opt_last, opt_step, opt_display, opt_wait) ==
255 false) {
256 return EXIT_FAILURE;
257 }
258
259 if (!opt_display)
260 opt_wait = false; // turn off the waiting
261
262 // Get the option values
263 if (!opt_ipath.empty())
264 ipath = opt_ipath;
265
266 // Compare ipath and env_ipath. If they differ, we take into account
267 // the input path comming from the command line option
268 if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
269 if (ipath != env_ipath) {
270 std::cout << std::endl << "WARNING: " << std::endl;
271 std::cout << " Since -i <visp image path=" << ipath << "> "
272 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
273 << " we skip the environment variable." << std::endl;
274 }
275 }
276
277 // Test if an input path is set
278 if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty()) {
279 usage(argv[0], NULL, ipath, opt_ppath, opt_first, opt_last, opt_step);
280 std::cerr << std::endl << "ERROR:" << std::endl;
281 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
282 << " environment variable to specify the location of the " << std::endl
283 << " image path where test images are located." << std::endl
284 << " Use -p <personal image path> option if you want to " << std::endl
285 << " use personal images." << std::endl
286 << std::endl;
287
288 return EXIT_FAILURE;
289 }
290
291 // Declare an image, this is a gray level image (unsigned char)
292 // it size is not defined yet, it will be defined when the image will
293 // read on the disk
295
296 unsigned iter = opt_first;
297 std::ostringstream s;
298 char cfilename[FILENAME_MAX];
299
300 if (opt_ppath.empty()) {
301 // Warning : the daset is available https://visp.inria.fr/download/
302 dirname = vpIoTools::createFilePath(ipath, "cube");
303
304 // Build the name of the image file
305
306 s.setf(std::ios::right, std::ios::adjustfield);
307 s << "image." << std::setw(4) << std::setfill('0') << iter << "." << ext;
308 filename = vpIoTools::createFilePath(dirname, s.str());
309 } else {
310 snprintf(cfilename, FILENAME_MAX, opt_ppath.c_str(), iter);
311 filename = cfilename;
312 }
313 // Read image named "filename" and put the bitmap in I
314 try {
315 vpImageIo::read(I, filename);
316 } catch (...) {
317 std::cerr << std::endl << "ERROR:" << std::endl;
318 std::cerr << " Cannot read " << filename << std::endl;
319 std::cerr << " Check your -i " << ipath << " option, " << std::endl
320 << " or your -p " << opt_ppath << " option " << std::endl
321 << " or VISP_INPUT_IMAGE_PATH environment variable" << std::endl;
322 return EXIT_FAILURE;
323 }
324
325#if defined(VISP_HAVE_GTK)
326 vpDisplayGTK display;
327#elif defined(VISP_HAVE_X11)
328 vpDisplayX display;
329#elif defined(VISP_HAVE_GDI)
330 vpDisplayGDI display;
331#endif
332 if (opt_display) {
333
334 // We open a window using either X11 or GTK or GDI.
335 // Its size is automatically defined by the image (I) size
336 display.init(I, 100, 100, "Display...");
337
338 // Display the image
339 // The image class has a member that specify a pointer toward
340 // the display that has been initialized in the display declaration
341 // therefore is is no longer necessary to make a reference to the
342 // display variable.
345 }
346
347 // this is the loop over the image sequence
348 while (iter < opt_last) {
349 double tms = vpTime::measureTimeMs();
350
351 // set the new image name
352 if (opt_ppath.empty()) {
353 s.str("");
354 s << "image." << std::setw(4) << std::setfill('0') << iter << "." << ext;
355 filename = vpIoTools::createFilePath(dirname, s.str());
356 } else {
357 snprintf(cfilename, FILENAME_MAX, opt_ppath.c_str(), iter);
358 filename = cfilename;
359 }
360
361 std::cout << "read : " << filename << std::endl;
362 // read the image
363 vpImageIo::read(I, filename);
364 if (opt_display) {
365 // Display the image
367 // Flush the display
369 }
370 if (opt_wait) {
371 std::cout << "A click in the image to continue..." << std::endl;
372 // Wait for a blocking mouse click
374 } else {
375 // Synchronise the loop to 40 ms
376 vpTime::wait(tms, 40);
377 }
378
379 iter += opt_step;
380 }
381 // double tms_2 = vpTime::measureTimeMs() ;
382 // double tms_total = tms_2 - tms_1 ;
383 // std::cout << "Total Time : "<< tms_total<<std::endl;
384 return EXIT_SUCCESS;
385 } catch (const vpException &e) {
386 std::cout << "Catch an exception: " << e << std::endl;
387 return EXIT_FAILURE;
388 }
389}
390#else
391int main()
392{
393 std::cout << "You do not have X11, or GDI (Graphical Device Interface), or GTK functionalities to display images..."
394 << std::endl;
395 std::cout << "Tip if you are on a unix-like system:" << std::endl;
396 std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
397 std::cout << "Tip if you are on a windows-like system:" << std::endl;
398 std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
399 return EXIT_SUCCESS;
400}
401#endif
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:132
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:59
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:135
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)
VISP_EXPORT int wait(double t0, double t)
VISP_EXPORT double measureTimeMs()