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