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>
#include <iostream>
#include <sstream>
#include <vector>
#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 "c:df:hmn:io:st:?"
#define GRAB_COLOR
#ifdef ENABLE_VISP_NAMESPACE
#endif
void usage(const char *name, const char *badparam, unsigned int camera, unsigned int &nframes, std::string &opath)
{
if (badparam)
fprintf(stderr, "\nERREUR: Bad parameter [%s]\n", badparam);
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\
For a given camera, mediatype (or video mode) as well as framerate\n\
can be set.\n\
If more than one camera is connected, this example allows also to \n\
acquire images from all the cameras.\n\
\n\
SYNOPSIS\n\
%s [-t <mediatype>] [-f <framerate>] \n\
[-c <camera id>] [-m] [-n <frames>] [-i] [-s] [-d] \n\
[-o <filename>] [-h]\n\
\n\
OPTIONS Default\n\
-t [%%u] \n\
MediaType (or video mode) to set for the active \n\
camera. Use -s option so see which are the supported \n\
Mediatypes. You can select the active camera \n\
using -c option.\n\
\n\
-f [%%d] \n\
Framerate to set for the active camera.\n\
You can select the active camera using -c option.\n",
name);
fprintf(stdout, "\n\
-c [%%u] %u\n\
Active camera identifier.\n\
Zero is for the first camera found on the bus.\n\
\n\
-m \n\
Flag to active multi camera acquisition. \n\
You need at least two cameras connected on the bus.\n\
\n\
-n [%%u] %u\n\
Number of frames to acquire.\n\
\n\
-i \n\
Flag to print camera information.\n\
\n\
-s \n\
Print camera settings capabilities such as MediaType \n\
and sizes available and exit.\n\
\n\
-d \n\
Flag to turn off image display.\n\
\n\
-o [%%s] \n\
Filename for image saving. \n\
Example: -o %s\n\
The first %%d is for the camera id, %%04d\n\
is for the image numbering.\n\
\n\
-h \n\
Print the help.\n\
\n",
camera, nframes, opath.c_str());
exit(0);
}
void read_options(int argc, const char **argv, bool &multi, unsigned int &camera, unsigned int &nframes,
bool &verbose_info, bool &verbose_settings, bool &mediatype_is_set, unsigned int &mediatypeID,
bool &framerate_is_set, double &framerate, bool &display, bool &save, std::string &opath)
{
const char *optarg;
int c;
switch (c) {
case 'c':
camera = atoi(optarg);
break;
case 'd':
display = false;
break;
case 'f':
framerate_is_set = true;
framerate = atoi(optarg);
break;
case 'i':
verbose_info = true;
break;
case 'm':
multi = true;
break;
case 'n':
nframes = atoi(optarg);
break;
case 'o':
save = true;
opath = optarg;
break;
case 's':
verbose_settings = true;
break;
case 't':
mediatype_is_set = true;
mediatypeID = atoi(optarg);
break;
default:
usage(argv[0], nullptr, camera, nframes, opath);
break;
}
}
if ((c == 1) || (c == -1)) {
usage(argv[0], nullptr, camera, nframes, opath);
std::cerr << "ERROR: " << std::endl;
std::cerr << " Bad argument " << optarg << std::endl << std::endl;
}
}
int main(int argc, const char **argv)
{
try {
unsigned int camera = 0;
bool multi = false;
bool verbose_info = false;
bool verbose_settings = false;
bool display = true;
unsigned int nframes = 50;
bool mediatype_is_set = false;
unsigned int mediatypeID;
bool framerate_is_set = false;
double framerate;
bool save = false;
#ifdef GRAB_COLOR
std::string opath = "C:/temp/I%d-%04d.ppm";
#else
std::string opath = "C:/temp/I%d-%04d.pgm";
#endif
#if defined(VISP_HAVE_GDI)
#elif defined(VISP_HAVE_GTK)
#endif
read_options(argc, argv, multi, camera, nframes, verbose_info, verbose_settings, mediatype_is_set, mediatypeID,
framerate_is_set, framerate, display, save, opath);
if (multi) {
if (ncameras < 2) {
std::cout << "You have only " << ncameras << " camera connected on the bus." << std::endl;
std::cout << "It is not possible to active multi-camera acquisition." << std::endl;
std::cout << "Disable -m command line option, or connect an other " << std::endl;
std::cout << "cameras on the bus." << std::endl;
delete g;
return EXIT_FAILURE;
}
}
if (camera >= ncameras) {
std::cout << "You have only " << ncameras;
std::cout << " camera connected on the bus." << std::endl;
std::cout << "It is not possible to select camera " << camera << std::endl;
std::cout << "Check your -c <camera> command line option." << std::endl;
delete g;
return EXIT_FAILURE;
}
if (multi) {
camera = 0;
delete[] g;
for (unsigned int i = 0; i < ncameras; i++) {
}
}
else {
ncameras = 1;
delete[] g;
}
#ifdef GRAB_COLOR
#else
#endif
if (display)
#ifdef VISP_HAVE_GDI
#else
#endif
if (mediatype_is_set) {
}
if (framerate_is_set) {
for (unsigned int i = 0; i < ncameras; i++) {
unsigned int c;
if (multi)
c = i;
else
c = camera;
std::cout << "camera " << c << std::endl;
if (!g[i].setFramerate(framerate))
std::cout << "Set Framerate failed !!" << std::endl << std::endl;
}
}
if (verbose_info || verbose_settings) {
std::cout << "----------------------------------------------------------" << std::endl;
std::cout << "---- Device List : " << std::endl;
std::cout << "----------------------------------------------------------" << std::endl;
for (unsigned i = 0; i < ncameras; i++) {
unsigned int c;
if (multi)
c = i;
else
c = camera;
if (verbose_info) {
unsigned int width, height;
std::cout << "----------------------------------------------------------" << std::endl
<< "---- MediaType and framerate currently used by device " << std::endl
<< "---- (or camera) " << c << std::endl
<<
"---- Current MediaType : " << g[i].
getMediaType() << std::endl
<< "---- Current format : " << width << " x " << height << " at " << framerate << " fps"
<< std::endl
<< "----------------------------------------------------------" << std::endl;
}
if (verbose_settings) {
std::cout << "----------------------------------------------------------" << std::endl
<< "---- MediaTypes supported by device (or camera) " << c << std::endl
<< "---- One of the MediaType below can be set using " << std::endl
<< "---- option -t <mediatype>." << std::endl
<< "----------------------------------------------------------" << std::endl;
}
}
delete[] g;
delete[] I;
if (display)
delete[] d;
return EXIT_SUCCESS;
}
for (unsigned int i = 0; i < ncameras; i++) {
unsigned int c;
if (multi)
c = i;
else
c = camera;
std::cout <<
"Image size for camera " << c <<
" : width: " << I[i].
getWidth() <<
" height: " << I[i].
getHeight()
<< std::endl;
if (display) {
std::stringstream title;
title << "Images captured by camera ";
title << c;
d[i].
init(I[i], 100 + i * 50, 100 + i * 50, title.c_str());
}
}
std::cout << "Capture in process..." << std::endl;
double tbegin = 0, ttotal = 0;
ttotal = 0;
for (unsigned i = 0; i < nframes; i++) {
for (unsigned c = 0; c < ncameras; c++) {
if (display) {
}
if (save) {
char buf[FILENAME_MAX];
snprintf(buf, FILENAME_MAX, opath.c_str(), c, 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[] g;
delete[] I;
if (display)
delete[] d;
return EXIT_SUCCESS;
}
std::cout << "Catch an exception: " << e << std::endl;
return EXIT_FAILURE;
}
}
#else
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;
return EXIT_SUCCESS;
}
#endif
#else
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;
return EXIT_SUCCESS;
}
#endif
class for windows direct show devices
void acquire(vpImage< unsigned char > &I)
void getFormat(unsigned int &width, unsigned int &height, double &framerate)
bool setDevice(unsigned int id)
unsigned int getDeviceNumber()
bool getStreamCapabilities()
bool setMediaType(int mediaTypeID)
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...
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="") VP_OVERRIDE
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
error that can be emitted by ViSP classes.
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
unsigned int getWidth() const
unsigned int getHeight() const
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
VISP_EXPORT double measureTimeMs()