38 #include <visp3/core/vpConfig.h>
39 #include <visp3/core/vpDebug.h>
49 #if (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK))
51 #include <visp3/core/vpDisplay.h>
52 #include <visp3/core/vpImage.h>
53 #include <visp3/core/vpTime.h>
54 #include <visp3/gui/vpDisplayGTK.h>
55 #include <visp3/gui/vpDisplayX.h>
56 #include <visp3/io/vpImageIo.h>
57 #include <visp3/io/vpParseArgv.h>
58 #include <visp3/sensor/vpV4l2Grabber.h>
61 #define GETOPTARGS "df:i:hn:o:p:s:t:v:x"
84 void usage(
const char *name,
const char *badparam,
unsigned fps,
unsigned input,
unsigned scale,
long niter,
86 const std::string &opath)
89 Grab grey level images using the Video For Linux Two framegrabber. \n\
90 Display these images using X11 or GTK.\n\
93 %s [-v <video device>] [-f <fps=25|50>] \n\
94 [-i <input=0|1|2|3> [-s <scale=1|2|4>] [-p <pixel format>]\n\
95 [-n <niter>] [-t <image type>] [-o <filename>] [-x] [-d] [-h]\n",
100 -v <video device> %s\n\
101 Video device to access to the camera\n\
104 Framerate in term od number of images per second.\n\
105 Possible values are 25 (for 25Hz) or 50 (for %%) Hz)\n\
108 Framegrabber active input. Values can be 0, 1, 2, 4\n\
110 -p <pixel format> %d\n\
111 Camera pixel format. Values must be in [0-%d]:\n\
113 1 for RGB24 format\n\
114 2 for RGB32 format\n\
115 3 for BGR24 format\n\
118 -t <image type> %d\n\
119 Kind of images that are acquired/displayed by ViSP. \n\
120 Values must be in [0-1]:\n\
121 0 for grey images in unsigned char \n\
122 1 for color images in vpRGBa\n\
125 Framegrabber subsampling factor. \n\
126 If 1, full resolution image acquisition.\n\
127 If 2, half resolution image acquisition. The \n\
128 subsampling is achieved by the hardware.\n\
131 Number of images to acquire.\n\
134 Turn off the display.\n\
137 Activates the extra verbose mode.\n\
139 -o [%%s] : Filename for image saving. \n\
141 The %%d is for the image numbering. The format is set \n\
142 by the extension of the file (ex .png, .pgm, ...) \n\
145 Print the help.\n\n",
150 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
174 bool getOptions(
int argc,
const char **argv,
unsigned &fps,
unsigned &input,
unsigned &scale,
bool &display,
176 vpImage_type &image_type,
bool &save, std::string &opath)
187 fps = (unsigned)atoi(optarg_);
190 input = (unsigned)atoi(optarg_);
193 niter = atol(optarg_);
203 scale = (unsigned)atoi(optarg_);
206 image_type = (vpImage_type)atoi(optarg_);
209 device = std::string(optarg_);
215 usage(argv[0],
nullptr, fps, input, scale, niter, device, pixelformat, image_type, opath);
220 usage(argv[0], optarg_, fps, input, scale, niter, device, pixelformat, image_type, opath);
226 if ((c == 1) || (c == -1)) {
228 usage(argv[0],
nullptr, fps, input, scale, niter, device, pixelformat, image_type, opath);
229 std::cerr <<
"ERROR: " << std::endl;
230 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
246 int main(
int argc,
const char **argv)
249 unsigned int opt_fps = 25;
250 unsigned int opt_input = 0;
251 unsigned int opt_scale = 1;
254 bool opt_verbose =
false;
255 bool opt_display =
true;
256 std::string opt_device =
"/dev/video0";
257 bool opt_save =
false;
259 std::string opt_opath =
"/tmp/I%04d.ppm";
261 vpImage_type opt_image_type = color_image;
264 if (getOptions(argc, argv, opt_fps, opt_input, opt_scale, opt_display, opt_verbose, opt_iter, opt_device,
265 opt_pixelformat, opt_image_type, opt_save, opt_opath) ==
false) {
289 if (opt_image_type == grey_image) {
294 std::cout <<
"Grey image size: width : " << Ig.
getWidth() <<
" height: " << Ig.
getHeight() << std::endl;
300 std::cout <<
"Color image size: width : " << Ic.
getWidth() <<
" height: " << Ic.
getHeight() << std::endl;
305 #if defined(VISP_HAVE_X11)
307 #elif defined(VISP_HAVE_GTK)
317 if (opt_image_type == grey_image) {
318 display.init(Ig, 100, 100,
"V4L2 grey images framegrabbing");
322 display.init(Ic, 100, 100,
"V4L2 color images framegrabbing");
329 while (cpt++ < opt_iter) {
333 if (opt_image_type == grey_image) {
352 char buf[FILENAME_MAX];
353 snprintf(buf, FILENAME_MAX, opt_opath.c_str(), cpt);
354 std::string filename(buf);
355 std::cout <<
"Write: " << filename << std::endl;
356 if (opt_image_type == grey_image) {
370 std::cout <<
"Catch an exception: " << e << std::endl;
377 std::cout <<
"You do not have X11, or GTK functionalities to display images..." << std::endl;
378 std::cout <<
"Tip if you are on a unix-like system:" << std::endl;
379 std::cout <<
"- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
380 std::cout <<
"Tip if you are on a windows-like system:" << std::endl;
381 std::cout <<
"- Install GTK, configure again ViSP using cmake and build again this example" << std::endl;
388 std::cout <<
"You do not have Video 4 Linux 2 functionality enabled" << std::endl;
389 std::cout <<
"Tip if you are on a unix-like system:" << std::endl;
390 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...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
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)
void display(vpImage< unsigned char > &I, const std::string &title)
Display a gray-scale image.
VISP_EXPORT double measureTimeMs()