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