Visual Servoing Platform  version 3.0.1
imageSequenceReader.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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 an image sequence.
32  *
33  * Authors:
34  * Eric Marchand
35  *
36  *****************************************************************************/
37 
48 #include <visp3/core/vpImage.h>
49 #include <visp3/io/vpImageIo.h>
50 #include <visp3/io/vpParseArgv.h>
51 #include <visp3/core/vpIoTools.h>
52 #include <visp3/core/vpDebug.h>
53 #include <visp3/io/vpVideoReader.h>
54 #include <visp3/gui/vpDisplayOpenCV.h>
55 #include <visp3/gui/vpDisplayX.h>
56 #include <visp3/gui/vpDisplayGTK.h>
57 #include <visp3/gui/vpDisplayGDI.h>
58 
59 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV) || defined(VISP_HAVE_GTK)
60 
61 // List of allowed command line options
62 #define GETOPTARGS "cdi:p:f:h"
63 
64 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath);
65 bool getOptions(int argc, const char **argv,
66  std::string &ipath, std::string &ppath, int &first, bool &click_allowed, bool &display);
67 
78 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath)
79 {
80  fprintf(stdout, "\n\
81 Read an image sequence on the disk.\n\
82 \n\
83 SYNOPSIS\n\
84  %s [-i <input images path>] [-p <personal image sequence path>]\n\
85  [-c][-d][-h]\n \
86 ", name);
87 
88  fprintf(stdout, "\n\
89 OPTIONS: Default\n\
90  -i <input images path> %s\n\
91  Set ViSP-images input path.\n\
92  From this path read \"ViSP-images/cube/image.%%04d.pgm\"\n\
93  images.\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 image sequence path> %s\n\
99  Specify a personal folder containing an image sequence \n\
100  to process.\n\
101  Example : \"/Temp/ViSP-images/cube/image.%%04d.pgm\"\n\
102  %%04d is for the image numbering.\n\
103 \n\
104  -f <index of the first frame> \n\
105  Specify the first image index.\n\
106 \n\
107  -c\n\
108  Disable the mouse click. Useful to automaze the \n\
109  execution of this program without humain intervention.\n\
110 \n\
111  -d \n\
112  Turn off the display.\n\
113 \n\
114  -h\n\
115  Print the help.\n\n",
116  ipath.c_str(), ppath.c_str());
117 
118  if (badparam) {
119  fprintf(stderr, "ERROR: \n" );
120  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
121  }
122 }
137 bool getOptions(int argc, const char **argv,
138  std::string &ipath, std::string &ppath, int &first, 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 'f': first = atoi(optarg_); break;
150  case 'h': usage(argv[0], NULL, ipath, ppath); return false; break;
151 
152  default:
153  usage(argv[0], optarg_, ipath, ppath); return false; break;
154  }
155  }
156 
157  if ((c == 1) || (c == -1)) {
158  // standalone param or error
159  usage(argv[0], NULL, ipath, ppath);
160  std::cerr << "ERROR: " << std::endl;
161  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
162  return false;
163  }
164 
165  return true;
166 }
167 
168 
169 
170 int
171 main(int argc, const char ** argv)
172 {
173  try {
174  std::string env_ipath;
175  std::string opt_ipath;
176  std::string ipath;
177  std::string opt_ppath;
178  std::string filename;
179  int opt_first = 1;
180  bool opt_click_allowed = true;
181  bool opt_display = true;
182 
183  std::cout << "-------------------------------------------------------" << std::endl ;
184  std::cout << " videoImageSequenceReader.cpp" <<std::endl << std::endl ;
185 
186  std::cout << " reading an image sequence" << std::endl ;
187  std::cout << "-------------------------------------------------------" << std::endl ;
188  std::cout << std::endl ;
189 
190  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH environment variable value
191  env_ipath = vpIoTools::getViSPImagesDataPath();
192 
193  // Set the default input path
194  if (! env_ipath.empty())
195  ipath = env_ipath;
196 
197  // Read the command line options
198  if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_first, opt_click_allowed,
199  opt_display) == false) {
200  exit (-1);
201  }
202 
203  // Get the option values
204  if (!opt_ipath.empty())
205  ipath = opt_ipath;
206 
207  // Compare ipath and env_ipath. If they differ, we take into account
208  // the input path comming from the command line option
209  if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
210  if (ipath != env_ipath) {
211  std::cout << std::endl
212  << "WARNING: " << std::endl;
213  std::cout << " Since -i <visp image path=" << ipath << "> "
214  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
215  << " we skip the environment variable." << std::endl;
216  }
217  }
218 
219  // Test if an input path is set
220  if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty()){
221  usage(argv[0], NULL, ipath, opt_ppath);
222  std::cerr << std::endl
223  << "ERROR:" << std::endl;
224  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
225  << std::endl
226  << " environment variable to specify the location of the " << std::endl
227  << " video path where test images are located." << std::endl << std::endl;
228  exit(-1);
229  }
230 
231 
233 
234 
235  // vpImage is a template class you can declare vpImage of ... everything...
236  vpImage<vpRGBa> I ;
237 
238  //Create the video Reader
239  vpVideoReader reader;
240 
241  if (opt_ppath.empty())
242  {
243  filename = vpIoTools::createFilePath(ipath, "ViSP-images/mire-2/image.%04d.pgm");
244  }
245  else
246  {
247  filename.assign(opt_ppath);
248  }
249 
250  //Initialize the reader and get the first frame.
251  reader.setFileName(filename);
252  reader.setFirstFrameIndex(opt_first);
253  reader.open(I);
254 
255  // We open a window using either X11, GTK, GDI or OpenCV.
256 #if defined VISP_HAVE_X11
257  vpDisplayX display;
258 #elif defined VISP_HAVE_GTK
259  vpDisplayGTK display;
260 #elif defined VISP_HAVE_GDI
261  vpDisplayGDI display;
262 #elif defined VISP_HAVE_OPENCV
263  vpDisplayOpenCV display;
264 #endif
265 
266  if (opt_display) {
267  // Display size is automatically defined by the image (I) size
268  display.init(I, 100, 100,"Display video frame") ;
269  vpDisplay::display(I) ;
270  vpDisplay::flush(I) ;
271  }
272 
273  if (opt_display && opt_click_allowed)
274  {
275  std::cout << "Click on the image to read and display the second frame" << std::endl;
277  }
278 
279  reader.getFrame(I,opt_first+1);
280 
281  if (opt_display)
282  {
283  vpDisplay::display(I) ;
284  vpDisplay::flush(I);
285  }
286 
287  if (opt_display && opt_click_allowed)
288  {
289  std::cout << "Click on the image to read and display the last frame" << std::endl;
291  }
292 
293  reader.getFrame(I,reader.getLastFrameIndex());
294 
295  if (opt_display)
296  {
297  vpDisplay::display(I) ;
298  vpDisplay::flush(I);
299  }
300 
301  if (opt_display && opt_click_allowed)
302  {
303  std::cout << "Click to see the video" << std::endl;
305  }
306 
307  int lastFrame = reader.getLastFrameIndex();
308 
309  for (int i = opt_first; i <= lastFrame; i++)
310  {
311  reader.getFrame(I,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 
325  return 0;
326  }
327  catch(vpException &e) {
328  std::cout << "Catch an exception: " << e << std::endl;
329  return 1;
330  }
331 }
332 #else
333 int main()
334 {
335  std::cout << "Sorry, no display is available. We quit this example."
336  << std::endl;
337  return 0;
338 }
339 #endif
340 
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1157
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:153
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
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
static void flush(const vpImage< unsigned char > &I)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
void open(vpImage< vpRGBa > &I)
bool getFrame(vpImage< vpRGBa > &I, long frame)
long getLastFrameIndex() const
static std::string createFilePath(const std::string &parent, const std::string child)
Definition: vpIoTools.cpp:1366
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:138
void setFileName(const char *filename)
void setFirstFrameIndex(const long first_frame)