Visual Servoing Platform  version 3.6.1 under development (2024-03-18)
vpVideoReader Class Reference

#include <visp3/io/vpVideoReader.h>

+ Inheritance diagram for vpVideoReader:

Public Member Functions

 vpVideoReader ()
 
virtual ~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
 
std::string getFrameName () const
 
long getFirstFrameIndex ()
 
long getLastFrameIndex ()
 
long getFrameStep () const
 
bool isVideoFormat () const
 
void open (vpImage< vpRGBa > &I)
 
void open (vpImage< unsigned char > &I)
 
vpVideoReaderoperator>> (vpImage< unsigned char > &I)
 
vpVideoReaderoperator>> (vpImage< vpRGBa > &I)
 
void resetFrameCounter ()
 
void setFileName (const std::string &filename)
 
void setFirstFrameIndex (const long first_frame)
 
void setLastFrameIndex (const long last_frame)
 
void setFrameStep (const long frame_step)
 
Inherited functionalities from vpFramegrabber
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.

This class has its own implementation to read 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 read 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 read 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 read 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-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) && defined(HAVE_OPENCV_HIGHGUI) && defined(HAVE_OPENCV_VIDEOIO)
try {
std::string videoname = "video.mp4";
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::string(argv[i]) == "-h") {
std::cout << "\nUsage: " << argv[0] << " [--name <video name> (default: " << videoname << ")]"
<< " [--help] [-h]\n"
<< std::endl;
return EXIT_SUCCESS;
}
}
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(HAVE_OPENCV_HIGHGUI)
#else
std::cout << "No image viewer is available..." << std::endl;
#endif
vpDisplay::setTitle(I, "Video reader");
unsigned cpt = 1;
while (!g.end()) {
double t = vpTime::measureTimeMs();
g.acquire(I);
vpDisplay::displayText(I, 20, 20, "Click to quit", vpColor::red);
std::stringstream ss;
ss << "Frame: " << cpt++;
vpDisplay::displayText(I, 40, 20, ss.str(), vpColor::red);
if (vpDisplay::getClick(I, false))
break;
vpTime::wait(t, 1000. / g.getFramerate());
}
std::cout << "End of video was reached" << std::endl;
} catch (const vpException &e) {
std::cout << e.getMessage() << std::endl;
}
#else
(void)argc;
(void)argv;
std::cout << "Install OpenCV and rebuild ViSP to use this example." << std::endl;
#endif
return EXIT_SUCCESS;
}
static const vpColor red
Definition: vpColor.h:211
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.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
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void setTitle(const vpImage< unsigned char > &I, const std::string &windowtitle)
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
const char * getMessage() const
Definition: vpException.cpp:64
unsigned int getWidth() const
Definition: vpImage.h:249
unsigned int getHeight() const
Definition: vpImage.h:184
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void acquire(vpImage< vpRGBa > &I)
void open(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
double getFramerate()
VISP_EXPORT int wait(double t0, double t)
VISP_EXPORT double measureTimeMs()

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/io/vpVideoReader.h>
int main()
{
#ifdef VISP_HAVE_OPENCV
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 positioning the video reader use acquire to read the video frame by frame
reader.acquire(I);
return 0;
#endif
}
bool getFrame(vpImage< vpRGBa > &I, long frame)

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/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;
}
void setLastFrameIndex(const long last_frame)
void setFirstFrameIndex(const long first_frame)

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

#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, mbtGenericTracking.cpp, mbtGenericTracking2.cpp, mbtKltTracking.cpp, templateTracker.cpp, testImageNormalizedCorrelation.cpp, testImageTemplateMatching.cpp, testKeyPoint-2.cpp, testKeyPoint-3.cpp, testKeyPoint-4.cpp, testKeyPoint.cpp, trackMeEllipse.cpp, trackMeNurbs.cpp, tutorial-circle-hough.cpp, tutorial-detection-object-mbt-deprecated.cpp, tutorial-detection-object-mbt.cpp, tutorial-detection-object-mbt2-deprecated.cpp, tutorial-detection-object-mbt2.cpp, tutorial-face-detector.cpp, tutorial-klt-tracker-with-reinit.cpp, tutorial-klt-tracker.cpp, tutorial-matching-keypoint-SIFT.cpp, tutorial-matching-keypoint-homography.cpp, tutorial-matching-keypoint.cpp, tutorial-mb-edge-tracker.cpp, tutorial-mb-generic-tracker-full.cpp, tutorial-mb-generic-tracker-stereo-mono.cpp, tutorial-mb-generic-tracker-stereo.cpp, tutorial-mb-generic-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-manipulation.cpp, tutorial-video-reader.cpp, and videoReader.cpp.

Definition at line 164 of file vpVideoReader.h.

Constructor & Destructor Documentation

◆ vpVideoReader()

vpVideoReader::vpVideoReader ( )

Basic constructor.

