ViSP
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-images-data package path or VISP_INPUT_IMAGE_PATH environment variable value
190  env_ipath = vpIoTools::getViSPImagesDataPath();
191 
192  // Set the default input path
193  if (! env_ipath.empty())
194  ipath = env_ipath;
195 
196  // Read the command line options
197  if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_click_allowed,
198  opt_display) == false) {
199  exit (-1);
200  }
201 
202  // Get the option values
203  if (!opt_ipath.empty())
204  ipath = opt_ipath;
205 
206  // Compare ipath and env_ipath. If they differ, we take into account
207  // the input path comming from the command line option
208  if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
209  if (ipath != env_ipath) {
210  std::cout << std::endl
211  << "WARNING: " << std::endl;
212  std::cout << " Since -i <visp image path=" << ipath << "> "
213  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
214  << " we skip the environment variable." << std::endl;
215  }
216  }
217 
218  // Test if an input path is set
219  if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty()){
220  usage(argv[0], NULL, ipath, opt_ppath);
221  std::cerr << std::endl
222  << "ERROR:" << std::endl;
223  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
224  << std::endl
225  << " environment variable to specify the location of the " << std::endl
226  << " video path where test images are located." << std::endl << std::endl;
227  exit(-1);
228  }
229 
230 
232 
233 
234  // vpImage is a template class you can declare vpImage of ... everything...
235  vpImage<vpRGBa> I ;
236 
237  //Create the video Reader
238  vpVideoReader reader;
239 
240  if (opt_ppath.empty())
241  {
242  filename = vpIoTools::createFilePath(ipath, "ViSP-images/video/cube.mpeg");
243  }
244  else
245  {
246  filename.assign(opt_ppath);
247  }
248 
249  //Initialize the reader and get the first frame.
250  std::cout << "Process video in " << filename << std::endl;
251  reader.setFileName(filename);
252  reader.open(I);
253 
254  // We open a window using either X11, GTK, GDI or OpenCV.
255 #if defined VISP_HAVE_X11
256  vpDisplayX display;
257 #elif defined VISP_HAVE_GTK
258  vpDisplayGTK display;
259 #elif defined VISP_HAVE_GDI
260  vpDisplayGDI display;
261 #elif defined VISP_HAVE_OPENCV
262  vpDisplayOpenCV display;
263 #endif
264 
265  if (opt_display) {
266  // Display size is automatically defined by the image (I) size
267  display.init(I, 100, 100,"Display video frame") ;
268  vpDisplay::display(I) ;
269  vpDisplay::flush(I) ;
270  }
271 
272  // if (opt_display && opt_click_allowed)
273  // {
274  // std::cout << "Click on the image to read and display the last key frame" << std::endl;
275  // vpDisplay::getClick(I);
276  // }
277  //
278  // reader.getFrame(I,reader.getLastFrameIndex());
279  //
280  // if (opt_display)
281  // {
282  // vpDisplay::display(I) ;
283  // vpDisplay::flush(I);
284  // }
285 
286  if (opt_display && opt_click_allowed)
287  {
288  std::cout << "Click to see the video" << std::endl;
290  }
291 
292  while (! reader.end() ) {
293  std::cout << "Read frame: " << reader.getFrameIndex() << std::endl;
294  reader.acquire(I);
295  if (opt_display)
296  {
297  vpDisplay::display(I) ;
298  vpDisplay::flush(I);
299  }
300  }
301 
302  if (opt_display && opt_click_allowed)
303  {
304  std::cout << "Click to exit this example" << std::endl;
306  }
307  }
308  catch(vpException e) {
309  std::cout << "Catch an exception: " << e << std::endl;
310  }
311  return 0;
312 }
313 #else
314 int main()
315 {
316  std::cout << "Sorry, no display is available. We quit this example."
317  << std::endl;
318  return 0;
319 }
320 #endif
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
long getFrameIndex() const
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1071
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:132
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)
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 acquire(vpImage< vpRGBa > &I)
void setFileName(const char *filename)
virtual bool getClick(bool blocking=true)=0