Visual Servoing Platform  version 3.0.1
grabDirectShow.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  * Acquire images using DirectShow (under Windows only) and display it
32  * using GTK or GDI.
33  *
34  * Authors:
35  * Bruno Renier
36  * Fabien Spindler
37  *
38  *****************************************************************************/
39 
40 #include <visp3/core/vpConfig.h>
41 #include <visp3/core/vpDebug.h>
42 
50 #if defined (VISP_HAVE_DIRECTSHOW)
51 #if (defined (VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
52 
53 
54 #include <visp3/sensor/vpDirectShowGrabber.h>
55 #include <visp3/core/vpImage.h>
56 #include <visp3/io/vpImageIo.h>
57 #include <visp3/gui/vpDisplayGTK.h>
58 #include <visp3/gui/vpDisplayGDI.h>
59 #include <visp3/io/vpParseArgv.h>
60 #include <visp3/core/vpTime.h>
61 
62 // List of allowed command line options
63 #define GETOPTARGS "dhn:o:"
64 
75 void usage(const char *name, const char *badparam, unsigned &nframes, std::string &opath)
76 {
77  fprintf(stdout, "\n\
78 Acquire images using DirectShow (under Windows only) and display\n\
79 it using GTK or the windows GDI if GTK is not available.\n\
80 \n\
81 SYNOPSIS\n\
82  %s [-d] [-n] [-o] [-h] \n", name);
83 
84  fprintf(stdout, "\n\
85 OPTIONS: Default\n\
86  -d \n\
87  Turn off the display.\n\
88 \n\
89  -n [%%u] %u\n\
90  Number of frames to acquire. \n\
91 \n\
92  -o [%%s] \n\
93  Filename for image saving. \n\
94  Example: -o %s\n\
95  The %%d is for the image numbering.\n\
96 \n\
97  -h \n\
98  Print the help.\n\
99 \n", nframes, opath.c_str());
100  if (badparam) {
101  fprintf(stderr, "ERROR: \n" );
102  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
103  }
104 }
121 bool getOptions(int argc, const char **argv, bool &display,
122  unsigned &nframes, bool &save, std::string &opath)
123 {
124  const char *optarg;
125  int c;
126  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
127 
128  switch (c) {
129  case 'd': display = false; break;
130  case 'n':
131  nframes = atoi(optarg); break;
132  case 'o':
133  save = true;
134  opath = optarg; break;
135  case 'h': usage(argv[0], NULL, nframes, opath); return false; break;
136 
137  default:
138  usage(argv[0], optarg, nframes, opath);
139  return false; break;
140  }
141  }
142 
143  if ((c == 1) || (c == -1)) {
144  // standalone param or error
145  usage(argv[0], NULL, nframes, opath);
146  std::cerr << "ERROR: " << std::endl;
147  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
148  return false;
149  }
150 
151  return true;
152 }
153 
154 
163 int
164 main(int argc, const char ** argv)
165 {
166  try {
167  bool opt_display = true;
168  unsigned nframes = 50;
169  bool save = false;
170 
171  // Declare an image. It size is not defined yet. It will be defined when the
172  // image will acquired the first time.
173 #ifdef GRAB_COLOR
174  vpImage<vpRGBa> I; // This is a color image (in RGBa format)
175 #else
176  vpImage<unsigned char> I; // This is a B&W image
177 #endif
178 
179  // Set default output image name for saving
180 #ifdef GRAB_COLOR
181  // Color images will be saved in PGM P6 format
182  std::string opath = "C:/temp/I%04d.ppm";
183 #else
184  // B&W images will be saved in PGM P5 format
185  std::string opath = "C:/temp/I%04d.pgm";
186 #endif
187 
188  // Read the command line options
189  if (getOptions(argc, argv, opt_display, nframes, save, opath) == false) {
190  exit (-1);
191  }
192  // Create the grabber
193  vpDirectShowGrabber* grabber = new vpDirectShowGrabber();
194 
195  //test if a camera is connected
196  if(grabber->getDeviceNumber() == 0)
197  {
198  vpCTRACE << "there is no camera detected on your computer." << std::endl ;
199  grabber->close();
200  exit(0);
201  }
202  // Initialize the grabber
203  grabber->open(I);
204 
205  // Acquire an image
206  grabber->acquire(I);
207 
208 
209  std::cout << "Image size: width : " << I.getWidth() << " height: "
210  << I.getHeight() << std::endl;
211 
212  // Creates a display
213 #if defined VISP_HAVE_GTK
214  vpDisplayGTK display;
215 #elif defined VISP_HAVE_GDI
216  vpDisplayGDI display;
217 #endif
218 
219  if (opt_display) {
220  display.init(I,100,100,"DirectShow Framegrabber");
221  }
222 
223  double tbegin=0, ttotal=0;
224 
225  ttotal = 0;
226  tbegin = vpTime::measureTimeMs();
227  // Loop for image acquisition and display
228  for (unsigned i = 0; i < nframes; i++) {
229  //Acquires an RGBa image
230  grabber->acquire(I);
231 
232  if (opt_display) {
233  //Displays the grabbed rgba image
235  vpDisplay::flush(I);
236  }
237 
238  if (save) {
239  char buf[FILENAME_MAX];
240  sprintf(buf, opath.c_str(), i);
241  std::string filename(buf);
242  std::cout << "Write: " << filename << std::endl;
243  vpImageIo::write(I, filename);
244  }
245  double tend = vpTime::measureTimeMs();
246  double tloop = tend - tbegin;
247  tbegin = tend;
248  std::cout << "loop time: " << tloop << " ms" << std::endl;
249  ttotal += tloop;
250  }
251  std::cout << "Mean loop time: " << ttotal / nframes << " ms" << std::endl;
252  std::cout << "Mean frequency: " << 1000./(ttotal / nframes) << " fps" << std::endl;
253 
254  // Release the framegrabber
255  delete grabber;
256  return 0;
257  }
258  catch(vpException &e) {
259  std::cout << "Catch an exception: " << e << std::endl;
260  return 1;
261  }
262 }
263 #else // (defined (VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
264 
265 int
266 main()
267 {
268  vpTRACE("GDI or GTK is not available...") ;
269 }
270 #endif // (defined (VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
271 #else // defined (VISP_HAVE_DIRECTSHOW)
272 int
273 main()
274 {
275  vpTRACE("DirectShow is not available...") ;
276 }
277 #endif // defined (VISP_HAVE_DIRECTSHOW)
278 
279 
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
error that can be emited by ViSP classes.
Definition: vpException.h:73
class for windows direct show devices
static void flush(const vpImage< unsigned char > &I)
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:93
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
static void write(const vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:368
#define vpTRACE
Definition: vpDebug.h:414
static void display(const vpImage< unsigned char > &I)
void acquire(vpImage< unsigned char > &I)
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:138
unsigned int getHeight() const
Definition: vpImage.h:175
#define vpCTRACE
Definition: vpDebug.h:337
unsigned int getDeviceNumber()
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
unsigned int getWidth() const
Definition: vpImage.h:226