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