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