Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
grabDirectShow.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  * Acquire images using DirectShow (under Windows only) and display it
33  * using GTK or GDI.
34  *
35 *****************************************************************************/
36 
37 #include <visp3/core/vpConfig.h>
38 #include <visp3/core/vpDebug.h>
39 
47 #if defined(VISP_HAVE_DIRECTSHOW)
48 #if (defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
49 
50 #include <visp3/core/vpImage.h>
51 #include <visp3/core/vpTime.h>
52 #include <visp3/gui/vpDisplayGDI.h>
53 #include <visp3/gui/vpDisplayGTK.h>
54 #include <visp3/io/vpImageIo.h>
55 #include <visp3/io/vpParseArgv.h>
56 #include <visp3/sensor/vpDirectShowGrabber.h>
57 
58 // List of allowed command line options
59 #define GETOPTARGS "dhn:o:"
60 
61 #ifdef ENABLE_VISP_NAMESPACE
62 using namespace VISP_NAMESPACE_NAME;
63 #endif
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",
83  name);
84 
85  fprintf(stdout, "\n\
86 OPTIONS: Default\n\
87  -d \n\
88  Turn off the display.\n\
89 \n\
90  -n [%%u] %u\n\
91  Number of frames to acquire. \n\
92 \n\
93  -o [%%s] \n\
94  Filename for image saving. \n\
95  Example: -o %s\n\
96  The %%d is for the image numbering.\n\
97 \n\
98  -h \n\
99  Print the help.\n\
100 \n",
101 nframes, opath.c_str());
102  if (badparam) {
103  fprintf(stderr, "ERROR: \n");
104  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
105  }
106 }
123 bool getOptions(int argc, const char **argv, bool &display, unsigned &nframes, bool &save, std::string &opath)
124 {
125  const char *optarg;
126  int c;
127  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
128 
129  switch (c) {
130  case 'd':
131  display = false;
132  break;
133  case 'n':
134  nframes = atoi(optarg);
135  break;
136  case 'o':
137  save = true;
138  opath = optarg;
139  break;
140  case 'h':
141  usage(argv[0], nullptr, nframes, opath);
142  return false;
143  break;
144 
145  default:
146  usage(argv[0], optarg, nframes, opath);
147  return false;
148  break;
149  }
150  }
151 
152  if ((c == 1) || (c == -1)) {
153  // standalone param or error
154  usage(argv[0], nullptr, nframes, opath);
155  std::cerr << "ERROR: " << std::endl;
156  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
157  return false;
158  }
159 
160  return true;
161 }
162 
171 int main(int argc, const char **argv)
172 {
173  try {
174  bool opt_display = true;
175  unsigned nframes = 50;
176  bool save = false;
177 
178 // Declare an image. It size is not defined yet. It will be defined when the
179 // image will acquired the first time.
180 #ifdef GRAB_COLOR
181  vpImage<vpRGBa> I; // This is a color image (in RGBa format)
182 #else
183  vpImage<unsigned char> I; // This is a B&W image
184 #endif
185 
186 // Set default output image name for saving
187 #ifdef GRAB_COLOR
188  // Color images will be saved in PGM P6 format
189  std::string opath = "C:/temp/I%04d.ppm";
190 #else
191  // B&W images will be saved in PGM P5 format
192  std::string opath = "C:/temp/I%04d.pgm";
193 #endif
194 
195  // Read the command line options
196  if (getOptions(argc, argv, opt_display, nframes, save, opath) == false) {
197  return EXIT_FAILURE;
198  }
199  // Create the grabber
200  vpDirectShowGrabber *grabber = new vpDirectShowGrabber();
201 
202  // test if a camera is connected
203  if (grabber->getDeviceNumber() == 0) {
204  vpCTRACE << "there is no camera detected on your computer." << std::endl;
205  grabber->close();
206  exit(0);
207  }
208  // Initialize the grabber
209  grabber->open(I);
210 
211  // Acquire an image
212  grabber->acquire(I);
213 
214  std::cout << "Image size: width : " << I.getWidth() << " height: " << 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, 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  snprintf(buf, FILENAME_MAX, opath.c_str(), i);
245  std::string filename(buf);
246  std::cout << "Write: " << filename << std::endl;
247  vpImageIo::write(I, filename);
248  }
249  double tend = vpTime::measureTimeMs();
250  double 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 EXIT_SUCCESS;
261  }
262  catch (const vpException &e) {
263  std::cout << "Catch an exception: " << e << std::endl;
264  return EXIT_FAILURE;
265  }
266 }
267 #else // (defined (VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
268 
269 int main()
270 {
271  std::cout << "You do not have GDI (Graphical Device Interface), or GTK functionalities to display images..."
272  << std::endl;
273  std::cout << "Tip if you are on a windows-like system:" << std::endl;
274  std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
275  return EXIT_SUCCESS;
276 }
277 #endif // (defined (VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
278 #else // defined (VISP_HAVE_DIRECTSHOW)
279 int main()
280 {
281  std::cout << "This example requires Direct Show SDK. " << std::endl;
282  std::cout << "Tip if you are on a windows-like system:" << std::endl;
283  std::cout << "- Install Direct Show, configure again ViSP using cmake and build again this example" << std::endl;
284  return EXIT_SUCCESS;
285 }
286 #endif // defined (VISP_HAVE_DIRECTSHOW)
class for windows direct show devices
void acquire(vpImage< unsigned char > &I)
unsigned int getDeviceNumber()
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:133
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
error that can be emitted by ViSP classes.
Definition: vpException.h:60
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:291
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70
VISP_EXPORT double measureTimeMs()