Visual Servoing Platform  version 3.3.0
videoReader.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Reading a video file.
33  *
34  * Authors:
35  * Eric Marchand
36  *
37  *****************************************************************************/
38 
49 #include <visp3/core/vpConfig.h>
50 #include <visp3/core/vpDebug.h>
51 #include <visp3/core/vpImage.h>
52 #include <visp3/core/vpIoTools.h>
53 #include <visp3/gui/vpDisplayGDI.h>
54 #include <visp3/gui/vpDisplayGTK.h>
55 #include <visp3/gui/vpDisplayOpenCV.h>
56 #include <visp3/gui/vpDisplayX.h>
57 #include <visp3/io/vpImageIo.h>
58 #include <visp3/io/vpParseArgv.h>
59 #include <visp3/io/vpVideoReader.h>
60 
61 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV) || defined(VISP_HAVE_GTK)
62 
63 // List of allowed command line options
64 #define GETOPTARGS "cdi:p:h"
65 
66 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath);
67 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath, bool &click_allowed,
68  bool &display);
69 
80 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath)
81 {
82  fprintf(stdout, "\n\
83 Read a video file on the disk.\n\
84 \n\
85 SYNOPSIS\n\
86  %s [-i <input video path>] \n\
87  [-h]\n \
88 ", name);
89 
90  fprintf(stdout, "\n\
91 OPTIONS: Default\n\
92  -i <input video path> %s\n\
93  Set video input path.\n\
94  From this path read \"video/cube.mpeg\"\n\
95  video.\n\
96  Setting the VISP_INPUT_IMAGE_PATH environment\n\
97  variable produces the same behaviour than using\n\
98  this option.\n\
99 \n\
100  -p <personal video path> %s\n\
101  Specify a personal folder containing a video \n\
102  to process.\n\
103  Example : \"/Temp/ViSP-images/video/video.mpeg\"\n\
104 \n\
105  -c\n\
106  Disable the mouse click. Useful to automaze the \n\
107  execution of this program without humain intervention.\n\
108 \n\
109  -d \n\
110  Turn off the display.\n\
111 \n\
112  -h\n\
113  Print the help.\n\n", 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, bool &click_allowed, bool &display)
134 {
135  const char *optarg_;
136  int c;
137  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
138 
139  switch (c) {
140  case 'c':
141  click_allowed = false;
142  break;
143  case 'd':
144  display = false;
145  break;
146  case 'i':
147  ipath = optarg_;
148  break;
149  case 'p':
150  ppath = optarg_;
151  break;
152  case 'h':
153  usage(argv[0], NULL, ipath, ppath);
154  return false;
155  break;
156 
157  default:
158  usage(argv[0], optarg_, ipath, ppath);
159  return false;
160  break;
161  }
162  }
163 
164  if ((c == 1) || (c == -1)) {
165  // standalone param or error
166  usage(argv[0], NULL, ipath, ppath);
167  std::cerr << "ERROR: " << std::endl;
168  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
169  return false;
170  }
171 
172  return true;
173 }
174 
175 int main(int argc, const char **argv)
176 {
177  try {
178  std::string env_ipath;
179  std::string opt_ipath;
180  std::string ipath;
181  std::string opt_ppath;
182  std::string filename;
183  bool opt_click_allowed = true;
184  bool opt_display = true;
185 
186  std::cout << "-------------------------------------------------------" << std::endl;
187  std::cout << " videoReader.cpp" << std::endl << std::endl;
188 
189  std::cout << " reading a video file" << std::endl;
190  std::cout << "-------------------------------------------------------" << std::endl;
191  std::cout << std::endl;
192 
193  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
194  // environment variable value
195  env_ipath = vpIoTools::getViSPImagesDataPath();
196 
197  // Set the default input path
198  if (!env_ipath.empty())
199  ipath = env_ipath;
200 
201  // Read the command line options
202  if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_click_allowed, 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 << "WARNING: " << std::endl;
215  std::cout << " Since -i <visp image path=" << ipath << "> "
216  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
217  << " we skip the environment variable." << std::endl;
218  }
219  }
220 
221  // Test if an input path is set
222  if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty()) {
223  usage(argv[0], NULL, ipath, opt_ppath);
224  std::cerr << std::endl << "ERROR:" << std::endl;
225  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
226  << " environment variable to specify the location of the " << std::endl
227  << " video path where test images are located." << std::endl
228  << std::endl;
229  exit(-1);
230  }
231 
233 
234  // vpImage is a template class you can declare vpImage of ...
235  // everything...
236  vpImage<vpRGBa> I;
237 
238  // Create the video Reader
239  vpVideoReader reader;
240 
241  if (opt_ppath.empty()) {
242  filename = vpIoTools::createFilePath(ipath, "video/cube.mpeg");
243  } else {
244  filename.assign(opt_ppath);
245  }
246 
247  // Initialize the reader and get the first frame.
248  std::cout << "Process video in " << filename << std::endl;
249  reader.setFileName(filename);
250  reader.open(I);
251 
252 // We open a window using either X11, GTK, GDI or OpenCV.
253 #if defined VISP_HAVE_X11
254  vpDisplayX display;
255 #elif defined VISP_HAVE_GTK
256  vpDisplayGTK display;
257 #elif defined VISP_HAVE_GDI
258  vpDisplayGDI display;
259 #elif defined VISP_HAVE_OPENCV
260  vpDisplayOpenCV display;
261 #endif
262 
263  if (opt_display) {
264  // Display size is automatically defined by the image (I) size
265  display.init(I, 100, 100, "Display video frame");
267  vpDisplay::flush(I);
268  }
269 
270  // if (opt_display && opt_click_allowed)
271  // {
272  // std::cout << "Click on the image to read and display the last key
273  // frame" << std::endl; vpDisplay::getClick(I);
274  // }
275  //
276  // reader.getFrame(I,reader.getLastFrameIndex());
277  //
278  // if (opt_display)
279  // {
280  // vpDisplay::display(I) ;
281  // vpDisplay::flush(I);
282  // }
283 
284  if (opt_display && opt_click_allowed) {
285  std::cout << "Click to see the video" << std::endl;
287  }
288 
289  while (!reader.end()) {
290  reader.acquire(I);
291  std::cout << "Display frame: " << reader.getFrameIndex() << std::endl;
292  if (opt_display) {
294  if (opt_click_allowed) {
295  vpDisplay::displayText(I, 15, 15, "A click to stop...", vpColor::red);
296 
297  if (vpDisplay::getClick(I, false)) {
298  break;
299  }
300  }
301  vpDisplay::flush(I);
302  }
303  }
304 
305  if (opt_display && opt_click_allowed) {
306  std::cout << "Click to exit this example" << std::endl;
308  }
309  } catch (const vpException &e) {
310  std::cout << "Catch an exception: " << e << std::endl;
311  }
312  return EXIT_SUCCESS;
313 }
314 #else
315 int main()
316 {
317  std::cout << "Sorry, no display is available. We quit this example." << std::endl;
318  return EXIT_SUCCESS;
319 }
320 #endif
vpDisplayX
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:151
vpIoTools::getViSPImagesDataPath
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1292
vpVideoReader::end
bool end()
Definition: vpVideoReader.h:260
vpDisplayGDI
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
vpVideoReader::getFrameIndex
long getFrameIndex() const
Definition: vpVideoReader.h:295
vpDisplayOpenCV
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Definition: vpDisplayOpenCV.h:142
vpVideoReader::open
void open(vpImage< vpRGBa > &I)
Definition: vpVideoReader.cpp:175
vpParseArgv::parse
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
vpVideoReader
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
Definition: vpVideoReader.h:172
vpVideoReader::setFileName
void setFileName(const std::string &filename)
Definition: vpVideoReader.cpp:92
vpDisplay::display
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay_uchar.cpp:740
vpDisplayGTK
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:138
vpDisplay::displayText
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Definition: vpDisplay_uchar.cpp:664
vpIoTools::createFilePath
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1537
vpDisplay::flush
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay_uchar.cpp:716
vpImage< vpRGBa >
vpColor::red
static const vpColor red
Definition: vpColor.h:179
vpDisplay::getClick
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
Definition: vpDisplay_uchar.cpp:765
vpException
error that can be emited by ViSP classes.
Definition: vpException.h:72
vpVideoReader::acquire
void acquire(vpImage< vpRGBa > &I)
Definition: vpVideoReader.cpp:244