Visual Servoing Platform  version 3.3.0
vpVideoWriter Class Reference

#include <vpVideoWriter.h>

Public Member Functions

 vpVideoWriter ()
 
virtual ~vpVideoWriter ()
 
void close ()
 
unsigned int getCurrentFrameIndex () const
 
void open (vpImage< vpRGBa > &I)
 
void open (vpImage< unsigned char > &I)
 
void resetFrameCounter ()
 
void saveFrame (vpImage< vpRGBa > &I)
 
void saveFrame (vpImage< unsigned char > &I)
 
void setCodec (const int fourcc_codec)
 
void setFileName (const char *filename)
 
void setFileName (const std::string &filename)
 
void setFirstFrameIndex (unsigned int first_frame)
 
void setFramerate (const double frame_rate)
 

Detailed Description

Class that enables to write easily a video file or a sequence of images.

This class has its own implementation to write a sequence of PGM and PPM images.

This class may benefit from optional 3rd parties:

  • libpng: If installed this optional 3rd party is used to write a sequence of PNG images. Installation instructions are provided here https://visp.inria.fr/3rd_png.
  • libjpeg: If installed this optional 3rd party is used to write a sequence of JPEG images. Installation instructions are provided here https://visp.inria.fr/3rd_jpeg.
  • OpenCV: If installed this optional 3rd party is used to write a sequence of images where images could be in TIFF, BMP, DIB, PBM, RASTER, JPEG2000 format. If libpng or libjpeg is not installed, OpenCV is also used to consider these image formats. OpenCV allows also to consider AVI, MPEG, MPEG4, MOV, OGV, WMV, FLV, MKV video formats. Installation instructions are provided here https://visp.inria.fr/3rd_opencv.

The following example available in tutorial-video-recorder.cpp shows how this class can be used to record a video from a camera by default in an mpeg file.

#include <visp3/core/vpTime.h>
#include <visp3/gui/vpDisplayGDI.h>
#include <visp3/gui/vpDisplayGTK.h>
#include <visp3/gui/vpDisplayOpenCV.h>
#include <visp3/gui/vpDisplayX.h>
#include <visp3/io/vpVideoWriter.h>
#include <visp3/sensor/vpV4l2Grabber.h>
int main(int argc, const char *argv[])
{
#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)))
std::string opt_videoname = "video-recorded.mpg";
int opt_device = 0;
for (int i = 0; i < argc; i++) {
if (std::string(argv[i]) == "--device")
opt_device = atoi(argv[i + 1]);
else if (std::string(argv[i]) == "--name")
opt_videoname = std::string(argv[i + 1]);
else if (std::string(argv[i]) == "--help") {
std::cout << "\nUsage: " << argv[0] << " [--device <device number>] [--name <video name>] [--help]\n"
<< std::endl;
return 0;
}
}
std::cout << "Use device: " << opt_device << std::endl;
std::cout << "Record video in: " << opt_videoname << std::endl;
try {
// vpImage<vpRGBa> I; // for color images
vpImage<unsigned char> I; // for gray images
#if defined(VISP_HAVE_V4L2)
std::ostringstream device;
device << "/dev/video" << opt_device;
g.setDevice(device.str());
g.setScale(1); // Acquire full resolution images
g.open(I);
g.acquire(I);
#elif defined(VISP_HAVE_OPENCV)
cv::VideoCapture g(opt_device);
if (!g.isOpened()) { // check if we succeeded
std::cout << "Failed to open the camera" << std::endl;
return -1;
}
cv::Mat frame;
g >> frame; // get a new frame from camera
#endif
std::cout << "Image size: " << I.getWidth() << " " << I.getHeight() << std::endl;
#if defined(VISP_HAVE_X11)
#elif defined(VISP_HAVE_GDI)
#elif defined(VISP_HAVE_OPENCV)
#elif defined(VISP_HAVE_GTK)
#endif
d.init(I, 0, 0, "Camera view");
vpVideoWriter writer;
#if VISP_HAVE_OPENCV_VERSION >= 0x030000
writer.setCodec(cv::VideoWriter::fourcc('P', 'I', 'M', '1')); // MPEG-1 codec
#elif VISP_HAVE_OPENCV_VERSION >= 0x020100
writer.setCodec(CV_FOURCC('P', 'I', 'M', '1'));
#endif
writer.setFileName(opt_videoname);
writer.open(I);
bool recording = false;
for (;;) {
#if defined(VISP_HAVE_V4L2)
g.acquire(I);
#elif defined(VISP_HAVE_OPENCV)
g >> frame;
#endif
if (recording == false) {
vpDisplay::displayText(I, 10, 10, "A click to start recording", vpColor::green);
if (vpDisplay::getClick(I, false))
recording = true;
} else {
writer.saveFrame(I);
vpDisplay::displayText(I, 10, 10, "Recording: A click to stop and exit", vpColor::red);
if (vpDisplay::getClick(I, false))
break;
}
}
std::cout << "The video was recorded in \"" << opt_videoname << "\"" << std::endl;
} catch (const vpException &e) {
std::cout << "Catch an exception: " << e << std::endl;
}
#else
(void)argc;
(void)argv;
std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
#endif
}

