Visual Servoing Platform  version 3.6.1 under development (2024-05-09)
videoReader.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 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 https://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 *****************************************************************************/
35 
46 #include <visp3/core/vpConfig.h>
47 #include <visp3/core/vpDebug.h>
48 #include <visp3/core/vpImage.h>
49 #include <visp3/core/vpIoTools.h>
50 #include <visp3/gui/vpDisplayGDI.h>
51 #include <visp3/gui/vpDisplayGTK.h>
52 #include <visp3/gui/vpDisplayOpenCV.h>
53 #include <visp3/gui/vpDisplayX.h>
54 #include <visp3/io/vpImageIo.h>
55 #include <visp3/io/vpParseArgv.h>
56 #include <visp3/io/vpVideoReader.h>
57 
58 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV) || defined(VISP_HAVE_GTK)
59 
60 // List of allowed command line options
61 #define GETOPTARGS "cdi:p:h"
62 
63 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath);
64 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath, bool &click_allowed,
65  bool &display);
66 
77 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath)
78 {
79  fprintf(stdout, "\n\
80 Read a video file on the disk.\n\
81 \n\
82 SYNOPSIS\n\
83  %s [-i <input video path>] \n\
84  [-h]\n\
85 ",
86  name);
87 
88  fprintf(stdout, "\n\
89 OPTIONS: Default\n\
90  -i <input video path> %s\n\
91  Set video input path.\n\
92  From this path read \"video/cube.mpeg\"\n\
93  video.\n\
94  Setting the VISP_INPUT_IMAGE_PATH environment\n\
95  variable produces the same behaviour than using\n\
96  this option.\n\
97 \n\
98  -p <personal video path> %s\n\
99  Specify a personal folder containing a video \n\
100  to process.\n\
101  Example : \"/Temp/visp-images/video/video.mpeg\"\n\
102 \n\
103  -c\n\
104  Disable the mouse click. Useful to automate the \n\
105  execution of this program without human intervention.\n\
106 \n\
107  -d \n\
108  Turn off the display.\n\
109 \n\
110  -h\n\
111  Print the help.\n\n",
112  ipath.c_str(), ppath.c_str());
113 
114  if (badparam) {
115  fprintf(stderr, "ERROR: \n");
116  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
117  }
118 }
132 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath, bool &click_allowed, bool &display)
133 {
134  const char *optarg_;
135  int c;
136  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
137 
138  switch (c) {
139  case 'c':
140  click_allowed = false;
141  break;
142  case 'd':
143  display = false;
144  break;
145  case 'i':
146  ipath = optarg_;
147  break;
148  case 'p':
149  ppath = optarg_;
150  break;
151  case 'h':
152  usage(argv[0], nullptr, ipath, ppath);
153  return false;
154  break;
155 
156  default:
157  usage(argv[0], optarg_, ipath, ppath);
158  return false;
159  break;
160  }
161  }
162 
163  if ((c == 1) || (c == -1)) {
164  // standalone param or error
165  usage(argv[0], nullptr, ipath, ppath);
166  std::cerr << "ERROR: " << std::endl;
167  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
168  return false;
169  }
170 
171  return true;
172 }
173 
174 int main(int argc, const char **argv)
175 {
176  try {
177  std::string env_ipath;
178  std::string opt_ipath;
179  std::string ipath;
180  std::string opt_ppath;
181  std::string filename;
182  bool opt_click_allowed = true;
183  bool opt_display = true;
184 
185  std::cout << "-------------------------------------------------------" << std::endl;
186  std::cout << " videoReader.cpp" << std::endl << std::endl;
187 
188  std::cout << " reading a video file" << std::endl;
189  std::cout << "-------------------------------------------------------" << std::endl;
190  std::cout << std::endl;
191 
192  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
193  // environment variable value
194  env_ipath = vpIoTools::getViSPImagesDataPath();
195 
196  // Set the default input path
197  if (!env_ipath.empty())
198  ipath = env_ipath;
199 
200  // Read the command line options
201  if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_click_allowed, opt_display) == false) {
202  return EXIT_FAILURE;
203  }
204 
205  // Get the option values
206  if (!opt_ipath.empty())
207  ipath = opt_ipath;
208 
209  // Compare ipath and env_ipath. If they differ, we take into account
210  // the input path coming from the command line option
211  if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
212  if (ipath != env_ipath) {
213  std::cout << std::endl << "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], nullptr, ipath, opt_ppath);
223  std::cerr << std::endl << "ERROR:" << std::endl;
224  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
225  << " environment variable to specify the location of the " << std::endl
226  << " video path where test images are located." << std::endl
227  << std::endl;
228  return EXIT_FAILURE;
229  }
230 
232 
233  // vpImage is a template class you can declare vpImage of ...
234  // everything...
235  vpImage<vpRGBa> I;
236 
237  // Create the video Reader
238  vpVideoReader reader;
239 
240  if (opt_ppath.empty()) {
241  filename = vpIoTools::createFilePath(ipath, "video/cube.mpeg");
242  } else {
243  filename.assign(opt_ppath);
244  }
245 
246  // Initialize the reader and get the first frame.
247  std::cout << "Process video in " << filename << std::endl;
248  reader.setFileName(filename);
249  reader.open(I);
250 
251 // We open a window using either X11, GTK, GDI or OpenCV.
252 #if defined(VISP_HAVE_X11)
254 #elif defined(VISP_HAVE_GTK)
256 #elif defined(VISP_HAVE_GDI)
258 #elif defined(HAVE_OPENCV_HIGHGUI)
260 #endif
261 
262  if (opt_display) {
263  // Display size is automatically defined by the image (I) size
264  display.init(I, 100, 100, "Display video frame");
266  vpDisplay::flush(I);
267  }
268 
269  // if (opt_display && opt_click_allowed)
270  // {
271  // std::cout << "Click on the image to read and display the last key
272  // frame" << std::endl; vpDisplay::getClick(I);
273  // }
274  //
275  // reader.getFrame(I,reader.getLastFrameIndex());
276  //
277  // if (opt_display)
278  // {
279  // vpDisplay::display(I) ;
280  // vpDisplay::flush(I);
281  // }
282 
283  if (opt_display && opt_click_allowed) {
284  std::cout << "Click to see the video" << std::endl;
286  }
287 
288  while (!reader.end()) {
289  reader.acquire(I);
290  std::cout << "Display frame: " << reader.getFrameIndex() << std::endl;
291  if (opt_display) {
293  if (opt_click_allowed) {
294  vpDisplay::displayText(I, 15, 15, "A click to stop...", vpColor::red);
295 
296  if (vpDisplay::getClick(I, false)) {
297  break;
298  }
299  }
300  vpDisplay::flush(I);
301  }
302  }
303 
304  if (opt_display && opt_click_allowed) {
305  std::cout << "Click to exit this example" << std::endl;
307  }
308  } catch (const vpException &e) {
309  std::cout << "Catch an exception: " << e << std::endl;
310  }
311  return EXIT_SUCCESS;
312 }
313 #else
314 int main()
315 {
316  std::cout << "Sorry, no display is available. We quit this example." << std::endl;
317  return EXIT_SUCCESS;
318 }
319 #endif
static const vpColor red
Definition: vpColor.h:211
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:128
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:128
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
Definition: vpException.h:59
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1832
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:2195
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void acquire(vpImage< vpRGBa > &I)
void open(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
long getFrameIndex() const
void display(vpImage< unsigned char > &I, const std::string &title)
Display a gray-scale image.