Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
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") {
40  std::cout << "\nUsage: " << argv[0] << " [--device <device number>] [--name <video name>] [--help]\n"
41  << std::endl;
42  return 0;
43  }
44  }
45 
46  std::cout << "Use device: " << opt_device << std::endl;
47  std::cout << "Record video in: " << opt_videoname << std::endl;
48 
49  try {
50  // vpImage<vpRGBa> I; // for color images
51  vpImage<unsigned char> I; // for gray images
52 
53 #if defined(VISP_HAVE_V4L2)
54  vpV4l2Grabber g;
55  std::ostringstream device;
56  device << "/dev/video" << opt_device;
57  g.setDevice(device.str());
58  g.setScale(1); // Acquire full resolution images
59  g.open(I);
60  g.acquire(I);
61 #elif defined(VISP_HAVE_OPENCV)
62  cv::VideoCapture g(opt_device);
63  if (!g.isOpened()) { // check if we succeeded
64  std::cout << "Failed to open the camera" << std::endl;
65  return -1;
66  }
67  cv::Mat frame;
68  g >> frame; // get a new frame from camera
69  vpImageConvert::convert(frame, I);
70 #endif
71 
72  std::cout << "Image size: " << I.getWidth() << " " << I.getHeight() << std::endl;
73 
74 #if defined(VISP_HAVE_X11)
75  vpDisplayX d;
76 #elif defined(VISP_HAVE_GDI)
77  vpDisplayGDI d;
78 #elif defined(VISP_HAVE_OPENCV)
80 #elif defined(VISP_HAVE_GTK)
81  vpDisplayGTK d;
82 #endif
83  d.init(I, 0, 0, "Camera view");
84  vpVideoWriter writer;
85 
86 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
87  writer.setCodec(cv::VideoWriter::fourcc('P', 'I', 'M', '1')); // MPEG-1 codec
88 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
89  writer.setCodec(CV_FOURCC('P', 'I', 'M', '1'));
90 #endif
91  writer.setFileName(opt_videoname);
92  writer.open(I);
93  bool recording = false;
94 
95  for (;;) {
96 #if defined(VISP_HAVE_V4L2)
97  g.acquire(I);
98 #elif defined(VISP_HAVE_OPENCV)
99  g >> frame;
100  vpImageConvert::convert(frame, I);
101 #endif
103  if (recording == false) {
104  vpDisplay::displayText(I, 10, 10, "A click to start recording", vpColor::green);
105  if (vpDisplay::getClick(I, false))
106  recording = true;
107  } else {
108  writer.saveFrame(I);
109  vpDisplay::displayText(I, 10, 10, "Recording: A click to stop and exit", vpColor::red);
110  if (vpDisplay::getClick(I, false))
111  break;
112  }
113 
114  vpDisplay::flush(I);
115  }
116  std::cout << "The video was recorded in \"" << opt_videoname << "\"" << std::endl;
117  } catch (const vpException &e) {
118  std::cout << "Catch an exception: " << e << std::endl;
119  }
120 #else
121  (void)argc;
122  (void)argv;
123  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
124 #endif
125 }
void acquire(vpImage< unsigned char > &I)
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
void open(vpImage< unsigned char > &I)
unsigned int getWidth() const
Definition: vpImage.h:239
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:129
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:151
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:183
static void flush(const vpImage< unsigned char > &I)
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...
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:138
void saveFrame(vpImage< vpRGBa > &I)
void setScale(unsigned scale=vpV4l2Grabber::DEFAULT_SCALE)
void open(vpImage< vpRGBa > &I)
void setFileName(const char *filename)
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
void setCodec(const int fourcc_codec)
unsigned int getHeight() const
Definition: vpImage.h:178