Visual Servoing Platform  version 3.0.0
vpVideoReader Class Reference

#include <visp3/io/vpVideoReader.h>

+ Inheritance diagram for vpVideoReader:

Public Member Functions

 vpVideoReader ()
 
 ~vpVideoReader ()
 
void acquire (vpImage< vpRGBa > &I)
 
void acquire (vpImage< unsigned char > &I)
 
void close ()
 
bool end ()
 
bool getFrame (vpImage< vpRGBa > &I, long frame)
 
bool getFrame (vpImage< unsigned char > &I, long frame)
 
double getFramerate ()
 
long getFrameIndex () const
 
long getFirstFrameIndex () const
 
long getLastFrameIndex () const
 
void open (vpImage< vpRGBa > &I)
 
void open (vpImage< unsigned char > &I)
 
void resetFrameCounter ()
 
void setFileName (const char *filename)
 
void setFileName (const std::string &filename)
 
void setFirstFrameIndex (const long first_frame)
 
void setLastFrameIndex (const long last_frame)
 
unsigned int getHeight () const
 
unsigned int getWidth () const
 

Public Attributes

bool init
 

Protected Attributes

unsigned int height
 
unsigned int width
 

Detailed Description

Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the vpFrameGrabber Class, it can be used like an other frame grabber class.

The following example available in tutorial-video-reader.cpp shows how this class is really easy to use. It enables to read a video file named video.mpeg.

#include <visp3/gui/vpDisplayGDI.h>
#include <visp3/gui/vpDisplayOpenCV.h>
#include <visp3/gui/vpDisplayX.h>
#include <visp3/core/vpTime.h>
#include <visp3/io/vpVideoReader.h>
int main(int argc, char** argv)
{
#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100) || defined(VISP_HAVE_FFMPEG)
try {
std::string videoname = "video.mpg";
for (int i=0; i<argc; i++) {
if (std::string(argv[i]) == "--name")
videoname = std::string(argv[i+1]);
else if (std::string(argv[i]) == "--help") {
std::cout << "\nUsage: " << argv[0] << " [--name <video name>] [--help]\n" << std::endl;
return 0;
}
}
g.setFileName(videoname);
g.open(I);
std::cout << "video name: " << videoname << std::endl;
std::cout << "video framerate: " << g.getFramerate() << "Hz" << std::endl;
std::cout << "video dimension: " << I.getWidth() << " " << I.getHeight() << std::endl;
#ifdef VISP_HAVE_X11
vpDisplayX d(I);
#elif defined(VISP_HAVE_GDI)
#elif defined(VISP_HAVE_OPENCV)
#else
std::cout << "No image viewer is available..." << std::endl;
#endif
vpDisplay::setTitle(I, "Video reader");
while (! g.end() ) {
double t = vpTime::measureTimeMs();
g.acquire(I);
if (vpDisplay::getClick(I, false)) break;
vpTime::wait(t, 1000. / g.getFramerate());
}
}
catch(vpException e) {
std::cout << e.getMessage() << std::endl;
}
#else
(void)argc;
(void)argv;
std::cout << "Install OpenCV or ffmpeg and rebuild ViSP to use this example." << std::endl;
#endif
}

As shown in the next example, this class allows also to access to a specific frame. But be careful, for video files, the getFrame() method is not precise and returns the nearest intra key frame from the expected frame. You can use the getFrame() method to position the reader in the video and then use the acquire() method to get the following frames one by one.

#include <visp3/core/vpImage.h>
#include <visp3/core/vpRGBa.h>
#include <visp3/io/vpVideoReader.h>
int main()
{
#ifdef VISP_HAVE_FFMPEG
vpVideoReader reader;
// Initialize the reader.
reader.setFileName("video.mpeg");
reader.open(I);
// Read the nearest key frame from the 3th frame
reader.getFrame(I,2);
// After positionning the video reader use acquire to read the video frame by frame
reader.acquire(I);
return 0;
#endif
}

The other following example explains how to use the class to read a sequence of images. The images are stored in the folder "./image" and are named "image0000.jpeg", "image0001.jpeg", "image0002.jpeg", ... As explained in setFirstFrameIndex() and setLastFrameIndex() it is also possible to set the first and last image numbers to read a portion of the sequence. If these two functions are not used, first and last image numbers are set automatically to match the first and image images of the sequence.

#include <visp3/core/vpImage.h>
#include <visp3/core/vpRGBa.h>
#include <visp3/io/vpVideoReader.h>
int main()
{
vpVideoReader reader;
// Initialize the reader.
reader.setFileName("./image/image%04d.jpeg");
reader.setFirstFrameIndex(10);
reader.setLastFrameIndex(20);
reader.open(I);
while (! reader.end() )
reader.acquire(I);
return 0;
}

Note that it is also possible to access to a specific frame using getFrame().

