Visual Servoing Platform  version 3.5.0 under development (2022-02-15)
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 
46 #include <iostream>
47 
48 #include <visp3/core/vpConfig.h>
49 
50 #if defined(VISP_HAVE_FLYCAPTURE)
51 
52 #include <visp3/core/vpImage.h>
53 #include <visp3/core/vpImageConvert.h>
54 #include <visp3/gui/vpDisplayGDI.h>
55 #include <visp3/gui/vpDisplayOpenCV.h>
56 #include <visp3/gui/vpDisplayX.h>
57 #include <visp3/io/vpImageIo.h>
58 #include <visp3/io/vpParseArgv.h>
59 #include <visp3/sensor/vpFlyCaptureGrabber.h>
60 
61 #define GETOPTARGS "cdhi:n:o:"
62 
73 void usage(const char *name, const char *badparam, unsigned int icamera, std::string &opath)
74 {
75  fprintf(stdout, "\n\
76 Acquire and display images using PointGrey FlyCapture SDK.\n\
77 \n\
78 SYNOPSIS\n\
79  %s [-c] [-d] [-i <camera index>] [-o <output image filename>] [-h] \n", name);
80 
81  fprintf(stdout, "\n\
82 OPTIONS: Default\n\
83  -c \n\
84  Disable mouse click and acquire only 10 images.\n\
85 \n\
86  -d \n\
87  Turn off the display.\n\
88 \n\
89  -i [%%d] %u\n\
90  Camera index to connect (0 for the first one). \n\
91 \n\
92  -o [%%s]\n\
93  Filename for image saving. \n\
94  Example: -o %s\n\
95  The %%d is for the image numbering.\n\
96 \n\
97  -h \n\
98  Print the help.\n\
99 \n", icamera, opath.c_str());
100 
101  if (badparam) {
102  fprintf(stderr, "ERROR: \n");
103  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
104  }
105 }
106 
123 bool getOptions(int argc, const char **argv, bool &display, bool &click, bool &save, std::string &opath,
124  unsigned int &icamera)
125 {
126  const char *optarg_;
127  int c;
128  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
129 
130  switch (c) {
131  case 'c':
132  click = false;
133  break;
134  case 'd':
135  display = false;
136  break;
137  case 'i':
138  icamera = (unsigned int)atoi(optarg_);
139  break;
140  case 'o':
141  save = true;
142  opath = optarg_;
143  break;
144  case 'h':
145  usage(argv[0], NULL, icamera, opath);
146  return false;
147  break;
148 
149  default:
150  usage(argv[0], optarg_, icamera, opath);
151  return false;
152  break;
153  }
154  }
155 
156  if ((c == 1) || (c == -1)) {
157  // standalone param or error
158  usage(argv[0], NULL, icamera, 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 // usage: binary <device name>
168 // device name: 0 is the default to dial with the first camera,
169 // 1 to dial with a second camera attached to the computer
170 int main(int argc, const char **argv)
171 {
172  try {
173  bool opt_display = true;
174  bool opt_click = true;
175  bool opt_save = false;
176  unsigned int opt_icamera = 0;
177  std::string opt_opath = "I%04d.pgm";
178  // vpImage<vpRGBa> I; // for color images
179  vpImage<unsigned char> I; // for gray images
180 
181  // Read the command line options
182  if (getOptions(argc, argv, opt_display, opt_click, opt_save, opt_opath, opt_icamera) == false) {
183  return 0;
184  }
185 
186  std::cout << "Use device : " << opt_icamera << std::endl;
188  g.setCameraIndex(opt_icamera); // open the default camera
189  g.open(I);
190  std::cout << "Camera serial: " << g.getCameraSerial(g.getCameraIndex()) << std::endl;
191  std::cout << "Image size : " << I.getWidth() << " " << I.getHeight() << std::endl;
192 
193  vpDisplay *display = NULL;
194  if (opt_display) {
195 #if defined(VISP_HAVE_X11)
196  display = new vpDisplayX(I);
197 #elif defined(VISP_HAVE_GDI)
198  display = new vpDisplayGDI(I);
199 #elif defined(VISP_HAVE_OPENCV)
200  display = new vpDisplayOpenCV(I);
201 #else
202  std::cout << "No image viewer is available..." << std::endl;
203 #endif
204  }
205 
206  for (;;) {
207  g.acquire(I); // get a new frame from camera
208 
209  if (opt_save) {
210  static unsigned int frame = 0;
211  char buf[FILENAME_MAX];
212  sprintf(buf, opt_opath.c_str(), frame++);
213  std::string filename(buf);
214  std::cout << "Write: " << filename << std::endl;
215  vpImageIo::write(I, filename);
216  }
217 
219  vpDisplay::displayText(I, 10, 10, "A click to quit...", vpColor::red);
220  vpDisplay::flush(I);
221  if (opt_click && opt_display) {
222  if (vpDisplay::getClick(I, false) == true)
223  break;
224  } else {
225  static unsigned int cpt = 0;
226  if (cpt++ == 10)
227  break;
228  }
229  }
230  if (display)
231  delete display;
232 
233  // The camera connection will be closed automatically in vpFlyCapture
234  // destructor
235  return EXIT_SUCCESS;
236  } catch (const vpException &e) {
237  std::cout << "Catch an exception: " << e.getStringMessage() << std::endl;
238  return EXIT_FAILURE;
239  }
240 }
241 
242 #else
243 int main() {
244  std::cout << "You do not have PointGrey FlyCapture SDK enabled..." << std::endl;
245  std::cout << "Tip:" << std::endl;
246  std::cout << "- Install FlyCapture SDK, configure again ViSP using cmake and build again this example" << std::endl;
247  return EXIT_SUCCESS;
248 }
249 #endif
Class that defines generic functionnalities for display.
Definition: vpDisplay.h:177
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:134
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)
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:293
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:217
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:188
const std::string & getStringMessage() const
Send a reference (constant) related the error message (can be empty).
Definition: vpException.cpp:92
static unsigned int getCameraSerial(unsigned int index)
unsigned int getWidth() const
Definition: vpImage.h:246