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