ViSP  2.9.0
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_IMAGE_PATH environment variable value
195  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
196  if (ptenv != NULL)
197  env_ipath = ptenv;
198 
199  // Set the default input path
200  if (! env_ipath.empty())
201  ipath = env_ipath;
202 
203  // Read the command line options
204  if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_first, opt_click_allowed,
205  opt_display) == false) {
206  exit (-1);
207  }
208 
209  // Get the option values
210  if (!opt_ipath.empty())
211  ipath = opt_ipath;
212 
213  // Compare ipath and env_ipath. If they differ, we take into account
214  // the input path comming from the command line option
215  if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
216  if (ipath != env_ipath) {
217  std::cout << std::endl
218  << "WARNING: " << std::endl;
219  std::cout << " Since -i <visp image path=" << ipath << "> "
220  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
221  << " we skip the environment variable." << std::endl;
222  }
223  }
224 
225  // Test if an input path is set
226  if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty()){
227  usage(argv[0], NULL, ipath, opt_ppath);
228  std::cerr << std::endl
229  << "ERROR:" << std::endl;
230  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
231  << std::endl
232  << " environment variable to specify the location of the " << std::endl
233  << " video path where test images are located." << std::endl << std::endl;
234  exit(-1);
235  }
236 
237 
239 
240 
241  // vpImage is a template class you can declare vpImage of ... everything...
242  vpImage<vpRGBa> I ;
243 
244  //Create the video Reader
245  vpVideoReader reader;
246 
247  if (opt_ppath.empty())
248  {
249  filename = ipath + vpIoTools::path("/ViSP-images/mire-2/image.%04d.pgm");
250  }
251  else
252  {
253  filename.assign(opt_ppath);
254  }
255 
256  //Initialize the reader and get the first frame.
257  reader.setFileName(filename.c_str());
258  reader.setFirstFrameIndex(opt_first);
259  reader.open(I);
260 
261  // We open a window using either X11, GTK, GDI or OpenCV.
262 #if defined VISP_HAVE_X11
263  vpDisplayX display;
264 #elif defined VISP_HAVE_GTK
265  vpDisplayGTK display;
266 #elif defined VISP_HAVE_GDI
267  vpDisplayGDI display;
268 #elif defined VISP_HAVE_OPENCV
269  vpDisplayOpenCV display;
270 #endif
271 
272  if (opt_display) {
273  // Display size is automatically defined by the image (I) size
274  display.init(I, 100, 100,"Display video frame") ;
275  vpDisplay::display(I) ;
276  vpDisplay::flush(I) ;
277 
278  }
279 
280  if (opt_display && opt_click_allowed)
281  {
282  std::cout << "Click on the image to read and display the second frame" << std::endl;
284  }
285 
286  reader.getFrame(I,opt_first+1);
287 
288  if (opt_display)
289  {
290  vpDisplay::display(I) ;
291  vpDisplay::flush(I);
292  }
293 
294  if (opt_display && opt_click_allowed)
295  {
296  std::cout << "Click on the image to read and display the last frame" << std::endl;
298  }
299 
300  reader.getFrame(I,reader.getLastFrameIndex());
301 
302  if (opt_display)
303  {
304  vpDisplay::display(I) ;
305  vpDisplay::flush(I);
306  }
307 
308  if (opt_display && opt_click_allowed)
309  {
310  std::cout << "Click to see the video" << std::endl;
312  }
313 
314  int lastFrame = reader.getLastFrameIndex();
315 
316  for (int i = opt_first; i <= lastFrame; i++)
317  {
318  reader.getFrame(I,i);
319  if (opt_display)
320  {
321  vpDisplay::display(I) ;
322  vpDisplay::flush(I);
323  }
324  }
325 
326  if (opt_display && opt_click_allowed)
327  {
328  std::cout << "Click to exit the test" << std::endl;
330  }
331 
332  return 0;
333  }
334  catch(vpException e) {
335  std::cout << "Catch an exception: " << e << std::endl;
336  return 1;
337  }
338 }
339 #else
340 int main()
341 {
342  std::cout << "Sorry, no display is available. We quit this example."
343  << std::endl;
344  return 0;
345 }
346 #endif
347 
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
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 std::string path(const char *pathname)
Definition: vpIoTools.cpp:715
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1994
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
void open(vpImage< vpRGBa > &I)
bool getFrame(vpImage< vpRGBa > &I, long frame)
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
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