ViSP
grabOpenCV.cpp
1 /****************************************************************************
2  *
3  * $Id: grabOpenCV.cpp 5023 2014-12-03 16:07:48Z 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  * Acquire images using DirectShow (under Windows only) and display it
36  * using GTK or GDI.
37  *
38  * Authors:
39  * Nicolas Melchior
40  *
41  *****************************************************************************/
42 
43 #include <visp/vpConfig.h>
44 #include <visp/vpDebug.h>
45 
53 #if defined (VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION < 0x020408)
54 
55 
56 #include <visp/vpOpenCVGrabber.h>
57 #include <visp/vpImage.h>
58 #include <visp/vpImageIo.h>
59 #include <visp/vpDisplayOpenCV.h>
60 #include <visp/vpParseArgv.h>
61 #include <visp/vpTime.h>
62 
63 // List of allowed command line options
64 #define GETOPTARGS "dhn:o:D:"
65 
66 void usage(const char *name, const char *badparam, unsigned int &nframes, std::string &opath);
67 bool getOptions(int argc, const char **argv, bool &display,
68  unsigned int &nframes, bool &save, std::string &opath, int &deviceType);
69 
80 void usage(const char *name, const char *badparam, unsigned int &nframes, std::string &opath)
81 {
82  fprintf(stdout, "\n\
83 Acquire and display images using OpenCV library.\n\
84 \n\
85 SYNOPSIS\n\
86  %s [-d] [-n] [-o] [-h] \n", name);
87 
88  fprintf(stdout, "\n\
89 OPTIONS: Default\n\
90  -d \n\
91  Turn off the display.\n\
92 \n\
93  -D [%%s] ANY\n\
94  Type of device to detect. \n\
95  It can be ANY, MIL, VFW, V4L, V4L2, DC1394, CMU1394, DSHOW. \n\
96 \n\
97  -n [%%u] %u\n\
98  Number of frames to acquire. \n\
99 \n\
100  -o [%%s] \n\
101  Filename for image saving. \n\
102  Example: -o %s\n\
103  The %%d is for the image numbering.\n\
104 \n\
105  -h \n\
106  Print the help.\n\
107 \n", nframes, opath.c_str());
108  if (badparam) {
109  fprintf(stderr, "ERROR: \n" );
110  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
111  }
112 }
130 bool getOptions(int argc, const char **argv, bool &display,
131  unsigned int &nframes, bool &save, std::string &opath, int &deviceType)
132 {
133  const char *optarg_;
134  int c;
135  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
136 
137  switch (c) {
138  case 'd': display = false; break;
139  case 'D':
140  if (strcmp( optarg_ ,"ANY") == 0 ) {deviceType = CV_CAP_ANY;}
141  else if ( strcmp( optarg_ ,"MIL") == 0) {deviceType = CV_CAP_MIL;}
142  else if ( strcmp( optarg_ ,"VFW") == 0) {deviceType = CV_CAP_VFW;}
143  else if ( strcmp( optarg_ ,"V4L") == 0) {deviceType = CV_CAP_V4L;}
144  else if ( strcmp( optarg_ ,"V4L2") == 0) {deviceType = CV_CAP_V4L2;}
145  else if ( strcmp( optarg_ ,"DC1394") == 0) {deviceType = CV_CAP_DC1394;}
146  else if ( strcmp( optarg_ ,"CMU1394") == 0) {deviceType = CV_CAP_CMU1394;}
147  else if ( strcmp( optarg_ ,"DSHOW") == 0) {deviceType = CV_CAP_DSHOW;}
148  else {std::cout << "Unknown type of device" << std::endl;
149  deviceType = 0;}
150  break;
151  case 'n':
152  nframes = (unsigned int)atoi(optarg_); break;
153  case 'o':
154  save = true;
155  opath = optarg_; break;
156  case 'h': usage(argv[0], NULL, nframes, opath); return false; break;
157 
158  default:
159  usage(argv[0], optarg_, nframes, opath);
160  return false; break;
161  }
162  }
163 
164  if ((c == 1) || (c == -1)) {
165  // standalone param or error
166  usage(argv[0], NULL, nframes, opath);
167  std::cerr << "ERROR: " << std::endl;
168  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
169  return false;
170  }
171 
172  return true;
173 }
174 
175 
183 int
184 main(int argc, const char ** argv)
185 {
186  try {
187  bool opt_display = true;
188  unsigned nframes = 50;
189  bool save = false;
190  int deviceType = CV_CAP_ANY;
191 
192  // Declare an image. It size is not defined yet. It will be defined when the
193  // image will acquired the first time.
194 #ifdef GRAB_COLOR
195  vpImage<vpRGBa> I; // This is a color image (in RGBa format)
196 #else
197  vpImage<unsigned char> I; // This is a B&W image
198 #endif
199 
200  // Set default output image name for saving
201 #ifdef GRAB_COLOR
202  // Color images will be saved in PGM P6 format
203 # if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
204  std::string opath = "/tmp/I%04d.ppm";
205 # elif defined(_WIN32)
206  std::string opath = "C:/temp/I%04d.ppm";
207 # endif
208 #else
209  // B&W images will be saved in PGM P5 format
210 # if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
211  std::string opath = "/tmp/I%04d.pgm";
212 # elif defined(_WIN32)
213  std::string opath = "C:/temp/I%04d.pgm";
214 # endif
215 #endif
216 
217  // Read the command line options
218  if (getOptions(argc, argv, opt_display, nframes, save, opath, deviceType) == false) {
219  exit (-1);
220  }
221  // Create the grabber
222  vpOpenCVGrabber grabber ;
223  try {
224  // Set the type of device to detect. Here for example we expect to find a firewire camera.
225  grabber.setDeviceType(deviceType);
226 
227  // Initialize the grabber
228  grabber.open(I);
229 
230  // Acquire an image
231  grabber.acquire(I);
232  }
233  catch(...)
234  {
235  vpCTRACE << "Cannot acquire an image... "
236  << "Check if a camera is connected to your computer."
237  << std::endl ;
238  return 0;
239  }
240 
241  std::cout << "Image size: width : " << I.getWidth() << " height: "
242  << I.getHeight() << std::endl;
243 
244  // Creates a display
245  vpDisplayOpenCV display;
246 
247  if (opt_display) {
248  display.init(I,100,100,"OpenCV framegrabber");
249  }
250 
251  double tbegin=0, tend=0, tloop=0, ttotal=0;
252 
253  ttotal = 0;
254  tbegin = vpTime::measureTimeMs();
255  // Loop for image acquisition and display
256  for (unsigned i = 0; i < nframes; i++) {
257  //Acquires an RGBa image
258  grabber.acquire(I);
259 
260  if (opt_display) {
261  //Displays the grabbed image
263  vpDisplay::flush(I);
264  }
265 
266  if (save) {
267  char buf[FILENAME_MAX];
268  sprintf(buf, opath.c_str(), i);
269  std::string filename(buf);
270  std::cout << "Write: " << filename << std::endl;
271  vpImageIo::write(I, filename);
272  }
273  tend = vpTime::measureTimeMs();
274  tloop = tend - tbegin;
275  tbegin = tend;
276  std::cout << "loop time: " << tloop << " ms" << std::endl;
277  ttotal += tloop;
278  }
279  std::cout << "Mean loop time: " << ttotal / nframes << " ms" << std::endl;
280  std::cout << "Mean frequency: " << 1000./(ttotal / nframes) << " fps" << std::endl;
281 
282  return 0;
283  }
284  catch(vpException e) {
285  std::cout << "Catch an exception: " << e << std::endl;
286  return 1;
287  }
288 }
289 #else // defined (VISP_HAVE_OPENCV)
290 int
291 main()
292 {
293  vpTRACE("OpenCV is not available...") ;
294 }
295 #endif // defined (VISP_HAVE_OPENCV)
296 
297 
static void write(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:476
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
unsigned int getWidth() const
Definition: vpImage.h:161
#define vpTRACE
Definition: vpDebug.h:418
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
#define vpCTRACE
Definition: vpDebug.h:341
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:210
The vpDisplayOpenCV allows to display image using the opencv library.
unsigned int getHeight() const
Definition: vpImage.h:152