45 #include <visp3/core/vpConfig.h>
46 #include <visp3/core/vpDebug.h>
50 #if (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK))
52 #include <visp3/core/vpDisplay.h>
53 #include <visp3/core/vpImage.h>
54 #include <visp3/core/vpTime.h>
55 #include <visp3/gui/vpDisplayGTK.h>
56 #include <visp3/gui/vpDisplayX.h>
57 #include <visp3/io/vpImageIo.h>
58 #include <visp3/io/vpParseArgv.h>
59 #include <visp3/sensor/vpV4l2Grabber.h>
62 #define GETOPTARGS "df:i:hn:o:p:s:t:v:x"
64 #ifdef ENABLE_VISP_NAMESPACE
90 void usage(
const char *name,
const char *badparam,
unsigned fps,
unsigned input,
unsigned scale,
long niter,
92 const std::string &opath)
95 Grab grey level images using the Video For Linux Two framegrabber. \n\
96 Display these images using X11 or GTK.\n\
99 %s [-v <video device>] [-f <fps=25|50>] \n\
100 [-i <input=0|1|2|3> [-s <scale=1|2|4>] [-p <pixel format>]\n\
101 [-n <niter>] [-t <image type>] [-o <filename>] [-x] [-d] [-h]\n",
106 -v <video device> %s\n\
107 Video device to access to the camera\n\
110 Framerate in term od number of images per second.\n\
111 Possible values are 25 (for 25Hz) or 50 (for %%) Hz)\n\
114 Framegrabber active input. Values can be 0, 1, 2, 4\n\
116 -p <pixel format> %d\n\
117 Camera pixel format. Values must be in [0-%d]:\n\
119 1 for RGB24 format\n\
120 2 for RGB32 format\n\
121 3 for BGR24 format\n\
124 -t <image type> %d\n\
125 Kind of images that are acquired/displayed by ViSP. \n\
126 Values must be in [0-1]:\n\
127 0 for grey images in unsigned char \n\
128 1 for color images in vpRGBa\n\
131 Framegrabber subsampling factor. \n\
132 If 1, full resolution image acquisition.\n\
133 If 2, half resolution image acquisition. The \n\
134 subsampling is achieved by the hardware.\n\
137 Number of images to acquire.\n\
140 Turn off the display.\n\
143 Activates the extra verbose mode.\n\
145 -o [%%s] : Filename for image saving. \n\
147 The %%d is for the image numbering. The format is set \n\
148 by the extension of the file (ex .png, .pgm, ...) \n\
151 Print the help.\n\n",
156 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
180 bool getOptions(
int argc,
const char **argv,
unsigned &fps,
unsigned &input,
unsigned &scale,
bool &display,
182 vpImage_type &image_type,
bool &save, std::string &opath)
193 fps = (unsigned)atoi(optarg_);
196 input = (unsigned)atoi(optarg_);
199 niter = atol(optarg_);
209 scale = (unsigned)atoi(optarg_);
212 image_type = (vpImage_type)atoi(optarg_);
215 device = std::string(optarg_);
221 usage(argv[0],
nullptr, fps, input, scale, niter, device, pixelformat, image_type, opath);
226 usage(argv[0], optarg_, fps, input, scale, niter, device, pixelformat, image_type, opath);
232 if ((c == 1) || (c == -1)) {
234 usage(argv[0],
nullptr, fps, input, scale, niter, device, pixelformat, image_type, opath);
235 std::cerr <<
"ERROR: " << std::endl;
236 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
252 int main(
int argc,
const char **argv)
255 unsigned int opt_fps = 25;
256 unsigned int opt_input = 0;
257 unsigned int opt_scale = 1;
260 bool opt_verbose =
false;
261 bool opt_display =
true;
262 std::string opt_device =
"/dev/video0";
263 bool opt_save =
false;
265 std::string opt_opath =
"/tmp/I%04d.ppm";
267 vpImage_type opt_image_type = color_image;
270 if (getOptions(argc, argv, opt_fps, opt_input, opt_scale, opt_display, opt_verbose, opt_iter, opt_device,
271 opt_pixelformat, opt_image_type, opt_save, opt_opath) ==
false) {
295 if (opt_image_type == grey_image) {
300 std::cout <<
"Grey image size: width : " << Ig.
getWidth() <<
" height: " << Ig.
getHeight() << std::endl;
307 std::cout <<
"Color image size: width : " << Ic.
getWidth() <<
" height: " << Ic.
getHeight() << std::endl;
312 #if defined(VISP_HAVE_X11)
314 #elif defined(VISP_HAVE_GTK)
324 if (opt_image_type == grey_image) {
325 display.init(Ig, 100, 100,
"V4L2 grey images framegrabbing");
330 display.init(Ic, 100, 100,
"V4L2 color images framegrabbing");
337 while (cpt++ < opt_iter) {
341 if (opt_image_type == grey_image) {
361 char buf[FILENAME_MAX];
362 snprintf(buf, FILENAME_MAX, opt_opath.c_str(), cpt);
363 std::string filename(buf);
364 std::cout <<
"Write: " << filename << std::endl;
365 if (opt_image_type == grey_image) {
381 std::cout <<
"Catch an exception: " << e << std::endl;
388 std::cout <<
"You do not have X11, or GTK functionalities to display images..." << std::endl;
389 std::cout <<
"Tip if you are on a unix-like system:" << std::endl;
390 std::cout <<
"- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
391 std::cout <<
"Tip if you are on a windows-like system:" << std::endl;
392 std::cout <<
"- Install GTK, configure again ViSP using cmake and build again this example" << std::endl;
399 std::cout <<
"You do not have Video 4 Linux 2 functionality enabled" << std::endl;
400 std::cout <<
"Tip if you are on a unix-like system:" << std::endl;
401 std::cout <<
"- Install libv4l2, configure again ViSP using cmake and build again this example" << std::endl;
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
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)
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)
VISP_EXPORT double measureTimeMs()