#include <visp3/core/vpImage.h>
#include <visp3/core/vpRGBa.h>
#include <visp3/io/vpVideoReader.h>
int main()
{
vpVideoReader reader;
// Initialize the reader.
reader.setFileName("./image/image%04d.jpeg");
reader.open(I);
// Read the 3th frame
reader.getFrame(I,2);
return 0;
}
Examples:
AROgre.cpp, AROgreBasic.cpp, imageSequenceReader.cpp, mbtEdgeKltTracking.cpp, mbtEdgeTracking.cpp, mbtKltTracking.cpp, templateTracker.cpp, testKeyPoint-2.cpp, testKeyPoint-3.cpp, testKeyPoint-4.cpp, testKeyPoint.cpp, trackMeNurbs.cpp, tutorial-detection-object-mbt.cpp, tutorial-detection-object-mbt2.cpp, tutorial-face-detector.cpp, tutorial-klt-tracker-with-reinit.cpp, tutorial-klt-tracker.cpp, tutorial-matching-keypoint-homography.cpp, tutorial-matching-keypoint-SIFT.cpp, tutorial-matching-keypoint.cpp, tutorial-matching-surf-deprecated.cpp, tutorial-matching-surf-homography-deprecated.cpp, tutorial-mb-edge-tracker.cpp, tutorial-mb-hybrid-tracker.cpp, tutorial-mb-klt-tracker.cpp, tutorial-mb-tracker-full.cpp, tutorial-mb-tracker.cpp, tutorial-template-tracker.cpp, tutorial-video-reader.cpp, and videoReader.cpp.

Definition at line 159 of file vpVideoReader.h.

Constructor & Destructor Documentation

vpVideoReader::vpVideoReader ( )

Basic constructor.

Definition at line 54 of file vpVideoReader.cpp.

vpVideoReader::~vpVideoReader ( )

Basic destructor.

Definition at line 69 of file vpVideoReader.cpp.

Member Function Documentation

void vpVideoReader::acquire ( vpImage< unsigned char > &  I)
virtual

Grabs the kth image in the stack of frames and increments the frame counter in order to grab the next image (k+1) during the next use of the method.

This method enables to use the class as frame grabber.

Parameters
I: The image where the frame is stored.

Implements vpFrameGrabber.

Definition at line 312 of file vpVideoReader.cpp.

References vpDiskGrabber::acquire(), vpFFMPEG::acquire(), vpImageConvert::convert(), open(), and setLastFrameIndex().

void vpVideoReader::close ( )
inlinevirtual

This virtual function is used to de-allocate the memory used by a specific frame grabber

Implements vpFrameGrabber.

Examples:
AROgre.cpp, AROgreBasic.cpp, mbtEdgeKltTracking.cpp, mbtEdgeTracking.cpp, mbtKltTracking.cpp, and templateTracker.cpp.

Definition at line 242 of file vpVideoReader.h.

long vpVideoReader::getFirstFrameIndex ( ) const
inline

Gets the first frame index.

Returns
Returns the first frame index.
Examples:
mbtEdgeKltTracking.cpp, mbtEdgeTracking.cpp, mbtKltTracking.cpp, and tutorial-klt-tracker.cpp.

Definition at line 270 of file vpVideoReader.h.

bool vpVideoReader::getFrame ( vpImage< vpRGBa > &  I,
long  frame_index 
)

Gets the $ frame $ th frame and stores it in the image $ I $.

Warning
For the video files this method is not precise, and returns the nearest key frame from the expected frame. But this method enables to position the reader where you want. Then, use the acquire method to grab the following images one after one.
Parameters
I: The vpImage used to stored the frame.
frame_index: The index of the frame which has to be read.
Returns
It returns true if the frame could be read. Else it returns false.
Examples:
imageSequenceReader.cpp, and trackMeNurbs.cpp.

Definition at line 360 of file vpVideoReader.cpp.

References vpDiskGrabber::acquire(), vpImageConvert::convert(), vpFFMPEG::getFrame(), setLastFrameIndex(), and vpERROR_TRACE.

Referenced by open().

bool vpVideoReader::getFrame ( vpImage< unsigned char > &  I,
long  frame_index 
)

Gets the $ frame $ th frame and stores it in the image $ I $.

Warning
For the video files this method is not precise, and returns the nearest key frame from the expected frame. But this method enables to position the reader where you want. Then, use the acquire method to grab the following images one after one.
Parameters
I: The vpImage used to stored the frame.
frame_index: The index of the frame which has to be read.
Returns
It returns true if the frame could be read. Else it returns false.

Definition at line 438 of file vpVideoReader.cpp.

References vpDiskGrabber::acquire(), vpImageConvert::convert(), vpFFMPEG::getFrame(), setLastFrameIndex(), and vpERROR_TRACE.

long vpVideoReader::getFrameIndex ( ) const
inline

Get the frame index of the next image. This index is updated at each call of the acquire method. It can be used to detect the end of a file (comparison with getLastFrameIndex()).

