Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
tutorial-video-recorder.cpp
1 
2 #include <visp3/gui/vpDisplayGDI.h>
3 #include <visp3/gui/vpDisplayGTK.h>
4 #include <visp3/gui/vpDisplayOpenCV.h>
5 #include <visp3/gui/vpDisplayX.h>
6 #include <visp3/core/vpTime.h>
7 #include <visp3/io/vpVideoWriter.h>
8 #include <visp3/sensor/vpV4l2Grabber.h>
9 
16 int main(int argc, const char *argv[])
17 {
18 #if ((defined(VISP_HAVE_V4L2) || (VISP_HAVE_OPENCV_VERSION >= 0x020100)) && (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV) || defined(VISP_HAVE_GTK)))
19  std::string opt_videoname = "video-recorded.mpg";
20  int opt_device = 0;
21 
22  for (int i=0; i<argc; i++) {
23  if (std::string(argv[i]) == "--device")
24  opt_device = atoi(argv[i+1]);
25  else if (std::string(argv[i]) == "--name")
26  opt_videoname = std::string(argv[i+1]);
27  else if (std::string(argv[i]) == "--help") {
28  std::cout << "\nUsage: " << argv[0] << " [--device <device number>] [--name <video name>] [--help]\n" << std::endl;
29  return 0;
30  }
31  }
32 
33  std::cout << "Use device: " << opt_device << std::endl;
34  std::cout << "Record video in: " << opt_videoname << std::endl;
35 
36  try {
37  //vpImage<vpRGBa> I; // for color images
38  vpImage<unsigned char> I; // for gray images
39 
40 #if defined(VISP_HAVE_V4L2)
41  vpV4l2Grabber g;
42  std::ostringstream device;
43  device << "/dev/video" << opt_device;
44  g.setDevice(device.str());
45  g.open(I);
46  g.acquire(I);
47 #elif defined(VISP_HAVE_OPENCV)
48  cv::VideoCapture g(opt_device);
49  if(!g.isOpened()) { // check if we succeeded
50  std::cout << "Failed to open the camera" << std::endl;
51  return -1;
52  }
53  cv::Mat frame;
54  g >> frame; // get a new frame from camera
55  vpImageConvert::convert(frame, I);
56 #endif
57 
58  std::cout << "Image size: " << I.getWidth() << " " << I.getHeight() << std::endl;
59 
60 #if defined(VISP_HAVE_X11)
61  vpDisplayX d;
62 #elif defined(VISP_HAVE_GDI)
63  vpDisplayGDI d;
64 #elif defined(VISP_HAVE_OPENCV)
66 #elif defined(VISP_HAVE_GTK)
67  vpDisplayGTK d;
68 #endif
69  d.init(I, 0, 0, "Camera view");
70  vpVideoWriter writer;
71 
72 #ifdef VISP_HAVE_FFMPEG
73  // Set up the bit rate
74  writer.setBitRate(1000000);
75  // Set up the codec to use
76 # if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54,51,110) // libavcodec 54.51.100
77  writer.setCodec(CODEC_ID_MPEG2VIDEO);
78 # else
79  writer.setCodec(AV_CODEC_ID_MPEG2VIDEO);
80 # endif
81 #elif VISP_HAVE_OPENCV_VERSION >= 0x030000
82  writer.setCodec( cv::VideoWriter::fourcc('P','I','M','1') ); // MPEG-1 codec
83 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
84  writer.setCodec( CV_FOURCC('P','I','M','1') );
85 #endif
86  writer.setFileName(opt_videoname);
87  writer.open(I);
88  bool recording = false;
89 
90  for(;;) {
91 #if defined(VISP_HAVE_V4L2)
92  g.acquire(I);
93 #elif defined(VISP_HAVE_OPENCV)
94  g >> frame;
95  vpImageConvert::convert(frame, I);
96 #endif
98  if (recording == false) {
99  vpDisplay::displayText(I, 10, 10, "A click to start recording", vpColor::green);
100  if (vpDisplay::getClick(I, false))
101  recording = true;
102  }
103  else {
104  writer.saveFrame(I);
105  vpDisplay::displayText(I, 10, 10, "Recording: A click to stop and exit", vpColor::red);
106  if (vpDisplay::getClick(I, false))
107  break;
108  }
109 
110  vpDisplay::flush(I);
111  }
112  std::cout << "The video was recorded in \"" << opt_videoname << "\"" << std::endl;
113  }
114  catch(vpException &e) {
115  std::cout << "Catch an exception: " << e << std::endl;
116  }
117 #else
118  (void)argc;
119  (void)argv;
120  std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
121 #endif
122 }
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:226
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
void setBitRate(const unsigned int bitrate)
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:153
void setDevice(const std::string &devname)
error that can be emited by ViSP classes.
Definition: vpException.h:73
static const vpColor green
Definition: vpColor.h:166
static void flush(const vpImage< unsigned char > &I)
static const vpColor red
Definition: vpColor.h:163
void setCodec(const AVCodecID codec_id)
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 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.
unsigned int getHeight() const
Definition: vpImage.h:175