ViSP
grab1394CMU.cpp
1 /****************************************************************************
2  *
3  * $Id: grab1394CMU.cpp 4659 2014-02-09 14:11:51Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 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  * Video capture example based on CMU 1394 Digital Camera SDK.
36  *
37  * Author:
38  * Fabien Spindler
39  *
40  *****************************************************************************/
41 
42 
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <iostream>
52 
53 #include <visp/vpConfig.h>
54 #include <visp/vp1394CMUGrabber.h>
55 #include <visp/vpImage.h>
56 #include <visp/vpImageIo.h>
57 #include <visp/vpDisplayGDI.h>
58 #include <visp/vpDisplayOpenCV.h>
59 #include <visp/vpParseArgv.h>
60 #include <visp/vpTime.h>
61 
62 #define GRAB_COLOR
63 
64 // List of allowed command line options
65 #define GETOPTARGS "dhn:o:"
66 
67 void usage(const char *name, const char *badparam, unsigned &nframes, std::string &opath);
68 bool getOptions(int argc, const char **argv, bool &display,
69  unsigned int &nframes, bool &save, std::string &opath);
70 
81 void usage(const char *name, const char *badparam, unsigned &nframes, std::string &opath)
82 {
83  fprintf(stdout, "\n\
84 Acquire images using CMU 1394 Digital Camera SDK (available under Windows only) and display\n\
85 it using GDI or OpenCV if GDI is not available.\n\
86 \n\
87 SYNOPSIS\n\
88  %s [-d] [-n] [-o] [-h] \n", name);
89 
90  fprintf(stdout, "\n\
91 OPTIONS: Default\n\
92  -d \n\
93  Turn off the display.\n\
94 \n\
95  -n [%%u] %u\n\
96  Number of frames to acquire. \n\
97 \n\
98  -o [%%s] \n\
99  Filename for image saving. \n\
100  Example: -o %s\n\
101  The %%d is for the image numbering.\n\
102 \n\
103  -h \n\
104  Print the help.\n\
105 \n", nframes, opath.c_str());
106  if (badparam) {
107  fprintf(stderr, "ERROR: \n" );
108  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
109  }
110 }
127 bool getOptions(int argc, const char **argv, bool &display,
128  unsigned int &nframes, bool &save, std::string &opath)
129 {
130  const char *optarg_;
131  int c;
132  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
133 
134  switch (c) {
135  case 'd': display = false; break;
136  case 'n':
137  nframes = (unsigned int)atoi(optarg_); break;
138  case 'o':
139  save = true;
140  opath = optarg_; break;
141  case 'h': usage(argv[0], NULL, nframes, opath); return false; break;
142 
143  default:
144  usage(argv[0], optarg_, nframes, opath);
145  return false; break;
146  }
147  }
148 
149  if ((c == 1) || (c == -1)) {
150  // standalone param or error
151  usage(argv[0], NULL, nframes, opath);
152  std::cerr << "ERROR: " << std::endl;
153  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
154  return false;
155  }
156 
157  return true;
158 }
159 
160 
167 #if defined(VISP_HAVE_CMU1394)
168 int
169 main(int argc, const char ** argv)
170 {
171  bool opt_display = true;
172  unsigned nframes = 50;
173  bool save = false;
174 
175  // Declare an image. It size is not defined yet. It will be defined when the
176  // image will acquired the first time.
177 #ifdef GRAB_COLOR
178  vpImage<vpRGBa> I; // This is a color image (in RGBa format)
179 #else
180  vpImage<unsigned char> I; // This is a B&W image
181 #endif
182 
183  // Set default output image name for saving
184 #ifdef GRAB_COLOR
185  // Color images will be saved in PGM P6 format
186  std::string opath = "C:/temp/I%04d.ppm";
187 #else
188  // B&W images will be saved in PGM P5 format
189  std::string opath = "C:/temp/I%04d.pgm";
190 #endif
191 
192  // Read the command line options
193  if (getOptions(argc, argv, opt_display, nframes, save, opath) == false) {
194  exit (-1);
195  }
196 
197  // Create the grabber
199  unsigned short gain_min, gain_max;
200  g.getGainMinMax(gain_min, gain_max);
201  std::cout << "Gain range [" << gain_min << ", " << gain_max << "]" << std::endl;
202  unsigned short shutter_min, shutter_max;
203  g.getShutterMinMax(shutter_min, shutter_max);
204  std::cout << "Shutter range [" << shutter_min << ", " << shutter_max << "]" << std::endl;
205  g.setFramerate(4); // 30 fps
206  std::cout << "Actual framerate: " << g.getFramerate() << std::endl;
207  g.setVideoMode(0,0);
208  g.acquire(I);
209 
210  std::cout << "Image size: width : " << I.getWidth() << " height: "
211  << I.getHeight() << std::endl;
212 
213 #if (defined (VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
214 
215  // Creates a display
216 #if defined VISP_HAVE_OPENCV
217  vpDisplayOpenCV display;
218 #elif defined VISP_HAVE_GDI
219  vpDisplayGDI display;
220 #endif
221  if (opt_display) {
222  display.init(I,100,100,"DirectShow Framegrabber");
223  }
224 #endif
225 
226  try {
227  double tbegin=0, tend=0, tloop=0, ttotal=0;
228 
229  ttotal = 0;
230  tbegin = vpTime::measureTimeMs();
231  // Loop for image acquisition and display
232  for (unsigned i = 0; i < nframes; i++) {
233  //Acquires an RGBa image
234  g.acquire(I);
235 
236 #if (defined (VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
237  if (opt_display) {
238  //Displays the grabbed rgba image
240  vpDisplay::flush(I);
241  if (vpDisplay::getClick(I, false)) // A click to exit
242  break;
243  }
244 #endif
245 
246  if (save) {
247  char buf[FILENAME_MAX];
248  sprintf(buf, opath.c_str(), i);
249  std::string filename(buf);
250  std::cout << "Write: " << filename << std::endl;
251  vpImageIo::write(I, filename);
252  }
253  tend = vpTime::measureTimeMs();
254  tloop = tend - tbegin;
255  tbegin = tend;
256  std::cout << "loop time: " << tloop << " ms" << std::endl;
257  ttotal += tloop;
258  }
259  std::cout << "Mean loop time: " << ttotal / nframes << " ms" << std::endl;
260  std::cout << "Mean frequency: " << 1000./(ttotal / nframes) << " fps" << std::endl;
261  }
262  catch(vpException e) {
263  std::cout << "Catch an exception: " << e << std::endl;
264  return 1;
265  }
266 }
267 #else
268 int main()
269 {
270  std::cout << "This example requires CMU 1394 Digital Camera SDK. " << std::endl;
271  return 0;
272 }
273 #endif
274 
static void write(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:476
void setVideoMode(unsigned long format, unsigned long mode)
unsigned int getWidth() const
Definition: vpImage.h:161
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:132
error that can be emited by ViSP classes.
Definition: vpException.h:76
static double measureTimeMs()
Definition: vpTime.cpp:86
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2232
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:80
void getGainMinMax(unsigned short &min, unsigned short &max)
Firewire cameras video capture based on CMU 1394 Digital Camera SDK.
void setFramerate(unsigned long fps)
void acquire(vpImage< unsigned char > &I)
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:210
The vpDisplayOpenCV allows to display image using the opencv library.
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
void getShutterMinMax(unsigned short &min, unsigned short &max)
unsigned int getHeight() const
Definition: vpImage.h:152
virtual bool getClick(bool blocking=true)=0