Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
tutorial-grabber-opencv.cpp
1 
2 #include <stdlib.h>
3 #include <visp3/core/vpImageConvert.h>
4 #include <visp3/gui/vpDisplayOpenCV.h>
5 
6 #include "record_helper.h"
7 
8 // usage: binary -h
9 // device name: 0 is the default to dial with the first camera,
10 // 1 to dial with a second camera attached to the computer
11 int main(int argc, char **argv)
12 {
13  int opt_device = 0;
14  std::string opt_seqname;
15  int opt_record_mode = 0;
16 
17  for (int i = 0; i < argc; i++) {
18  if (std::string(argv[i]) == "--camera_device")
19  opt_device = std::atoi(argv[i + 1]);
20  else if (std::string(argv[i]) == "--seqname")
21  opt_seqname = std::string(argv[i + 1]);
22  else if (std::string(argv[i]) == "--record")
23  opt_record_mode = std::atoi(argv[i + 1]);
24  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
25  std::cout << "\nUsage: " << argv[0]
26  << " [--camera_device <camera device (default: 0>]"
27  << " [--seqname <sequence name (default: empty>] [--record <0: continuous | 1: single shot (default: 0)>]"
28  << " [--help] [-h]\n"
29  << "\nExample to visualize images:\n"
30  << " " << argv[0] << "\n"
31  << "\nExample to visualize images from a second camera:\n"
32  << " " << argv[0] << " --camera_device 1\n"
33  << "\nExamples to record a sequence:\n"
34  << " " << argv[0] << " --seqname I%04d.png \n"
35  << " " << argv[0] << " --seqname folder/I%04d.png --record 0\n"
36  << "\nExamples to record single shot images:\n"
37  << " " << argv[0] << " --seqname I%04d.png --record 1\n"
38  << " " << argv[0] << " --seqname folder/I%04d.png --record 1\n"
39  << std::endl;
40  return 0;
41  }
42  }
43 
44  std::cout << "Use device : " << opt_device << std::endl;
45  std::cout << "Recording : " << (opt_seqname.empty() ? "disabled" : "enabled") << std::endl;
46 
47  std::string text_record_mode = std::string("Record mode: ") + (opt_record_mode ? std::string("single") : std::string("continuous"));
48 
49  if (! opt_seqname.empty()) {
50  std::cout << text_record_mode << std::endl;
51  std::cout << "Record name: " << opt_seqname << std::endl;
52  }
53 
54 #if (VISP_HAVE_OPENCV_VERSION >= 0x020100)
55  try {
56  cv::VideoCapture cap(opt_device); // open the default camera
57  if (!cap.isOpened()) { // check if we succeeded
58  std::cout << "Failed to open the camera" << std::endl;
59  return -1;
60  }
61  cv::Mat frame;
62  int i = 0;
63  while ((i++ < 20) && !cap.read(frame)) {
64  }; // warm up camera by skiping unread frames
65 
66  std::cout << "Image size : " << frame.rows << " " << frame.cols << std::endl;
67 
68  // vpImage<vpRGBa> I; // for color images
69  vpImage<unsigned char> I; // for gray images
70  vpImageConvert::convert(frame, I);
71 
72  vpDisplayOpenCV d(I);
73 
74  bool quit = false;
75  while (! quit) {
76  double t = vpTime::measureTimeMs();
77  cap >> frame; // get a new frame from camera
78  // Convert the image in ViSP format and display it
79  vpImageConvert::convert(frame, I);
80 
82 
83  quit = record_helper(opt_seqname, opt_record_mode, I);
84 
85  std::stringstream ss;
86  ss << "Acquisition time: " << std::setprecision(3) << vpTime::measureTimeMs() - t << " ms";
87  vpDisplay::displayText(I, I.getHeight() - 20, 10, ss.str(), vpColor::red);
89  }
90  } catch (const vpException &e) {
91  std::cout << "Catch an exception: " << e << std::endl;
92  }
93 #else
94  (void) argc;
95  (void) argv;
96  std::cout << "Install OpenCV, configure and build ViSP again to use this example" << std::endl;
97 #endif
98 }
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
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:71
static void flush(const vpImage< unsigned char > &I)
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:88
static const vpColor red
Definition: vpColor.h:180
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
unsigned int getHeight() const
Definition: vpImage.h:178