ViSP  2.8.0
grab1394CMU.cpp
1 /****************************************************************************
2  *
3  * $Id: grab1394CMU.cpp 4323 2013-07-18 09:24:01Z 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  * 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 
77 void usage(const char *name, const char *badparam, unsigned &nframes, std::string &opath)
78 {
79  fprintf(stdout, "\n\
80 Acquire images using CMU 1394 Digital Camera SDK (available under Windows only) and display\n\
81 it using GDI or OpenCV if GDI is not available.\n\
82 \n\
83 SYNOPSIS\n\
84  %s [-d] [-n] [-o] [-h] \n", name);
85 
86  fprintf(stdout, "\n\
87 OPTIONS: Default\n\
88  -d \n\
89  Turn off the display.\n\
90 \n\
91  -n [%%u] %u\n\
92  Number of frames to acquire. \n\
93 \n\
94  -o [%%s] \n\
95  Filename for image saving. \n\
96  Example: -o %s\n\
97  The %%d is for the image numbering.\n\
98 \n\
99  -h \n\
100  Print the help.\n\
101 \n", nframes, opath.c_str());
102  if (badparam) {
103  fprintf(stderr, "ERROR: \n" );
104  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
105  }
106 }
123 bool getOptions(int argc, const char **argv, bool &display,
124  unsigned int &nframes, bool &save, std::string &opath)
125 {
126  const char *optarg;
127  int c;
128  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
129 
130  switch (c) {
131  case 'd': display = false; break;
132  case 'n':
133  nframes = (unsigned int)atoi(optarg); break;
134  case 'o':
135  save = true;
136  opath = optarg; break;
137  case 'h': usage(argv[0], NULL, nframes, opath); return false; break;
138 
139  default:
140  usage(argv[0], optarg, nframes, opath);
141  return false; break;
142  }
143  }
144 
145  if ((c == 1) || (c == -1)) {
146  // standalone param or error
147  usage(argv[0], NULL, nframes, opath);
148  std::cerr << "ERROR: " << std::endl;
149  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
150  return false;
151  }
152 
153  return true;
154 }
155 
156 
163 #if defined(VISP_HAVE_CMU1394)
164 int
165 main(int argc, const char ** argv)
166 {
167  bool opt_display = true;
168  unsigned nframes = 50;
169  bool save = false;
170 
171  // Declare an image. It size is not defined yet. It will be defined when the
172  // image will acquired the first time.
173 #ifdef GRAB_COLOR
174  vpImage<vpRGBa> I; // This is a color image (in RGBa format)
175 #else
176  vpImage<unsigned char> I; // This is a B&W image
177 #endif
178 
179  // Set default output image name for saving
180 #ifdef GRAB_COLOR
181  // Color images will be saved in PGM P6 format
182  std::string opath = "C:/temp/I%04d.ppm";
183 #else
184  // B&W images will be saved in PGM P5 format
185  std::string opath = "C:/temp/I%04d.pgm";
186 #endif
187 
188  // Read the command line options
189  if (getOptions(argc, argv, opt_display, nframes, save, opath) == false) {
190  exit (-1);
191  }
192 
193  // Create the grabber
195  unsigned short gain_min, gain_max;
196  g.getGainMinMax(gain_min, gain_max);
197  std::cout << "Gain range [" << gain_min << ", " << gain_max << "]" << std::endl;
198  unsigned short shutter_min, shutter_max;
199  g.getShutterMinMax(shutter_min, shutter_max);
200  std::cout << "Shutter range [" << shutter_min << ", " << shutter_max << "]" << std::endl;
201  g.setFramerate(4); // 30 fps
202  std::cout << "Actual framerate: " << g.getFramerate() << std::endl;
203  g.setVideoMode(0,0);
204  g.acquire(I);
205 
206  std::cout << "Image size: width : " << I.getWidth() << " height: "
207  << I.getHeight() << std::endl;
208 
209 #if (defined (VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
210 
211  // Creates a display
212 #if defined VISP_HAVE_OPENCV
213  vpDisplayOpenCV display;
214 #elif defined VISP_HAVE_GDI
215  vpDisplayGDI display;
216 #endif
217  if (opt_display) {
218  display.init(I,100,100,"DirectShow Framegrabber");
219  }
220 #endif
221 
222  try {
223  double tbegin=0, tend=0, tloop=0, ttotal=0;
224 
225  ttotal = 0;
226  tbegin = vpTime::measureTimeMs();
227  // Loop for image acquisition and display
228  for (unsigned i = 0; i < nframes; i++) {
229  //Acquires an RGBa image
230  g.acquire(I);
231 
232 #if (defined (VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
233  if (opt_display) {
234  //Displays the grabbed rgba image
236  vpDisplay::flush(I);
237  if (vpDisplay::getClick(I, false)) // A click to exit
238  break;
239  }
240 #endif
241 
242  if (save) {
243  char buf[FILENAME_MAX];
244  sprintf(buf, opath.c_str(), i);
245  std::string filename(buf);
246  std::cout << "Write: " << filename << std::endl;
247  vpImageIo::write(I, filename);
248  }
249  tend = vpTime::measureTimeMs();
250  tloop = tend - tbegin;
251  tbegin = tend;
252  std::cout << "loop time: " << tloop << " ms" << std::endl;
253  ttotal += tloop;
254  }
255  std::cout << "Mean loop time: " << ttotal / nframes << " ms" << std::endl;
256  std::cout << "Mean frequency: " << 1000./(ttotal / nframes) << " fps" << std::endl;
257  }
258  catch(...)
259  {
260  std::cout << "Failure: exit" << std::endl;
261  return(-1);
262  }
263 }
264 #else
265 int main()
266 {
267  std::cout << "This example requires CMU 1394 Digital Camera SDK. " << std::endl;
268  return 0;
269 }
270 #endif
271 
static void write(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:442
void setVideoMode(unsigned long format, unsigned long mode)
unsigned int getWidth() const
Definition: vpImage.h:159
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:133
static double measureTimeMs()
Definition: vpTime.cpp:86
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
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:203
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:150
virtual bool getClick(bool blocking=true)=0