Visual Servoing Platform  version 3.6.1 under development (2024-03-18)
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) && defined(VISP_HAVE_THREADS)
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  }
74  else if (std::string(argv[i]) == "--seqname") {
75  opt_seqname = std::string(argv[i + 1]);
76  i++;
77  }
78  else if (std::string(argv[i]) == "--record") {
79  opt_record_mode = std::atoi(argv[i + 1]);
80  i++;
81  }
82  else if (std::string(argv[i]) == "--no-display") {
83  opt_display = false;
84  }
85  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
86  usage(argv, 0);
87  return EXIT_SUCCESS;
88  }
89  else {
90  usage(argv, i);
91  return EXIT_FAILURE;
92  }
93  }
94 
95  if ((!opt_display) && (!opt_seqname.empty())) {
96  opt_record_mode = 0;
97  }
98 
99  std::cout << "Settings : " << (opt_change_settings ? "modified" : "current") << std::endl;
100  std::cout << "Recording : " << (opt_seqname.empty() ? "disabled" : "enabled") << std::endl;
101  std::cout << "Display : " << (opt_display ? "enabled" : "disabled") << std::endl;
102 
103  std::string text_record_mode =
104  std::string("Record mode: ") + (opt_record_mode ? std::string("single") : std::string("continuous"));
105 
106  if (!opt_seqname.empty()) {
107  std::cout << text_record_mode << std::endl;
108  std::cout << "Record name: " << opt_seqname << std::endl;
109  }
110 
111  vpImage<vpRGBa> I; // Create a gray level image container
113  vpFlyCaptureGrabber g; // Create a grabber based on FlyCapture SDK third party lib
115 
117  if (opt_change_settings) {
118  try {
119  g.setShutter(true); // Turn auto shutter on
120  g.setGain(true); // Turn auto gain on
121  g.setVideoModeAndFrameRate(FlyCapture2::VIDEOMODE_640x480Y8, FlyCapture2::FRAMERATE_60);
122  }
123  catch (...) { // If settings are not available just catch execption to
124  // continue with default settings
125  std::cout << "Warning: cannot modify camera settings" << std::endl;
126  }
127  }
130  g.open(I);
132 
133  std::cout << "Image size : " << I.getWidth() << " " << I.getHeight() << std::endl;
134 
135  vpDisplay *d = nullptr;
136  if (opt_display) {
137 #if !(defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
138  std::cout << "No image viewer is available..." << std::endl;
139  opt_display = false;
140 #endif
141  }
142  if (opt_display) {
143 #ifdef VISP_HAVE_X11
144  d = new vpDisplayX(I);
145 #elif defined(VISP_HAVE_GDI)
146  d = new vpDisplayGDI(I);
147 #elif defined(HAVE_OPENCV_HIGHGUI)
148  d = new vpDisplayOpenCV(I);
149 #endif
150  }
151 
152  vpImageQueue<vpRGBa> image_queue(opt_seqname, opt_record_mode);
153  vpImageStorageWorker<vpRGBa> image_storage_worker(std::ref(image_queue));
154  std::thread image_storage_thread(&vpImageStorageWorker<vpRGBa>::run, &image_storage_worker);
155 
156  bool quit = false;
157  while (!quit) {
158  double t = vpTime::measureTimeMs();
160  g.acquire(I);
166  quit = image_queue.record(I);
168  std::stringstream ss;
169  ss << "Acquisition time: " << std::setprecision(3) << vpTime::measureTimeMs() - t << " ms";
170  vpDisplay::displayText(I, I.getHeight() - 20, 10, ss.str(), vpColor::red);
171  vpDisplay::flush(I);
172  }
173  image_queue.cancel();
174  image_storage_thread.join();
175 
176  if (d) {
177  delete d;
178  }
179  }
180  catch (const vpException &e) {
181  std::cout << "Catch an exception: " << e.getStringMessage() << std::endl;
182  }
183 #else
184  (void)argc;
185  (void)argv;
186 #ifndef VISP_HAVE_FLYCAPTURE
187  std::cout << "Install Flycapture SDK, configure and build ViSP again to use this example" << std::endl;
188 #endif
189 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
190  std::cout << "This tutorial should be built with c++11 support" << std::endl;
191 #endif
192 #endif
193 }
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 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:249
unsigned int getHeight() const
Definition: vpImage.h:184
VISP_EXPORT double measureTimeMs()