Definition at line 51 of file vpVideoReader.cpp.

◆ ~vpVideoReader()

vpVideoReader::~vpVideoReader ( )
virtual

Basic destructor.

Definition at line 64 of file vpVideoReader.cpp.

Member Function Documentation

◆ acquire() [1/2]

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 339 of file vpVideoReader.cpp.

References vpDiskGrabber::acquire(), vpImageConvert::convert(), vpDiskGrabber::getImageName(), vpDiskGrabber::getImageNumber(), open(), vpDiskGrabber::setImageNumber(), and vpDiskGrabber::setStep().

◆ acquire() [2/2]

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

Grabs the current (k) 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. If open() was not called previously, this method opens the video reader.

This method enables to use the class as frame grabber.

Parameters
I: The image where the frame is stored.

Implements vpFrameGrabber.

Examples
AROgre.cpp, AROgreBasic.cpp, mbtEdgeKltTracking.cpp, mbtEdgeTracking.cpp, mbtGenericTracking.cpp, mbtGenericTracking2.cpp, mbtKltTracking.cpp, templateTracker.cpp, testImageTemplateMatching.cpp, testKeyPoint-2.cpp, testKeyPoint-3.cpp, testKeyPoint-4.cpp, testKeyPoint.cpp, trackMeEllipse.cpp, tutorial-circle-hough.cpp, tutorial-detection-object-mbt-deprecated.cpp, tutorial-detection-object-mbt.cpp, tutorial-detection-object-mbt2-deprecated.cpp, tutorial-detection-object-mbt2.cpp, tutorial-face-detector.cpp, tutorial-klt-tracker-with-reinit.cpp, tutorial-klt-tracker.cpp, tutorial-matching-keypoint-SIFT.cpp, tutorial-matching-keypoint-homography.cpp, tutorial-matching-keypoint.cpp, tutorial-mb-edge-tracker.cpp, tutorial-mb-generic-tracker-full.cpp, tutorial-mb-generic-tracker-stereo-mono.cpp, tutorial-mb-generic-tracker-stereo.cpp, tutorial-mb-generic-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-manipulation.cpp, tutorial-video-reader.cpp, and videoReader.cpp.

Definition at line 245 of file vpVideoReader.cpp.

References vpDiskGrabber::acquire(), vpImageConvert::convert(), vpDiskGrabber::getImageName(), vpDiskGrabber::getImageNumber(), open(), vpDiskGrabber::setImageNumber(), setLastFrameIndex(), and vpDiskGrabber::setStep().

Referenced by operator>>().

◆ close()

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, mbtGenericTracking.cpp, mbtGenericTracking2.cpp, mbtKltTracking.cpp, and templateTracker.cpp.

Definition at line 230 of file vpVideoReader.h.

◆ end()

◆ getFirstFrameIndex()

long vpVideoReader::getFirstFrameIndex ( )
inline

◆ getFrame() [1/2]

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 512 of file vpVideoReader.cpp.

References vpDiskGrabber::acquire(), vpImageConvert::convert(), vpImage< Type >::getHeight(), vpDiskGrabber::getImageNumber(), vpImage< Type >::getWidth(), vpFrameGrabber::height, vpDiskGrabber::setImageNumber(), setLastFrameIndex(), vpERROR_TRACE, and vpFrameGrabber::width.

◆ getFrame() [2/2]

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 435 of file vpVideoReader.cpp.

References vpDiskGrabber::acquire(), vpImageConvert::convert(), vpImage< Type >::getHeight(), vpDiskGrabber::getImageNumber(), vpImage< Type >::getWidth(), vpFrameGrabber::height, vpDiskGrabber::setImageNumber(), setLastFrameIndex(), vpERROR_TRACE, and vpFrameGrabber::width.

Referenced by open().

◆ getFrameIndex()

long vpVideoReader::getFrameIndex ( ) const
inline

Get the frame index of the current 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.
See also
end()
Examples
imageSequenceReader.cpp, mbtEdgeKltTracking.cpp, mbtEdgeTracking.cpp, mbtGenericTracking.cpp, mbtGenericTracking2.cpp, mbtKltTracking.cpp, templateTracker.cpp, testImageTemplateMatching.cpp, testKeyPoint-2.cpp, testKeyPoint-4.cpp, trackMeEllipse.cpp, tutorial-klt-tracker-with-reinit.cpp, tutorial-klt-tracker.cpp, tutorial-mb-generic-tracker-full.cpp, tutorial-video-manipulation.cpp, and videoReader.cpp.

Definition at line 272 of file vpVideoReader.h.

◆ getFrameName()

std::string vpVideoReader::getFrameName ( ) const
inline

Return the name of the file in which the last frame was read.

Examples
trackMeEllipse.cpp.

Definition at line 277 of file vpVideoReader.h.

◆ getFramerate()

double vpVideoReader::getFramerate ( )
inline

Return the frame rate 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 255 of file vpVideoReader.h.

