ViSP  3.0.0
grabOpenCV.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 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  * Acquire images using DirectShow (under Windows only) and display it
32  * using GTK or GDI.
33  *
34  * Authors:
35  * Nicolas Melchior
36  *
37  *****************************************************************************/
38 
39 #include <visp3/core/vpConfig.h>
40 #include <visp3/core/vpDebug.h>
41 
49 #if defined (VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION < 0x020408)
50 
51 
52 #include <visp3/sensor/vpOpenCVGrabber.h>
53 #include <visp3/core/vpImage.h>
54 #include <visp3/io/vpImageIo.h>
55 #include <visp3/gui/vpDisplayOpenCV.h>
56 #include <visp3/io/vpParseArgv.h>
57 #include <visp3/core/vpTime.h>
58 
59 // List of allowed command line options
60 #define GETOPTARGS "dhn:o:D:"
61 
62 void usage(const char *name, const char *badparam, unsigned int &nframes, std::string &opath);
63 bool getOptions(int argc, const char **argv, bool &display,
64  unsigned int &nframes, bool &save, std::string &opath, int &deviceType);
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  try {
183  bool opt_display = true;
184  unsigned nframes = 50;
185  bool save = false;
186  int deviceType = CV_CAP_ANY;
187 
188  // Declare an image. It size is not defined yet. It will be defined when the
189  // image will acquired the first time.
190 #ifdef GRAB_COLOR
191  vpImage<vpRGBa> I; // This is a color image (in RGBa format)
192 #else
193  vpImage<unsigned char> I; // This is a B&W image
194 #endif
195 
196  // Set default output image name for saving
197 #ifdef GRAB_COLOR
198  // Color images will be saved in PGM P6 format
199 # if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
200  std::string opath = "/tmp/I%04d.ppm";
201 # elif defined(_WIN32)
202  std::string opath = "C:/temp/I%04d.ppm";
203 # endif
204 #else
205  // B&W images will be saved in PGM P5 format
206 # if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
207  std::string opath = "/tmp/I%04d.pgm";
208 # elif defined(_WIN32)
209  std::string opath = "C:/temp/I%04d.pgm";
210 # endif
211 #endif
212 
213  // Read the command line options
214  if (getOptions(argc, argv, opt_display, nframes, save, opath, deviceType) == false) {
215  exit (-1);
216  }
217  // Create the grabber
218  vpOpenCVGrabber grabber ;
219  try {
220  // Set the type of device to detect. Here for example we expect to find a firewire camera.
221  grabber.setDeviceType(deviceType);
222 
223  // Initialize the grabber
224  grabber.open(I);
225 
226  // Acquire an image
227  grabber.acquire(I);
228  }
229  catch(...)
230  {
231  vpCTRACE << "Cannot acquire an image... "
232  << "Check if a camera is connected to your computer."
233  << std::endl ;
234  return 0;
235  }
236 
237  std::cout << "Image size: width : " << I.getWidth() << " height: "
238  << I.getHeight() << std::endl;
239 
240  // Creates a display
241  vpDisplayOpenCV display;
242 
243  if (opt_display) {
244  display.init(I,100,100,"OpenCV framegrabber");
245  }
246 
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  return 0;
279  }
280  catch(vpException e) {
281  std::cout << "Catch an exception: " << e << 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 
static void write(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:472
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
unsigned int getWidth() const
Definition: vpImage.h:161
error that can be emited by ViSP classes.
Definition: vpException.h:73
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2233
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
#define vpTRACE
Definition: vpDebug.h:414
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
The vpDisplayOpenCV allows to display image using the opencv library.
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:93
#define vpCTRACE
Definition: vpDebug.h:337
unsigned int getHeight() const
Definition: vpImage.h:152