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