Visual Servoing Platform  version 3.5.1 under development (2023-09-22)
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) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
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  } else if (std::string(argv[i]) == "--seqname") {
72  opt_seqname = std::string(argv[i + 1]);
73  i++;
74  } else if (std::string(argv[i]) == "--record") {
75  opt_record_mode = std::atoi(argv[i + 1]);
76  i++;
77  } else if (std::string(argv[i]) == "--no-display") {
78  opt_display = false;
79  } else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
80  usage(argv, 0);
81  return EXIT_SUCCESS;
82  } else {
83  usage(argv, i);
84  return EXIT_FAILURE;
85  }
86  }
87 
88  if ((!opt_display) && (!opt_seqname.empty())) {
89  opt_record_mode = 0;
90  }
91 
92  std::cout << "Settings : " << (opt_change_settings ? "modified" : "current") << std::endl;
93  std::cout << "Recording : " << (opt_seqname.empty() ? "disabled" : "enabled") << std::endl;
94  std::cout << "Display : " << (opt_display ? "enabled" : "disabled") << std::endl;
95 
96  std::string text_record_mode =
97  std::string("Record mode: ") + (opt_record_mode ? std::string("single") : std::string("continuous"));
98 
99  if (!opt_seqname.empty()) {
100  std::cout << text_record_mode << std::endl;
101  std::cout << "Record name: " << opt_seqname << std::endl;
102  }
103 
104  vpImage<vpRGBa> I; // Create a color image container
105  bool reset = false; // Disable bus reset during construction (default)
107  vp1394TwoGrabber g(reset); // Create a grabber based on libdc1394-2.x third party lib
109 
111  if (opt_change_settings) {
112  try {
114  g.setFramerate(vp1394TwoGrabber::vpFRAMERATE_60);
115  } catch (...) { // If settings are not available just catch execption to
116  // continue with default settings
117  std::cout << "Warning: cannot modify camera settings" << std::endl;
118  }
119  }
122  g.open(I);
124 
125  std::cout << "Image size : " << I.getWidth() << " " << I.getHeight() << std::endl;
126 
127  vpDisplay *d = NULL;
128  if (opt_display) {
129 #if !(defined(VISP_HAVE_X11) || defined(VISP_HAVE_OPENCV))
130  std::cout << "No image viewer is available..." << std::endl;
131  opt_display = false;
132 #endif
133  }
134  if (opt_display) {
135 #ifdef VISP_HAVE_X11
136  d = new vpDisplayX(I);
137 #elif defined(HAVE_OPENCV_HIGHGUI)
138  d = new vpDisplayOpenCV(I);
139 #endif
140  }
141 
142  vpImageQueue<vpRGBa> image_queue(opt_seqname, opt_record_mode);
143  vpImageStorageWorker<vpRGBa> image_storage_worker(std::ref(image_queue));
144  std::thread image_storage_thread(&vpImageStorageWorker<vpRGBa>::run, &image_storage_worker);
145 
146  bool quit = false;
147  while (!quit) {
148  double t = vpTime::measureTimeMs();
150  g.acquire(I);
154  quit = image_queue.record(I);
156  std::stringstream ss;
157  ss << "Acquisition time: " << std::setprecision(3) << vpTime::measureTimeMs() - t << " ms";
158  vpDisplay::displayText(I, I.getHeight() - 20, 10, ss.str(), vpColor::red);
159  vpDisplay::flush(I);
160  }
161  image_queue.cancel();
162  image_storage_thread.join();
163 
164  if (d) {
165  delete d;
166  }
167  } catch (const vpException &e) {
168  std::cout << "Catch an exception: " << e << std::endl;
169  }
170 #else
171  (void)argc;
172  (void)argv;
173 #ifndef VISP_HAVE_DC1394
174  std::cout << "Install libdc1394, configure and build ViSP again to use this example" << std::endl;
175 #endif
176 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
177  std::cout << "This tutorial should be built with c++11 support" << std::endl;
178 #endif
179 #endif
180 }
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: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
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:184
VISP_EXPORT double measureTimeMs()