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