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