ViSP  2.6.2
grabOpenCV.cpp
1 /****************************************************************************
2  *
3  * $Id: grabOpenCV.cpp 3619 2012-03-09 17:28:57Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 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] \n\
90  Type of device to detect. 1394\n\
91  It can be ANY, MIL, USB, 1394. \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 = 0;}
137  else if ( strcmp( optarg ,"MIL") == 0) {deviceType = 100;}
138  else if ( strcmp( optarg ,"USB") == 0) {deviceType = 200;}
139  else if ( strcmp( optarg ,"1394") == 0) {deviceType = 300;}
140  else {std::cout << "Unknown type of device" << std::endl;
141  deviceType = 0;}
142  break;
143  case 'n':
144  nframes = (unsigned int)atoi(optarg); break;
145  case 'o':
146  save = true;
147  opath = optarg; break;
148  case 'h': usage(argv[0], NULL, nframes, opath); return false; break;
149 
150  default:
151  usage(argv[0], optarg, nframes, opath);
152  return false; break;
153  }
154  }
155 
156  if ((c == 1) || (c == -1)) {
157  // standalone param or error
158  usage(argv[0], NULL, nframes, opath);
159  std::cerr << "ERROR: " << std::endl;
160  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
161  return false;
162  }
163 
164  return true;
165 }
166 
167 
175 int
176 main(int argc, const char ** argv)
177 {
178  bool opt_display = true;
179  unsigned nframes = 50;
180  bool save = false;
181  int deviceType = CV_CAP_DC1394;
182 
183  // Declare an image. It size is not defined yet. It will be defined when the
184  // image will acquired the first time.
185 #ifdef GRAB_COLOR
186  vpImage<vpRGBa> I; // This is a color image (in RGBa format)
187 #else
188  vpImage<unsigned char> I; // This is a B&W image
189 #endif
190 
191  // Set default output image name for saving
192 #ifdef GRAB_COLOR
193  // Color images will be saved in PGM P6 format
194 # if defined(UNIX)
195  std::string opath = "/tmp/I%04d.ppm";
196 # elif defined(WIN32)
197  std::string opath = "C:/temp/I%04d.ppm";
198 # endif
199 #else
200  // B&W images will be saved in PGM P5 format
201 # if defined(UNIX)
202  std::string opath = "/tmp/I%04d.pgm";
203 # elif defined(WIN32)
204  std::string opath = "C:/temp/I%04d.pgm";
205 # endif
206 #endif
207 
208  // Read the command line options
209  if (getOptions(argc, argv, opt_display, nframes, save, opath, deviceType) == false) {
210  exit (-1);
211  }
212  // Create the grabber
213  vpOpenCVGrabber grabber ;
214  try {
215  // Set the type of device to detect. Here for example we expect to find a firewire camera.
216  grabber.setDeviceType(deviceType);
217 
218  // Initialize the grabber
219  grabber.open(I);
220 
221  // Acquire an image
222  grabber.acquire(I);
223  }
224  catch(...)
225  {
226  vpCTRACE << "Cannot acquire an image... "
227  << "Check if a camera is connected to your computer."
228  << std::endl ;
229  return 0;
230  }
231 
232  std::cout << "Image size: width : " << I.getWidth() << " height: "
233  << I.getHeight() << std::endl;
234 
235  // Creates a display
236  vpDisplayOpenCV display;
237 
238  if (opt_display) {
239  display.init(I,100,100,"OpenCV framegrabber");
240  }
241 
242  try {
243  double tbegin=0, tend=0, tloop=0, ttotal=0;
244 
245  ttotal = 0;
246  tbegin = vpTime::measureTimeMs();
247  // Loop for image acquisition and display
248  for (unsigned i = 0; i < nframes; i++) {
249  //Acquires an RGBa image
250  grabber.acquire(I);
251 
252  if (opt_display) {
253  //Displays the grabbed image
255  vpDisplay::flush(I);
256  }
257 
258  if (save) {
259  char buf[FILENAME_MAX];
260  sprintf(buf, opath.c_str(), i);
261  std::string filename(buf);
262  std::cout << "Write: " << filename << std::endl;
263  vpImageIo::write(I, filename);
264  }
265  tend = vpTime::measureTimeMs();
266  tloop = tend - tbegin;
267  tbegin = tend;
268  std::cout << "loop time: " << tloop << " ms" << std::endl;
269  ttotal += tloop;
270  }
271  std::cout << "Mean loop time: " << ttotal / nframes << " ms" << std::endl;
272  std::cout << "Mean frequency: " << 1000./(ttotal / nframes) << " fps" << std::endl;
273 
274  }
275  catch(...)
276  {
277  vpCERROR << "Failure: exit" << std::endl;
278  return(-1);
279  }
280 }
281 #else // defined (VISP_HAVE_OPENCV)
282 int
283 main()
284 {
285  vpTRACE("OpenCV is not available...") ;
286 }
287 #endif // defined (VISP_HAVE_OPENCV)
288 
289 
void setDeviceType(int type)
static void write(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:355
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
unsigned int getWidth() const
Definition: vpImage.h:154
#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:1964
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:186
The vpDisplayOpenCV allows to display image using the opencv library.
void acquire(vpImage< unsigned char > &I)
unsigned int getHeight() const
Definition: vpImage.h:145
Class for cameras video capture using OpenCV library.