43#include <visp3/core/vpConfig.h>
44#include <visp3/core/vpDebug.h>
48#if defined(VISP_HAVE_DISPLAY)
50#include <visp3/core/vpDisplay.h>
51#include <visp3/core/vpImage.h>
52#include <visp3/core/vpTime.h>
53#include <visp3/gui/vpDisplayFactory.h>
54#include <visp3/io/vpImageIo.h>
55#include <visp3/io/vpParseArgv.h>
56#include <visp3/sensor/vpV4l2Grabber.h>
59#define GETOPTARGS "df:i:hn:o:p:s:t:v:x"
61#ifdef ENABLE_VISP_NAMESPACE
87void usage(
const char *name,
const char *badparam,
unsigned fps,
unsigned input,
unsigned scale,
long niter,
89 const std::string &opath)
92Grab grey level images using the Video For Linux Two framegrabber. \n\
93Display these images using X11 or GTK.\n\
96 %s [-v <video device>] [-f <fps=25|50>] \n\
97 [-i <input=0|1|2|3> [-s <scale=1|2|4>] [-p <pixel format>]\n\
98 [-n <niter>] [-t <image type>] [-o <filename>] [-x] [-d] [-h]\n",
103 -v <video device> %s\n\
104 Video device to access to the camera\n\
107 Framerate in term od number of images per second.\n\
108 Possible values are 25 (for 25Hz) or 50 (for %%) Hz)\n\
111 Framegrabber active input. Values can be 0, 1, 2, 4\n\
113 -p <pixel format> %d\n\
114 Camera pixel format. Values must be in [0-%d]:\n\
116 1 for RGB24 format\n\
117 2 for RGB32 format\n\
118 3 for BGR24 format\n\
121 -t <image type> %d\n\
122 Kind of images that are acquired/displayed by ViSP. \n\
123 Values must be in [0-1]:\n\
124 0 for grey images in unsigned char \n\
125 1 for color images in vpRGBa\n\
128 Framegrabber subsampling factor. \n\
129 If 1, full resolution image acquisition.\n\
130 If 2, half resolution image acquisition. The \n\
131 subsampling is achieved by the hardware.\n\
134 Number of images to acquire.\n\
137 Turn off the display.\n\
140 Activates the extra verbose mode.\n\
142 -o [%%s] : Filename for image saving. \n\
144 The %%d is for the image numbering. The format is set \n\
145 by the extension of the file (ex .png, .pgm, ...) \n\
148 Print the help.\n\n",
153 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
177bool getOptions(
int argc,
const char **argv,
unsigned &fps,
unsigned &input,
unsigned &scale,
bool &display,
179 vpImage_type &image_type,
bool &save, std::string &opath)
190 fps =
static_cast<unsigned int>(atoi(optarg_));
193 input =
static_cast<unsigned int>(atoi(optarg_));
196 niter = atol(optarg_);
206 scale =
static_cast<unsigned int>(atoi(optarg_));
209 image_type = (vpImage_type)atoi(optarg_);
212 device = std::string(optarg_);
218 usage(argv[0],
nullptr, fps, input, scale, niter, device, pixelformat, image_type, opath);
222 usage(argv[0], optarg_, fps, input, scale, niter, device, pixelformat, image_type, opath);
227 if ((c == 1) || (c == -1)) {
229 usage(argv[0],
nullptr, fps, input, scale, niter, device, pixelformat, image_type, opath);
230 std::cerr <<
"ERROR: " << std::endl;
231 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
247int main(
int argc,
const char **argv)
250#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
256 unsigned int opt_fps = 25;
257 unsigned int opt_input = 0;
258 unsigned int opt_scale = 1;
261 bool opt_verbose =
false;
262 bool opt_display =
true;
263 std::string opt_device =
"/dev/video0";
264 bool opt_save =
false;
266 std::string opt_opath =
"/tmp/I%04d.ppm";
268 vpImage_type opt_image_type = color_image;
271 if (getOptions(argc, argv, opt_fps, opt_input, opt_scale, opt_display, opt_verbose, opt_iter, opt_device,
272 opt_pixelformat, opt_image_type, opt_save, opt_opath) ==
false) {
296 if (opt_image_type == grey_image) {
301 std::cout <<
"Grey image size: width : " << Ig.
getWidth() <<
" height: " << Ig.
getHeight() << std::endl;
308 std::cout <<
"Color image size: width : " << Ic.
getWidth() <<
" height: " << Ic.
getHeight() << std::endl;
317 if (opt_image_type == grey_image) {
318 display->init(Ig, 100, 100,
"V4L2 grey images framegrabbing");
323 display->init(Ic, 100, 100,
"V4L2 color images framegrabbing");
330 while (cpt++ < opt_iter) {
334 if (opt_image_type == grey_image) {
354 char buf[FILENAME_MAX];
355 snprintf(buf, FILENAME_MAX, opt_opath.c_str(), cpt);
357 std::cout <<
"Write: " <<
filename << std::endl;
358 if (opt_image_type == grey_image) {
371#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
372 if (display !=
nullptr) {
379 std::cout <<
"Catch an exception: " <<
e << std::endl;
380#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
381 if (display !=
nullptr) {
391 std::cout <<
"You do not have X11, or GTK functionalities to display images..." << std::endl;
392 std::cout <<
"Tip if you are on a unix-like system:" << std::endl;
393 std::cout <<
"- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
394 std::cout <<
"Tip if you are on a windows-like system:" << std::endl;
395 std::cout <<
"- Install GTK, configure again ViSP using cmake and build again this example" << std::endl;
402 std::cout <<
"You do not have Video 4 Linux 2 functionality enabled" << std::endl;
403 std::cout <<
"Tip if you are on a unix-like system:" << std::endl;
404 std::cout <<
"- Install libv4l2, configure again ViSP using cmake and build again this example" << std::endl;
Class that defines generic functionalities for display.
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)
Definition of the vpImage class member functions.
unsigned int getWidth() const
unsigned int getHeight() const
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
@ framerate_50fps
50 frames per second
@ framerate_25fps
25 frames per second
void setFramerate(vpV4l2FramerateType framerate)
void setVerboseMode(bool verbose)
void setInput(unsigned input=vpV4l2Grabber::DEFAULT_INPUT)
void open(vpImage< unsigned char > &I)
void setScale(unsigned scale=vpV4l2Grabber::DEFAULT_SCALE)
void setPixelFormat(vpV4l2PixelFormatType pixelformat)
void setDevice(const std::string &devname)
void acquire(vpImage< unsigned char > &I)
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()