File: grabV4l2.cpp

package info (click to toggle)
visp 3.6.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 119,296 kB
  • sloc: cpp: 500,914; ansic: 52,904; xml: 22,642; python: 7,365; java: 4,247; sh: 482; makefile: 237; objc: 145
file content (393 lines) | stat: -rw-r--r-- 12,861 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/****************************************************************************
 *
 * ViSP, open source Visual Servoing Platform software.
 * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
 *
 * This software is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * See the file LICENSE.txt at the root directory of this source
 * distribution for additional information about the GNU GPL.
 *
 * For using ViSP with software that can not be combined with the GNU
 * GPL, please contact Inria about acquiring a ViSP Professional
 * Edition License.
 *
 * See https://visp.inria.fr for more information.
 *
 * This software was developed at:
 * Inria Rennes - Bretagne Atlantique
 * Campus Universitaire de Beaulieu
 * 35042 Rennes Cedex
 * France
 *
 * If you have questions regarding the use of this file, please contact
 * Inria at visp@inria.fr
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * Description:
 * Acquire images using 1394 device with cfox (MAC OSX) and display it
 * using GTK or GTK.
 *
*****************************************************************************/

#include <stdlib.h>
#include <visp3/core/vpConfig.h>
#include <visp3/core/vpDebug.h>
/*!
  \file grabV4l2.cpp

  \brief Example of image framegrabbing using vpV4l2Grabber class.

*/

#ifdef VISP_HAVE_V4L2

#if (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK))

#include <visp3/core/vpDisplay.h>
#include <visp3/core/vpImage.h>
#include <visp3/core/vpTime.h>
#include <visp3/gui/vpDisplayGTK.h>
#include <visp3/gui/vpDisplayX.h>
#include <visp3/io/vpImageIo.h>
#include <visp3/io/vpParseArgv.h>
#include <visp3/sensor/vpV4l2Grabber.h>

// List of allowed command line options
#define GETOPTARGS "df:i:hn:o:p:s:t:v:x"

typedef enum {
  grey_image = 0, // for ViSP unsigned char grey images
  color_image     // for ViSP vpRGBa color images
} vpImage_type;

/*!

  Print the program options.

  \param name : Program name.
  \param badparam : Bad parameter name.
  \param fps : Framerate.
  \param input : Card input number.
  \param scale : Subsampling factor.
  \param niter : Number of images to acquire.
  \param device : Video device name.
  \param pixelformat : Pixel format.
  \param image_type : 0 for unsigned char, 1 for vpRGBa images
  \param opath : Image filename when saving.

*/
void usage(const char *name, const char *badparam, unsigned fps, unsigned input, unsigned scale, long niter,
           const std::string &device, vpV4l2Grabber::vpV4l2PixelFormatType pixelformat, const vpImage_type &image_type,
           const std::string &opath)
{
  fprintf(stdout, "\n\
Grab grey level images using the Video For Linux Two framegrabber. \n\
Display these images using X11 or GTK.\n\
\n\
SYNOPSIS\n\
  %s [-v <video device>] [-f <fps=25|50>] \n\
     [-i <input=0|1|2|3> [-s <scale=1|2|4>] [-p <pixel format>]\n\
     [-n <niter>] [-t <image type>] [-o <filename>] [-x] [-d] [-h]\n",
          name);

  fprintf(stdout, "\n\
OPTIONS:                                                  Default\n\
  -v <video device>                                         %s\n\
     Video device to access to the camera\n\
\n\
  -f <fps>                                                  %u\n\
     Framerate in term od number of images per second.\n\
     Possible values are 25 (for 25Hz) or 50 (for %%) Hz)\n\
\n\
  -i <input>                                                %u\n\
     Framegrabber active input. Values can be 0, 1, 2, 4\n\
\n\
  -p <pixel format>                                         %d\n\
     Camera pixel format. Values must be in [0-%d]:\n\
       0 for gray format\n\
       1 for RGB24 format\n\
       2 for RGB32 format\n\
       3 for BGR24 format\n\
       4 for YUYV format\n\
\n\
  -t <image type>                                           %d\n\
     Kind of images that are acquired/displayed by ViSP. \n\
     Values must be in [0-1]:\n\
       0 for grey images in unsigned char \n\
       1 for color images in vpRGBa\n\
\n\
  -s <scale>                                                %u\n\
     Framegrabber subsampling factor. \n\
     If 1, full resolution image acquisition.\n\
     If 2, half resolution image acquisition. The \n\
     subsampling is achieved by the hardware.\n\
\n\
  -n <niter>                                                %ld\n\
     Number of images to acquire.\n\
\n\
  -d \n\
     Turn off the display.\n\
\n\
  -x \n\
     Activates the extra verbose mode.\n\
\n\
  -o [%%s] : Filename for image saving.                     \n\
     Example: -o %s\n\
     The %%d is for the image numbering. The format is set \n\
     by the extension of the file (ex .png, .pgm, ...) \n\
                    \n\
  -h \n\
     Print the help.\n\n",
          device.c_str(), fps, input, pixelformat, vpV4l2Grabber::V4L2_MAX_FORMAT - 1, image_type, scale, niter,
          opath.c_str());

  if (badparam)
    fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
}

