Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
grabFlyCapture.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Acquire images using OpenCV cv::VideoCapture.
33  *
34  * Authors:
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
39 #include <visp3/core/vpConfig.h>
40 
48 #include <iostream>
49 
50 #include <visp3/core/vpConfig.h>
51 
52 #if defined(VISP_HAVE_FLYCAPTURE)
53 
54 #include <visp3/core/vpImage.h>
55 #include <visp3/core/vpImageConvert.h>
56 #include <visp3/gui/vpDisplayGDI.h>
57 #include <visp3/gui/vpDisplayOpenCV.h>
58 #include <visp3/gui/vpDisplayX.h>
59 #include <visp3/io/vpImageIo.h>
60 #include <visp3/io/vpParseArgv.h>
61 #include <visp3/sensor/vpFlyCaptureGrabber.h>
62 
63 #define GETOPTARGS "cdhi:n:o:"
64 
75 void usage(const char *name, const char *badparam, unsigned int icamera, std::string &opath)
76 {
77  fprintf(stdout, "\n\
78 Acquire and display images using PointGrey FlyCapture SDK.\n\
79 \n\
80 SYNOPSIS\n\
81  %s [-c] [-d] [-i <camera index>] [-o <output image filename>] [-h] \n", name);
82 
83  fprintf(stdout, "\n\
84 OPTIONS: Default\n\
85  -c \n\
86  Disable mouse click and acquire only 10 images.\n\
87 \n\
88  -d \n\
89  Turn off the display.\n\
90 \n\
91  -i [%%d] %u\n\
92  Camera index to connect (0 for the first one). \n\
93 \n\
94  -o [%%s]\n\
95  Filename for image saving. \n\
96  Example: -o %s\n\
97  The %%d is for the image numbering.\n\
98 \n\
99  -h \n\
100  Print the help.\n\
101 \n", icamera, opath.c_str());
102 
103  if (badparam) {
104  fprintf(stderr, "ERROR: \n");
105  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
106  }
107 }
108 
125 bool getOptions(int argc, const char **argv, bool &display, bool &click, bool &save, std::string &opath,
126  unsigned int &icamera)
127 {
128  const char *optarg_;
129  int c;
130  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
131 
132  switch (c) {
133  case 'c':
134  click = false;
135  break;
136  case 'd':
137  display = false;
138  break;
139  case 'i':
140  icamera = (unsigned int)atoi(optarg_);
141  break;
142  case 'o':
143  save = true;
144  opath = optarg_;
145  break;
146  case 'h':
147  usage(argv[0], NULL, icamera, opath);
148  return false;
149  break;
150 
151  default:
152  usage(argv[0], optarg_, icamera, opath);
153  return false;
154  break;
155  }
156  }
157 
158  if ((c == 1) || (c == -1)) {
159  // standalone param or error
160  usage(argv[0], NULL, icamera, opath);
161  std::cerr << "ERROR: " << std::endl;
162  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
163  return false;
164  }
165 
166  return true;
167 }
168 
169 // usage: binary <device name>
170 // device name: 0 is the default to dial with the first camera,
171 // 1 to dial with a second camera attached to the computer
172 int main(int argc, const char **argv)
173 {
174  try {
175  bool opt_display = true;
176  bool opt_click = true;
177  bool opt_save = false;
178  unsigned int opt_icamera = 0;
179  std::string opt_opath = "I%04d.pgm";
180  // vpImage<vpRGBa> I; // for color images
181  vpImage<unsigned char> I; // for gray images
182 
183  // Read the command line options
184  if (getOptions(argc, argv, opt_display, opt_click, opt_save, opt_opath, opt_icamera) == false) {
185  return 0;
186  }
187 
188  std::cout << "Use device : " << opt_icamera << std::endl;
190  g.setCameraIndex(opt_icamera); // open the default camera
191  g.open(I);
192  std::cout << "Camera serial: " << g.getCameraSerial(g.getCameraIndex()) << std::endl;
193  std::cout << "Image size : " << I.getWidth() << " " << I.getHeight() << std::endl;
194 
195  vpDisplay *display = NULL;
196  if (opt_display) {
197 #if defined(VISP_HAVE_X11)
198  display = new vpDisplayX(I);
199 #elif defined(VISP_HAVE_GDI)
200  display = new vpDisplayGDI(I);
201 #elif defined(VISP_HAVE_OPENCV)
202  display = new vpDisplayOpenCV(I);
203 #else
204  std::cout << "No image viewer is available..." << std::endl;
205 #endif
206  }
207 
208  for (;;) {
209  g.acquire(I); // get a new frame from camera
210 
211  if (opt_save) {
212  static unsigned int frame = 0;
213  char buf[FILENAME_MAX];
214  sprintf(buf, opt_opath.c_str(), frame++);
215  std::string filename(buf);
216  std::cout << "Write: " << filename << std::endl;
217  vpImageIo::write(I, filename);
218  }
219 
221  vpDisplay::displayText(I, 10, 10, "A click to quit...", vpColor::red);
222  vpDisplay::flush(I);
223  if (opt_click && opt_display) {
224  if (vpDisplay::getClick(I, false) == true)
225  break;
226  } else {
227  static unsigned int cpt = 0;
228  if (cpt++ == 10)
229  break;
230  }
231  }
232  if (display)
233  delete display;
234 
235  // The camera connection will be closed automatically in vpFlyCapture
236  // destructor
237  return EXIT_SUCCESS;
238  } catch (const vpException &e) {
239  std::cout << "Catch an exception: " << e.getStringMessage() << std::endl;
240  return EXIT_FAILURE;
241  }
242 }
243 
244 #else
245 int main() {
246  std::cout << "You do not have PointGrey FlyCapture SDK enabled..." << std::endl;
247  std::cout << "Tip:" << std::endl;
248  std::cout << "- Install FlyCapture SDK, configure again ViSP using cmake and build again this example" << std::endl;
249  return EXIT_SUCCESS;
250 }
251 #endif
unsigned int getCameraIndex() const
Class that defines generic functionnalities for display.
Definition: vpDisplay.h:171
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
unsigned int getWidth() const
Definition: vpImage.h:239
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
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:151
void acquire(vpImage< unsigned char > &I)
error that can be emited by ViSP classes.
Definition: vpException.h:71
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:69
void open(vpImage< unsigned char > &I)
static const vpColor red
Definition: vpColor.h:180
static void write(const vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:375
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
static unsigned int getCameraSerial(unsigned int index)
unsigned int getHeight() const
Definition: vpImage.h:178
const std::string & getStringMessage(void) const
Send a reference (constant) related the error message (can be empty).
Definition: vpException.cpp:92