ViSP  2.8.0
grab1394Two.cpp
1 /****************************************************************************
2  *
3  * $Id: grab1394Two.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Firewire cameras video capture.
36  *
37  * Authors:
38  * Fabien Spindler
39  *
40  *****************************************************************************/
41 
42 
58 #include <visp/vpConfig.h>
59 #include <visp/vpDebug.h>
60 #include <stdlib.h>
61 #include <stdio.h>
62 #include <iostream>
63 #include <sstream>
64 #include <list>
65 #if defined(VISP_HAVE_DC1394_2)
66 
67 #include <visp/vp1394TwoGrabber.h>
68 #include <visp/vpImage.h>
69 #include <visp/vpImageIo.h>
70 #include <visp/vpDisplay.h>
71 #include <visp/vpDisplayX.h>
72 #include <visp/vpTime.h>
73 #include <visp/vpParseArgv.h>
74 #include <visp/vpRGBa.h>
75 
76 #define GRAB_CxOLOR
77 
78 
79 // List of allowed command line options
80 #define GETOPTARGS "b:c:df:g:hH:L:mn:io:p:rsT:v:W:"
81 
82 
83 #define DUAL_ACQ
84 
85 
101 void usage(const char *name, const char *badparam, unsigned int camera,
102  const unsigned int &nframes, const std::string &opath,
103  const unsigned int &roi_left, const unsigned int &roi_top,
104  const unsigned int &roi_width, const unsigned int &roi_height,
105  const unsigned int &ringbuffersize, const unsigned int &panControl)
106 {
107  if (badparam)
108  fprintf(stderr, "\nERROR: Bad parameter [%s]\n", badparam);
109 
110  fprintf(stderr, "\n\
111 SYNOPSIS\n\
112  %s [-v <video mode>] [-f <framerate>] \n\
113  [-g <color coding>] [-c <camera id>] [-m] [-n <frames>] \n\
114  [-i] [-s] [-d] [-o <filename>] [-L <format 7 roi left position>] \n\
115  [-T <format 7 roi top position>] [-W <format 7 roi width>] \n\
116  [-H <format 7 roi height>] [-b <ring buffer size>] \n\
117  [-p <pan control value>] [-R] [-h]\n\
118  \n\
119 DESCRIPTION\n\
120  Test for firewire camera image acquisition.\n\
121  \n\
122 EXAMPLES\n\
123  \n\
124  %s -s\n\
125  Indicates the current settings for the first camera found on the bus.\n\n\
126  %s -i\n\
127  Gives information on the first camera found on the bus.\n\n\
128  %s -s -m\n\
129  Indicates the current settings for all the cameras found on the bus.\n\n\
130  %s -i -m\n\
131  Gives information on all the cameras found on the bus.\n\
132  %s -c 1\n\
133  Grab images from camera 1.\n\n\
134  %s -m\n\
135  Grab images from all the cameras.\n\n\
136  \n\
137  If a stereo camera is connected to the bus like the PointGrey Bumblebee,\n\
138  you may set the pan control to select the camera view:\n\
139  %s -p 0\n\
140  Transmit right imge.\n\
141  %s -p 1\n\
142  Transmit left imge.\n\
143  \n\
144 OPTIONS Default\n\
145  -v [%%u] : Video mode to set for the active camera.\n\
146  Use -s option so see which are the supported \n\
147  video modes. You can select the active \n\
148  camera using -c option.\n\
149  \n\
150  -f [%%u] : Framerate to set for the active camera.\n\
151  Use -s option so see which are the supported \n\
152  framerates. You can select the active \n\
153  camera using -c option.\n\
154  \n\
155  -g [%%u] : Color coding to set for the active camera\n\
156  in format 7 video mode. Use -s option so see if \n\
157  format 7 is supported by the camera and if so, \n\
158  which are the supported color codings. You can \n\
159  select the active camera using -c option.\n\
160  See -t <top>, -l <left>, -w <width>, \n\
161  -h <height> option to set format 7 roi.\n\
162  \n\
163  -L [%%u] : Format 7 region of interest (roi) left %u\n\
164  position. This option is only used if video\n\
165  mode is format 7.\n\
166  \n\
167  -T [%%u] : Format 7 region of interest (roi) top %u\n\
168  position. This option is only used if video\n\
169  mode is format 7.\n\
170  \n\
171  -W [%%u] : Format 7 region of interest (roi) width. %u\n\
172  Is set to zero, use the maximum width. This\n\
173  option is only used if video mode is format 7.\n\
174  \n\
175  -H [%%u] : Format 7 region of interest (roi) height. %u\n\
176  Is set to zero, use the maximum height. This\n\
177  option is only used if video mode is format 7.\n\
178  \n\
179  -c [%%u] : Active camera identifier. %u\n\
180  Zero is for the first camera found on the bus.\n\
181  \n\
182  -m : Flag to active multi camera acquisition. \n\
183  You need at least two cameras connected on \n\
184  the bus.\n\
185  \n\
186  -n [%%u] : Number of frames to acquire. %u\n\
187  \n\
188  -i : Flag to print camera information.\n\
189  \n\
190  -s : Print camera settings capabilities such as \n\
191  video mode and framerates available and exit.\n\
192  \n\
193  -d : Flag to turn off image display.\n\
194  \n\
195  -b [%%u] : Ring buffer size used during capture %u\n\
196  \n\
197  -p [%%u] : Pan control value used to control single or %u\n\
198  multiple image transmission from stereo vision \n\
199  cameras by setting the PAN register 0x884.\n\
200  \n\
201  -o [%%s] : Filename for image saving. \n\
202  Example: -o %s\n\
203  The first %%d is for the camera id. The second\n\
204  %%04d is for the image numbering. The format is set \n\
205  by the extension of the file (ex .png, .pgm, ...) \n\
206  \n\
207  -r : Reset the bus attached to the first camera found.\n\
208  Bus reset may help to make firewire working if the\n\
209  program was not properly stopped by a CTRL-C.\n\
210  \n\
211  -h : Print this help.\n\
212  \n",
213  name, name, name, name, name, name, name, name, name,
214  roi_left, roi_top, roi_width, roi_height,
215  camera, nframes, ringbuffersize, panControl, opath.c_str());
216 }
217 
260 void read_options(int argc, const char **argv, bool &multi, unsigned int &camera,
261  unsigned int &nframes, bool &verbose_info,
262  bool &verbose_settings,
263  bool &videomode_is_set,
265  bool &framerate_is_set,
267  bool &colorcoding_is_set,
269  bool &ringbuffersize_is_set,
270  unsigned int &ringbuffersize,
271  bool &display, bool &save, std::string &opath,
272  unsigned int &roi_left, unsigned int &roi_top,
273  unsigned int &roi_width, unsigned int &roi_height,
274  bool &reset,
275  unsigned int &panControl, bool & panControl_is_set)
276 {
277  /*
278  * Lecture des options.
279  */
280  const char *optarg;
281  int c;
282 
283  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
284  switch (c) {
285  case 'c':
286  camera = (unsigned int)atoi(optarg); break;
287  case 'd':
288  display = false; break;
289  case 'f':
290  framerate_is_set = true;
291  framerate = (vp1394TwoGrabber::vp1394TwoFramerateType) atoi(optarg);
292  break;
293  case 'g':
294  colorcoding_is_set = true;
295  colorcoding = (vp1394TwoGrabber::vp1394TwoColorCodingType) atoi(optarg);
296  break;
297  case 'H':
298  roi_height = (unsigned int) atoi(optarg); break;
299  case 'i':
300  verbose_info = true; break;
301  case 'L':
302  roi_left = (unsigned int) atoi(optarg); break;
303  case 'm':
304  multi = true; break;
305  case 'n':
306  nframes = (unsigned int)atoi(optarg); break;
307  case 'o':
308  save = true;
309  opath = optarg; break;
310  case 'b':
311  ringbuffersize_is_set = true;
312  ringbuffersize = (unsigned int) atoi(optarg); break;
313  case 'p':
314  panControl = (unsigned int) atoi(optarg);
315  panControl_is_set = true;
316  break;
317  case 'r':
318  reset = true; break;
319  case 's':
320  verbose_settings = true; break;
321  case 'T':
322  roi_top = (unsigned int) atoi(optarg); break;
323  case 'v':
324  videomode_is_set = true;
325  videomode = (vp1394TwoGrabber::vp1394TwoVideoModeType) atoi(optarg);
326  break;
327  case 'W':
328  roi_width = (unsigned int) atoi(optarg); break;
329  case 'h':
330  case '?':
331  usage(argv[0], NULL, camera, nframes, opath,
332  roi_left, roi_top, roi_width, roi_height, ringbuffersize,
333  panControl);
334  exit(0);
335  break;
336  }
337  }
338 
339  if ((c == 1) || (c == -1)) {
340  // standalone param or error
341  usage(argv[0], NULL, camera, nframes, opath,
342  roi_left, roi_top, roi_width, roi_height, ringbuffersize, panControl);
343  std::cerr << "ERROR: " << std::endl;
344  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
345  exit(-1);
346  }
347 }
348 
358 int
359 main(int argc, const char ** argv)
360 {
361  try {
362  unsigned int camera = 0;
363  bool multi = false;
364  bool verbose_info = false;
365  bool verbose_settings = false;
366  bool display = true;
367  unsigned int nframes = 50;
368  unsigned int offset;
369  bool videomode_is_set = false;
371  bool framerate_is_set = false;
373  bool colorcoding_is_set = false;
375  bool ringbuffersize_is_set = false;
376  unsigned int ringbuffersize = 4;
377  bool save = false;
378  bool reset = false;
379  unsigned int panControl = 0;
380  bool panControl_is_set = false;
381 
382  // Format 7 roi
383  unsigned int roi_left=0, roi_top=0, roi_width=0, roi_height=0;
384 
385  // Default output path for image saving
386  std::string opath = "/tmp/I%d-%04d.ppm";
387 
388  read_options(argc, argv, multi, camera, nframes,
389  verbose_info, verbose_settings,
390  videomode_is_set, videomode,
391  framerate_is_set, framerate,
392  colorcoding_is_set, colorcoding,
393  ringbuffersize_is_set, ringbuffersize,
394  display, save, opath,
395  roi_left, roi_top, roi_width, roi_height, reset,
396  panControl, panControl_is_set);
397 
398  // Create a grabber
399  vp1394TwoGrabber g(reset);
400 
401  if (reset) {
402  // The experience shows that for some Marlin cameras (F131B and
403  // F033C) a tempo of 1s is requested after a bus reset.
404  vpTime::wait(1000); // Wait 1000 ms
405  }
406 
407  // Number of cameras connected on the bus
408  unsigned int ncameras = 0;
409  g.getNumCameras(ncameras);
410 
411  std::cout << "Number of cameras on the bus: " << ncameras << std::endl;
412 
413  // Check the consistancy of the options
414  if (multi) {
415  // ckeck if two cameras are connected
416  if (ncameras < 2) {
417  std::cout << "You have only " << ncameras << " camera connected on the bus." << std::endl;
418  std::cout << "It is not possible to active multi-camera acquisition." << std::endl;
419  std::cout << "Disable -m command line option, or connect an other " << std::endl;
420  std::cout << "cameras on the bus." << std::endl;
421  g.close();
422  return(0);
423  }
424  }
425  if (camera >= ncameras) {
426  std::cout << "You have only " << ncameras;
427  std::cout << " camera connected on the bus." << std::endl;
428  std::cout << "It is not possible to select camera " << camera << std::endl;
429  std::cout << "Check your -c <camera> command line option." << std::endl;
430  g.close();
431  return(0);
432  }
433 
434  if (multi) {
435  camera = 0; // to over write a bad option usage
436  }
437  else {
438  ncameras = 1; // acquisition from only one camera
439  }
440  // Offset is used to set the correspondancy between and image and the
441  // camera. For example, images comming from camera (i+offset) are available
442  // in I[i]
443  offset = camera;
444 
445  // Display information for each camera
446  if (verbose_info || verbose_settings) {
447  for (unsigned int i=0; i < ncameras; i ++) {
448 
449  g.setCamera(i+offset);
450 
451  if (verbose_info)
452  g.printCameraInfo();
453 
454  if (verbose_settings) {
458  std::list<vp1394TwoGrabber::vp1394TwoVideoModeType> lmode;
459  std::list<vp1394TwoGrabber::vp1394TwoFramerateType> lfps;
460  std::list<vp1394TwoGrabber::vp1394TwoColorCodingType> lcoding;
461  std::list<vp1394TwoGrabber::vp1394TwoVideoModeType>::const_iterator it_lmode;
462  std::list<vp1394TwoGrabber::vp1394TwoFramerateType>::const_iterator it_lfps;
463  std::list<vp1394TwoGrabber::vp1394TwoColorCodingType>::const_iterator it_lcoding;
464  uint64_t guid;
465 
466  g.getVideoMode(curmode);
467  g.getFramerate(curfps);
468  g.getColorCoding(curcoding);
469  g.getVideoModeSupported(lmode);
470  g.getGuid(guid);
471 
472  std::cout << "----------------------------------------------------------"
473  << std::endl
474  << "---- Video modes and framerates supported by camera "
475  << i+offset << " ----" << std::endl
476  << "---- with guid 0x" << std::hex << guid << " ----" << std::endl
477  << "---- * is for the current settings ----"
478  << std::endl
479  << "---- between ( ) you have the corresponding option ----"
480  << std::endl
481  << "---- to use. ----"
482  << std::endl
483  << "----------------------------------------------------------"
484  << std::endl;
485 
486  for (it_lmode = lmode.begin(); it_lmode != lmode.end(); ++it_lmode) {
487  // Parse the list of supported modes
488  vp1394TwoGrabber::vp1394TwoVideoModeType supmode = *it_lmode;
489  if (curmode == supmode)
490  std::cout << " * " << vp1394TwoGrabber::videoMode2string(supmode)
491  << " (-v " << supmode << ")" << std::endl;
492  else
493  std::cout << " " << vp1394TwoGrabber::videoMode2string(supmode)
494  << " (-v " << supmode << ")" << std::endl;
495 
496  if (g.isVideoModeFormat7(supmode)){
497  // Format 7 video mode; no framerate setting, but color
498  // coding setting
499  g.getColorCodingSupported(supmode, lcoding);
500  for (it_lcoding = lcoding.begin(); it_lcoding != lcoding.end(); ++it_lcoding) {
502  supcoding = *it_lcoding;
503  if ( (curmode == supmode) && (supcoding == curcoding) )
504  std::cout << " * "
506  << " (-g " << supcoding << ")" << std::endl;
507  else
508  std::cout << " "
510  << " (-g " << supcoding << ")" << std::endl;
511  }
512  }
513  else {
514 
515  // Parse the list of supported framerates for a supported mode
516  g.getFramerateSupported(supmode, lfps);
517  for (it_lfps = lfps.begin(); it_lfps != lfps.end(); ++it_lfps) {
519  if ( (curmode == supmode) && (supfps == curfps) )
520  std::cout << " * "
522  << " (-f " << supfps << ")" << std::endl;
523  else
524  std::cout << " "
526  << " (-f " << supfps << ")" << std::endl;
527  }
528  }
529  }
530  std::cout << "----------------------------------------------------------"
531  << std::endl;
532 
533  }
534  }
535  return 0;
536  }
537 
538  // If requested set the PAN register 0x884 to control single or
539  // multiple image transmission from stereo vision cameras.
540  if (panControl_is_set) {
541  g.setPanControl(panControl);
542  }
543 
544  // If required modify camera settings
545  if (videomode_is_set) {
546  g.setCamera(camera);
547  g.setVideoMode(videomode);
548  }
549  else {
550  // get The actual video mode
551  g.setCamera(camera);
552  g.getVideoMode(videomode);
553  }
554  if (framerate_is_set) {
555  g.setCamera(camera);
556  g.setFramerate(framerate);
557  }
558  if (colorcoding_is_set) {
559  g.setCamera(camera);
560  g.setColorCoding(colorcoding);
561  }
562  if (ringbuffersize_is_set) {
563  g.setRingBufferSize(ringbuffersize);
564  }
565 
566  // In format 7 set roi to the hole image
567  if (g.isVideoModeFormat7(videomode))
568  g.setFormat7ROI(roi_left, roi_top, roi_width, roi_height);
569 
570  // Array to know if color images or grey level images are acquired
571  bool *grab_color = new bool [ncameras];
572 
573 #ifdef VISP_HAVE_X11
574  // allocate a display for each camera to consider
575  vpDisplayX *d = NULL;
576  if (display)
577  d = new vpDisplayX [ncameras];
578 #endif
579 
580  // allocate an Grey and color image for each camera to consider
581  vpImage<vpRGBa> *Ic = new vpImage<vpRGBa> [ncameras];
582  vpImage<unsigned char> *Ig = new vpImage<unsigned char> [ncameras];
583 
584  // Do a first acquisition to initialise the display
585  for (unsigned int i=0; i < ncameras; i ++) {
586  // Set the active camera on the bus
587  g.setCamera(i+offset);
588  // Ask each camera to know if color images or grey level images are
589  // acquired
590  grab_color[i] = g.isColor();
591  // Acquire the first image
592  if (grab_color[i]) {
593  g.acquire(Ic[i]);
594  std::cout << "Image size for camera " << i+offset << " : width: "
595  << Ic[i].getWidth() << " height: " << Ic[i].getHeight()
596  << std::endl;
597 
598 #ifdef VISP_HAVE_X11
599  if (display) {
600  // Initialise the display
601  char title[100];
602  sprintf(title, "Images captured by camera %u", i+offset);
603  d[i].init(Ic[i], (int)(100+i*50), (int)(100+i*50), title) ;
604  vpDisplay::display(Ic[i]);
605  vpDisplay::flush(Ic[i]);
606  }
607 #endif
608  }
609  else {
610  g.acquire(Ig[i]);
611  std::cout << "Image size for camera " << i+offset << " : width: "
612  << Ig[i].getWidth() << " height: " << Ig[i].getHeight()
613  << std::endl;
614 
615 #ifdef VISP_HAVE_X11
616  if (display) {
617  // Initialise the display
618  char title[100];
619  sprintf(title, "Images captured by camera %u", i+offset);
620  d[i].init(Ig[i], (int)(100+i*50), (int)(100+i*50), title) ;
621  vpDisplay::display(Ig[i]);
622  vpDisplay::flush(Ig[i]);
623  }
624 #endif
625 
626  }
627  }
628 
629  // Main loop for single or multi-camera acquisition and display
630  std::cout << "Capture in process..." << std::endl;
631 
632  double tbegin=0, tend=0, tloop=0, ttotal=0;
633 
634  ttotal = 0;
635  tbegin = vpTime::measureTimeMs();
636  for (unsigned int i = 0; i < nframes; i++) {
637  for (unsigned int c = 0; c < ncameras; c++) {
638  // Set the active camera on the bus
639  g.setCamera(c+offset);
640  // Acquire an image
641  if (grab_color[c]) {
642  g.acquire(Ic[c]);
643 #ifdef VISP_HAVE_X11
644  if (display) {
645  // Display the last image acquired
646  vpDisplay::display(Ic[c]);
647  vpDisplay::flush(Ic[c]);
648  }
649 #endif
650  }
651  else {
652  g.acquire(Ig[c]);
653 #ifdef VISP_HAVE_X11
654  if (display) {
655  // Display the last image acquired
656  vpDisplay::display(Ig[c]);
657  vpDisplay::flush(Ig[c]);
658  }
659 #endif
660 
661  }
662  if (save) {
663  char buf[FILENAME_MAX];
664  sprintf(buf, opath.c_str(), c+offset, i);
665  std::string filename(buf);
666  std::cout << "Write: " << filename << std::endl;
667  if (grab_color[c]) {
668  vpImageIo::write(Ic[c], filename);
669  }
670  else {
671  vpImageIo::write(Ig[c], filename);
672  }
673  }
674  }
675  tend = vpTime::measureTimeMs();
676  tloop = tend - tbegin;
677  tbegin = tend;
678  std::cout << "loop time: " << tloop << " ms" << std::endl;
679  ttotal += tloop;
680  }
681 
682  std::cout << "Mean loop time: " << ttotal / nframes << " ms" << std::endl;
683  std::cout << "Mean frequency: " << 1000./(ttotal / nframes) << " fps" << std::endl;
684 
685  // Release the framegrabber
686  g.close();
687 
688  // Free memory
689 
690  delete [] Ic;
691  delete [] Ig;
692  delete [] grab_color;
693 
694 #ifdef VISP_HAVE_X11
695  if (display)
696  delete [] d;
697 #endif
698  }
699  catch (...) {
700  vpCERROR << "Failure: exit" << std::endl;
701  }
702 
703  std::cout << " the end" << std::endl;
704 }
705 #else
706 int
707 main()
708 {
709  vpTRACE("Ieee 1394 grabber capabilities are not available...\n"
710  "You should install libdc1394-2 to use this example.") ;
711 }
712 
713 #endif
static void write(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:442
static std::string colorCoding2string(vp1394TwoColorCodingType colorcoding)
static std::string framerate2string(vp1394TwoFramerateType fps)
unsigned int getWidth() const
Definition: vpImage.h:159
#define vpTRACE
Definition: vpDebug.h:401
#define vpCERROR
Definition: vpDebug.h:354
Define the X11 console to display images.
Definition: vpDisplayX.h:152
static double measureTimeMs()
Definition: vpTime.cpp:86
static int wait(double t0, double t)
Definition: vpTime.cpp:149
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1991
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:203
static std::string videoMode2string(vp1394TwoVideoModeType videomode)
unsigned int getHeight() const
Definition: vpImage.h:150
Class for firewire ieee1394 video devices using libdc1394-2.x api.
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
Definition: vpDisplayX.cpp:184