Visual Servoing Platform  version 3.6.1 under development (2024-05-09)
imageSequenceReader.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  * Reading an image sequence.
33  *
34 *****************************************************************************/
35 
46 #include <visp3/core/vpDebug.h>
47 #include <visp3/core/vpImage.h>
48 #include <visp3/core/vpIoTools.h>
49 #include <visp3/gui/vpDisplayGDI.h>
50 #include <visp3/gui/vpDisplayGTK.h>
51 #include <visp3/gui/vpDisplayOpenCV.h>
52 #include <visp3/gui/vpDisplayX.h>
53 #include <visp3/io/vpImageIo.h>
54 #include <visp3/io/vpParseArgv.h>
55 #include <visp3/io/vpVideoReader.h>
56 
57 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV) || defined(VISP_HAVE_GTK)
58 
59  // List of allowed command line options
60 #define GETOPTARGS "cdi:p:f:h"
61 
62 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath);
63 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath, int &first, bool &click_allowed,
64  bool &display);
65 
76 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath)
77 {
78 #if VISP_HAVE_DATASET_VERSION >= 0x030600
79  std::string ext("png");
80 #else
81  std::string ext("pgm");
82 #endif
83  fprintf(stdout, "\n\
84 Read an image sequence on the disk.\n\
85 \n\
86 SYNOPSIS\n\
87  %s [-i <input images path>] [-p <personal image sequence path>]\n\
88  [-c][-d][-h]\n\
89 ",
90 name);
91 
92  fprintf(stdout, "\n\
93 OPTIONS: Default\n\
94  -i <input images path> %s\n\
95  Set ViSP-images input path.\n\
96  From this path read \"cube/image.%%04d.%s\"\n\
97  images.\n\
98  Setting the VISP_INPUT_IMAGE_PATH environment\n\
99  variable produces the same behaviour than using\n\
100  this option.\n\
101 \n\
102  -p <personal image sequence path> %s\n\
103  Specify a personal folder containing an image sequence \n\
104  to process.\n\
105  Example : \"/Temp/visp-images/cube/image.%%04d.%s\"\n\
106  %%04d is for the image numbering.\n\
107 \n\
108  -f <index of the first frame> \n\
109  Specify the first image index.\n\
110 \n\
111  -c\n\
112  Disable the mouse click. Useful to automate the \n\
113  execution of this program without human intervention.\n\
114 \n\
115  -d \n\
116  Turn off the display.\n\
117 \n\
118  -h\n\
119  Print the help.\n\n",
120  ipath.c_str(), ext.c_str(), ppath.c_str(), ext.c_str());
121 
122  if (badparam) {
123  fprintf(stderr, "ERROR: \n");
124  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
125  }
126 }
141 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath, int &first, bool &click_allowed,
142  bool &display)
143 {
144  const char *optarg_;
145  int c;
146  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
147 
148  switch (c) {
149  case 'c':
150  click_allowed = false;
151  break;
152  case 'd':
153  display = false;
154  break;
155  case 'i':
156  ipath = optarg_;
157  break;
158  case 'p':
159  ppath = optarg_;
160  break;
161  case 'f':
162  first = atoi(optarg_);
163  break;
164  case 'h':
165  usage(argv[0], nullptr, ipath, ppath);
166  return false;
167  break;
168 
169  default:
170  usage(argv[0], optarg_, ipath, ppath);
171  return false;
172  break;
173  }
174  }
175 
176  if ((c == 1) || (c == -1)) {
177  // standalone param or error
178  usage(argv[0], nullptr, ipath, ppath);
179  std::cerr << "ERROR: " << std::endl;
180  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
181  return false;
182  }
183 
184  return true;
185 }
186 
187 int main(int argc, const char **argv)
188 {
189  try {
190  std::string env_ipath;
191  std::string opt_ipath;
192  std::string ipath;
193  std::string opt_ppath;
194  std::string filename;
195  int opt_first = 1;
196  bool opt_click_allowed = true;
197  bool opt_display = true;
198 
199 #if VISP_HAVE_DATASET_VERSION >= 0x030600
200  std::string ext("png");
201 #else
202  std::string ext("pgm");
203 #endif
204 
205  std::cout << "-------------------------------------------------------" << std::endl;
206  std::cout << " videoImageSequenceReader.cpp" << std::endl << std::endl;
207 
208  std::cout << " reading an image sequence" << std::endl;
209  std::cout << "-------------------------------------------------------" << std::endl;
210  std::cout << std::endl;
211 
212  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
213  // environment variable value
214  env_ipath = vpIoTools::getViSPImagesDataPath();
215 
216  // Set the default input path
217  if (!env_ipath.empty())
218  ipath = env_ipath;
219 
220  // Read the command line options
221  if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_first, opt_click_allowed, opt_display) == false) {
222  return EXIT_FAILURE;
223  }
224 
225  // Get the option values
226  if (!opt_ipath.empty())
227  ipath = opt_ipath;
228 
229  // Compare ipath and env_ipath. If they differ, we take into account
230  // the input path coming from the command line option
231  if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
232  if (ipath != env_ipath) {
233  std::cout << std::endl << "WARNING: " << std::endl;
234  std::cout << " Since -i <visp image path=" << ipath << "> "
235  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
236  << " we skip the environment variable." << std::endl;
237  }
238  }
239 
240  // Test if an input path is set
241  if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty()) {
242  usage(argv[0], nullptr, ipath, opt_ppath);
243  std::cerr << std::endl << "ERROR:" << std::endl;
244  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
245  << " environment variable to specify the location of the " << std::endl
246  << " video path where test images are located." << std::endl
247  << std::endl;
248  return EXIT_FAILURE;
249  }
250 
252 
253  // vpImage is a template class you can declare vpImage of ...
254  // everything...
255  vpImage<vpRGBa> I;
256 
257  // Create the video Reader
258  vpVideoReader reader;
259 
260  if (opt_ppath.empty()) {
261  filename = vpIoTools::createFilePath(ipath, "mire-2/image.%04d." + ext);
262  }
263  else {
264  filename.assign(opt_ppath);
265  }
266 
267  // Initialize the reader and get the first frame.
268  reader.setFileName(filename);
269  reader.setFirstFrameIndex(opt_first);
270  reader.open(I);
271  std::cout << "Current image number: " << reader.getFrameIndex() << std::endl;
272  if ((opt_first) != reader.getFrameIndex()) {
273  std::cout << "Unable to get requested image number: " << opt_first << std::endl;
274  return EXIT_FAILURE;
275  }
276 
277  // We open a window using either X11, GTK, GDI or OpenCV.
278 #if defined(VISP_HAVE_X11)
280 #elif defined(VISP_HAVE_GTK)
282 #elif defined(VISP_HAVE_GDI)
284 #elif defined(HAVE_OPENCV_HIGHGUI)
286 #endif
287 
288  if (opt_display) {
289  // Display size is automatically defined by the image (I) size
290  display.init(I, 100, 100, "Display video frame");
292  vpDisplay::flush(I);
293  }
294 
295  if (opt_display && opt_click_allowed) {
296  std::cout << "Click in the image to read and display the second frame" << std::endl;
298  }
299 
300  reader.getFrame(I, opt_first + 1);
301  std::cout << "Current image number (should be " << opt_first + 1 << "): " << reader.getFrameIndex() << std::endl;
302  if ((opt_first + 1) != reader.getFrameIndex()) {
303  std::cout << "Unable to get requested image number: " << opt_first + 1 << std::endl;
304  return EXIT_FAILURE;
305  }
306 
307  if (opt_display) {
309  vpDisplay::flush(I);
310  }
311 
312  if (opt_display && opt_click_allowed) {
313  std::cout << "Click on the image to read and display the last frame" << std::endl;
315  }
316 
317  reader.getFrame(I, reader.getLastFrameIndex());
318  std::cout << "Current image number (should be " << reader.getLastFrameIndex() << "): " << reader.getFrameIndex()
319  << std::endl;
320 
321  if (opt_display) {
323  vpDisplay::flush(I);
324  }
325 
326  if (opt_display && opt_click_allowed) {
327  std::cout << "Click to see the video" << std::endl;
329  }
330 
331  int lastFrame = reader.getLastFrameIndex();
332 
333  for (int i = opt_first; i <= lastFrame; i++) {
334  reader.getFrame(I, i);
335  std::cout << "Current image number: " << reader.getFrameIndex() << std::endl;
336  if (opt_display) {
338  vpDisplay::flush(I);
339  }
340  }
341 
342  if (opt_display && opt_click_allowed) {
343  std::cout << "Click to exit the test" << std::endl;
345  }
346 
347  return EXIT_SUCCESS;
348  }
349  catch (const vpException &e) {
350  std::cout << "Catch an exception: " << e << std::endl;
351  return EXIT_FAILURE;
352  }
353 }
354 #else
355 int main()
356 {
357  std::cout << "Sorry, no display is available. We quit this example." << std::endl;
358  return EXIT_SUCCESS;
359 }
360 #endif
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:128
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:128
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 std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1832
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:2195
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
long getLastFrameIndex()
void open(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
void setFirstFrameIndex(const long first_frame)
bool getFrame(vpImage< vpRGBa > &I, long frame)
long getFrameIndex() const
void display(vpImage< unsigned char > &I, const std::string &title)
Display a gray-scale image.