Visual Servoing Platform  version 3.6.1 under development (2024-04-18)

Example of framegrabbing using vpDirectShowGrabber class.

Grab grey level images using DirectShow frame grabbing capabilities. Display the images using the GTK or GDI display.

* ViSP, open source Visual Servoing Platform software.
* Copyright (C) 2005 - 2023 by Inria. All rights reserved.
* This software is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* See the file LICENSE.txt at the root directory of this source
* distribution for additional information about the GNU GPL.
* For using ViSP with software that can not be combined with the GNU
* GPL, please contact Inria about acquiring a ViSP Professional
* Edition License.
* See for more information.
* This software was developed at:
* Inria Rennes - Bretagne Atlantique
* Campus Universitaire de Beaulieu
* 35042 Rennes Cedex
* France
* If you have questions regarding the use of this file, please contact
* Inria at
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
* Description:
* Acquire images using DirectShow (under Windows only) and display it
* using GTK or GDI.
#include <visp3/core/vpConfig.h>
#include <visp3/core/vpDebug.h>
#if (defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
#include <visp3/core/vpImage.h>
#include <visp3/core/vpTime.h>
#include <visp3/gui/vpDisplayGDI.h>
#include <visp3/gui/vpDisplayGTK.h>
#include <visp3/io/vpImageIo.h>
#include <visp3/io/vpParseArgv.h>
#include <visp3/sensor/vpDirectShowGrabber.h>
// List of allowed command line options
#define GETOPTARGS "dhn:o:"
void usage(const char *name, const char *badparam, unsigned &nframes, std::string &opath)
fprintf(stdout, "\n\
Acquire images using DirectShow (under Windows only) and display\n\
it using GTK or the windows GDI if GTK is not available.\n\
%s [-d] [-n] [-o] [-h] \n",
fprintf(stdout, "\n\
OPTIONS: Default\n\
-d \n\
Turn off the display.\n\
-n [%%u] %u\n\
Number of frames to acquire. \n\
-o [%%s] \n\
Filename for image saving. \n\
Example: -o %s\n\
The %%d is for the image numbering.\n\
-h \n\
Print the help.\n\
nframes, opath.c_str());
if (badparam) {
fprintf(stderr, "ERROR: \n");
fprintf(stderr, "\nBad parameter [%s]\n", badparam);
bool getOptions(int argc, const char **argv, bool &display, unsigned &nframes, bool &save, std::string &opath)
const char *optarg;
int c;
while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
switch (c) {
case 'd':
display = false;
case 'n':
nframes = atoi(optarg);
case 'o':
save = true;
opath = optarg;
case 'h':
usage(argv[0], nullptr, nframes, opath);
return false;
usage(argv[0], optarg, nframes, opath);
return false;
if ((c == 1) || (c == -1)) {
// standalone param or error
usage(argv[0], nullptr, nframes, opath);
std::cerr << "ERROR: " << std::endl;
std::cerr << " Bad argument " << optarg << std::endl << std::endl;
return false;
return true;
int main(int argc, const char **argv)
try {
bool opt_display = true;
unsigned nframes = 50;
bool save = false;
// Declare an image. It size is not defined yet. It will be defined when the
// image will acquired the first time.
vpImage<vpRGBa> I; // This is a color image (in RGBa format)
vpImage<unsigned char> I; // This is a B&W image
// Set default output image name for saving
// Color images will be saved in PGM P6 format
std::string opath = "C:/temp/I%04d.ppm";
// B&W images will be saved in PGM P5 format
std::string opath = "C:/temp/I%04d.pgm";
// Read the command line options
if (getOptions(argc, argv, opt_display, nframes, save, opath) == false) {
// Create the grabber
// test if a camera is connected
if (grabber->getDeviceNumber() == 0) {
vpCTRACE << "there is no camera detected on your computer." << std::endl;
// Initialize the grabber
// Acquire an image
std::cout << "Image size: width : " << I.getWidth() << " height: " << I.getHeight() << std::endl;
// Creates a display
#if defined(VISP_HAVE_GTK)
#elif defined(VISP_HAVE_GDI)
if (opt_display) {
display.init(I, 100, 100, "DirectShow Framegrabber");
double tbegin = 0, ttotal = 0;
ttotal = 0;
// Loop for image acquisition and display
for (unsigned i = 0; i < nframes; i++) {
// Acquires an RGBa image
if (opt_display) {
// Displays the grabbed rgba image
if (save) {
char buf[FILENAME_MAX];
snprintf(buf, FILENAME_MAX, opath.c_str(), i);
std::string filename(buf);
std::cout << "Write: " << filename << std::endl;
vpImageIo::write(I, filename);
double tend = vpTime::measureTimeMs();
double tloop = tend - tbegin;
tbegin = tend;
std::cout << "loop time: " << tloop << " ms" << std::endl;
ttotal += tloop;
std::cout << "Mean loop time: " << ttotal / nframes << " ms" << std::endl;
std::cout << "Mean frequency: " << 1000. / (ttotal / nframes) << " fps" << std::endl;
// Release the framegrabber
delete grabber;
} catch (const vpException &e) {
std::cout << "Catch an exception: " << e << std::endl;
#else // (defined (VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
int main()
std::cout << "You do not have GDI (Graphical Device Interface), or GTK functionalities to display images..."
<< std::endl;
std::cout << "Tip if you are on a windows-like system:" << std::endl;
std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
#endif // (defined (VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
#else // defined (VISP_HAVE_DIRECTSHOW)
int main()
std::cout << "This example requires Direct Show SDK. " << std::endl;
std::cout << "Tip if you are on a windows-like system:" << std::endl;
std::cout << "- Install Direct Show, configure again ViSP using cmake and build again this example" << std::endl;
#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:128
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:128
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: vpImageIo.cpp:287
unsigned int getWidth() const
Definition: vpImage.h:245
unsigned int getHeight() const
Definition: vpImage.h:184
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
#define vpCTRACE
Definition: vpDebug.h:329
void display(vpImage< unsigned char > &I, const std::string &title)
Display a gray-scale image.
VISP_EXPORT double measureTimeMs()