ViSP  2.9.0
videoReader.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 a video file.
36  *
37  * Authors:
38  * Eric Marchand
39  *
40  *****************************************************************************/
41 
52 #include <visp/vpConfig.h>
53 #include <visp/vpImage.h>
54 #include <visp/vpImageIo.h>
55 #include <visp/vpParseArgv.h>
56 #include <visp/vpIoTools.h>
57 #include <visp/vpDebug.h>
58 #include <visp/vpVideoReader.h>
59 #include <visp/vpDisplayOpenCV.h>
60 #include <visp/vpDisplayX.h>
61 #include <visp/vpDisplayGTK.h>
62 #include <visp/vpDisplayGDI.h>
63 
64 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV) || defined(VISP_HAVE_GTK)
65 
66 // List of allowed command line options
67 #define GETOPTARGS "cdi:p:h"
68 
69 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath);
70 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath,
71  bool &click_allowed, bool &display);
72 
83 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath)
84 {
85  fprintf(stdout, "\n\
86 Read a video file on the disk.\n\
87 \n\
88 SYNOPSIS\n\
89  %s [-i <input video path>] \n\
90  [-h]\n \
91 ", name);
92 
93  fprintf(stdout, "\n\
94 OPTIONS: Default\n\
95  -i <input video path> %s\n\
96  Set video input path.\n\
97  From this path read \"ViSP-images/video/video.mpeg\"\n\
98  video.\n\
99  Setting the VISP_INPUT_IMAGE_PATH environment\n\
100  variable produces the same behaviour than using\n\
101  this option.\n\
102 \n\
103  -p <personal video path> %s\n\
104  Specify a personal folder containing a video \n\
105  to process.\n\
106  Example : \"/Temp/ViSP-images/video/video.mpeg\"\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",
117  ipath.c_str(), ppath.c_str());
118 
119  if (badparam) {
120  fprintf(stderr, "ERROR: \n" );
121  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
122  }
123 }
137 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath,
138  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 'h': usage(argv[0], NULL, ipath, ppath); return false; break;
150 
151  default:
152  usage(argv[0], optarg_, ipath, ppath); return false; break;
153  }
154  }
155 
156  if ((c == 1) || (c == -1)) {
157  // standalone param or error
158  usage(argv[0], NULL, ipath, ppath);
159  std::cerr << "ERROR: " << std::endl;
160  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
161  return false;
162  }
163 
164  return true;
165 }
166 
167 
168 
169 int
170 main(int argc, const char ** argv)
171 {
172  try {
173  std::string env_ipath;
174  std::string opt_ipath;
175  std::string ipath;
176  std::string opt_ppath;
177  std::string filename;
178  bool opt_click_allowed = true;
179  bool opt_display = true;
180 
181  std::cout << "-------------------------------------------------------" << std::endl ;
182  std::cout << " videoReader.cpp" <<std::endl << std::endl ;
183 
184  std::cout << " reading a video file" << std::endl ;
185  std::cout << "-------------------------------------------------------" << std::endl ;
186  std::cout << std::endl ;
187 
188 
189  // Get the VISP_IMAGE_PATH environment variable value
190  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
191  if (ptenv != NULL)
192  env_ipath = ptenv;
193 
194  // Set the default input path
195  if (! env_ipath.empty())
196  ipath = env_ipath;
197 
198  // Read the command line options
199  if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_click_allowed,
200  opt_display) == false) {
201  exit (-1);
202  }
203 
204  // Get the option values
205  if (!opt_ipath.empty())
206  ipath = opt_ipath;
207 
208  // Compare ipath and env_ipath. If they differ, we take into account
209  // the input path comming from the command line option
210  if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
211  if (ipath != env_ipath) {
212  std::cout << std::endl
213  << "WARNING: " << std::endl;
214  std::cout << " Since -i <visp image path=" << ipath << "> "
215  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
216  << " we skip the environment variable." << std::endl;
217  }
218  }
219 
220  // Test if an input path is set
221  if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty()){
222  usage(argv[0], NULL, ipath, opt_ppath);
223  std::cerr << std::endl
224  << "ERROR:" << std::endl;
225  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
226  << std::endl
227  << " environment variable to specify the location of the " << std::endl
228  << " video path where test images are located." << std::endl << std::endl;
229  exit(-1);
230  }
231 
232 
234 
235 
236  // vpImage is a template class you can declare vpImage of ... everything...
237  vpImage<vpRGBa> I ;
238 
239  //Create the video Reader
240  vpVideoReader reader;
241 
242  if (opt_ppath.empty())
243  {
244  filename = ipath + vpIoTools::path("/ViSP-images/video/cube.mpeg");
245  }
246  else
247  {
248  filename.assign(opt_ppath);
249  }
250 
251  //Initialize the reader and get the first frame.
252  reader.setFileName(filename.c_str());
253  reader.open(I);
254 
255  // We open a window using either X11, GTK, GDI or OpenCV.
256 #if defined VISP_HAVE_X11
257  vpDisplayX display;
258 #elif defined VISP_HAVE_GTK
259  vpDisplayGTK display;
260 #elif defined VISP_HAVE_GDI
261  vpDisplayGDI display;
262 #elif defined VISP_HAVE_OPENCV
263  vpDisplayOpenCV display;
264 #endif
265 
266  if (opt_display) {
267  // Display size is automatically defined by the image (I) size
268  display.init(I, 100, 100,"Display video frame") ;
269  vpDisplay::display(I) ;
270  vpDisplay::flush(I) ;
271  }
272 
273  // if (opt_display && opt_click_allowed)
274  // {
275  // std::cout << "Click on the image to read and display the last key frame" << std::endl;
276  // vpDisplay::getClick(I);
277  // }
278  //
279  // reader.getFrame(I,reader.getLastFrameIndex());
280  //
281  // if (opt_display)
282  // {
283  // vpDisplay::display(I) ;
284  // vpDisplay::flush(I);
285  // }
286 
287  if (opt_display && opt_click_allowed)
288  {
289  std::cout << "Click to see the video" << std::endl;
291  }
292 
293  int lastFrame = reader.getLastFrameIndex();
294  //To go to the beginning of the video
295  reader.getFrame(I,0);
296 
297  for (int i = 0; i <= lastFrame; i++)
298  {
299  reader.acquire(I);
300  if (opt_display)
301  {
302  vpDisplay::display(I) ;
303  vpDisplay::flush(I);
304  }
305  }
306 
307  if (opt_display && opt_click_allowed)
308  {
309  std::cout << "Click to exit the test" << std::endl;
311  }
312  }
313  catch(vpException e) {
314  std::cout << "Catch an exception: " << e << std::endl;
315  }
316  return 0;
317 }
318 #else
319 int main()
320 {
321  std::cout << "Sorry, no display is available. We quit this example."
322  << std::endl;
323  return 0;
324 }
325 #endif
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 acquire(vpImage< vpRGBa > &I)
void setFileName(const char *filename)
virtual bool getClick(bool blocking=true)=0