The following example shows also how this class can be used to write an image sequence. The images are stored in the folder "./image" and are named "image0000.jpeg", "image0001.jpeg", "image0002.jpeg", ...

#include <visp3/core/vpConfig.h>
#include <visp3/io/vpVideoWriter.h>
int main()
{
//Initialize the writer.
writer.setFileName("./image/image%04d.jpeg");
writer.open(I);
for ( ; ; )
{
//Here the code to capture or create an image and stores it in I.
//Save the image
writer.saveFrame(I);
}
writer.close();
return 0;
}

The other following example explains how to use the class to write directly an mpeg file.

#include <visp3/io/vpVideoWriter.h>
int main()
{
vpVideoWriter writer;
// Set up the framerate to 30Hz. Default is 25Hz.
writer.setFramerate(30);
#if VISP_HAVE_OPENCV_VERSION >= 0x030000
writer.setCodec( cv::VideoWriter::fourcc('P','I','M','1') );
#elif VISP_HAVE_OPENCV_VERSION >= 0x020100
writer.setCodec( CV_FOURCC('P','I','M','1') );
#endif
writer.setFileName("./test.mpeg");
writer.open(I);
for ( ; ; )
{
// Here the code to capture or create an image and store it in I.
// Save the image
writer.saveFrame(I);
}
writer.close();
return 0;
}
Examples
grabV4l2MultiCpp11Thread.cpp, tutorial-grabber-1394-writer.cpp, and tutorial-video-recorder.cpp.

Definition at line 159 of file vpVideoWriter.h.

Constructor & Destructor Documentation

◆ vpVideoWriter()

vpVideoWriter::vpVideoWriter ( )

Basic constructor.

Definition at line 55 of file vpVideoWriter.cpp.

◆ ~vpVideoWriter()

vpVideoWriter::~vpVideoWriter ( )
virtual

Basic destructor.

Definition at line 79 of file vpVideoWriter.cpp.

Member Function Documentation

◆ close()

void vpVideoWriter::close ( )

Deallocates parameters use to write the video or the image sequence.

Examples
tutorial-grabber-1394-writer.cpp.

Definition at line 272 of file vpVideoWriter.cpp.

References vpException::notInitialized, and vpERROR_TRACE.

◆ getCurrentFrameIndex()

unsigned int vpVideoWriter::getCurrentFrameIndex ( ) const
inline

Gets the current frame index.

Returns
Returns the current frame index.

Definition at line 213 of file vpVideoWriter.h.

◆ open() [1/2]

void vpVideoWriter::open ( vpImage< unsigned char > &  I)

Sets all the parameters needed to write the video or the image sequence.

Parameters
I: One image with the right dimensions.

Definition at line 167 of file vpVideoWriter.cpp.

References vpException::fatalError, vpImage::getHeight(), vpImage::getWidth(), vpException::ioError, vpImageException::noFileNameError, and vpERROR_TRACE.

◆ open() [2/2]

void vpVideoWriter::open ( vpImage< vpRGBa > &  I)

Sets all the parameters needed to write the video or the image sequence.

Parameters
I: One image with the right dimensions.
Examples
grabV4l2MultiCpp11Thread.cpp, tutorial-grabber-1394-writer.cpp, and tutorial-video-recorder.cpp.

Definition at line 132 of file vpVideoWriter.cpp.

References vpException::fatalError, vpImage::getHeight(), vpImage::getWidth(), vpImageException::noFileNameError, and vpERROR_TRACE.

◆ resetFrameCounter()

void vpVideoWriter::resetFrameCounter ( )
inline

Reset the frame counter and sets it to the first image index.

By default the first frame index is set to 0.

Definition at line 222 of file vpVideoWriter.h.

◆ saveFrame() [1/2]

void vpVideoWriter::saveFrame ( vpImage< unsigned char > &  I)

Saves the image as a frame of the video or as an image belonging to the image sequence.

Each time this method is used, the frame counter is incremented and thus the file name change for the case of an image sequence.

Parameters
I: The image which has to be saved

Definition at line 239 of file vpVideoWriter.cpp.

References vpImageConvert::convert(), vpException::notInitialized, vpERROR_TRACE, and vpImageIo::write().

◆ saveFrame() [2/2]

void vpVideoWriter::saveFrame ( vpImage< vpRGBa > &  I)

Saves the image as a frame of the video or as an image belonging to the image sequence.

Each time this method is used, the frame counter is incremented and thus the file name change for the case of an image sequence.

Parameters
I: The image which has to be saved
Examples
grabV4l2MultiCpp11Thread.cpp, tutorial-grabber-1394-writer.cpp, and tutorial-video-recorder.cpp.

