Visual Servoing Platform  version 3.5.1 under development (2023-09-22)
tutorial-grabber-flycapture.cpp
1 #include <visp3/core/vpImage.h>
3 #include <visp3/gui/vpDisplayGDI.h>
4 #include <visp3/gui/vpDisplayOpenCV.h>
5 #include <visp3/gui/vpDisplayX.h>
6 #include <visp3/io/vpImageStorageWorker.h>
7 #include <visp3/sensor/vpFlyCaptureGrabber.h>
8 
9 void usage(const char *argv[], int error)
10 {
11  std::cout << "SYNOPSIS" << std::endl
12  << " " << argv[0] << " [--change-settings]"
13  << " [--seqname <sequence name>]"
14  << " [--record <mode>]"
15  << " [--no-display]"
16  << " [--help] [-h]" << std::endl
17  << std::endl;
18  std::cout << "DESCRIPTION" << std::endl
19  << " --change-settings" << std::endl
20  << " Force camera settings to auto shutter, auto gain and 640x480 MONO8 image" << std::endl
21  << " acquisition at 60 fps." << std::endl
22  << std::endl
23  << " --seqname <sequence name>" << std::endl
24  << " Name of the sequence of image to create (ie: /tmp/image%04d.jpg)." << std::endl
25  << " Default: empty." << std::endl
26  << std::endl
27  << " --record <mode>" << std::endl
28  << " Allowed values for mode are:" << std::endl
29  << " 0: record all the captures images (continuous mode)," << std::endl
30  << " 1: record only images selected by a user click (single shot mode)." << std::endl
31  << " Default mode: 0" << std::endl
32  << std::endl
33  << " --no-display" << std::endl
34  << " Disable displaying captured images." << std::endl
35  << " When used and sequence name specified, record mode is internally set to 1 (continuous mode)."
36  << std::endl
37  << std::endl
38  << " --help, -h" << std::endl
39  << " Print this helper message." << std::endl
40  << std::endl;
41  std::cout << "USAGE" << std::endl
42  << " Example to visualize images:" << std::endl
43  << " " << argv[0] << std::endl
44  << std::endl
45  << " Examples to record a sequence:" << std::endl
46  << " " << argv[0] << " --seqname I%04d.png" << std::endl
47  << " " << argv[0] << " --seqname folder/I%04d.png --record 0" << std::endl
48  << std::endl
49  << " Examples to record single shot images:\n"
50  << " " << argv[0] << " --seqname I%04d.png --record 1\n"
51  << " " << argv[0] << " --seqname folder/I%04d.png --record 1" << std::endl
52  << std::endl;
53 
54  if (error) {
55  std::cout << "Error" << std::endl
56  << " "
57  << "Unsupported parameter " << argv[error] << std::endl;
58  }
59 }
60 
61 int main(int argc, const char *argv[])
62 {
63 #if defined(VISP_HAVE_FLYCAPTURE) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
64  try {
65  std::string opt_seqname;
66  int opt_record_mode = 0;
67  bool opt_change_settings = false;
68  bool opt_display = true;
69 
70  for (int i = 1; i < argc; i++) {
71  if (std::string(argv[i]) == "--change-settings") {
72  opt_change_settings = true;
73  } else if (std::string(argv[i]) == "--seqname") {
74  opt_seqname = std::string(argv[i + 1]);
75  i++;
76  } else if (std::string(argv[i]) == "--record") {
77  opt_record_mode = std::atoi(argv[i + 1]);
78  i++;
79  } else if (std::string(argv[i]) == "--no-display") {
80  opt_display = false;
81  } else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
82  usage(argv, 0);
83  return EXIT_SUCCESS;
84  } else {
85  usage(argv, i);
86  return EXIT_FAILURE;
87  }
88  }
89 
90  if ((!opt_display) && (!opt_seqname.empty())) {
91  opt_record_mode = 0;
92  }
93 
94  std::cout << "Settings : " << (opt_change_settings ? "modified" : "current") << std::endl;
95  std::cout << "Recording : " << (opt_seqname.empty() ? "disabled" : "enabled") << std::endl;
96  std::cout << "Display : " << (opt_display ? "enabled" : "disabled") << std::endl;
97 
98  std::string text_record_mode =
99  std::string("Record mode: ") + (opt_record_mode ? std::string("single") : std::string("continuous"));
100 
101  if (!opt_seqname.empty()) {
102  std::cout << text_record_mode << std::endl;
103  std::cout << "Record name: " << opt_seqname << std::endl;
104  }
105 
106  vpImage<vpRGBa> I; // Create a gray level image container
108  vpFlyCaptureGrabber g; // Create a grabber based on FlyCapture SDK third party lib
110 
112  if (opt_change_settings) {
113  try {
114  g.setShutter(true); // Turn auto shutter on
115  g.setGain(true); // Turn auto gain on
116  g.setVideoModeAndFrameRate(FlyCapture2::VIDEOMODE_640x480Y8, FlyCapture2::FRAMERATE_60);
117  } catch (...) { // If settings are not available just catch execption to
118  // continue with default settings
119  std::cout << "Warning: cannot modify camera settings" << std::endl;
120  }
121  }
124  g.open(I);
126 
127  std::cout << "Image size : " << I.getWidth() << " " << I.getHeight() << std::endl;
128 
129  vpDisplay *d = NULL;
130  if (opt_display) {
131 #if !(defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
132  std::cout << "No image viewer is available..." << std::endl;
133  opt_display = false;
134 #endif
135  }
136  if (opt_display) {
137 #ifdef VISP_HAVE_X11
138  d = new vpDisplayX(I);
139 #elif defined(VISP_HAVE_GDI)
140  d = new vpDisplayGDI(I);
141 #elif defined(HAVE_OPENCV_HIGHGUI)
142  d = new vpDisplayOpenCV(I);
143 #endif
144  }
145 
146  vpImageQueue<vpRGBa> image_queue(opt_seqname, opt_record_mode);
147  vpImageStorageWorker<vpRGBa> image_storage_worker(std::ref(image_queue));
148  std::thread image_storage_thread(&vpImageStorageWorker<vpRGBa>::run, &image_storage_worker);
149 
150  bool quit = false;
151  while (!quit) {
152  double t = vpTime::measureTimeMs();
154  g.acquire(I);
160  quit = image_queue.record(I);
162  std::stringstream ss;
163  ss << "Acquisition time: " << std::setprecision(3) << vpTime::measureTimeMs() - t << " ms";
164  vpDisplay::displayText(I, I.getHeight() - 20, 10, ss.str(), vpColor::red);
165  vpDisplay::flush(I);
166  }
167  image_queue.cancel();
168  image_storage_thread.join();
169 
170  if (d) {
171  delete d;
172  }
173  } catch (const vpException &e) {
174  std::cout << "Catch an exception: " << e.getStringMessage() << std::endl;
175  }
176 #else
177  (void)argc;
178  (void)argv;
179 #ifndef VISP_HAVE_FLYCAPTURE
180  std::cout << "Install Flycapture SDK, configure and build ViSP again to use this example" << std::endl;
181 #endif
182 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
183  std::cout << "This tutorial should be built with c++11 support" << std::endl;
184 #endif
185 #endif
186 }
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:132
Class that defines generic functionalities for display.
Definition: vpDisplay.h:173
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
float setGain(bool gain_auto, float gain_value=0)
float setShutter(bool auto_shutter, float shutter_ms=10)
void open(vpImage< unsigned char > &I)
void acquire(vpImage< unsigned char > &I)
void setVideoModeAndFrameRate(FlyCapture2::VideoMode video_mode, FlyCapture2::FrameRate frame_rate)
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:184
VISP_EXPORT double measureTimeMs()