Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
grabDirectShow.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  * Acquire images using DirectShow (under Windows only) and display it
33  * using GTK or GDI.
34  *
35  * Authors:
36  * Bruno Renier
37  * Fabien Spindler
38  *
39  *****************************************************************************/
40 
41 #include <visp3/core/vpConfig.h>
42 #include <visp3/core/vpDebug.h>
43 
51 #if defined(VISP_HAVE_DIRECTSHOW)
52 #if (defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
53 
54 #include <visp3/core/vpImage.h>
55 #include <visp3/core/vpTime.h>
56 #include <visp3/gui/vpDisplayGDI.h>
57 #include <visp3/gui/vpDisplayGTK.h>
58 #include <visp3/io/vpImageIo.h>
59 #include <visp3/io/vpParseArgv.h>
60 #include <visp3/sensor/vpDirectShowGrabber.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, unsigned &nframes, bool &save, std::string &opath)
122 {
123  const char *optarg;
124  int c;
125  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
126 
127  switch (c) {
128  case 'd':
129  display = false;
130  break;
131  case 'n':
132  nframes = atoi(optarg);
133  break;
134  case 'o':
135  save = true;
136  opath = optarg;
137  break;
138  case 'h':
139  usage(argv[0], NULL, nframes, opath);
140  return false;
141  break;
142 
143  default:
144  usage(argv[0], optarg, nframes, opath);
145  return false;
146  break;
147  }
148  }
149 
150  if ((c == 1) || (c == -1)) {
151  // standalone param or error
152  usage(argv[0], NULL, nframes, opath);
153  std::cerr << "ERROR: " << std::endl;
154  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
155  return false;
156  }
157 
158  return true;
159 }
160 
169 int main(int argc, const char **argv)
170 {
171  try {
172  bool opt_display = true;
173  unsigned nframes = 50;
174  bool save = false;
175 
176 // Declare an image. It size is not defined yet. It will be defined when the
177 // image will acquired the first time.
178 #ifdef GRAB_COLOR
179  vpImage<vpRGBa> I; // This is a color image (in RGBa format)
180 #else
181  vpImage<unsigned char> I; // This is a B&W image
182 #endif
183 
184 // Set default output image name for saving
185 #ifdef GRAB_COLOR
186  // Color images will be saved in PGM P6 format
187  std::string opath = "C:/temp/I%04d.ppm";
188 #else
189  // B&W images will be saved in PGM P5 format
190  std::string opath = "C:/temp/I%04d.pgm";
191 #endif
192 
193  // Read the command line options
194  if (getOptions(argc, argv, opt_display, nframes, save, opath) == false) {
195  exit(-1);
196  }
197  // Create the grabber
198  vpDirectShowGrabber *grabber = new vpDirectShowGrabber();
199 
200  // test if a camera is connected
201  if (grabber->getDeviceNumber() == 0) {
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  std::cout << "Image size: width : " << I.getWidth() << " height: " << I.getHeight() << std::endl;
213 
214 // Creates a display
215 #if defined VISP_HAVE_GTK
216  vpDisplayGTK display;
217 #elif defined VISP_HAVE_GDI
218  vpDisplayGDI display;
219 #endif
220 
221  if (opt_display) {
222  display.init(I, 100, 100, "DirectShow Framegrabber");
223  }
224 
225  double tbegin = 0, ttotal = 0;
226 
227  ttotal = 0;
228  tbegin = vpTime::measureTimeMs();
229  // Loop for image acquisition and display
230  for (unsigned i = 0; i < nframes; i++) {
231  // Acquires an RGBa image
232  grabber->acquire(I);
233 
234  if (opt_display) {
235  // Displays the grabbed rgba image
237  vpDisplay::flush(I);
238  }
239 
240  if (save) {
241  char buf[FILENAME_MAX];
242  sprintf(buf, opath.c_str(), i);
243  std::string filename(buf);
244  std::cout << "Write: " << filename << std::endl;
245  vpImageIo::write(I, filename);
246  }
247  double tend = vpTime::measureTimeMs();
248  double tloop = tend - tbegin;
249  tbegin = tend;
250  std::cout << "loop time: " << tloop << " ms" << std::endl;
251  ttotal += tloop;
252  }
253  std::cout << "Mean loop time: " << ttotal / nframes << " ms" << std::endl;
254  std::cout << "Mean frequency: " << 1000. / (ttotal / nframes) << " fps" << std::endl;
255 
256  // Release the framegrabber
257  delete grabber;
258  return EXIT_SUCCESS;
259  } catch (const vpException &e) {
260  std::cout << "Catch an exception: " << e << std::endl;
261  return EXIT_FAILURE;
262  }
263 }
264 #else // (defined (VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
265 
266 int main()
267 {
268  std::cout << "You do not have GDI (Graphical Device Interface), or GTK functionalities to display images..." << std::endl;
269  std::cout << "Tip if you are on a windows-like system:" << std::endl;
270  std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
271  return EXIT_SUCCESS;
272 }
273 #endif // (defined (VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
274 #else // defined (VISP_HAVE_DIRECTSHOW)
275 int main()
276 {
277  std::cout << "This example requires Direct Show SDK. " << std::endl;
278  std::cout << "Tip if you are on a windows-like system:" << std::endl;
279  std::cout << "- Install Direct Show, configure again ViSP using cmake and build again this example" << std::endl;
280  return EXIT_SUCCESS;
281 }
282 #endif // defined (VISP_HAVE_DIRECTSHOW)
unsigned int getWidth() const
Definition: vpImage.h:239
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
error that can be emited by ViSP classes.
Definition: vpException.h:71
class for windows direct show devices
static void flush(const vpImage< unsigned char > &I)
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:88
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
static void write(const vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:375
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
#define vpCTRACE
Definition: vpDebug.h:338
unsigned int getDeviceNumber()
unsigned int getHeight() const
Definition: vpImage.h:178
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")