Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
grabDirectShow.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
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 https://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
35#include <visp3/core/vpConfig.h>
36#include <visp3/core/vpDebug.h>
37
44
45#if defined(VISP_HAVE_DIRECTSHOW)
46#if defined(VISP_HAVE_DISPLAY)
47
48#include <visp3/core/vpImage.h>
49#include <visp3/core/vpTime.h>
50#include <visp3/gui/vpDisplayFactory.h>
51#include <visp3/io/vpImageIo.h>
52#include <visp3/io/vpParseArgv.h>
53#include <visp3/sensor/vpDirectShowGrabber.h>
54
55// List of allowed command line options
56#define GETOPTARGS "dhn:o:"
57
58#ifdef ENABLE_VISP_NAMESPACE
59using namespace VISP_NAMESPACE_NAME;
60#endif
61
72void usage(const char *name, const char *badparam, unsigned &nframes, std::string &opath)
73{
74 fprintf(stdout, "\n\
75Acquire images using DirectShow (under Windows only) and display\n\
76it using GTK or the windows GDI if GTK is not available.\n\
77\n\
78SYNOPSIS\n\
79 %s [-d] [-n] [-o] [-h] \n",
80 name);
81
82 fprintf(stdout, "\n\
83OPTIONS: Default\n\
84 -d \n\
85 Turn off the display.\n\
86\n\
87 -n [%%u] %u\n\
88 Number of frames to acquire. \n\
89\n\
90 -o [%%s] \n\
91 Filename for image saving. \n\
92 Example: -o %s\n\
93 The %%d is for the image numbering.\n\
94\n\
95 -h \n\
96 Print the help.\n\
97\n",
98nframes, opath.c_str());
99 if (badparam) {
100 fprintf(stderr, "ERROR: \n");
101 fprintf(stderr, "\nBad parameter [%s]\n", badparam);
102 }
103}
120bool getOptions(int argc, const char **argv, bool &display, unsigned &nframes, bool &save, std::string &opath)
121{
122 const char *optarg;
123 int c;
124 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
125
126 switch (c) {
127 case 'd':
128 display = false;
129 break;
130 case 'n':
131 nframes = atoi(optarg);
132 break;
133 case 'o':
134 save = true;
135 opath = optarg;
136 break;
137 case 'h':
138 usage(argv[0], nullptr, nframes, opath);
139 return false;
140
141 default:
142 usage(argv[0], optarg, nframes, opath);
143 return false;
144 }
145 }
146
147 if ((c == 1) || (c == -1)) {
148 // standalone param or error
149 usage(argv[0], nullptr, 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
166int main(int argc, const char **argv)
167{
168#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
169 vpDisplay *display = nullptr;
170#endif
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 return EXIT_FAILURE;
196 }
197 // Create the grabber
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 (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
216 std::shared_ptr<vpDisplay> display = vpDisplayFactory::createDisplay();
217#else
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
238 }
239
240 if (save) {
241 char buf[FILENAME_MAX];
242 snprintf(buf, FILENAME_MAX, 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#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
259 if (display != nullptr) {
260 delete display;
261 }
262#endif
263 return EXIT_SUCCESS;
264 }
265 catch (const vpException &e) {
266 std::cout << "Catch an exception: " << e << std::endl;
267#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
268 if (display != nullptr) {
269 delete display;
270 }
271#endif
272 return EXIT_FAILURE;
273 }
274}
275#else // (defined (VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
276
277int main()
278{
279 std::cout << "You do not have GDI (Graphical Device Interface), or GTK functionalities to display images..."
280 << std::endl;
281 std::cout << "Tip if you are on a windows-like system:" << std::endl;
282 std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
283 return EXIT_SUCCESS;
284}
285#endif // (defined (VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
286#else // defined (VISP_HAVE_DIRECTSHOW)
287int main()
288{
289 std::cout << "This example requires Direct Show SDK. " << std::endl;
290 std::cout << "Tip if you are on a windows-like system:" << std::endl;
291 std::cout << "- Install Direct Show, configure again ViSP using cmake and build again this example" << std::endl;
292 return EXIT_SUCCESS;
293}
294#endif // defined (VISP_HAVE_DIRECTSHOW)
class for windows direct show devices
void acquire(vpImage< unsigned char > &I)
Class that defines generic functionalities for display.
Definition vpDisplay.h:171
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 of the vpImage class member functions.
Definition vpImage.h:131
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
#define vpCTRACE
Definition vpDebug.h:362
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.
VISP_EXPORT double measureTimeMs()