ViSP
imageSequenceReader.cpp
1 /****************************************************************************
2  *
3  * $Id: imageDiskRW.cpp 2158 2009-05-07 07:24:51Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Reading an image sequence.
36  *
37  * Authors:
38  * Eric Marchand
39  *
40  *****************************************************************************/
41 
52 #include <visp/vpImage.h>
53 #include <visp/vpImageIo.h>
54 #include <visp/vpParseArgv.h>
55 #include <visp/vpIoTools.h>
56 #include <visp/vpDebug.h>
57 #include <visp/vpVideoReader.h>
58 #include <visp/vpDisplayOpenCV.h>
59 #include <visp/vpDisplayX.h>
60 #include <visp/vpDisplayGTK.h>
61 #include <visp/vpDisplayGDI.h>
62 
63 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV) || defined(VISP_HAVE_GTK)
64 
65 // List of allowed command line options
66 #define GETOPTARGS "cdi:p:f:h"
67 
68 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath);
69 bool getOptions(int argc, const char **argv,
70  std::string &ipath, std::string &ppath, int &first, bool &click_allowed, bool &display);
71 
82 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath)
83 {
84  fprintf(stdout, "\n\
85 Read an image sequence on the disk.\n\
86 \n\
87 SYNOPSIS\n\
88  %s [-i <input images path>] [-p <personal image sequence path>]\n\
89  [-c][-d][-h]\n \
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 \"ViSP-images/cube/image.%%04d.pgm\"\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.pgm\"\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 automaze the \n\
113  execution of this program without humain 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(), ppath.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,
142  std::string &ipath, std::string &ppath, int &first, bool &click_allowed, 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': click_allowed = false; break;
150  case 'd': display = false; break;
151  case 'i': ipath = optarg_; break;
152  case 'p': ppath = optarg_; break;
153  case 'f': first = atoi(optarg_); break;
154  case 'h': usage(argv[0], NULL, ipath, ppath); return false; break;
155 
156  default:
157  usage(argv[0], optarg_, ipath, ppath); return false; break;
158  }
159  }
160 
161  if ((c == 1) || (c == -1)) {
162  // standalone param or error
163  usage(argv[0], NULL, ipath, ppath);
164  std::cerr << "ERROR: " << std::endl;
165  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
166  return false;
167  }
168 
169  return true;
170 }
171 
172 
173 
174 int
175 main(int argc, const char ** argv)
176 {
177  try {
178  std::string env_ipath;
179  std::string opt_ipath;
180  std::string ipath;
181  std::string opt_ppath;
182  std::string filename;
183  int opt_first = 1;
184  bool opt_click_allowed = true;
185  bool opt_display = true;
186 
187  std::cout << "-------------------------------------------------------" << std::endl ;
188  std::cout << " videoImageSequenceReader.cpp" <<std::endl << std::endl ;
189 
190  std::cout << " reading an image sequence" << std::endl ;
191  std::cout << "-------------------------------------------------------" << std::endl ;
192  std::cout << std::endl ;
193 
194  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH environment variable value
195  env_ipath = vpIoTools::getViSPImagesDataPath();
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_ppath, opt_first, opt_click_allowed,
203  opt_display) == false) {
204  exit (-1);
205  }
206 
207  // Get the option values
208  if (!opt_ipath.empty())
209  ipath = opt_ipath;
210 
211  // Compare ipath and env_ipath. If they differ, we take into account
212  // the input path comming from the command line option
213  if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
214  if (ipath != env_ipath) {
215  std::cout << std::endl
216  << "WARNING: " << std::endl;
217  std::cout << " Since -i <visp image path=" << ipath << "> "
218  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
219  << " we skip the environment variable." << std::endl;
220  }
221  }
222 
223  // Test if an input path is set
224  if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty()){
225  usage(argv[0], NULL, ipath, opt_ppath);
226  std::cerr << std::endl
227  << "ERROR:" << std::endl;
228  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
229  << std::endl
230  << " environment variable to specify the location of the " << std::endl
231  << " video path where test images are located." << std::endl << std::endl;
232  exit(-1);
233  }
234 
235 
237 
238 
239  // vpImage is a template class you can declare vpImage of ... everything...
240  vpImage<vpRGBa> I ;
241 
242  //Create the video Reader
243  vpVideoReader reader;
244 
245  if (opt_ppath.empty())
246  {
247  filename = vpIoTools::createFilePath(ipath, "ViSP-images/mire-2/image.%04d.pgm");
248  }
249  else
250  {
251  filename.assign(opt_ppath);
252  }
253 
254  //Initialize the reader and get the first frame.
255  reader.setFileName(filename);
256  reader.setFirstFrameIndex(opt_first);
257  reader.open(I);
258 
259  // We open a window using either X11, GTK, GDI or OpenCV.
260 #if defined VISP_HAVE_X11
261  vpDisplayX display;
262 #elif defined VISP_HAVE_GTK
263  vpDisplayGTK display;
264 #elif defined VISP_HAVE_GDI
265  vpDisplayGDI display;
266 #elif defined VISP_HAVE_OPENCV
267  vpDisplayOpenCV display;
268 #endif
269 
270  if (opt_display) {
271  // Display size is automatically defined by the image (I) size
272  display.init(I, 100, 100,"Display video frame") ;
273  vpDisplay::display(I) ;
274  vpDisplay::flush(I) ;
275  }
276 
277  if (opt_display && opt_click_allowed)
278  {
279  std::cout << "Click on the image to read and display the second frame" << std::endl;
281  }
282 
283  reader.getFrame(I,opt_first+1);
284 
285  if (opt_display)
286  {
287  vpDisplay::display(I) ;
288  vpDisplay::flush(I);
289  }
290 
291  if (opt_display && opt_click_allowed)
292  {
293  std::cout << "Click on the image to read and display the last frame" << std::endl;
295  }
296 
297  reader.getFrame(I,reader.getLastFrameIndex());
298 
299  if (opt_display)
300  {
301  vpDisplay::display(I) ;
302  vpDisplay::flush(I);
303  }
304 
305  if (opt_display && opt_click_allowed)
306  {
307  std::cout << "Click to see the video" << std::endl;
309  }
310 
311  int lastFrame = reader.getLastFrameIndex();
312 
313  for (int i = opt_first; i <= lastFrame; i++)
314  {
315  reader.getFrame(I,i);
316  if (opt_display)
317  {
318  vpDisplay::display(I) ;
319  vpDisplay::flush(I);
320  }
321  }
322 
323  if (opt_display && opt_click_allowed)
324  {
325  std::cout << "Click to exit the test" << std::endl;
327  }
328 
329  return 0;
330  }
331  catch(vpException e) {
332  std::cout << "Catch an exception: " << e << std::endl;
333  return 1;
334  }
335 }
336 #else
337 int main()
338 {
339  std::cout << "Sorry, no display is available. We quit this example."
340  << std::endl;
341  return 0;
342 }
343 #endif
344 
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1071
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:132
long getLastFrameIndex() const
Define the X11 console to display images.
Definition: vpDisplayX.h:152
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:76
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2232
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:80
void open(vpImage< vpRGBa > &I)
bool getFrame(vpImage< vpRGBa > &I, long frame)
static std::string createFilePath(const std::string &parent, const std::string child)
Definition: vpIoTools.cpp:1245
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:210
The vpDisplayOpenCV allows to display image using the opencv library.
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:145
void setFileName(const char *filename)
void setFirstFrameIndex(const long first_frame)
virtual bool getClick(bool blocking=true)=0