44 #include <visp/vpConfig.h>
45 #include <visp/vpDebug.h>
56 #if (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK))
58 #include <visp/vpDisplay.h>
59 #include <visp/vpDisplayX.h>
60 #include <visp/vpDisplayGTK.h>
61 #include <visp/vpImage.h>
62 #include <visp/vpImageIo.h>
63 #include <visp/vpTime.h>
64 #include <visp/vpParseArgv.h>
65 #include <visp/vpV4l2Grabber.h>
68 #define GETOPTARGS "df:i:hn:o:p:s:t:v:x"
91 void usage(
const char *name,
const char *badparam,
unsigned fps,
92 unsigned input,
unsigned scale,
long niter,
char *device,
94 const vpImage_type &image_type,
const std::string &opath)
97 Grab grey level images using the Video For Linux Two framegrabber. \n\
98 Display these images using X11 or GTK.\n\
101 %s [-v <video device>] [-f <fps=25|50>] \n\
102 [-i <input=0|1|2|3> [-s <scale=1|2|4>] [-p <pixel format>]\n\
103 [-n <niter>] [-t <image type>] [-o <filename>] [-x] [-d] [-h]\n", name);
107 -v <video device> %s\n\
108 Video device to access to the camera\n\
111 Framerate in term od number of images per second.\n\
112 Possible values are 25 (for 25Hz) or 50 (for %%) Hz)\n\
115 Framegrabber active input. Values can be 0, 1, 2, 4\n\
117 -p <pixel format> %d\n\
118 Camera pixel format. Values must be in [0-%d]:\n\
120 1 for RGB24 format\n\
121 2 for RGB32 format\n\
122 3 for BGR24 format\n\
125 -t <image type> %d\n\
126 Kind of images that are acquired/displayed by ViSP. \n\
127 Values must be in [0-1]:\n\
128 0 for grey images in unsigned char \n\
129 1 for color images in vpRGBa\n\
132 Framegrabber subsampling factor. \n\
133 If 1, full resolution image acquisition.\n\
134 If 2, half resolution image acquisition. The \n\
135 subsampling is achieved by the hardware.\n\
138 Number of images to acquire.\n\
141 Turn off the display.\n\
144 Activates the extra verbose mode.\n\
146 -o [%%s] : Filename for image saving. \n\
148 The %%d is for the image numbering. The format is set \n\
149 by the extension of the file (ex .png, .pgm, ...) \n\
152 Print the help.\n\n",
153 device, fps, input, pixelformat,
157 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
181 bool getOptions(
int argc,
const char **argv,
unsigned &fps,
unsigned &input,
182 unsigned &scale,
bool &display,
bool &verbose,
183 long &niter,
char *device,
185 vpImage_type &image_type,
bool &save, std::string &opath)
192 case 'd': display =
false;
break;
193 case 'f': fps = (unsigned) atoi(optarg);
break;
194 case 'i': input = (unsigned) atoi(optarg);
break;
195 case 'n': niter = atol(optarg);
break;
198 opath = optarg;
break;
200 case 's': scale = (unsigned) atoi(optarg);
break;
201 case 't': image_type = (vpImage_type) atoi(optarg);
break;
202 case 'v': sprintf(device,
"%s", optarg);
break;
203 case 'x': verbose =
true;
break;
204 case 'h': usage(argv[0], NULL, fps, input, scale, niter,
205 device, pixelformat, image_type, opath);
209 usage(argv[0], optarg, fps, input, scale, niter,
210 device, pixelformat, image_type, opath);
return false;
break;
214 if ((c == 1) || (c == -1)) {
216 usage(argv[0], NULL, fps, input, scale, niter,
217 device, pixelformat, image_type, opath);
218 std::cerr <<
"ERROR: " << std::endl;
219 std::cerr <<
" Bad argument " << optarg << std::endl << std::endl;
236 main(
int argc,
const char ** argv)
239 unsigned int opt_fps = 25;
240 unsigned int opt_input = 0;
241 unsigned int opt_scale = 1;
244 bool opt_verbose =
false;
245 bool opt_display =
true;
247 bool opt_save =
false;
248 sprintf(opt_device,
"/dev/video0");
250 std::string opt_opath =
"/tmp/I%04d.ppm";
252 vpImage_type opt_image_type = color_image;
255 if (getOptions(argc, argv, opt_fps, opt_input, opt_scale, opt_display,
256 opt_verbose, opt_iter, opt_device,
257 opt_pixelformat, opt_image_type, opt_save, opt_opath) ==
false) {
281 if (opt_image_type == grey_image) {
286 std::cout <<
"Grey image size: width : " << Ig.
getWidth() <<
" height: "
294 std::cout <<
"Color image size: width : " << Ic.
getWidth() <<
" height: "
301 #if defined VISP_HAVE_X11
303 #elif defined VISP_HAVE_GTK
313 if (opt_image_type == grey_image) {
314 display.
init(Ig, 100, 100,
"V4L2 grey images framegrabbing") ;
319 display.
init(Ic, 100, 100,
"V4L2 color images framegrabbing") ;
326 while(cpt ++ < opt_iter)
331 if (opt_image_type == grey_image) {
351 char buf[FILENAME_MAX];
352 sprintf(buf, opt_opath.c_str(), cpt);
353 std::string filename(buf);
354 std::cout <<
"Write: " << filename << std::endl;
355 if (opt_image_type == grey_image) {
371 std::cout <<
"Catch an exception: " << e << std::endl;
379 vpTRACE(
"X11 or GTK display are not available") ;
386 vpTRACE(
"Video 4 Linux 2 frame grabber drivers are not available") ;
static void write(const vpImage< unsigned char > &I, const char *filename)
void acquire(vpImage< unsigned char > &I)
void open(vpImage< unsigned char > &I)
unsigned int getWidth() const
Define the X11 console to display images.
void setDevice(const char *devname)
error that can be emited by ViSP classes.
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
void setVerboseMode(bool verbose)
static double measureTimeMs()
static void flush(const vpImage< unsigned char > &I)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
static void display(const vpImage< unsigned char > &I)
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
void setScale(unsigned scale=vpV4l2Grabber::DEFAULT_SCALE)
void setInput(unsigned input=vpV4l2Grabber::DEFAULT_INPUT)
Class for the Video4Linux2 video device.
unsigned int getHeight() const
void setPixelFormat(vpV4l2PixelFormatType pixelformat)
void setFramerate(vpV4l2FramerateType framerate)