Test frame grabbing capabilities using video for linux two (V4L2) video device. Only grabbing of grey level images is possible in this example. Display these images using X11 or GTK.
#include <stdlib.h>
#include <visp3/core/vpConfig.h>
#include <visp3/core/vpDebug.h>
#ifdef VISP_HAVE_V4L2
#if (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK))
#include <visp3/core/vpDisplay.h>
#include <visp3/core/vpImage.h>
#include <visp3/core/vpTime.h>
#include <visp3/gui/vpDisplayGTK.h>
#include <visp3/gui/vpDisplayX.h>
#include <visp3/io/vpImageIo.h>
#include <visp3/io/vpParseArgv.h>
#include <visp3/sensor/vpV4l2Grabber.h>
#define GETOPTARGS "df:i:hn:o:p:s:t:v:x"
typedef enum {
grey_image = 0,
color_image
} vpImage_type;
void usage(const char *name, const char *badparam, unsigned fps, unsigned input, unsigned scale, long niter,
const std::string &opath)
{
fprintf(stdout, "\n\
Grab grey level images using the Video For Linux Two framegrabber. \n\
Display these images using X11 or GTK.\n\
\n\
SYNOPSIS\n\
%s [-v <video device>] [-f <fps=25|50>] \n\
[-i <input=0|1|2|3> [-s <scale=1|2|4>] [-p <pixel format>]\n\
[-n <niter>] [-t <image type>] [-o <filename>] [-x] [-d] [-h]\n", name);
fprintf(stdout, "\n\
OPTIONS: Default\n\
-v <video device> %s\n\
Video device to access to the camera\n\
\n\
-f <fps> %u\n\
Framerate in term od number of images per second.\n\
Possible values are 25 (for 25Hz) or 50 (for %%) Hz)\n\
\n\
-i <input> %u\n\
Framegrabber active input. Values can be 0, 1, 2, 4\n\
\n\
-p <pixel format> %d\n\
Camera pixel format. Values must be in [0-%d]:\n\
0 for gray format\n\
1 for RGB24 format\n\
2 for RGB32 format\n\
3 for BGR24 format\n\
4 for YUYV format\n\
\n\
-t <image type> %d\n\
Kind of images that are acquired/displayed by ViSP. \n\
Values must be in [0-1]:\n\
0 for grey images in unsigned char \n\
1 for color images in vpRGBa\n\
\n\
-s <scale> %u\n\
Framegrabber subsampling factor. \n\
If 1, full resolution image acquisition.\n\
If 2, half resolution image acquisition. The \n\
subsampling is achieved by the hardware.\n\
\n\
-n <niter> %ld\n\
Number of images to acquire.\n\
\n\
-d \n\
Turn off the display.\n\
\n\
-x \n\
Activates the extra verbose mode.\n\
\n\
-o [%%s] : Filename for image saving. \n\
Example: -o %s\n\
The %%d is for the image numbering. The format is set \n\
by the extension of the file (ex .png, .pgm, ...) \n\
\n\
-h \n\
opath.c_str());
if (badparam)
fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
}
bool getOptions(int argc, const char **argv, unsigned &fps, unsigned &input, unsigned &scale, bool &display,
vpImage_type &image_type, bool &save, std::string &opath)
{
const char *optarg_;
int c;
switch (c) {
case 'd':
display = false;
break;
case 'f':
fps = (unsigned)atoi(optarg_);
break;
case 'i':
input = (unsigned)atoi(optarg_);
break;
case 'n':
niter = atol(optarg_);
break;
case 'o':
save = true;
opath = optarg_;
break;
case 'p':
break;
case 's':
scale = (unsigned)atoi(optarg_);
break;
case 't':
image_type = (vpImage_type)atoi(optarg_);
break;
case 'v':
sprintf(device, "%s", optarg_);
break;
case 'x':
verbose = true;
break;
case 'h':
usage(argv[0], NULL, fps, input, scale, niter, device, pixelformat, image_type, opath);
return false;
break;
default:
usage(argv[0], optarg_, fps, input, scale, niter, device, pixelformat, image_type, opath);
return false;
break;
}
}
if ((c == 1) || (c == -1)) {
usage(argv[0], NULL, fps, input, scale, niter, device, pixelformat, image_type, 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 {
unsigned int opt_fps = 25;
unsigned int opt_input = 0;
unsigned int opt_scale = 1;
long opt_iter = 100;
bool opt_verbose = false;
bool opt_display = true;
char opt_device[20];
bool opt_save = false;
sprintf(opt_device, "/dev/video0");
std::string opt_opath = "/tmp/I%04d.ppm";
vpImage_type opt_image_type = color_image;
if (getOptions(argc, argv, opt_fps, opt_input, opt_scale, opt_display, opt_verbose, opt_iter, opt_device,
opt_pixelformat, opt_image_type, opt_save, opt_opath) == false) {
exit(-1);
}
if (opt_fps == 25)
else
if (opt_image_type == grey_image) {
std::cout <<
"Grey image size: width : " << Ig.
getWidth() <<
" height: " << Ig.
getHeight() << std::endl;
} else {
std::cout <<
"Color image size: width : " << Ic.
getWidth() <<
" height: " << Ic.
getHeight() << std::endl;
}
#if defined VISP_HAVE_X11
#elif defined VISP_HAVE_GTK
#endif
if (opt_display) {
if (opt_image_type == grey_image) {
display.
init(Ig, 100, 100,
"V4L2 grey images framegrabbing");
} else {
display.
init(Ic, 100, 100,
"V4L2 color images framegrabbing");
}
}
long cpt = 1;
while (cpt++ < opt_iter) {
if (opt_image_type == grey_image) {
if (opt_display) {
}
} else {
if (opt_display) {
}
}
if (opt_save) {
char buf[FILENAME_MAX];
sprintf(buf, opt_opath.c_str(), cpt);
std::string filename(buf);
std::cout << "Write: " << filename << std::endl;
if (opt_image_type == grey_image) {
} else {
}
}
}
return 0;
std::cout << "Catch an exception: " << e << std::endl;
return 1;
}
}
#else
int main() {
vpTRACE(
"X11 or GTK display are not available"); }
#endif
#else
int main() {
vpTRACE(
"Video 4 Linux 2 frame grabber drivers are not available"); }
#endif