ViSP
 All Classes Functions Variables Enumerations Enumerator Friends Groups Pages
grab1394CMU.cpp
1 /****************************************************************************
2  *
3  * $Id: grab1394CMU.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  * 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 
196  g.open(I);
197  g.acquire(I);
198 
199  std::cout << "Image size: width : " << I.getWidth() << " height: "
200  << I.getHeight() << std::endl;
201 
202 #if (defined (VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
203 
204  // Creates a display
205 #if defined VISP_HAVE_OPENCV
206  vpDisplayOpenCV display;
207 #elif defined VISP_HAVE_GDI
208  vpDisplayGDI display;
209 #endif
210  if (opt_display) {
211  display.init(I,100,100,"DirectShow Framegrabber");
212  }
213 #endif
214 
215  try {
216  double tbegin=0, tend=0, tloop=0, ttotal=0;
217 
218  ttotal = 0;
219  tbegin = vpTime::measureTimeMs();
220  // Loop for image acquisition and display
221  for (unsigned i = 0; i < nframes; i++) {
222  //Acquires an RGBa image
223  g.acquire(I);
224 
225 #if (defined (VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
226  if (opt_display) {
227  //Displays the grabbed rgba image
229  vpDisplay::flush(I);
230  if (vpDisplay::getClick(I, false)) // A click to exit
231  break;
232  }
233 #endif
234 
235  if (save) {
236  char buf[FILENAME_MAX];
237  sprintf(buf, opath.c_str(), i);
238  std::string filename(buf);
239  std::cout << "Write: " << filename << std::endl;
240 #ifdef GRAB_COLOR
241  vpImageIo::writePPM(I, filename);
242 #else
243  vpImageIo::writePGM(I, filename);
244 #endif
245  }
246  tend = vpTime::measureTimeMs();
247  tloop = tend - tbegin;
248  tbegin = tend;
249  std::cout << "loop time: " << tloop << " ms" << std::endl;
250  ttotal += tloop;
251  }
252  std::cout << "Mean loop time: " << ttotal / nframes << " ms" << std::endl;
253  std::cout << "Mean frequency: " << 1000./(ttotal / nframes) << " fps" << std::endl;
254  }
255  catch(...)
256  {
257  std::cout << "Failure: exit" << std::endl;
258  return(-1);
259  }
260 }
261 #else
262 int main()
263 {
264  std::cout << "This example requires CMU 1394 Digital Camera SDK. " << std::endl;
265  return 0;
266 }
267 #endif
268