/*!

  Set the program options.

  \param argc : Command line number of parameters.
  \param argv : Array of command line parameters.
  \param fps : Framerate.
  \param input : Card input.
  \param scale : Subsampling factor.
  \param display : Display activation.
  \param verbose : Verbose mode activation.
  \param niter : Number of images to acquire.
  \param device : Video device name.
  \param pixelformat : Pixel format.
  \param image_type : 0 for unsigned char, 1 for vpRGBa images
  \param save : Image saving activation.
  \param opath : Image filename when saving.

  \return false if the program has to be stopped, true otherwise.

*/
bool getOptions(int argc, const char **argv, unsigned &fps, unsigned &input, unsigned &scale, bool &display,
                bool &verbose, long &niter, std::string &device, vpV4l2Grabber::vpV4l2PixelFormatType &pixelformat,
                vpImage_type &image_type, bool &save, std::string &opath)
{
  const char *optarg_;
  int c;
  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {

    switch (c) {
    case 'd':
      display = false;
      break;
    case 'f':
      fps = (unsigned)atoi(optarg_);
      break;
    case 'i':
      input = (unsigned)atoi(optarg_);
      break;
    case 'n':
      niter = atol(optarg_);
      break;
    case 'o':
      save = true;
      opath = optarg_;
      break;
    case 'p':
      pixelformat = (vpV4l2Grabber::vpV4l2PixelFormatType)atoi(optarg_);
      break;
    case 's':
      scale = (unsigned)atoi(optarg_);
      break;
    case 't':
      image_type = (vpImage_type)atoi(optarg_);
      break;
    case 'v':
      device = std::string(optarg_);
      break;
    case 'x':
      verbose = true;
      break;
    case 'h':
      usage(argv[0], NULL, fps, input, scale, niter, device, pixelformat, image_type, opath);
      return false;
      break;

    default:
      usage(argv[0], optarg_, fps, input, scale, niter, device, pixelformat, image_type, opath);
      return false;
      break;
    }
  }

  if ((c == 1) || (c == -1)) {
    // standalone param or error
    usage(argv[0], NULL, fps, input, scale, niter, device, pixelformat, image_type, opath);
    std::cerr << "ERROR: " << std::endl;
    std::cerr << "  Bad argument " << optarg_ << std::endl << std::endl;
    return false;
  }

  return true;
}

