Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
tutorial-grabber-opencv.cpp
1
2#include <stdlib.h>
3#include <visp3/core/vpImageConvert.h>
4#include <visp3/gui/vpDisplayOpenCV.h>
5#include <visp3/io/vpImageStorageWorker.h>
6
7#if defined(HAVE_OPENCV_VIDEOIO)
8#include <opencv2/videoio.hpp>
9#endif
10
11#define USE_COLOR // Comment to acquire gray level images
12
13void usage(const char *argv [], int error)
14{
15 std::cout << "SYNOPSIS" << std::endl
16 << " " << argv[0] << " [--device <index>]"
17 << " [--seqname <sequence name>]"
18 << " [--record <mode>]"
19 << " [--no-display]"
20 << " [--help] [-h]" << std::endl
21 << std::endl;
22 std::cout << "DESCRIPTION" << std::endl
23 << " --device <index> " << std::endl
24 << " Camera device index. Set 0 to dial with the first camera," << std::endl
25 << " and 1 to dial with the second camera attached to the computer." << std::endl
26 << " Default: 0 to consider /dev/video0 device." << std::endl
27 << std::endl
28 << " --seqname <sequence name>" << std::endl
29 << " Name of the sequence of image to create (ie: /tmp/image%04d.jpg)." << std::endl
30 << " Default: empty." << std::endl
31 << std::endl
32 << " --record <mode>" << std::endl
33 << " Allowed values for mode are:" << std::endl
34 << " 0: record all the captures images (continuous mode)," << std::endl
35 << " 1: record only images selected by a user click (single shot mode)." << std::endl
36 << " Default mode: 0" << std::endl
37 << std::endl
38 << " --no-display" << std::endl
39 << " Disable displaying captured images." << std::endl
40 << " When used and sequence name specified, record mode is internally set to 1 (continuous mode)."
41 << std::endl
42 << std::endl
43 << " --help, -h" << std::endl
44 << " Print this helper message." << std::endl
45 << std::endl;
46 std::cout << "USAGE" << std::endl
47 << " Example to visualize images:" << std::endl
48 << " " << argv[0] << std::endl
49 << std::endl
50 << " Example to visualize images from a second camera:" << std::endl
51 << " " << argv[0] << " --device 1" << std::endl
52 << std::endl
53 << " Examples to record a sequence:" << std::endl
54 << " " << argv[0] << " --seqname I%04d.png" << std::endl
55 << " " << argv[0] << " --seqname folder/I%04d.png --record 0" << std::endl
56 << std::endl
57 << " Examples to record single shot images:\n"
58 << " " << argv[0] << " --seqname I%04d.png --record 1\n"
59 << " " << argv[0] << " --seqname folder/I%04d.png --record 1" << std::endl
60 << std::endl;
61
62 if (error) {
63 std::cout << "Error" << std::endl
64 << " "
65 << "Unsupported parameter " << argv[error] << std::endl;
66 }
67}
68
69// usage: binary -h
70// device name: 0 is the default to dial with the first camera,
71// 1 to dial with a second camera attached to the computer
72int main(int argc, const char *argv [])
73{
74 int opt_device = 0;
75 std::string opt_seqname;
76 int opt_record_mode = 0;
77 bool opt_display = true;
78
79 for (int i = 1; i < argc; i++) {
80 if (std::string(argv[i]) == "--device") {
81 opt_device = std::atoi(argv[i + 1]);
82 i++;
83 }
84 else if (std::string(argv[i]) == "--seqname") {
85 opt_seqname = std::string(argv[i + 1]);
86 i++;
87 }
88 else if (std::string(argv[i]) == "--record") {
89 opt_record_mode = std::atoi(argv[i + 1]);
90 i++;
91 }
92 else if (std::string(argv[i]) == "--no-display") {
93 opt_display = false;
94 }
95 else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
96 usage(argv, 0);
97 return EXIT_SUCCESS;
98 }
99 else {
100 usage(argv, i);
101 return EXIT_FAILURE;
102 }
103 }
104
105 if ((!opt_display) && (!opt_seqname.empty())) {
106 opt_record_mode = 0;
107 }
108
109 std::cout << "Use device : " << opt_device << std::endl;
110 std::cout << "Recording : " << (opt_seqname.empty() ? "disabled" : "enabled") << std::endl;
111 std::cout << "Display : " << (opt_display ? "enabled" : "disabled") << std::endl;
112
113 std::string text_record_mode =
114 std::string("Record mode: ") + (opt_record_mode ? std::string("single") : std::string("continuous"));
115
116 if (!opt_seqname.empty()) {
117 std::cout << text_record_mode << std::endl;
118 std::cout << "Record name: " << opt_seqname << std::endl;
119 }
120
121#if defined(HAVE_OPENCV_VIDEOIO) && defined(HAVE_OPENCV_HIGHGUI) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
122 try {
123 cv::VideoCapture cap(opt_device); // open the default camera
124 if (!cap.isOpened()) { // check if we succeeded
125 std::cout << "Failed to open the camera" << std::endl;
126 return EXIT_FAILURE;
127 }
128 cv::Mat frame;
129 int i = 0;
130 while ((i++ < 20) && !cap.read(frame)) {
131 }; // warm up camera by skiping unread frames
132
133 std::cout << "Image size : " << frame.rows << " " << frame.cols << std::endl;
134
135#ifdef USE_COLOR
136 vpImage<vpRGBa> I; // To acquire color images
137#else
138 vpImage<unsigned char> I; // To acquire gray images
139#endif
140 vpImageConvert::convert(frame, I);
141
142 vpDisplayOpenCV *d = NULL;
143 if (opt_display) {
144 d = new vpDisplayOpenCV(I);
145 }
146
147#ifdef USE_COLOR
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#else
152 vpImageQueue<unsigned char> image_queue(opt_seqname, opt_record_mode);
153 vpImageStorageWorker<unsigned char> image_storage_worker(std::ref(image_queue));
154 std::thread image_storage_thread(&vpImageStorageWorker<unsigned char>::run, &image_storage_worker);
155#endif
156
157 bool quit = false;
158 while (!quit) {
159 double t = vpTime::measureTimeMs();
160 cap >> frame; // get a new frame from camera
161 // Convert the image in ViSP format and display it
162 vpImageConvert::convert(frame, I);
163
165
166 quit = image_queue.record(I);
167
168 std::stringstream ss;
169 ss << "Acquisition time: " << std::setprecision(3) << vpTime::measureTimeMs() - t << " ms";
170 vpDisplay::displayText(I, I.getHeight() - 20, 10, ss.str(), vpColor::red);
172 }
173 image_queue.cancel();
174 image_storage_thread.join();
175
176 if (d) {
177 delete d;
178 }
179 }
180 catch (const vpException &e) {
181 std::cout << "Catch an exception: " << e << std::endl;
182 }
183#else
184 (void)argc;
185 (void)argv;
186#if !defined(HAVE_OPENCV_VIDEOIO)
187 std::cout << "Install OpenCV videoio module, configure and build ViSP again to use this example" << std::endl;
188#endif
189#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
190 std::cout << "This tutorial should be built with c++11 support" << std::endl;
191#endif
192#endif
193}
static const vpColor red
Definition vpColor.h:211
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
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
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Definition of the vpImage class member functions.
Definition vpImage.h:135
unsigned int getHeight() const
Definition vpImage.h:184
VISP_EXPORT double measureTimeMs()