Visual Servoing Platform  version 3.5.0 under development (2022-02-15)
tutorial-grabber-flycapture.cpp
1 #include <visp3/core/vpImage.h>
3 #include <visp3/gui/vpDisplayGDI.h>
4 #include <visp3/gui/vpDisplayX.h>
5 #include <visp3/gui/vpDisplayOpenCV.h>
6 #include <visp3/sensor/vpFlyCaptureGrabber.h>
7 #include <visp3/io/vpImageStorageWorker.h>
8 
9 int main(int argc, char **argv)
10 {
11 #if defined(VISP_HAVE_FLYCAPTURE) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
12  try {
13  std::string opt_seqname;
14  int opt_record_mode = 0;
15  bool opt_change_settings = false;
16 
17  for (int i = 0; i < argc; i++) {
18  if (std::string(argv[i]) == "--seqname")
19  opt_seqname = std::string(argv[i + 1]);
20  else if (std::string(argv[i]) == "--record")
21  opt_record_mode = std::atoi(argv[i + 1]);
22  else if (std::string(argv[i]) == "--change_settings")
23  opt_change_settings = true;
24  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
25  std::cout << "\nUsage: " << argv[0]
26  << " [--seqname <sequence name (default: empty>] [--record <0: continuous | 1: single shot (default: 0)>]"
27  " [--change_settings] [--help] [-h]\n"
28  << "\nExample to visualize images:\n"
29  << " " << argv[0] << "\n"
30  << "\nExamples to record a sequence:\n"
31  << " " << argv[0] << " --seqname I%04d.png \n"
32  << " " << argv[0] << " --seqname folder/I%04d.png --record 0\n"
33  << "\nExamples to record single shot images:\n"
34  << " " << argv[0] << " --seqname I%04d.png --record 1\n"
35  << " " << argv[0] << " --seqname folder/I%04d.png --record 1\n"
36  << std::endl;
37  return 0;
38  }
39  }
40 
41  std::cout << "Settings : " << (opt_change_settings ? "modified" : "current") << std::endl;
42  std::cout << "Recording : " << (opt_seqname.empty() ? "disabled" : "enabled") << std::endl;
43 
44  std::string text_record_mode = std::string("Record mode: ") + (opt_record_mode ? std::string("single") : std::string("continuous"));
45 
46  if (! opt_seqname.empty()) {
47  std::cout << text_record_mode << std::endl;
48  std::cout << "Record name: " << opt_seqname << std::endl;
49  }
50 
51  vpImage<unsigned char> I; // Create a gray level image container
53  vpFlyCaptureGrabber g; // Create a grabber based on FlyCapture SDK third party lib
55 
57  if (opt_change_settings) {
58  try {
59  g.setShutter(true); // Turn auto shutter on
60  g.setGain(true); // Turn auto gain on
61  g.setVideoModeAndFrameRate(FlyCapture2::VIDEOMODE_640x480Y8, FlyCapture2::FRAMERATE_60);
62  } catch (...) { // If settings are not available just catch execption to
63  // continue with default settings
64  std::cout << "Warning: cannot modify camera settings" << std::endl;
65  }
66  }
69  g.open(I);
71 
72  std::cout << "Image size : " << I.getWidth() << " " << I.getHeight() << std::endl;
73 
74 #if defined(VISP_HAVE_X11)
75  vpDisplayX d(I);
76 #elif defined(VISP_HAVE_GDI)
77  vpDisplayGDI d(I);
78 #elif defined(VISP_HAVE_OPENCV)
79  vpDisplayOpenCV d(I);
80 #else
81  std::cout << "No image viewer is available..." << std::endl;
82 #endif
83 
84  vpImageQueue<unsigned char> image_queue(opt_seqname, opt_record_mode);
85  vpImageStorageWorker<unsigned char> image_storage_worker(std::ref(image_queue));
86  std::thread image_storage_thread(&vpImageStorageWorker<unsigned char>::run, &image_storage_worker);
87 
88  bool quit = false;
89  while (! quit) {
90  double t = vpTime::measureTimeMs();
92  g.acquire(I);
98  quit = image_queue.record(I);
100  std::stringstream ss;
101  ss << "Acquisition time: " << std::setprecision(3) << vpTime::measureTimeMs() - t << " ms";
102  vpDisplay::displayText(I, I.getHeight() - 20, 10, ss.str(), vpColor::red);
103  vpDisplay::flush(I);
104  }
105  image_queue.cancel();
106  image_storage_thread.join();
107  } catch (const vpException &e) {
108  std::cout << "Catch an exception: " << e.getStringMessage() << std::endl;
109  }
110 #else
111  (void) argc;
112  (void) argv;
113 #ifndef VISP_HAVE_FLYCAPTURE
114  std::cout << "Install Flycapture SDK, configure and build ViSP again to use this example" << std::endl;
115 #endif
116 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
117  std::cout << "This turorial should be built with c++11 support" << std::endl;
118 #endif
119 #endif
120 }
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
float setShutter(bool auto_shutter, float shutter_ms=10)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:134
void acquire(vpImage< unsigned char > &I)
error that can be emited by ViSP classes.
Definition: vpException.h:71
static void flush(const vpImage< unsigned char > &I)
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:126
void open(vpImage< unsigned char > &I)
static const vpColor red
Definition: vpColor.h:217
float setGain(bool gain_auto, float gain_value=0)
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
unsigned int getHeight() const
Definition: vpImage.h:188
const std::string & getStringMessage() const
Send a reference (constant) related the error message (can be empty).
Definition: vpException.cpp:92
unsigned int getWidth() const
Definition: vpImage.h:246
void setVideoModeAndFrameRate(FlyCapture2::VideoMode video_mode, FlyCapture2::FrameRate frame_rate)