Definition at line 206 of file vpVideoWriter.cpp.

References vpImageConvert::convert(), vpException::notInitialized, vpERROR_TRACE, and vpImageIo::write().

◆ setCodec()

void vpVideoWriter::setCodec ( const int  fourcc_codec)
inline
Examples
tutorial-video-recorder.cpp.

Definition at line 228 of file vpVideoWriter.h.

◆ setFileName() [1/2]

void vpVideoWriter::setFileName ( const char *  filename)

It enables to set the path and the name of the files which will be saved.

If you want to write a sequence of images, $ filename $ corresponds to the path followed by the image name template. For exemple, if you want to write different images named image0001.jpeg, image0002.jpg, ... and located in the folder /local/image, $ filename $ will be "/local/image/image%04d.jpg".

Parameters
filename: filename template of an image sequence.
Examples
grabV4l2MultiCpp11Thread.cpp, tutorial-grabber-1394-writer.cpp, and tutorial-video-recorder.cpp.

Definition at line 92 of file vpVideoWriter.cpp.

References vpException::badValue, vpException::memoryAllocationError, vpImageException::noFileNameError, and vpERROR_TRACE.

Referenced by setFileName().

◆ setFileName() [2/2]

void vpVideoWriter::setFileName ( const std::string &  filename)

It enables to set the path and the name of the files which will be saved.

If you want to write a sequence of images, $ filename $ corresponds to the path followed by the image name template. For exemple, if you want to write different images named image0001.jpeg, image0002.jpg, ... and located in the folder /local/image, $ filename $ will be "/local/image/image%04d.jpg".

Parameters
filename: filename template of an image sequence.

Definition at line 125 of file vpVideoWriter.cpp.

References setFileName().

◆ setFirstFrameIndex()

void vpVideoWriter::setFirstFrameIndex ( unsigned int  first_frame)
inline

Enables to set the first frame index.

Parameters
first_frame: The first frame index.

Definition at line 238 of file vpVideoWriter.h.

◆ setFramerate()

void vpVideoWriter::setFramerate ( const double  frame_rate)
inline

Sets the framerate in Hz of the video when encoding.

Parameters
frame_rate: the expected framerate.

By default the framerate is set to 25Hz.

Definition at line 247 of file vpVideoWriter.h.

vpDisplayX
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:151
vpV4l2Grabber::open
void open(vpImage< unsigned char > &I)
Definition: vpV4l2Grabber.cpp:410
vpVideoWriter::saveFrame
void saveFrame(vpImage< vpRGBa > &I)
Definition: vpVideoWriter.cpp:206
vpV4l2Grabber::setDevice
void setDevice(const std::string &devname)
Definition: vpV4l2Grabber.h:285
vpImageConvert::convert
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Definition: vpImageConvert.cpp:79
vpVideoWriter::setFramerate
void setFramerate(const double frame_rate)
Definition: vpVideoWriter.h:247
vpDisplayGDI
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
vpImage::getHeight
unsigned int getHeight() const
Definition: vpImage.h:222
vpVideoWriter::open
void open(vpImage< vpRGBa > &I)
Definition: vpVideoWriter.cpp:132
vpV4l2Grabber::acquire
void acquire(vpImage< unsigned char > &I)
Definition: vpV4l2Grabber.cpp:526
vpDisplayOpenCV
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Definition: vpDisplayOpenCV.h:142
vpImage::getWidth
unsigned int getWidth() const
Definition: vpImage.h:280
vpDisplay::display
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay_uchar.cpp:740
vpDisplayGTK
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:138
vpDisplay::displayText
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Definition: vpDisplay_uchar.cpp:664
vpVideoWriter::setFileName
void setFileName(const char *filename)
Definition: vpVideoWriter.cpp:92
vpDisplayX::init
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
Definition: vpDisplayX.cpp:252
vpV4l2Grabber
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
Definition: vpV4l2Grabber.h:135
vpColor::green
static const vpColor green
Definition: vpColor.h:182
vpDisplay::flush
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay_uchar.cpp:716
vpImage< unsigned char >
vpColor::red
static const vpColor red
Definition: vpColor.h:179
vpVideoWriter::close
void close()
Definition: vpVideoWriter.cpp:272
vpDisplay::getClick
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
Definition: vpDisplay_uchar.cpp:765
vpV4l2Grabber::setScale
void setScale(unsigned scale=vpV4l2Grabber::DEFAULT_SCALE)
Definition: vpV4l2Grabber.cpp:387
vpVideoWriter
Class that enables to write easily a video file or a sequence of images.
Definition: vpVideoWriter.h:160
vpVideoWriter::setCodec
void setCodec(const int fourcc_codec)
Definition: vpVideoWriter.h:228
vpException
error that can be emited by ViSP classes.
Definition: vpException.h:72