ViSP  2.8.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 - 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 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 
79 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath)
80 {
81  fprintf(stdout, "\n\
82 Read a video file on the disk.\n\
83 \n\
84 SYNOPSIS\n\
85  %s [-i <input video path>] \n\
86  [-h]\n \
87 ", name);
88 
89  fprintf(stdout, "\n\
90 OPTIONS: Default\n\
91  -i <input video path> %s\n\
92  Set video input path.\n\
93  From this path read \"ViSP-images/video/video.mpeg\"\n\
94  video.\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 video path> %s\n\
100  Specify a personal folder containing a video \n\
101  to process.\n\
102  Example : \"/Temp/ViSP-images/video/video.mpeg\"\n\
103 \n\
104  -c\n\
105  Disable the mouse click. Useful to automaze the \n\
106  execution of this program without humain intervention.\n\
107 \n\
108  -d \n\
109  Turn off the display.\n\
110 \n\
111  -h\n\
112  Print the help.\n\n",
113  ipath.c_str(), ppath.c_str());
114 
115  if (badparam) {
116  fprintf(stderr, "ERROR: \n" );
117  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
118  }
119 }
133 bool getOptions(int argc, const char **argv,
134  std::string &ipath, std::string &ppath, bool &click_allowed, bool &display)
135 {
136  const char *optarg;
137  int c;
138  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
139 
140  switch (c) {
141  case 'c': click_allowed = false; break;
142  case 'd': display = false; break;
143  case 'i': ipath = optarg; break;
144  case 'p': ppath = optarg; break;
145  case 'h': usage(argv[0], NULL, ipath, ppath); return false; break;
146 
147  default:
148  usage(argv[0], optarg, ipath, ppath); return false; break;
149  }
150  }
151 
152  if ((c == 1) || (c == -1)) {
153  // standalone param or error
154  usage(argv[0], NULL, ipath, ppath);
155  std::cerr << "ERROR: " << std::endl;
156  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
157  return false;
158  }
159 
160  return true;
161 }
162 
163 
164 
165 int
166 main(int argc, const char ** argv)
167 {
168 
169  std::string env_ipath;
170  std::string opt_ipath;
171  std::string ipath;
172  std::string opt_ppath;
173  std::string filename;
174  bool opt_click_allowed = true;
175  bool opt_display = true;
176 
177  std::cout << "-------------------------------------------------------" << std::endl ;
178  std::cout << " videoReader.cpp" <<std::endl << std::endl ;
179 
180  std::cout << " reading a video file" << std::endl ;
181  std::cout << "-------------------------------------------------------" << std::endl ;
182  std::cout << std::endl ;
183 
184 
185  // Get the VISP_IMAGE_PATH environment variable value
186  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
187  if (ptenv != NULL)
188  env_ipath = ptenv;
189 
190  // Set the default input path
191  if (! env_ipath.empty())
192  ipath = env_ipath;
193 
194  // Read the command line options
195  if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_click_allowed,
196  opt_display) == false) {
197  exit (-1);
198  }
199 
200  // Get the option values
201  if (!opt_ipath.empty())
202  ipath = opt_ipath;
203 
204  // Compare ipath and env_ipath. If they differ, we take into account
205  // the input path comming from the command line option
206  if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
207  if (ipath != env_ipath) {
208  std::cout << std::endl
209  << "WARNING: " << std::endl;
210  std::cout << " Since -i <visp image path=" << ipath << "> "
211  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
212  << " we skip the environment variable." << std::endl;
213  }
214  }
215 
216  // Test if an input path is set
217  if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty()){
218  usage(argv[0], NULL, ipath, opt_ppath);
219  std::cerr << std::endl
220  << "ERROR:" << std::endl;
221  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
222  << std::endl
223  << " environment variable to specify the location of the " << std::endl
224  << " video path where test images are located." << std::endl << std::endl;
225  exit(-1);
226  }
227 
228 
230 
231 
232  // vpImage is a template class you can declare vpImage of ... everything...
233  vpImage<vpRGBa> I ;
234 
235  //Create the video Reader
236  vpVideoReader reader;
237 
238  if (opt_ppath.empty())
239  {
240  filename = ipath + vpIoTools::path("/ViSP-images/video/cube.mpeg");
241  }
242  else
243  {
244  filename.assign(opt_ppath);
245  }
246 
247  //Initialize the reader and get the first frame.
248  reader.setFileName(filename.c_str());
249  try
250  {
251  reader.open(I);
252  }
253  catch(...)
254  {
255  return 0;
256  }
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 last key frame" << std::endl;
288  // vpDisplay::getClick(I);
289  // }
290  //
291  // reader.getFrame(I,reader.getLastFrameIndex());
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 to see the video" << std::endl;
303  }
304 
305  int lastFrame = reader.getLastFrameIndex();
306  //To go to the beginning of the video
307  reader.getFrame(I,0);
308 
309  for (int i = 0; i <= lastFrame; i++)
310  {
311  reader.acquire(I);
312  if (opt_display)
313  {
314  vpDisplay::display(I) ;
315  vpDisplay::flush(I);
316  }
317  }
318 
319  if (opt_display && opt_click_allowed)
320  {
321  std::cout << "Click to exit the test" << std::endl;
323  }
324  return 0;
325 }
326 #else
327 int main()
328 {
329  std::cout << "Sorry, no display is available. We quit this example."
330  << std::endl;
331  return 0;
332 }
333 #endif
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 acquire(vpImage< vpRGBa > &I)
void setFileName(const char *filename)
virtual bool getClick(bool blocking=true)=0