Visual Servoing Platform  version 3.0.1
grabFlyCapture.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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 OpenCV cv::VideoCapture.
32  *
33  * Authors:
34  * Fabien Spindler
35  *
36  *****************************************************************************/
37 
38 #include <visp3/core/vpConfig.h>
39 
47 #include <iostream>
48 
49 #include <visp3/core/vpConfig.h>
50 
51 #if defined(VISP_HAVE_FLYCAPTURE)
52 
53 #include <visp3/gui/vpDisplayX.h>
54 #include <visp3/gui/vpDisplayGDI.h>
55 #include <visp3/gui/vpDisplayOpenCV.h>
56 #include <visp3/core/vpImage.h>
57 #include <visp3/core/vpImageConvert.h>
58 #include <visp3/io/vpParseArgv.h>
59 #include <visp3/io/vpImageIo.h>
60 #include <visp3/sensor/vpFlyCaptureGrabber.h>
61 
62 #define GETOPTARGS "cdhi:n:o:"
63 
74 void usage(const char *name, const char *badparam,
75  unsigned int icamera,
76  std::string &opath)
77 {
78  fprintf(stdout, "\n\
79 Acquire and display images using PointGrey FlyCapture SDK.\n\
80 \n\
81 SYNOPSIS\n\
82  %s [-c] [-d] [-i <camera index>] [-o <output image filename>] [-h] \n", name);
83 
84  fprintf(stdout, "\n\
85 OPTIONS: Default\n\
86  -c \n\
87  Disable mouse click and acquire only 10 images.\n\
88 \n\
89  -d \n\
90  Turn off the display.\n\
91 \n\
92  -i [%%d] %u\n\
93  Camera index to connect (0 for the first one). \n\
94 \n\
95  -o [%%s]\n\
96  Filename for image saving. \n\
97  Example: -o %s\n\
98  The %%d is for the image numbering.\n\
99 \n\
100  -h \n\
101  Print the help.\n\
102 \n", icamera, opath.c_str());
103 
104  if (badparam) {
105  fprintf(stderr, "ERROR: \n" );
106  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
107  }
108 }
109 
126 bool getOptions(int argc, const char **argv, bool &display, bool &click,
127  bool &save, std::string &opath, unsigned int &icamera)
128 {
129  const char *optarg_;
130  int c;
131  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
132 
133  switch (c) {
134  case 'c': click = false; break;
135  case 'd': display = false; break;
136  case 'i': icamera = (unsigned int)atoi(optarg_); break;
137  case 'o':
138  save = true;
139  opath = optarg_; break;
140  case 'h': usage(argv[0], NULL, icamera, opath); return false; break;
141 
142  default:
143  usage(argv[0], optarg_, icamera, opath);
144  return false; break;
145  }
146  }
147 
148  if ((c == 1) || (c == -1)) {
149  // standalone param or error
150  usage(argv[0], NULL, icamera, opath);
151  std::cerr << "ERROR: " << std::endl;
152  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
153  return false;
154  }
155 
156  return true;
157 }
158 
159 // usage: binary <device name>
160 // device name: 0 is the default to dial with the first camera,
161 // 1 to dial with a second camera attached to the computer
162 int main(int argc, const char** argv)
163 {
164  try {
165  bool opt_display = true;
166  bool opt_click = true;
167  bool opt_save = false;
168  unsigned int opt_icamera = 0;
169  std::string opt_opath = "I%04d.pgm";
170  //vpImage<vpRGBa> I; // for color images
171  vpImage<unsigned char> I; // for gray images
172 
173  // Read the command line options
174  if (getOptions(argc, argv, opt_display, opt_click, opt_save,
175  opt_opath, opt_icamera) == false) {
176  return 0;
177  }
178 
179  std::cout << "Use device : " << opt_icamera << std::endl;
181  g.setCameraIndex(opt_icamera); // open the default camera
182  g.open(I);
183  std::cout << "Camera serial: " << g.getCameraSerial( g.getCameraIndex() ) << std::endl;
184  std::cout << "Image size : " << I.getWidth() << " " << I.getHeight() << std::endl;
185 
186  vpDisplay *display = NULL;
187  if (opt_display) {
188 #if defined(VISP_HAVE_X11)
189  display = new vpDisplayX(I);
190 #elif defined(VISP_HAVE_GDI)
191  display = new vpDisplayGDI(I);
192 #elif defined(VISP_HAVE_OPENCV)
193  display = new vpDisplayOpenCV(I);
194 #else
195  std::cout << "No image viewer is available..." << std::endl;
196 #endif
197  }
198 
199  for(;;) {
200  g.acquire(I); // get a new frame from camera
201 
202  if (opt_save) {
203  static unsigned int frame = 0;
204  char buf[FILENAME_MAX];
205  sprintf(buf, opt_opath.c_str(), frame++);
206  std::string filename(buf);
207  std::cout << "Write: " << filename << std::endl;
208  vpImageIo::write(I, filename);
209  }
210 
212  vpDisplay::displayText(I, 10, 10, "A click to quit...", vpColor::red);
213  vpDisplay::flush(I);
214  if (opt_click && opt_display) {
215  if (vpDisplay::getClick(I, false) == true)
216  break;
217  }
218  else {
219  static unsigned int cpt = 0;
220  if (cpt ++ == 10)
221  break;
222  }
223  }
224  if (display)
225  delete display;
226 
227  // The camera connection will be closed automatically in vpFlyCapture destructor
228  return 0;
229  }
230  catch(vpException &e) {
231  std::cout << "Catch an exception: " << e.getStringMessage() << std::endl;
232  return 0;
233  }
234 }
235 
236 #else
237 int main()
238 {
239  std::cout << "PointGrey FlyCapture SDK is not available..." << std::endl;
240 }
241 
242 #endif
Class that defines generic functionnalities for display.
Definition: vpDisplay.h:169
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:153
void acquire(vpImage< unsigned char > &I)
error that can be emited by ViSP classes.
Definition: vpException.h:73
static void flush(const vpImage< unsigned char > &I)
void setCameraIndex(unsigned int index)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
void open(vpImage< unsigned char > &I)
static const vpColor red
Definition: vpColor.h:163
static void write(const vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:368
unsigned int getCameraIndex() const
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
unsigned int getHeight() const
Definition: vpImage.h:175
static unsigned int getCameraSerial(unsigned int index)
unsigned int getWidth() const
Definition: vpImage.h:226
const std::string & getStringMessage(void) const
Send a reference (constant) related the error message (can be empty).