◆ getFrameStep()

long vpVideoReader::getFrameStep ( ) const
inline

Gets the frame step.

Returns
Returns the frame step value.
Examples
trackMeEllipse.cpp.

Definition at line 310 of file vpVideoReader.h.

◆ getHeight()

unsigned int vpFrameGrabber::getHeight ( ) const
inlineinherited

Return the number of rows in the image.

Examples
AROgre.cpp, AROgreBasic.cpp, and testPylonGrabber.cpp.

Definition at line 106 of file vpFrameGrabber.h.

◆ getLastFrameIndex()

long vpVideoReader::getLastFrameIndex ( )
inline

◆ getWidth()

unsigned int vpFrameGrabber::getWidth ( ) const
inlineinherited

Return the number of columns in the image.

Examples
AROgre.cpp, AROgreBasic.cpp, and testPylonGrabber.cpp.

Definition at line 108 of file vpFrameGrabber.h.

◆ isVideoFormat()

bool vpVideoReader::isVideoFormat ( ) const

Indicate if the video is an encoded single video file.

Returns
true if the video format corresponds to an encoded single video file like one of the following avi, mpeg, mp4, mts, mov, ogv, wmv, flv, mkv. Return false, if the video is a sequence of successive images (png, jpeg, ppm, pgm...).
Examples
tutorial-klt-tracker.cpp, and tutorial-template-tracker.cpp.

Definition at line 891 of file vpVideoReader.cpp.

◆ open() [1/2]

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 209 of file vpVideoReader.cpp.

References getFrame(), and vpException::ioError.

◆ open() [2/2]

◆ operator>>() [1/2]

vpVideoReader & vpVideoReader::operator>> ( vpImage< unsigned char > &  I)

Operator that allows to capture a grey level image.

Parameters
I: The captured image.
#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 >> I;
}

Definition at line 830 of file vpVideoReader.cpp.

References acquire().

◆ operator>>() [2/2]

vpVideoReader & vpVideoReader::operator>> ( vpImage< vpRGBa > &  I)

Operator that allows to capture a grey level image.

Parameters
I: The captured image.
#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 >> I;
}

Definition at line 859 of file vpVideoReader.cpp.

References acquire().

◆ resetFrameCounter()

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 the acquire method).

Definition at line 327 of file vpVideoReader.h.

◆ setFileName()

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

It enables to set the path and the name of the video which has to be read.

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

If you want to read a sequence of images, filename corresponds rather to the path followed by the image name template. For example, 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, mbtGenericTracking.cpp, mbtGenericTracking2.cpp, mbtKltTracking.cpp, templateTracker.cpp, testImageNormalizedCorrelation.cpp, testImageTemplateMatching.cpp, testKeyPoint-2.cpp, testKeyPoint-3.cpp, testKeyPoint-4.cpp, testKeyPoint.cpp, trackMeEllipse.cpp, trackMeNurbs.cpp, tutorial-circle-hough.cpp, tutorial-detection-object-mbt-deprecated.cpp, tutorial-detection-object-mbt.cpp, tutorial-detection-object-mbt2-deprecated.cpp, tutorial-detection-object-mbt2.cpp, tutorial-face-detector.cpp, tutorial-klt-tracker-with-reinit.cpp, tutorial-klt-tracker.cpp, tutorial-matching-keypoint-SIFT.cpp, tutorial-matching-keypoint-homography.cpp, tutorial-matching-keypoint.cpp, tutorial-mb-edge-tracker.cpp, tutorial-mb-generic-tracker-full.cpp, tutorial-mb-generic-tracker-stereo-mono.cpp, tutorial-mb-generic-tracker-stereo.cpp, tutorial-mb-generic-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-manipulation.cpp, tutorial-video-reader.cpp, and videoReader.cpp.

Definition at line 86 of file vpVideoReader.cpp.

References vpException::badValue, vpIoTools::getName(), and vpImageException::noFileNameError.

◆ setFirstFrameIndex()

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, trackMeEllipse.cpp, trackMeNurbs.cpp, and tutorial-mb-generic-tracker-full.cpp.

Definition at line 338 of file vpVideoReader.h.

◆ setFrameStep()

void vpVideoReader::setFrameStep ( const long  frame_step)
inline

Sets the frame step index. The default frame step is 1

Parameters
frame_step: The frame index step.
See also
setFrameStep()
Examples
trackMeEllipse.cpp.

Definition at line 365 of file vpVideoReader.h.

◆ setLastFrameIndex()

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
mbtEdgeKltTracking.cpp, mbtEdgeTracking.cpp, mbtGenericTracking.cpp, mbtGenericTracking2.cpp, mbtKltTracking.cpp, templateTracker.cpp, and trackMeEllipse.cpp.

Definition at line 351 of file vpVideoReader.h.

Referenced by acquire(), and getFrame().

Member Data Documentation

◆ height

◆ init

◆ width