ViSP  3.0.0
videoReader.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Reading a video file.
32  *
33  * Authors:
34  * Eric Marchand
35  *
36  *****************************************************************************/
37 
48 #include <visp3/core/vpConfig.h>
49 #include <visp3/core/vpImage.h>
50 #include <visp3/io/vpImageIo.h>
51 #include <visp3/io/vpParseArgv.h>
52 #include <visp3/core/vpIoTools.h>
53 #include <visp3/core/vpDebug.h>
54 #include <visp3/io/vpVideoReader.h>
55 #include <visp3/gui/vpDisplayOpenCV.h>
56 #include <visp3/gui/vpDisplayX.h>
57 #include <visp3/gui/vpDisplayGTK.h>
58 #include <visp3/gui/vpDisplayGDI.h>
59 
60 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV) || defined(VISP_HAVE_GTK)
61 
62 // List of allowed command line options
63 #define GETOPTARGS "cdi:p:h"
64 
65 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath);
66 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath,
67  bool &click_allowed, bool &display);
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, std::string &ipath, std::string &ppath,
134  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  try {
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-images-data package path or VISP_INPUT_IMAGE_PATH environment variable value
186  env_ipath = vpIoTools::getViSPImagesDataPath();
187 
188  // Set the default input path
189  if (! env_ipath.empty())
190  ipath = env_ipath;
191 
192  // Read the command line options
193  if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_click_allowed,
194  opt_display) == false) {
195  exit (-1);
196  }
197 
198  // Get the option values
199  if (!opt_ipath.empty())
200  ipath = opt_ipath;
201 
202  // Compare ipath and env_ipath. If they differ, we take into account
203  // the input path comming from the command line option
204  if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
205  if (ipath != env_ipath) {
206  std::cout << std::endl
207  << "WARNING: " << std::endl;
208  std::cout << " Since -i <visp image path=" << ipath << "> "
209  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
210  << " we skip the environment variable." << std::endl;
211  }
212  }
213 
214  // Test if an input path is set
215  if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty()){
216  usage(argv[0], NULL, ipath, opt_ppath);
217  std::cerr << std::endl
218  << "ERROR:" << std::endl;
219  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
220  << std::endl
221  << " environment variable to specify the location of the " << std::endl
222  << " video path where test images are located." << std::endl << std::endl;
223  exit(-1);
224  }
225 
226 
228 
229 
230  // vpImage is a template class you can declare vpImage of ... everything...
231  vpImage<vpRGBa> I ;
232 
233  //Create the video Reader
234  vpVideoReader reader;
235 
236  if (opt_ppath.empty())
237  {
238  filename = vpIoTools::createFilePath(ipath, "ViSP-images/video/cube.mpeg");
239  }
240  else
241  {
242  filename.assign(opt_ppath);
243  }
244 
245  //Initialize the reader and get the first frame.
246  std::cout << "Process video in " << filename << std::endl;
247  reader.setFileName(filename);
248  reader.open(I);
249 
250  // We open a window using either X11, GTK, GDI or OpenCV.
251 #if defined VISP_HAVE_X11
252  vpDisplayX display;
253 #elif defined VISP_HAVE_GTK
254  vpDisplayGTK display;
255 #elif defined VISP_HAVE_GDI
256  vpDisplayGDI display;
257 #elif defined VISP_HAVE_OPENCV
258  vpDisplayOpenCV display;
259 #endif
260 
261  if (opt_display) {
262  // Display size is automatically defined by the image (I) size
263  display.init(I, 100, 100,"Display video frame") ;
264  vpDisplay::display(I) ;
265  vpDisplay::flush(I) ;
266  }
267 
268  // if (opt_display && opt_click_allowed)
269  // {
270  // std::cout << "Click on the image to read and display the last key frame" << std::endl;
271  // vpDisplay::getClick(I);
272  // }
273  //
274  // reader.getFrame(I,reader.getLastFrameIndex());
275  //
276  // if (opt_display)
277  // {
278  // vpDisplay::display(I) ;
279  // vpDisplay::flush(I);
280  // }
281 
282  if (opt_display && opt_click_allowed)
283  {
284  std::cout << "Click to see the video" << std::endl;
286  }
287 
288  while (! reader.end() ) {
289  std::cout << "Read frame: " << reader.getFrameIndex() << std::endl;
290  reader.acquire(I);
291  if (opt_display)
292  {
293  vpDisplay::display(I) ;
294  vpDisplay::flush(I);
295  }
296  }
297 
298  if (opt_display && opt_click_allowed)
299  {
300  std::cout << "Click to exit this example" << std::endl;
302  }
303  }
304  catch(vpException e) {
305  std::cout << "Catch an exception: " << e << std::endl;
306  }
307  return 0;
308 }
309 #else
310 int main()
311 {
312  std::cout << "Sorry, no display is available. We quit this example."
313  << std::endl;
314  return 0;
315 }
316 #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:1091
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
Define the X11 console to display images.
Definition: vpDisplayX.h:148
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:73
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2233
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
void open(vpImage< vpRGBa > &I)
static std::string createFilePath(const std::string &parent, const std::string child)
Definition: vpIoTools.cpp:1265
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:141
void acquire(vpImage< vpRGBa > &I)
void setFileName(const char *filename)
virtual bool getClick(bool blocking=true)=0