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