Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
grab1394CMU.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Video capture example based on CMU 1394 Digital Camera SDK.
33  *
34  * Author:
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
45 #include <iostream>
46 #include <stdio.h>
47 #include <stdlib.h>
48 
49 #include <visp3/core/vpConfig.h>
50 #include <visp3/core/vpImage.h>
51 #include <visp3/core/vpTime.h>
52 #include <visp3/gui/vpDisplayGDI.h>
53 #include <visp3/gui/vpDisplayOpenCV.h>
54 #include <visp3/io/vpImageIo.h>
55 #include <visp3/io/vpParseArgv.h>
56 #include <visp3/sensor/vp1394CMUGrabber.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, unsigned int &nframes, bool &save, std::string &opath);
65 
76 void usage(const char *name, const char *badparam, unsigned &nframes, std::string &opath)
77 {
78  fprintf(stdout, "\n\
79 Acquire images using CMU 1394 Digital Camera SDK (available under Windows only) and display\n\
80 it using GDI or OpenCV if GDI is not available.\n\
81 \n\
82 SYNOPSIS\n\
83  %s [-d] [-n] [-o] [-h] \n", name);
84 
85  fprintf(stdout, "\n\
86 OPTIONS: Default\n\
87  -d \n\
88  Turn off the display.\n\
89 \n\
90  -n [%%u] %u\n\
91  Number of frames to acquire. \n\
92 \n\
93  -o [%%s] \n\
94  Filename for image saving. \n\
95  Example: -o %s\n\
96  The %%d is for the image numbering.\n\
97 \n\
98  -h \n\
99  Print the help.\n\
100 \n", nframes, opath.c_str());
101  if (badparam) {
102  fprintf(stderr, "ERROR: \n");
103  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
104  }
105 }
122 bool getOptions(int argc, const char **argv, bool &display, unsigned int &nframes, bool &save, std::string &opath)
123 {
124  const char *optarg_;
125  int c;
126  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
127 
128  switch (c) {
129  case 'd':
130  display = false;
131  break;
132  case 'n':
133  nframes = (unsigned int)atoi(optarg_);
134  break;
135  case 'o':
136  save = true;
137  opath = optarg_;
138  break;
139  case 'h':
140  usage(argv[0], NULL, nframes, opath);
141  return false;
142  break;
143 
144  default:
145  usage(argv[0], optarg_, nframes, opath);
146  return false;
147  break;
148  }
149  }
150 
151  if ((c == 1) || (c == -1)) {
152  // standalone param or error
153  usage(argv[0], NULL, nframes, opath);
154  std::cerr << "ERROR: " << std::endl;
155  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
156  return false;
157  }
158 
159  return true;
160 }
161 
168 #if defined(VISP_HAVE_CMU1394)
169 int 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: " << I.getHeight() << std::endl;
211 
212 #if (defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
213 
214 // Creates a display
215 #if defined VISP_HAVE_OPENCV
216  vpDisplayOpenCV display;
217 #elif defined VISP_HAVE_GDI
218  vpDisplayGDI display;
219 #endif
220  if (opt_display) {
221  display.init(I, 100, 100, "DirectShow Framegrabber");
222  }
223 #endif
224 
225  try {
226  double tbegin = 0, ttotal = 0;
227 
228  ttotal = 0;
229  tbegin = vpTime::measureTimeMs();
230  // Loop for image acquisition and display
231  for (unsigned i = 0; i < nframes; i++) {
232  // Acquires an RGBa image
233  g.acquire(I);
234 
235 #if (defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
236  if (opt_display) {
237  // Displays the grabbed rgba image
239  vpDisplay::flush(I);
240  if (vpDisplay::getClick(I, false)) // A click to exit
241  break;
242  }
243 #endif
244 
245  if (save) {
246  char buf[FILENAME_MAX];
247  sprintf(buf, opath.c_str(), i);
248  std::string filename(buf);
249  std::cout << "Write: " << filename << std::endl;
250  vpImageIo::write(I, filename);
251  }
252  double tend = vpTime::measureTimeMs();
253  double tloop = tend - tbegin;
254  tbegin = tend;
255  std::cout << "loop time: " << tloop << " ms" << std::endl;
256  ttotal += tloop;
257  }
258  std::cout << "Mean loop time: " << ttotal / nframes << " ms" << std::endl;
259  std::cout << "Mean frequency: " << 1000. / (ttotal / nframes) << " fps" << std::endl;
260  return EXIT_SUCCESS;
261  } catch (const vpException &e) {
262  std::cout << "Catch an exception: " << e << std::endl;
263  return EXIT_FAILURE;
264  }
265 }
266 #else
267 int main()
268 {
269  std::cout << "This example requires CMU 1394 Digital Camera SDK. " << std::endl;
270  std::cout << "Tip if you are on a windows-like system:" << std::endl;
271  std::cout << "- Install CMU 1394 SDK, configure again ViSP using cmake and build again this example" << std::endl;
272  return EXIT_SUCCESS;
273 }
274 #endif
void setVideoMode(unsigned long format, unsigned long mode)
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
unsigned int getWidth() const
Definition: vpImage.h:239
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
error that can be emited by ViSP classes.
Definition: vpException.h:71
static void flush(const vpImage< unsigned char > &I)
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:88
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
static void write(const vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:375
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:178
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")