Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
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
71void usage(const char *name, const char *badparam, unsigned &nframes, std::string &opath)
72{
73 fprintf(stdout, "\n\
74Acquire images using DirectShow (under Windows only) and display\n\
75it using GTK or the windows GDI if GTK is not available.\n\
76\n\
77SYNOPSIS\n\
78 %s [-d] [-n] [-o] [-h] \n",
79 name);
80
81 fprintf(stdout, "\n\
82OPTIONS: Default\n\
83 -d \n\
84 Turn off the display.\n\
85\n\
86 -n [%%u] %u\n\
87 Number of frames to acquire. \n\
88\n\
89 -o [%%s] \n\
90 Filename for image saving. \n\
91 Example: -o %s\n\
92 The %%d is for the image numbering.\n\
93\n\
94 -h \n\
95 Print the help.\n\
96\n",
97 nframes, opath.c_str());
98 if (badparam) {
99 fprintf(stderr, "ERROR: \n");
100 fprintf(stderr, "\nBad parameter [%s]\n", badparam);
101 }
102}
119bool getOptions(int argc, const char **argv, bool &display, unsigned &nframes, bool &save, std::string &opath)
120{
121 const char *optarg;
122 int c;
123 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
124
125 switch (c) {
126 case 'd':
127 display = false;
128 break;
129 case 'n':
130 nframes = atoi(optarg);
131 break;
132 case 'o':
133 save = true;
134 opath = optarg;
135 break;
136 case 'h':
137 usage(argv[0], NULL, nframes, opath);
138 return false;
139 break;
140
141 default:
142 usage(argv[0], optarg, nframes, opath);
143 return false;
144 break;
145 }
146 }
147
148 if ((c == 1) || (c == -1)) {
149 // standalone param or error
150 usage(argv[0], NULL, nframes, opath);
151 std::cerr << "ERROR: " << std::endl;
152 std::cerr << " Bad argument " << optarg << std::endl << std::endl;
153 return false;
154 }
155
156 return true;
157}
158
167int main(int argc, const char **argv)
168{
169 try {
170 bool opt_display = true;
171 unsigned nframes = 50;
172 bool save = false;
173
174// Declare an image. It size is not defined yet. It will be defined when the
175// image will acquired the first time.
176#ifdef GRAB_COLOR
177 vpImage<vpRGBa> I; // This is a color image (in RGBa format)
178#else
179 vpImage<unsigned char> I; // This is a B&W image
180#endif
181
182// Set default output image name for saving
183#ifdef GRAB_COLOR
184 // Color images will be saved in PGM P6 format
185 std::string opath = "C:/temp/I%04d.ppm";
186#else
187 // B&W images will be saved in PGM P5 format
188 std::string opath = "C:/temp/I%04d.pgm";
189#endif
190
191 // Read the command line options
192 if (getOptions(argc, argv, opt_display, nframes, save, opath) == false) {
193 return EXIT_FAILURE;
194 }
195 // Create the grabber
197
198 // test if a camera is connected
199 if (grabber->getDeviceNumber() == 0) {
200 vpCTRACE << "there is no camera detected on your computer." << std::endl;
201 grabber->close();
202 exit(0);
203 }
204 // Initialize the grabber
205 grabber->open(I);
206
207 // Acquire an image
208 grabber->acquire(I);
209
210 std::cout << "Image size: width : " << I.getWidth() << " height: " << 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
236 }
237
238 if (save) {
239 char buf[FILENAME_MAX];
240 snprintf(buf, FILENAME_MAX, 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 EXIT_SUCCESS;
257 } catch (const vpException &e) {
258 std::cout << "Catch an exception: " << e << std::endl;
259 return EXIT_FAILURE;
260 }
261}
262#else // (defined (VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
263
264int main()
265{
266 std::cout << "You do not have GDI (Graphical Device Interface), or GTK functionalities to display images..."
267 << std::endl;
268 std::cout << "Tip if you are on a windows-like system:" << std::endl;
269 std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
270 return EXIT_SUCCESS;
271}
272#endif // (defined (VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
273#else // defined (VISP_HAVE_DIRECTSHOW)
274int main()
275{
276 std::cout << "This example requires Direct Show SDK. " << std::endl;
277 std::cout << "Tip if you are on a windows-like system:" << std::endl;
278 std::cout << "- Install Direct Show, configure again ViSP using cmake and build again this example" << std::endl;
279 return EXIT_SUCCESS;
280}
281#endif // defined (VISP_HAVE_DIRECTSHOW)
class for windows direct show devices
void acquire(vpImage< unsigned char > &I)
Display for windows using GDI (available on any windows 32 platform).
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
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:59
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition of the vpImage class member functions.
Definition vpImage.h:135
unsigned int getWidth() const
Definition vpImage.h:242
unsigned int getHeight() const
Definition vpImage.h:184
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
#define vpCTRACE
Definition vpDebug.h:333
VISP_EXPORT double measureTimeMs()