ViSP
grabDirectShow.cpp
1 /****************************************************************************
2  *
3  * $Id: grabDirectShow.cpp 4574 2014-01-09 08:48:51Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Acquire images using DirectShow (under Windows only) and display it
36  * using GTK or GDI.
37  *
38  * Authors:
39  * Bruno Renier
40  * Fabien Spindler
41  *
42  *****************************************************************************/
43 
44 #include <visp/vpConfig.h>
45 #include <visp/vpDebug.h>
46 
54 #if defined (VISP_HAVE_DIRECTSHOW)
55 #if (defined (VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
56 
57 
58 #include <visp/vpDirectShowGrabber.h>
59 #include <visp/vpImage.h>
60 #include <visp/vpImageIo.h>
61 #include <visp/vpDisplayGTK.h>
62 #include <visp/vpDisplayGDI.h>
63 #include <visp/vpParseArgv.h>
64 #include <visp/vpTime.h>
65 
66 // List of allowed command line options
67 #define GETOPTARGS "dhn:o:"
68 
79 void usage(const char *name, const char *badparam, unsigned &nframes, std::string &opath)
80 {
81  fprintf(stdout, "\n\
82 Acquire images using DirectShow (under Windows only) and display\n\
83 it using GTK or the windows GDI if GTK is not available.\n\
84 \n\
85 SYNOPSIS\n\
86  %s [-d] [-n] [-o] [-h] \n", name);
87 
88  fprintf(stdout, "\n\
89 OPTIONS: Default\n\
90  -d \n\
91  Turn off the display.\n\
92 \n\
93  -n [%%u] %u\n\
94  Number of frames to acquire. \n\
95 \n\
96  -o [%%s] \n\
97  Filename for image saving. \n\
98  Example: -o %s\n\
99  The %%d is for the image numbering.\n\
100 \n\
101  -h \n\
102  Print the help.\n\
103 \n", nframes, opath.c_str());
104  if (badparam) {
105  fprintf(stderr, "ERROR: \n" );
106  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
107  }
108 }
125 bool getOptions(int argc, const char **argv, bool &display,
126  unsigned &nframes, bool &save, std::string &opath)
127 {
128  const char *optarg;
129  int c;
130  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
131 
132  switch (c) {
133  case 'd': display = false; break;
134  case 'n':
135  nframes = atoi(optarg); break;
136  case 'o':
137  save = true;
138  opath = optarg; break;
139  case 'h': usage(argv[0], NULL, nframes, opath); return false; break;
140 
141  default:
142  usage(argv[0], optarg, nframes, opath);
143  return false; break;
144  }
145  }
146 
147  if ((c == 1) || (c == -1)) {
148  // standalone param or error
149  usage(argv[0], NULL, nframes, opath);
150  std::cerr << "ERROR: " << std::endl;
151  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
152  return false;
153  }
154 
155  return true;
156 }
157 
158 
167 int
168 main(int argc, const char ** argv)
169 {
170  try {
171  bool opt_display = true;
172  unsigned nframes = 50;
173  bool save = false;
174 
175  // Declare an image. It size is not defined yet. It will be defined when the
176  // image will acquired the first time.
177 #ifdef GRAB_COLOR
178  vpImage<vpRGBa> I; // This is a color image (in RGBa format)
179 #else
180  vpImage<unsigned char> I; // This is a B&W image
181 #endif
182 
183  // Set default output image name for saving
184 #ifdef GRAB_COLOR
185  // Color images will be saved in PGM P6 format
186  std::string opath = "C:/temp/I%04d.ppm";
187 #else
188  // B&W images will be saved in PGM P5 format
189  std::string opath = "C:/temp/I%04d.pgm";
190 #endif
191 
192  // Read the command line options
193  if (getOptions(argc, argv, opt_display, nframes, save, opath) == false) {
194  exit (-1);
195  }
196  // Create the grabber
197  vpDirectShowGrabber* grabber = new vpDirectShowGrabber();
198 
199  //test if a camera is connected
200  if(grabber->getDeviceNumber() == 0)
201  {
202  vpCTRACE << "there is no camera detected on your computer." << std::endl ;
203  grabber->close();
204  exit(0);
205  }
206  // Initialize the grabber
207  grabber->open(I);
208 
209  // Acquire an image
210  grabber->acquire(I);
211 
212 
213  std::cout << "Image size: width : " << I.getWidth() << " height: "
214  << I.getHeight() << std::endl;
215 
216  // Creates a display
217 #if defined VISP_HAVE_GTK
218  vpDisplayGTK display;
219 #elif defined VISP_HAVE_GDI
220  vpDisplayGDI display;
221 #endif
222 
223  if (opt_display) {
224  display.init(I,100,100,"DirectShow Framegrabber");
225  }
226 
227  double tbegin=0, tend=0, tloop=0, ttotal=0;
228 
229  ttotal = 0;
230  tbegin = vpTime::measureTimeMs();
231  // Loop for image acquisition and display
232  for (unsigned i = 0; i < nframes; i++) {
233  //Acquires an RGBa image
234  grabber->acquire(I);
235 
236  if (opt_display) {
237  //Displays the grabbed rgba image
239  vpDisplay::flush(I);
240  }
241 
242  if (save) {
243  char buf[FILENAME_MAX];
244  sprintf(buf, opath.c_str(), i);
245  std::string filename(buf);
246  std::cout << "Write: " << filename << std::endl;
247  vpImageIo::write(I, filename);
248  }
249  tend = vpTime::measureTimeMs();
250  tloop = tend - tbegin;
251  tbegin = tend;
252  std::cout << "loop time: " << tloop << " ms" << std::endl;
253  ttotal += tloop;
254  }
255  std::cout << "Mean loop time: " << ttotal / nframes << " ms" << std::endl;
256  std::cout << "Mean frequency: " << 1000./(ttotal / nframes) << " fps" << std::endl;
257 
258  // Release the framegrabber
259  delete grabber;
260  return 0;
261  }
262  catch(vpException e) {
263  std::cout << "Catch an exception: " << e << std::endl;
264  return 1;
265  }
266 }
267 #else // (defined (VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
268 
269 int
270 main()
271 {
272  vpTRACE("GDI or GTK is not available...") ;
273 }
274 #endif // (defined (VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
275 #else // defined (VISP_HAVE_DIRECTSHOW)
276 int
277 main()
278 {
279  vpTRACE("DirectShow is not available...") ;
280 }
281 #endif // defined (VISP_HAVE_DIRECTSHOW)
282 
283 
static void write(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:476
unsigned int getWidth() const
Definition: vpImage.h:161
#define vpTRACE
Definition: vpDebug.h:418
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:132
error that can be emited by ViSP classes.
Definition: vpException.h:76
static double measureTimeMs()
Definition: vpTime.cpp:86
class for windows direct show devices
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2232
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:80
#define vpCTRACE
Definition: vpDebug.h:341
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:210
void acquire(vpImage< unsigned char > &I)
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:145
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
unsigned int getDeviceNumber()
unsigned int getHeight() const
Definition: vpImage.h:152