Returns
Returns the current frame index.
Examples:
mbtEdgeKltTracking.cpp, mbtEdgeTracking.cpp, mbtKltTracking.cpp, templateTracker.cpp, testKeyPoint-2.cpp, testKeyPoint-4.cpp, tutorial-klt-tracker-with-reinit.cpp, tutorial-klt-tracker.cpp, and videoReader.cpp.

Definition at line 263 of file vpVideoReader.h.

double vpVideoReader::getFramerate ( )

Return the framerate in Hz used to encode the video stream.

If the video is a sequence of images, return -1.

Examples:
tutorial-video-reader.cpp.

Definition at line 720 of file vpVideoReader.cpp.

References vpFFMPEG::getFramerate(), and vpERROR_TRACE.

unsigned int vpFrameGrabber::getHeight ( ) const
inlineinherited

Return the number of rows in the image.

Examples:
AROgre.cpp, and AROgreBasic.cpp.

Definition at line 113 of file vpFrameGrabber.h.

long vpVideoReader::getLastFrameIndex ( ) const
inline

Gets the last frame index.

Returns
Returns the last frame index.
Examples:
imageSequenceReader.cpp.

Definition at line 276 of file vpVideoReader.h.

unsigned int vpFrameGrabber::getWidth ( ) const
inlineinherited

Return the number of columns in the image.

Examples:
AROgre.cpp, and AROgreBasic.cpp.

Definition at line 115 of file vpFrameGrabber.h.

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

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

Grab the first frame and stores it in the image $ I $.

Parameters
I: The image where the frame is stored.

Implements vpFrameGrabber.

Definition at line 202 of file vpVideoReader.cpp.

References vpException::fatalError, getFrame(), vpImage< Type >::getHeight(), vpImage< Type >::getWidth(), vpFFMPEG::GRAY_SCALED, vpFrameGrabber::height, vpFFMPEG::initStream(), vpException::ioError, vpImageException::noFileNameError, vpFFMPEG::openStream(), vpDiskGrabber::setGenericName(), vpDiskGrabber::setImageNumber(), vpERROR_TRACE, and vpFrameGrabber::width.

void vpVideoReader::resetFrameCounter ( )
inline

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

By default the first frame index is set to 0.

This method is useful if you use the class like a frame grabber (ie with theacquire method).

Definition at line 287 of file vpVideoReader.h.

void vpVideoReader::setFileName ( const char *  filename)

It enables to set the path and the name of the file(s) which as/have to be read.

If you want to read a video file, $ filename $ corresponds to the path to the file (example : /local/video.mpeg).

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

Parameters
filename: Path to a video file or file name template of a image sequence.
Examples:
AROgre.cpp, AROgreBasic.cpp, imageSequenceReader.cpp, mbtEdgeKltTracking.cpp, mbtEdgeTracking.cpp, mbtKltTracking.cpp, templateTracker.cpp, testKeyPoint-2.cpp, testKeyPoint-3.cpp, testKeyPoint-4.cpp, testKeyPoint.cpp, trackMeNurbs.cpp, tutorial-detection-object-mbt.cpp, tutorial-detection-object-mbt2.cpp, tutorial-face-detector.cpp, tutorial-klt-tracker-with-reinit.cpp, tutorial-klt-tracker.cpp, tutorial-matching-keypoint-homography.cpp, tutorial-matching-keypoint-SIFT.cpp, tutorial-matching-keypoint.cpp, tutorial-matching-surf-deprecated.cpp, tutorial-matching-surf-homography-deprecated.cpp, tutorial-mb-edge-tracker.cpp, tutorial-mb-hybrid-tracker.cpp, tutorial-mb-klt-tracker.cpp, tutorial-mb-tracker-full.cpp, tutorial-mb-tracker.cpp, tutorial-template-tracker.cpp, tutorial-video-reader.cpp, and videoReader.cpp.

Definition at line 93 of file vpVideoReader.cpp.

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

Referenced by setFileName().

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

It enables to set the path and the name of the file(s) which as/have to be read.

If you want to read a video file, $ filename $ corresponds to the path to the file (example : /local/video.mpeg).

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

Parameters
filename: Path to a video file or file name template of a image sequence.

Definition at line 126 of file vpVideoReader.cpp.

References setFileName().

void vpVideoReader::setFirstFrameIndex ( const long  first_frame)
inline

Enables to set the first frame index if you want to use the class like a grabber (ie with the acquire method).

Parameters
first_frame: The first frame index.
See also
setLastFrameIndex()
Examples:
AROgre.cpp, AROgreBasic.cpp, imageSequenceReader.cpp, templateTracker.cpp, and trackMeNurbs.cpp.

Definition at line 298 of file vpVideoReader.h.

void vpVideoReader::setLastFrameIndex ( const long  last_frame)
inline

Enables to set the last frame index.

Parameters
last_frame: The last frame index.
See also
setFirstFrameIndex()
Examples:
templateTracker.cpp.

Definition at line 309 of file vpVideoReader.h.

Referenced by acquire(), and getFrame().

Member Data Documentation