Visual Servoing Platform  version 3.4.0 under development (2021-04-11)
tutorial-video-recorder.cpp
1 
2 #include <visp3/core/vpTime.h>
3 #include <visp3/gui/vpDisplayGDI.h>
4 #include <visp3/gui/vpDisplayGTK.h>
5 #include <visp3/gui/vpDisplayOpenCV.h>
6 #include <visp3/gui/vpDisplayX.h>
7 #include <visp3/io/vpVideoWriter.h>
8 #include <visp3/sensor/vpV4l2Grabber.h>
9 
27 int main(int argc, const char *argv[])
28 {
29 #if ((defined(VISP_HAVE_V4L2) || (VISP_HAVE_OPENCV_VERSION >= 0x020100)) && \
30  (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV) || defined(VISP_HAVE_GTK)))
31  std::string opt_videoname = "video-recorded.mpg";
32  int opt_device = 0;
33 
34  for (int i = 0; i < argc; i++) {
35  if (std::string(argv[i]) == "--device")
36  opt_device = atoi(argv[i + 1]);
37  else if (std::string(argv[i]) == "--name")
38  opt_videoname = std::string(argv[i + 1]);
39  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
40  std::cout << "\nUsage: " << argv[0]
41  << " [--device <device number>] [--name <video name>] [--help][-h]\n"
42  << std::endl;
43  return 0;
44  }
45  }
46 
47  std::cout << "Use device: " << opt_device << std::endl;
48  std::cout << "Record video in: " << opt_videoname << std::endl;
49 
50  try {
51  // vpImage<vpRGBa> I; // for color images
52  vpImage<unsigned char> I; // for gray images
53 
54 #if defined(VISP_HAVE_V4L2)
55  vpV4l2Grabber g;
56  std::ostringstream device;
57  device << "/dev/video" << opt_device;
58  g.setDevice(device.str());
59  g.setScale(1); // Acquire full resolution images
60  g.open(I);
61  g.acquire(I);
62 #elif defined(VISP_HAVE_OPENCV)
63  cv::VideoCapture g(opt_device);
64  if (!g.isOpened()) { // check if we succeeded
65  std::cout << "Failed to open the camera" << std::endl;
66  return -1;
67  }
68  cv::Mat frame;
69  g >> frame; // get a new frame from camera
70  vpImageConvert::convert(frame, I);
71 #endif
72 
73  std::cout << "Image size: " << I.getWidth() << " " << I.getHeight() << std::endl;
74 
75 #if defined(VISP_HAVE_X11)
76  vpDisplayX d;
77 #elif defined(VISP_HAVE_GDI)
78  vpDisplayGDI d;
79 #elif defined(VISP_HAVE_OPENCV)
81 #elif defined(VISP_HAVE_GTK)
82  vpDisplayGTK d;
83 #endif
84  d.init(I, 0, 0, "Camera view");
85  vpVideoWriter writer;
86 
87 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
88  writer.setCodec(cv::VideoWriter::fourcc('P', 'I', 'M', '1')); // MPEG-1 codec
89 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
90  writer.setCodec(CV_FOURCC('P', 'I', 'M', '1'));
91 #endif
92  writer.setFileName(opt_videoname);
93  writer.open(I);
94  bool recording = false;
95 
96  for (;;) {
97 #if defined(VISP_HAVE_V4L2)
98  g.acquire(I);
99 #elif defined(VISP_HAVE_OPENCV)
100  g >> frame;
101  vpImageConvert::convert(frame, I);
102 #endif
104  if (recording == false) {
105  vpDisplay::displayText(I, 10, 10, "A click to start recording", vpColor::green);
106  if (vpDisplay::getClick(I, false))
107  recording = true;
108  } else {
109  writer.saveFrame(I);
110  vpDisplay::displayText(I, 10, 10, "Recording: A click to stop and exit", vpColor::red);
111  if (vpDisplay::getClick(I, false))
112  break;
113  }
114 
115  vpDisplay::flush(I);
116  }
117  std::cout << "The video was recorded in \"" << opt_videoname << "\"" << std::endl;
118  } catch (const vpException &e) {
119  std::cout << "Catch an exception: " << e << std::endl;
120  }
121 #else
122  (void)argc;
123  (void)argv;
124  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
125 #endif
126 }
void acquire(vpImage< unsigned char > &I)
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
void open(vpImage< unsigned char > &I)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:150
void setDevice(const std::string &devname)
error that can be emited by ViSP classes.
Definition: vpException.h:71
static const vpColor green
Definition: vpColor.h:220
static void flush(const vpImage< unsigned char > &I)
static const vpColor red
Definition: vpColor.h:217
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
void init(vpImage< unsigned char > &I, int win_x=-1, int win_y=-1, const std::string &win_title="")
Class that enables to write easily a video file or a sequence of images.
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:134
void saveFrame(vpImage< vpRGBa > &I)
void setScale(unsigned scale=vpV4l2Grabber::DEFAULT_SCALE)
void open(vpImage< vpRGBa > &I)
void setFileName(const char *filename)
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
unsigned int getHeight() const
Definition: vpImage.h:188
void setCodec(const int fourcc_codec)
unsigned int getWidth() const
Definition: vpImage.h:246