ViSP  2.8.0
Tutorial: Image frame grabbing

Images from firewire cameras

The next example shows how to use a framegrabber to acquire color images from a firewire camera under Unix. The following example suppose that libX11 and libdc1394-2 3rd party are available.

#include <visp/vp1394TwoGrabber.h>
#include <visp/vpDisplayX.h>
#include <visp/vpImage.h>
int main()
{
#ifdef VISP_HAVE_DC1394_2
vpImage<unsigned char> I; // Create a gray level image container
bool reset = true; // Enable bus reset during construction (default)
vp1394TwoGrabber g(reset); // Create a grabber based on libdc1394-2.x third party lib
g.open(I);
std::cout << "Image size: " << I.getWidth() << " " << I.getHeight() << std::endl;
#ifdef VISP_HAVE_X11
vpDisplayX d(I);
#else
std::cout << "No image viewer is available..." << std::endl;
#endif
while(1) {
g.acquire(I);
if (vpDisplay::getClick(I, false))
break;
}
#endif
}

Here after we explain the new lines that are introduced.

First an instance of the frame grabber is created. During the creating a bus reset is send. If you don't want to reset the firewire bus, just turn reset to false.

Once the grabber is created, we set the camera image size, color coding, and framerate.

Note that here you can specify some other settings such as the firewire transmission speed. For a more complete list of settings see vp1394TwoGrabber class.

g.setIsoTransmissionSpeed(vp1394TwoGrabber::vpISO_SPEED_800);

Then the grabber is initialized using:

g.open(I);

From now the color image I is also initialized with the size corresponding to the grabber settings.

Then we enter in a while loop where image acquisition is simply done by:

g.acquire(I);

We are waiting for a non blocking mouse event to break the while loop before ending the program.

if (vpDisplay::getClick(I, false)) break;

In the previous example we use vp1394TwoGrabber class that works for firewire cameras under Unix. If you are under Windows, you may use vp1394CMUGrabber class. A similar example is provided in tutorial-grabber-CMU1394.cpp.

Images from other cameras

If you want to grab images from an usb camera under Unix, you may use vpV4l2Grabber class. To this end libv4l should be installed. An example is provided in tutorial-grabber-v4l2.cpp.

It is also possible to grab images using OpenCV. You may find examples in tutorial-grabber-opencv.cpp and tutorial-grabber-opencv-bis.cpp.

Images from a video stream

With ViSP it also possible to get images from an input video stream. To this end we exploit ffmpeg 3rd party. The example below available in tutorial-grabber-video.cpp shows how o consider an mpeg video stream:

#include <visp/vpDisplayX.h>
#include <visp/vpVideoReader.h>
int main()
{
#ifdef VISP_HAVE_FFMPEG
g.setFileName("./video.mpg");
g.open(I);
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);
#else
std::cout << "No image viewer is available..." << std::endl;
#endif
vpDisplay::setTitle(I, "Video grabber");
while (! g.end() ) {
double t = vpTime::measureTimeMs();
g.acquire(I);
if (vpDisplay::getClick(I, false)) break;
vpTime::wait(t, 1000. / g.getFramerate());
}
#endif
}

We explain now the new lines that were introduced.

#include <visp/vpTime.h>
#include <visp/vpVideoReader.h>

Include the header of the vpTime class that allows to measure time, and of the vpVideoReader class that allows to read a video stream.

Create an instance of a video reader.

g.setFileName("./video.mpg");

Set the name of the video stream. Here video.mpg refers to a mpeg file located in the same folder than the executable.

The vpVideoReader class can also handle a sequence of images. For example, to read the following images:

% ls *.png
image0000.png
image0001.png
image0002.png
image0003.png
image0004.png
...

you may use the following

g.setFileName("./image%04d.png");

where you specify that each image number is coded with 4 digits. Here, ffmpeg is no yet mandatory, but rather libpng that should be available to read PNG images. Supported image formats are PPM, PGM, PNG and JPEG. In the last case, libjpeg should be installed.

Then as for any other grabber, you have to initialize the frame grabber using:

g.open(I);

Then we enter in the while loop until the last image was not reached:

while (! g.end() ) {

To get the next image in the stream, we just use:

g.acquire(I);

To synchronize the video decoding with the video framerate, we measure the beginning time of each loop iteration:

double t = vpTime::measureTimeMs();

The synchronization is done by waiting from the beginning of the iteration the corresponding time expressed in milliseconds by using:

vpTime::wait(t, 1000. / g.getFramerate());

You are now ready to see the next Tutorial: Blob tracking.