/*!
  \example grabV4l2.cpp

  Example of grey level images framegrabbing using vpV4l2Grabber class.

  Test frame grabbing capabilities using video for linux two (V4L2) video
  device.  Only grabbing of grey level images is possible in this
  example. Display these images using X11 or GTK.
*/
int main(int argc, const char **argv)
{
  try {
    unsigned int opt_fps = 25;
    unsigned int opt_input = 0;
    unsigned int opt_scale = 1;
    vpV4l2Grabber::vpV4l2PixelFormatType opt_pixelformat = vpV4l2Grabber::V4L2_YUYV_FORMAT;
    long opt_iter = 100;
    bool opt_verbose = false;
    bool opt_display = true;
    std::string opt_device = "/dev/video0";
    bool opt_save = false;
    // Default output path for image saving
    std::string opt_opath = "/tmp/I%04d.ppm";

    vpImage_type opt_image_type = color_image;

    // Read the command line options
    if (getOptions(argc, argv, opt_fps, opt_input, opt_scale, opt_display, opt_verbose, opt_iter, opt_device,
                   opt_pixelformat, opt_image_type, opt_save, opt_opath) == false) {
      return EXIT_FAILURE;
    }

    // Declare an image, this is a gray level image (unsigned char) and
    // an other one that is a color image. There size is not defined
    // yet. It will be defined when the image will acquired the first
    // time.
    vpImage<unsigned char> Ig; // grey level image
    vpImage<vpRGBa> Ic;        // color image

    // Creates the grabber
    vpV4l2Grabber g;

    // Initialize the grabber
    g.setVerboseMode(opt_verbose);
    g.setDevice(opt_device);
    g.setInput(opt_input);
    g.setScale(opt_scale);
    g.setPixelFormat(opt_pixelformat);
    if (opt_fps == 25)
      g.setFramerate(vpV4l2Grabber::framerate_25fps);
    else
      g.setFramerate(vpV4l2Grabber::framerate_50fps);
    if (opt_image_type == grey_image) {
      // Open the framegrabber with the specified settings on grey images
      g.open(Ig);
      // Acquire an image
      g.acquire(Ig);
      std::cout << "Grey image size: width : " << Ig.getWidth() << " height: " << Ig.getHeight() << std::endl;
    } else {
      // Open the framegrabber with the specified settings on color images
      g.open(Ic);
      // Acquire an image
      g.acquire(Ic);
      std::cout << "Color image size: width : " << Ic.getWidth() << " height: " << Ic.getHeight() << std::endl;
    }

// We open a window using either X11 or GTK.
// Its size is automatically defined by the image (I) size
#if defined(VISP_HAVE_X11)
    vpDisplayX display;
#elif defined(VISP_HAVE_GTK)
    vpDisplayGTK display;
#endif

    if (opt_display) {
      // Display the image
      // The image class has a member that specify a pointer toward
      // the display that has been initialized in the display declaration
      // therefore is is no longer necessary to make a reference to the
      // display variable.
      if (opt_image_type == grey_image) {
        display.init(Ig, 100, 100, "V4L2 grey images framegrabbing");
        vpDisplay::display(Ig);
        vpDisplay::flush(Ig);
      } else {
        display.init(Ic, 100, 100, "V4L2 color images framegrabbing");
        vpDisplay::display(Ic);
        vpDisplay::flush(Ic);
      }
    }
    // Acquisition loop
    long cpt = 1;
    while (cpt++ < opt_iter) {
      // Measure the initial time of an iteration
      double t = vpTime::measureTimeMs();
      // Acquire the image
      if (opt_image_type == grey_image) {
        g.acquire(Ig);
        if (opt_display) {
          // Display the image
          vpDisplay::display(Ig);
          // Flush the display
          vpDisplay::flush(Ig);
        }
      } else {
        g.acquire(Ic);
        if (opt_display) {
          // Display the image
          vpDisplay::display(Ic);
          // Flush the display
          vpDisplay::flush(Ic);
        }
      }

      if (opt_save) {
        char buf[FILENAME_MAX];
        snprintf(buf, FILENAME_MAX, opt_opath.c_str(), cpt);
        std::string filename(buf);
        std::cout << "Write: " << filename << std::endl;
        if (opt_image_type == grey_image) {
          vpImageIo::write(Ig, filename);
        } else {
          vpImageIo::write(Ic, filename);
        }
      }

      // Print the iteration duration
      std::cout << "time: " << vpTime::measureTimeMs() - t << " (ms)" << std::endl;
    }

    g.close();
    return EXIT_SUCCESS;
  } catch (const vpException &e) {
    std::cout << "Catch an exception: " << e << std::endl;
    return EXIT_FAILURE;
  }
}
#else
int main()
{
  std::cout << "You do not have X11, or GTK functionalities to display images..." << std::endl;
  std::cout << "Tip if you are on a unix-like system:" << std::endl;
  std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
  std::cout << "Tip if you are on a windows-like system:" << std::endl;
  std::cout << "- Install GTK, configure again ViSP using cmake and build again this example" << std::endl;
  return EXIT_SUCCESS;
}
#endif
#else
int main()
{
  std::cout << "You do not have Video 4 Linux 2 functionality enabled" << std::endl;
  std::cout << "Tip if you are on a unix-like system:" << std::endl;
  std::cout << "- Install libv4l2, configure again ViSP using cmake and build again this example" << std::endl;
  return EXIT_SUCCESS;
}
#endif