Visual Servoing Platform  version 3.0.1
grab1394CMU.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Video capture example based on CMU 1394 Digital Camera SDK.
32  *
33  * Author:
34  * Fabien Spindler
35  *
36  *****************************************************************************/
37 
38 
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <iostream>
48 
49 #include <visp3/core/vpConfig.h>
50 #include <visp3/sensor/vp1394CMUGrabber.h>
51 #include <visp3/core/vpImage.h>
52 #include <visp3/io/vpImageIo.h>
53 #include <visp3/gui/vpDisplayGDI.h>
54 #include <visp3/gui/vpDisplayOpenCV.h>
55 #include <visp3/io/vpParseArgv.h>
56 #include <visp3/core/vpTime.h>
57 
58 #define GRAB_COLOR
59 
60 // List of allowed command line options
61 #define GETOPTARGS "dhn:o:"
62 
63 void usage(const char *name, const char *badparam, unsigned &nframes, std::string &opath);
64 bool getOptions(int argc, const char **argv, bool &display,
65  unsigned int &nframes, bool &save, std::string &opath);
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, 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  double tend = vpTime::measureTimeMs();
250  double 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(vpException &e) {
259  std::cout << "Catch an exception: " << e << std::endl;
260  return 1;
261  }
262 }
263 #else
264 int main()
265 {
266  std::cout << "This example requires CMU 1394 Digital Camera SDK. " << std::endl;
267  return 0;
268 }
269 #endif
270 
void setVideoMode(unsigned long format, unsigned long mode)
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
error that can be emited by ViSP classes.
Definition: vpException.h:73
static void flush(const vpImage< unsigned char > &I)
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:93
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
static void write(const vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:368
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)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
void getShutterMinMax(unsigned short &min, unsigned short &max)
unsigned int getHeight() const
Definition: vpImage.h:175
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
unsigned int getWidth() const
Definition: vpImage.h:226