Grab grey level images using DirectShow frame grabbing capabilities. Display the images using the GTK or GDI display.
#include <visp3/core/vpConfig.h>
#include <visp3/core/vpDebug.h>
#if defined(VISP_HAVE_DIRECTSHOW)
#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>
#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\
\n\
SYNOPSIS\n\
%s [-d] [-n] [-o] [-h] \n", name);
fprintf(stdout, "\n\
OPTIONS: Default\n\
-d \n\
Turn off the display.\n\
\n\
-n [%%u] %u\n\
Number of frames to acquire. \n\
\n\
-o [%%s] \n\
Filename for image saving. \n\
Example: -o %s\n\
The %%d is for the image numbering.\n\
\n\
-h \n\
Print the help.\n\
\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;
switch (c) {
case 'd':
display = false;
break;
case 'n':
nframes = atoi(optarg);
break;
case 'o':
save = true;
opath = optarg;
break;
case 'h':
usage(argv[0], NULL, nframes, opath);
return false;
break;
default:
usage(argv[0], optarg, nframes, opath);
return false;
break;
}
}
if ((c == 1) || (c == -1)) {
usage(argv[0], NULL, 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;
#ifdef GRAB_COLOR
#else
#endif
#ifdef GRAB_COLOR
std::string opath = "C:/temp/I%04d.ppm";
#else
std::string opath = "C:/temp/I%04d.pgm";
#endif
if (getOptions(argc, argv, opt_display, nframes, save, opath) == false) {
exit(-1);
}
vpCTRACE <<
"there is no camera detected on your computer." << std::endl;
exit(0);
}
std::cout <<
"Image size: width : " << I.
getWidth() <<
" height: " << I.
getHeight() << std::endl;
#if defined VISP_HAVE_GTK
#elif defined VISP_HAVE_GDI
#endif
if (opt_display) {
display.
init(I, 100, 100,
"DirectShow Framegrabber");
}
double tbegin = 0, ttotal = 0;
ttotal = 0;
for (unsigned i = 0; i < nframes; i++) {
if (opt_display) {
}
if (save) {
char buf[FILENAME_MAX];
sprintf(buf, opath.c_str(), i);
std::string filename(buf);
std::cout << "Write: " << filename << std::endl;
}
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;
delete grabber;
return 0;
std::cout << "Catch an exception: " << e << std::endl;
return 1;
}
}
#else // (defined (VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
int main() {
vpTRACE(
"GDI or GTK is not available..."); }
#endif // (defined (VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
#else // defined (VISP_HAVE_DIRECTSHOW)
int main() {
vpTRACE(
"DirectShow is not available..."); }
#endif // defined (VISP_HAVE_DIRECTSHOW)