Visual Servoing Platform version 3.5.0
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//#define USE_COLOR // Comment to acquire gray level images
8
9// usage: binary -h
10// device name: 0 is the default to dial with the first camera,
11// 1 to dial with a second camera attached to the computer
12int main(int argc, char **argv)
13{
14 int opt_device = 0;
15 std::string opt_seqname;
16 int opt_record_mode = 0;
17
18 for (int i = 0; i < argc; i++) {
19 if (std::string(argv[i]) == "--camera_device")
20 opt_device = std::atoi(argv[i + 1]);
21 else if (std::string(argv[i]) == "--seqname")
22 opt_seqname = std::string(argv[i + 1]);
23 else if (std::string(argv[i]) == "--record")
24 opt_record_mode = std::atoi(argv[i + 1]);
25 else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
26 std::cout << "\nUsage: " << argv[0]
27 << " [--camera_device <camera device (default: 0>]"
28 << " [--seqname <sequence name (default: empty>] [--record <0: continuous | 1: single shot (default: 0)>]"
29 << " [--help] [-h]\n"
30 << "\nExample to visualize images:\n"
31 << " " << argv[0] << "\n"
32 << "\nExample to visualize images from a second camera:\n"
33 << " " << argv[0] << " --camera_device 1\n"
34 << "\nExamples to record a sequence:\n"
35 << " " << argv[0] << " --seqname I%04d.png \n"
36 << " " << argv[0] << " --seqname folder/I%04d.png --record 0\n"
37 << "\nExamples to record single shot images:\n"
38 << " " << argv[0] << " --seqname I%04d.png --record 1\n"
39 << " " << argv[0] << " --seqname folder/I%04d.png --record 1\n"
40 << std::endl;
41 return 0;
42 }
43 }
44
45 std::cout << "Use device : " << opt_device << std::endl;
46 std::cout << "Recording : " << (opt_seqname.empty() ? "disabled" : "enabled") << std::endl;
47
48 std::string text_record_mode = std::string("Record mode: ") + (opt_record_mode ? std::string("single") : std::string("continuous"));
49
50 if (! opt_seqname.empty()) {
51 std::cout << text_record_mode << std::endl;
52 std::cout << "Record name: " << opt_seqname << std::endl;
53 }
54
55#if (VISP_HAVE_OPENCV_VERSION >= 0x020100) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
56 try {
57 cv::VideoCapture cap(opt_device); // open the default camera
58 if (!cap.isOpened()) { // check if we succeeded
59 std::cout << "Failed to open the camera" << std::endl;
60 return -1;
61 }
62 cv::Mat frame;
63 int i = 0;
64 while ((i++ < 20) && !cap.read(frame)) {
65 }; // warm up camera by skiping unread frames
66
67 std::cout << "Image size : " << frame.rows << " " << frame.cols << std::endl;
68
69#ifdef USE_COLOR
70 vpImage<vpRGBa> I; // To acquire color images
71#else
72 vpImage<unsigned char> I; // To acquire gray images
73#endif
75
76 vpDisplayOpenCV d(I);
77
78#ifdef USE_COLOR
79 vpImageQueue<vpRGBa> image_queue(opt_seqname, opt_record_mode);
80 vpImageStorageWorker<vpRGBa> image_storage_worker(std::ref(image_queue));
81 std::thread image_storage_thread(&vpImageStorageWorker<vpRGBa>::run, &image_storage_worker);
82#else
83 vpImageQueue<unsigned char> image_queue(opt_seqname, opt_record_mode);
84 vpImageStorageWorker<unsigned char> image_storage_worker(std::ref(image_queue));
85 std::thread image_storage_thread(&vpImageStorageWorker<unsigned char>::run, &image_storage_worker);
86#endif
87
88 bool quit = false;
89 while (! quit) {
90 double t = vpTime::measureTimeMs();
91 cap >> frame; // get a new frame from camera
92 // Convert the image in ViSP format and display it
94
96
97 quit = image_queue.record(I);
98
99 std::stringstream ss;
100 ss << "Acquisition time: " << std::setprecision(3) << vpTime::measureTimeMs() - t << " ms";
101 vpDisplay::displayText(I, I.getHeight() - 20, 10, ss.str(), vpColor::red);
103 }
104 image_queue.cancel();
105 image_storage_thread.join();
106 } catch (const vpException &e) {
107 std::cout << "Catch an exception: " << e << std::endl;
108 }
109#else
110 (void) argc;
111 (void) argv;
112#if (VISP_HAVE_OPENCV_VERSION < 0x020100)
113 std::cout << "Install OpenCV, configure and build ViSP again to use this example" << std::endl;
114#endif
115#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
116 std::cout << "This turorial should be built with c++11 support" << std::endl;
117#endif
118#endif
119}
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 emited by ViSP classes.
Definition: vpException.h:72
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
unsigned int getHeight() const
Definition: vpImage.h:188
VISP_EXPORT double measureTimeMs()