Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
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 
60 #ifdef ENABLE_VISP_NAMESPACE
61 using namespace VISP_NAMESPACE_NAME;
62 #endif
63 
74 void usage(const char *name, const char *badparam, unsigned int icamera, std::string &opath)
75 {
76  fprintf(stdout, "\n\
77 Acquire and display images using PointGrey FlyCapture SDK.\n\
78 \n\
79 SYNOPSIS\n\
80  %s [-c] [-d] [-i <camera index>] [-o <output image filename>] [-h] \n",
81  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",
102 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, bool &save, std::string &opath,
127  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':
135  click = false;
136  break;
137  case 'd':
138  display = false;
139  break;
140  case 'i':
141  icamera = (unsigned int)atoi(optarg_);
142  break;
143  case 'o':
144  save = true;
145  opath = optarg_;
146  break;
147  case 'h':
148  usage(argv[0], nullptr, icamera, opath);
149  return false;
150  break;
151 
152  default:
153  usage(argv[0], optarg_, icamera, opath);
154  return false;
155  break;
156  }
157  }
158 
159  if ((c == 1) || (c == -1)) {
160  // standalone param or error
161  usage(argv[0], nullptr, icamera, opath);
162  std::cerr << "ERROR: " << std::endl;
163  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
164  return false;
165  }
166 
167  return true;
168 }
169 
170 // usage: binary <device name>
171 // device name: 0 is the default to dial with the first camera,
172 // 1 to dial with a second camera attached to the computer
173 int main(int argc, const char **argv)
174 {
175  try {
176  bool opt_display = true;
177  bool opt_click = true;
178  bool opt_save = false;
179  unsigned int opt_icamera = 0;
180  std::string opt_opath = "I%04d.pgm";
181  // vpImage<vpRGBa> I; // for color images
182  vpImage<unsigned char> I; // for gray images
183 
184  // Read the command line options
185  if (getOptions(argc, argv, opt_display, opt_click, opt_save, opt_opath, opt_icamera) == false) {
186  return EXIT_SUCCESS;
187  }
188 
189  std::cout << "Use device : " << opt_icamera << std::endl;
191  g.setCameraIndex(opt_icamera); // open the default camera
192  g.open(I);
193  std::cout << "Camera serial: " << g.getCameraSerial(g.getCameraIndex()) << std::endl;
194  std::cout << "Image size : " << I.getWidth() << " " << I.getHeight() << std::endl;
195 
196  vpDisplay *display = nullptr;
197  if (opt_display) {
198 #if defined(VISP_HAVE_X11)
199  display = new vpDisplayX(I);
200 #elif defined(VISP_HAVE_GDI)
201  display = new vpDisplayGDI(I);
202 #elif defined(HAVE_OPENCV_HIGHGUI)
203  display = new vpDisplayOpenCV(I);
204 #else
205  std::cout << "No image viewer is available..." << std::endl;
206 #endif
207  }
208 
209  for (;;) {
210  g.acquire(I); // get a new frame from camera
211 
212  if (opt_save) {
213  static unsigned int frame = 0;
214  char buf[FILENAME_MAX];
215  snprintf(buf, FILENAME_MAX, opt_opath.c_str(), frame++);
216  std::string filename(buf);
217  std::cout << "Write: " << filename << std::endl;
218  vpImageIo::write(I, filename);
219  }
220 
222  vpDisplay::displayText(I, 10, 10, "A click to quit...", vpColor::red);
223  vpDisplay::flush(I);
224  if (opt_click && opt_display) {
225  if (vpDisplay::getClick(I, false) == true)
226  break;
227  }
228  else {
229  static unsigned int cpt = 0;
230  if (cpt++ == 10)
231  break;
232  }
233  }
234  if (display)
235  delete display;
236 
237  // The camera connection will be closed automatically in vpFlyCapture
238  // destructor
239  return EXIT_SUCCESS;
240  }
241  catch (const vpException &e) {
242  std::cout << "Catch an exception: " << e.getStringMessage() << std::endl;
243  return EXIT_FAILURE;
244  }
245 }
246 
247 #else
248 int main()
249 {
250  std::cout << "You do not have PointGrey FlyCapture SDK enabled..." << std::endl;
251  std::cout << "Tip:" << std::endl;
252  std::cout << "- Install FlyCapture SDK, configure again ViSP using cmake and build again this example" << std::endl;
253  return EXIT_SUCCESS;
254 }
255 #endif
static const vpColor red
Definition: vpColor.h:217
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Class that defines generic functionalities for display.
Definition: vpDisplay.h:178
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:60
const std::string & getStringMessage() const
Definition: vpException.cpp:67
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:291
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70