ViSP  3.0.0
grabV4l2.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Acquire images using 1394 device with cfox (MAC OSX) and display it
32  * using GTK or GTK.
33  *
34  * Authors:
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
39 
40 #include <visp3/core/vpConfig.h>
41 #include <visp3/core/vpDebug.h>
42 #include <stdlib.h>
50 #ifdef VISP_HAVE_V4L2
51 
52 #if (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK))
53 
54 #include <visp3/core/vpDisplay.h>
55 #include <visp3/gui/vpDisplayX.h>
56 #include <visp3/gui/vpDisplayGTK.h>
57 #include <visp3/core/vpImage.h>
58 #include <visp3/io/vpImageIo.h>
59 #include <visp3/core/vpTime.h>
60 #include <visp3/io/vpParseArgv.h>
61 #include <visp3/sensor/vpV4l2Grabber.h>
62 
63 // List of allowed command line options
64 #define GETOPTARGS "df:i:hn:o:p:s:t:v:x"
65 
66 typedef enum {
67  grey_image = 0, // for ViSP unsigned char grey images
68  color_image // for ViSP vpRGBa color images
69 } vpImage_type;
70 
87 void usage(const char *name, const char *badparam, unsigned fps,
88  unsigned input, unsigned scale, long niter, char *device,
90  const vpImage_type &image_type, const std::string &opath)
91 {
92  fprintf(stdout, "\n\
93 Grab grey level images using the Video For Linux Two framegrabber. \n\
94 Display these images using X11 or GTK.\n\
95 \n\
96 SYNOPSIS\n\
97  %s [-v <video device>] [-f <fps=25|50>] \n\
98  [-i <input=0|1|2|3> [-s <scale=1|2|4>] [-p <pixel format>]\n\
99  [-n <niter>] [-t <image type>] [-o <filename>] [-x] [-d] [-h]\n", name);
100 
101  fprintf(stdout, "\n\
102 OPTIONS: Default\n\
103  -v <video device> %s\n\
104  Video device to access to the camera\n\
105 \n\
106  -f <fps> %u\n\
107  Framerate in term od number of images per second.\n\
108  Possible values are 25 (for 25Hz) or 50 (for %%) Hz)\n\
109 \n\
110  -i <input> %u\n\
111  Framegrabber active input. Values can be 0, 1, 2, 4\n\
112 \n\
113  -p <pixel format> %d\n\
114  Camera pixel format. Values must be in [0-%d]:\n\
115  0 for gray format\n\
116  1 for RGB24 format\n\
117  2 for RGB32 format\n\
118  3 for BGR24 format\n\
119  4 for YUYV format\n\
120 \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\
126 \n\
127  -s <scale> %u\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\
132 \n\
133  -n <niter> %ld\n\
134  Number of images to acquire.\n\
135 \n\
136  -d \n\
137  Turn off the display.\n\
138 \n\
139  -x \n\
140  Activates the extra verbose mode.\n\
141 \n\
142  -o [%%s] : Filename for image saving. \n\
143  Example: -o %s\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\
146  \n\
147  -h \n\
148  Print the help.\n\n",
149  device, fps, input, pixelformat,
150  vpV4l2Grabber::V4L2_MAX_FORMAT-1, image_type, scale, niter, opath.c_str());
151 
152  if (badparam)
153  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
154 }
155 
177 bool getOptions(int argc, const char **argv, unsigned &fps, unsigned &input,
178  unsigned &scale, bool &display, bool &verbose,
179  long &niter, char *device,
181  vpImage_type &image_type, bool &save, std::string &opath)
182 {
183  const char *optarg_;
184  int c;
185  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
186 
187  switch (c) {
188  case 'd': display = false; break;
189  case 'f': fps = (unsigned) atoi(optarg_); break;
190  case 'i': input = (unsigned) atoi(optarg_); break;
191  case 'n': niter = atol(optarg_); break;
192  case 'o':
193  save = true;
194  opath = optarg_; break;
195  case 'p': pixelformat = (vpV4l2Grabber::vpV4l2PixelFormatType) atoi(optarg_); break;
196  case 's': scale = (unsigned) atoi(optarg_); break;
197  case 't': image_type = (vpImage_type) atoi(optarg_); break;
198  case 'v': sprintf(device, "%s", optarg_); break;
199  case 'x': verbose = true; break;
200  case 'h': usage(argv[0], NULL, fps, input, scale, niter,
201  device, pixelformat, image_type, opath);
202  return false; break;
203 
204  default:
205  usage(argv[0], optarg_, fps, input, scale, niter,
206  device, pixelformat, image_type, opath); return false; break;
207  }
208  }
209 
210  if ((c == 1) || (c == -1)) {
211  // standalone param or error
212  usage(argv[0], NULL, fps, input, scale, niter,
213  device, pixelformat, image_type, opath);
214  std::cerr << "ERROR: " << std::endl;
215  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
216  return false;
217  }
218 
219  return true;
220 }
221 
231 int
232 main(int argc, const char ** argv)
233 {
234  try {
235  unsigned int opt_fps = 25;
236  unsigned int opt_input = 0;
237  unsigned int opt_scale = 1;
239  long opt_iter = 100;
240  bool opt_verbose = false;
241  bool opt_display = true;
242  char opt_device[20];
243  bool opt_save = false;
244  sprintf(opt_device, "/dev/video0");
245  // Default output path for image saving
246  std::string opt_opath = "/tmp/I%04d.ppm";
247 
248  vpImage_type opt_image_type = color_image;
249 
250  // Read the command line options
251  if (getOptions(argc, argv, opt_fps, opt_input, opt_scale, opt_display,
252  opt_verbose, opt_iter, opt_device,
253  opt_pixelformat, opt_image_type, opt_save, opt_opath) == false) {
254  exit (-1);
255  }
256 
257  // Declare an image, this is a gray level image (unsigned char) and
258  // an other one that is a color image. There size is not defined
259  // yet. It will be defined when the image will acquired the first
260  // time.
261  vpImage<unsigned char> Ig ; // grey level image
262  vpImage<vpRGBa> Ic ; // color image
263 
264  // Creates the grabber
265  vpV4l2Grabber g;
266 
267  // Initialize the grabber
268  g.setVerboseMode(opt_verbose);
269  g.setDevice(opt_device);
270  g.setInput(opt_input);
271  g.setScale(opt_scale);
272  g.setPixelFormat(opt_pixelformat);
273  if (opt_fps == 25)
275  else
277  if (opt_image_type == grey_image) {
278  // Open the framegrabber with the specified settings on grey images
279  g.open(Ig) ;
280  // Acquire an image
281  g.acquire(Ig) ;
282  std::cout << "Grey image size: width : " << Ig.getWidth() << " height: "
283  << Ig.getHeight() << std::endl;
284  }
285  else {
286  // Open the framegrabber with the specified settings on color images
287  g.open(Ic) ;
288  // Acquire an image
289  g.acquire(Ic) ;
290  std::cout << "Color image size: width : " << Ic.getWidth() << " height: "
291  << Ic.getHeight() << std::endl;
292  }
293 
294 
295  // We open a window using either X11 or GTK.
296  // Its size is automatically defined by the image (I) size
297 #if defined VISP_HAVE_X11
298  vpDisplayX display;
299 #elif defined VISP_HAVE_GTK
300  vpDisplayGTK display;
301 #endif
302 
303  if (opt_display) {
304  // Display the image
305  // The image class has a member that specify a pointer toward
306  // the display that has been initialized in the display declaration
307  // therefore is is no longuer necessary to make a reference to the
308  // display variable.
309  if (opt_image_type == grey_image) {
310  display.init(Ig, 100, 100, "V4L2 grey images framegrabbing") ;
311  vpDisplay::display(Ig) ;
312  vpDisplay::flush(Ig) ;
313  }
314  else {
315  display.init(Ic, 100, 100, "V4L2 color images framegrabbing") ;
316  vpDisplay::display(Ic) ;
317  vpDisplay::flush(Ic) ;
318  }
319  }
320  // Acquisition loop
321  long cpt = 1;
322  while(cpt ++ < opt_iter)
323  {
324  // Measure the initial time of an iteration
325  double t = vpTime::measureTimeMs();
326  // Acquire the image
327  if (opt_image_type == grey_image) {
328  g.acquire(Ig) ;
329  if (opt_display) {
330  // Display the image
331  vpDisplay::display(Ig) ;
332  // Flush the display
333  vpDisplay::flush(Ig) ;
334  }
335  }
336  else {
337  g.acquire(Ic) ;
338  if (opt_display) {
339  // Display the image
340  vpDisplay::display(Ic) ;
341  // Flush the display
342  vpDisplay::flush(Ic) ;
343  }
344  }
345 
346  if (opt_save) {
347  char buf[FILENAME_MAX];
348  sprintf(buf, opt_opath.c_str(), cpt);
349  std::string filename(buf);
350  std::cout << "Write: " << filename << std::endl;
351  if (opt_image_type == grey_image) {
352  vpImageIo::write(Ig, filename);
353  }
354  else {
355  vpImageIo::write(Ic, filename);
356  }
357  }
358 
359  // Print the iteration duration
360  std::cout << "time: " << vpTime::measureTimeMs() - t << " (ms)" << std::endl;
361  }
362 
363  g.close();
364  return 0;
365  }
366  catch(vpException e) {
367  std::cout << "Catch an exception: " << e << std::endl;
368  return 1;
369  }
370 }
371 #else
372 int
373 main()
374 {
375  vpTRACE("X11 or GTK display are not available") ;
376 }
377 #endif
378 #else
379 int
380 main()
381 {
382  vpTRACE("Video 4 Linux 2 frame grabber drivers are not available") ;
383 }
384 #endif
385 
static void write(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:472
void acquire(vpImage< unsigned char > &I)
50 frames per second
void open(vpImage< unsigned char > &I)
unsigned int getWidth() const
Definition: vpImage.h:161
Define the X11 console to display images.
Definition: vpDisplayX.h:148
void setDevice(const std::string &devname)
error that can be emited by ViSP classes.
Definition: vpException.h:73
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
void setVerboseMode(bool verbose)
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2233
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
#define vpTRACE
Definition: vpDebug.h:414
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:141
void setScale(unsigned scale=vpV4l2Grabber::DEFAULT_SCALE)
void setInput(unsigned input=vpV4l2Grabber::DEFAULT_INPUT)
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:93
Class for the Video4Linux2 video device.
25 frames per second
unsigned int getHeight() const
Definition: vpImage.h:152
void setPixelFormat(vpV4l2PixelFormatType pixelformat)
void setFramerate(vpV4l2FramerateType framerate)