ViSP  2.8.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 - 2013 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 
78 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath)
79 {
80  fprintf(stdout, "\n\
81 Read an image sequence on the disk.\n\
82 \n\
83 SYNOPSIS\n\
84  %s [-i <input images path>] [-p <personal image sequence path>]\n\
85  [-c][-d][-h]\n \
86 ", name);
87 
88  fprintf(stdout, "\n\
89 OPTIONS: Default\n\
90  -i <input images path> %s\n\
91  Set ViSP-images input path.\n\
92  From this path read \"ViSP-images/cube/image.%%04d.pgm\"\n\
93  images.\n\
94  Setting the VISP_INPUT_IMAGE_PATH environment\n\
95  variable produces the same behaviour than using\n\
96  this option.\n\
97 \n\
98  -p <personal image sequence path> %s\n\
99  Specify a personal folder containing an image sequence \n\
100  to process.\n\
101  Example : \"/Temp/ViSP-images/cube/image.%%04d.pgm\"\n\
102  %%04d is for the image numbering.\n\
103 \n\
104  -f <index of the first frame> \n\
105  Specify the first image index.\n\
106 \n\
107  -c\n\
108  Disable the mouse click. Useful to automaze the \n\
109  execution of this program without humain intervention.\n\
110 \n\
111  -d \n\
112  Turn off the display.\n\
113 \n\
114  -h\n\
115  Print the help.\n\n",
116  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,
138  std::string &ipath, std::string &ppath, int &first, bool &click_allowed, 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': click_allowed = false; break;
146  case 'd': display = false; break;
147  case 'i': ipath = optarg; break;
148  case 'p': ppath = optarg; break;
149  case 'f': first = atoi(optarg); break;
150  case 'h': usage(argv[0], NULL, ipath, ppath); return false; break;
151 
152  default:
153  usage(argv[0], optarg, ipath, ppath); return false; break;
154  }
155  }
156 
157  if ((c == 1) || (c == -1)) {
158  // standalone param or error
159  usage(argv[0], NULL, ipath, ppath);
160  std::cerr << "ERROR: " << std::endl;
161  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
162  return false;
163  }
164 
165  return true;
166 }
167 
168 
169 
170 int
171 main(int argc, const char ** argv)
172 {
173 
174  std::string env_ipath;
175  std::string opt_ipath;
176  std::string ipath;
177  std::string opt_ppath;
178  std::string filename;
179  int opt_first = 1;
180  bool opt_click_allowed = true;
181  bool opt_display = true;
182 
183  std::cout << "-------------------------------------------------------" << std::endl ;
184  std::cout << " videoImageSequenceReader.cpp" <<std::endl << std::endl ;
185 
186  std::cout << " reading an image sequence" << std::endl ;
187  std::cout << "-------------------------------------------------------" << std::endl ;
188  std::cout << std::endl ;
189 
190 
191  // Get the VISP_IMAGE_PATH environment variable value
192  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
193  if (ptenv != NULL)
194  env_ipath = ptenv;
195 
196  // Set the default input path
197  if (! env_ipath.empty())
198  ipath = env_ipath;
199 
200  // Read the command line options
201  if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_first, opt_click_allowed,
202  opt_display) == false) {
203  exit (-1);
204  }
205 
206  // Get the option values
207  if (!opt_ipath.empty())
208  ipath = opt_ipath;
209 
210  // Compare ipath and env_ipath. If they differ, we take into account
211  // the input path comming from the command line option
212  if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
213  if (ipath != env_ipath) {
214  std::cout << std::endl
215  << "WARNING: " << std::endl;
216  std::cout << " Since -i <visp image path=" << ipath << "> "
217  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
218  << " we skip the environment variable." << std::endl;
219  }
220  }
221 
222  // Test if an input path is set
223  if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty()){
224  usage(argv[0], NULL, ipath, opt_ppath);
225  std::cerr << std::endl
226  << "ERROR:" << std::endl;
227  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
228  << std::endl
229  << " environment variable to specify the location of the " << std::endl
230  << " video path where test images are located." << std::endl << std::endl;
231  exit(-1);
232  }
233 
234 
236 
237 
238  // vpImage is a template class you can declare vpImage of ... everything...
239  vpImage<vpRGBa> I ;
240 
241  //Create the video Reader
242  vpVideoReader reader;
243 
244  if (opt_ppath.empty())
245  {
246  filename = ipath + vpIoTools::path("/ViSP-images/mire-2/image.%04d.pgm");
247  }
248  else
249  {
250  filename.assign(opt_ppath);
251  }
252 
253  //Initialize the reader and get the first frame.
254  reader.setFileName(filename.c_str());
255  reader.setFirstFrameIndex(opt_first);
256  reader.open(I);
257 
258  // We open a window using either X11, GTK, GDI or OpenCV.
259 #if defined VISP_HAVE_X11
260  vpDisplayX display;
261 #elif defined VISP_HAVE_GTK
262  vpDisplayGTK display;
263 #elif defined VISP_HAVE_GDI
264  vpDisplayGDI display;
265 #elif defined VISP_HAVE_OPENCV
266  vpDisplayOpenCV display;
267 #endif
268 
269  if (opt_display)
270  {
271  try
272  {
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  catch(...)
279  {
280  vpERROR_TRACE("Error while displaying the image") ;
281  exit(-1);
282  }
283  }
284 
285  if (opt_display && opt_click_allowed)
286  {
287  std::cout << "Click on the image to read and display the second frame" << std::endl;
289  }
290 
291  reader.getFrame(I,opt_first+1);
292 
293  if (opt_display)
294  {
295  vpDisplay::display(I) ;
296  vpDisplay::flush(I);
297  }
298 
299  if (opt_display && opt_click_allowed)
300  {
301  std::cout << "Click on the image to read and display the last frame" << std::endl;
303  }
304 
305  reader.getFrame(I,reader.getLastFrameIndex());
306 
307  if (opt_display)
308  {
309  vpDisplay::display(I) ;
310  vpDisplay::flush(I);
311  }
312 
313  if (opt_display && opt_click_allowed)
314  {
315  std::cout << "Click to see the video" << std::endl;
317  }
318 
319  int lastFrame = reader.getLastFrameIndex();
320 
321  for (int i = opt_first; i <= lastFrame; i++)
322  {
323  reader.getFrame(I,i);
324  if (opt_display)
325  {
326  vpDisplay::display(I) ;
327  vpDisplay::flush(I);
328  }
329  }
330 
331  if (opt_display && opt_click_allowed)
332  {
333  std::cout << "Click to exit the test" << std::endl;
335  }
336 
337  return 0;
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 setFirstFrameIndex(const long firstFrame)
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
#define vpERROR_TRACE
Definition: vpDebug.h:379
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:133
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...
static std::string path(const char *pathname)
Definition: vpIoTools.cpp:715
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1991
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:203
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)
virtual bool getClick(bool blocking=true)=0