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