Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
tutorial-grabber-v4l2.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/vpV4l2Grabber.h>
7
8#define USE_COLOR // Comment to acquire gray level images
9
10void usage(const char *argv[], int error)
11{
12 std::cout << "SYNOPSIS" << std::endl
13 << " " << argv[0] << " [--device <index>]"
14 << " [--scale <subsampling factor>]"
15 << " [--seqname <sequence name>]"
16 << " [--record <mode>]"
17 << " [--no-display]"
18 << " [--help] [-h]" << std::endl
19 << std::endl;
20 std::cout << "DESCRIPTION" << std::endl
21 << " --device <index> " << std::endl
22 << " Camera device index. Set 0 to dial with the first camera," << std::endl
23 << " and 1 to dial with the second camera attached to the computer." << std::endl
24 << " Default: 0 to consider /dev/video0 device." << std::endl
25 << std::endl
26 << " --scale <subsampling factor>" << std::endl
27 << " Subsampling factor applied to the images captured by the device." << std::endl
28 << " Default: 1" << std::endl
29 << std::endl
30 << " --seqname <sequence name>" << std::endl
31 << " Name of the sequence of image to create (ie: /tmp/image%04d.jpg)." << std::endl
32 << " Default: empty." << std::endl
33 << std::endl
34 << " --record <mode>" << std::endl
35 << " Allowed values for mode are:" << std::endl
36 << " 0: record all the captures images (continuous mode)," << std::endl
37 << " 1: record only images selected by a user click (single shot mode)." << std::endl
38 << " Default mode: 0" << std::endl
39 << std::endl
40 << " --no-display" << std::endl
41 << " Disable displaying captured images." << std::endl
42 << " When used and sequence name specified, record mode is internally set to 1 (continuous mode)."
43 << std::endl
44 << std::endl
45 << " --help, -h" << std::endl
46 << " Print this helper message." << std::endl
47 << std::endl;
48 std::cout << "USAGE" << std::endl
49 << " Example to visualize images:" << std::endl
50 << " " << argv[0] << std::endl
51 << std::endl
52 << " Example to visualize images from a second camera:" << std::endl
53 << " " << argv[0] << " --device 1" << std::endl
54 << std::endl
55 << " Examples to record a sequence:" << std::endl
56 << " " << argv[0] << " --seqname I%04d.png" << std::endl
57 << " " << argv[0] << " --seqname folder/I%04d.png --record 0" << std::endl
58 << std::endl
59 << " Examples to record single shot images:\n"
60 << " " << argv[0] << " --seqname I%04d.png --record 1\n"
61 << " " << argv[0] << " --seqname folder/I%04d.png --record 1" << std::endl
62 << std::endl;
63
64 if (error) {
65 std::cout << "Error" << std::endl
66 << " "
67 << "Unsupported parameter " << argv[error] << std::endl;
68 }
69}
70
77int main(int argc, const char *argv[])
78{
79#if defined(VISP_HAVE_V4L2) && defined(VISP_HAVE_THREADS)
80#ifdef ENABLE_VISP_NAMESPACE
81 using namespace VISP_NAMESPACE_NAME;
82#endif
83#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
84 std::shared_ptr<vpDisplay> display;
85#else
86 vpDisplay *display = nullptr;
87#endif
88 try {
89 int opt_device = 0;
90 unsigned int opt_scale = 1; // Default value is 2 in the constructor. Turn
91 // it to 1 to avoid subsampling
92 std::string opt_seqname;
93 int opt_record_mode = 0;
94 bool opt_display = true;
95
96 for (int i = 1; i < argc; i++) {
97 if (std::string(argv[i]) == "--device" && i + 1 < argc) {
98 opt_device = std::atoi(argv[++i]);
99 }
100 else if (std::string(argv[i]) == "--scale" && i + 1 < argc) {
101 opt_scale = static_cast<unsigned int>(atoi(argv[++i]));
102 }
103 else if (std::string(argv[i]) == "--seqname" && i + 1 < argc) {
104 opt_seqname = std::string(argv[++i]);
105 }
106 else if (std::string(argv[i]) == "--record" && i + 1 < argc) {
107 opt_record_mode = std::atoi(argv[++i]);
108 }
109 else if (std::string(argv[i]) == "--no-display") {
110 opt_display = false;
111 }
112 else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
113 usage(argv, 0);
114 return EXIT_SUCCESS;
115 }
116 else {
117 usage(argv, i);
118 return EXIT_FAILURE;
119 }
120 }
121
122 if ((!opt_display) && (!opt_seqname.empty())) {
123 opt_record_mode = 0;
124 }
125 std::cout << "Use device : " << opt_device << std::endl;
126 std::cout << "Recording : " << (opt_seqname.empty() ? "disabled" : "enabled") << std::endl;
127 std::cout << "Display : " << (opt_display ? "enabled" : "disabled") << std::endl;
128
129 std::string text_record_mode =
130 std::string("Record mode: ") + (opt_record_mode ? std::string("single") : std::string("continuous"));
131
132 if (!opt_seqname.empty()) {
133 std::cout << text_record_mode << std::endl;
134 std::cout << "Record name: " << opt_seqname << std::endl;
135 }
136
137#ifdef USE_COLOR
138 vpImage<vpRGBa> I; // To acquire color images
139#else
140 vpImage<unsigned char> I; // To acquire gray images
141#endif
142
144 std::ostringstream device;
145 device << "/dev/video" << opt_device;
146 g.setDevice(device.str());
147 g.setScale(opt_scale);
148 g.open(I);
149
150 std::cout << "Image size : " << I.getWidth() << " " << I.getHeight() << std::endl;
151
152 if (opt_display) {
153#if !(defined(VISP_HAVE_DISPLAY))
154 std::cout << "No image viewer is available..." << std::endl;
155 opt_display = false;
156#else
157#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
159#else
161#endif
162#endif
163 }
164
165#ifdef USE_COLOR
166 vpImageQueue<vpRGBa> image_queue(opt_seqname, opt_record_mode);
167 vpImageStorageWorker<vpRGBa> image_storage_worker(std::ref(image_queue));
168 std::thread image_storage_thread(&vpImageStorageWorker<vpRGBa>::run, &image_storage_worker);
169#else
170 vpImageQueue<unsigned char> image_queue(opt_seqname, opt_record_mode);
171 vpImageStorageWorker<unsigned char> image_storage_worker(std::ref(image_queue));
172 std::thread image_storage_thread(&vpImageStorageWorker<unsigned char>::run, &image_storage_worker);
173#endif
174
175 bool quit = false;
176 while (!quit) {
177 double t = vpTime::measureTimeMs();
178 g.acquire(I);
179
181
182 quit = image_queue.record(I);
183
184 std::stringstream ss;
185 ss << "Acquisition time: " << std::setprecision(3) << vpTime::measureTimeMs() - t << " ms";
186 vpDisplay::displayText(I, I.getHeight() - 20, 10, ss.str(), vpColor::red);
188 }
189 image_queue.cancel();
190 image_storage_thread.join();
191 }
192 catch (const vpException &e) {
193 std::cout << "Catch an exception: " << e << std::endl;
194 }
195
196#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
197 if (display != nullptr) {
198 delete display;
199 }
200#endif
201#else
202 (void)argc;
203 (void)argv;
204#ifndef VISP_HAVE_V4L2
205 std::cout << "Install Video 4 Linux 2 (v4l2), configure and build ViSP again to use this example" << std::endl;
206#endif
207#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
208 std::cout << "This tutorial should be built with c++11 support" << std::endl;
209#endif
210
211#endif
212}
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
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
void setDevice(const std::string &devname)
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()