ViSP  2.8.0

#include <vpOpenCVGrabber.h>

+ Inheritance diagram for vpOpenCVGrabber:

Public Member Functions

 vpOpenCVGrabber ()
 
 ~vpOpenCVGrabber ()
 
void open ()
 
void open (vpImage< unsigned char > &I)
 
void open (vpImage< vpRGBa > &I)
 
void acquire (vpImage< unsigned char > &I)
 
void acquire (vpImage< vpRGBa > &I)
 
IplImage * acquire ()
 
void close ()
 
void getFramerate (double &framerate)
 
void setFramerate (const double framerate)
 
void setWidth (const unsigned int width)
 
void setHeight (const unsigned int height)
 
void setDeviceType (int type)
 
void setFlip (bool flipType)
 
unsigned int getHeight () const
 
unsigned int getWidth () const
 

Public Attributes

bool init
 

Protected Attributes

unsigned int height
 
unsigned int width
 

Detailed Description

Class for cameras video capture using OpenCV library.

Needs OpenCV available on http://opencv.willowgarage.com/wiki/.

The code below available in tutorial-grabber-opencv.cpp shows how to grab and display images using OpenCV wrappers impremented in ViSP.

#include <visp/vpDisplayOpenCV.h>
#include <visp/vpOpenCVGrabber.h>
int main()
{
#ifdef VISP_HAVE_OPENCV
g.open(I);
std::cout << "Image size: " << I.getWidth() << " " << I.getHeight() << std::endl;
while(1) {
g.acquire(I);
if (vpDisplay::getClick(I, false))
break;
}
#endif
}

Note that it is also possible to grab images using OpenCV library by using directly OpenCV cv::VideoCapture class. The following code corresponding to tutorial-grabber-opencv-bis.cpp shows how to grab images in a cv::Mat structure and then convert OpenCV images in ViSP images.

#include <stdlib.h>
#include <visp/vpDisplayOpenCV.h>
#include <visp/vpImageConvert.h>
// usage: binary <device name>
// device name: 0 is the default to dial with the first camera,
// 1 to dial with a second camera attached to the computer
int main(int argc, char** argv)
{
int device = 0;
if (argc > 1)
device = atoi(argv[1]);
std::cout << "Use device: " << device << std::endl;
#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100)
cv::VideoCapture cap(device); // open the default camera
if(!cap.isOpened()) { // check if we succeeded
std::cout << "Failed to open the camera" << std::endl;
return -1;
}
cv::Mat frame;
cap >> frame; // get a new frame from camera
std::cout << "Image size: " << frame.rows << " " << frame.cols << std::endl;
//vpImage<vpRGBa> I; // for color images
vpImage<unsigned char> I; // for gray images
for(;;) {
cap >> frame; // get a new frame from camera
// Convert the image in ViSP format and display it
if (vpDisplay::getClick(I, false)) // a click to exit
break;
}
#endif
}

An other example very close to the previous one available in grabOpenCV-2.cpp shows how to grab images in OpenCV IplImage structure and then how to convert OpenCV images in ViSP images.

Examples:
fernClassifier.cpp, grabOpenCV.cpp, HelloWorldOgre.cpp, HelloWorldOgreAdvanced.cpp, planarObjectDetector.cpp, and tutorial-grabber-opencv.cpp.

Definition at line 88 of file vpOpenCVGrabber.h.

Constructor & Destructor Documentation

vpOpenCVGrabber::vpOpenCVGrabber ( )

Basic Constructor.

Definition at line 60 of file vpOpenCVGrabber.cpp.

References vpFrameGrabber::height, vpFrameGrabber::init, and vpFrameGrabber::width.

vpOpenCVGrabber::~vpOpenCVGrabber ( )

Basic destructor that calls the close() method.

See also
close()

Definition at line 80 of file vpOpenCVGrabber.cpp.

References close().

Member Function Documentation

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

Grab a gray level image.

Parameters
I: Acquired gray level image.
Exceptions
vpFrameGrabberException::initializationErrorIf the initialization of the grabber was not done previously.

Implements vpFrameGrabber.

Examples:
fernClassifier.cpp, grabOpenCV.cpp, HelloWorldOgre.cpp, HelloWorldOgreAdvanced.cpp, planarObjectDetector.cpp, and tutorial-grabber-opencv.cpp.

Definition at line 144 of file vpOpenCVGrabber.cpp.

References close(), vpImageConvert::convert(), vpFrameGrabber::init, and vpFrameGrabberException::initializationError.

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

Grab a color image.

Parameters
I: Acquired color image.
Exceptions
vpFrameGrabberException::initializationErrorIf the initialization of the grabber was not done previously.

Implements vpFrameGrabber.

Definition at line 168 of file vpOpenCVGrabber.cpp.

References close(), vpImageConvert::convert(), vpFrameGrabber::init, and vpFrameGrabberException::initializationError.

IplImage * vpOpenCVGrabber::acquire ( )

Grab an image direclty in the OpenCV format.

Returns
Pointer to the image (must not be freed).
Exceptions
vpFrameGrabberException::initializationErrorIf the initialization of the grabber was not done previously.

Definition at line 192 of file vpOpenCVGrabber.cpp.

References close(), vpFrameGrabber::init, and vpFrameGrabberException::initializationError.

void vpOpenCVGrabber::close ( )
virtual

Stop the acquisition of images and free the camera.

Implements vpFrameGrabber.

Examples:
HelloWorldOgre.cpp, and HelloWorldOgreAdvanced.cpp.

Definition at line 211 of file vpOpenCVGrabber.cpp.

References vpFrameGrabber::init.

Referenced by acquire(), open(), setHeight(), setWidth(), and ~vpOpenCVGrabber().

void vpOpenCVGrabber::getFramerate ( double &  framerate)

Gets the capture frame rate.

Parameters
framerate: The value of the framerate is returned here.

Definition at line 225 of file vpOpenCVGrabber.cpp.

unsigned int vpFrameGrabber::getHeight ( ) const
inlineinherited

Return the number of rows in the image.

Examples:
AROgre.cpp, AROgreBasic.cpp, HelloWorldOgre.cpp, and HelloWorldOgreAdvanced.cpp.

Definition at line 119 of file vpFrameGrabber.h.

Referenced by vp1394Grabber::acquire(), and vp1394Grabber::open().

unsigned int vpFrameGrabber::getWidth ( ) const
inlineinherited

Return the number of columns in the image.

Examples:
AROgre.cpp, AROgreBasic.cpp, HelloWorldOgre.cpp, and HelloWorldOgreAdvanced.cpp.

Definition at line 121 of file vpFrameGrabber.h.

Referenced by vp1394Grabber::acquire(), and vp1394Grabber::open().

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

Initialization of the grabber. Call the generic initialization method.

Parameters
I: Gray level image. This parameter is not used.
See also
open()

Implements vpFrameGrabber.

Definition at line 116 of file vpOpenCVGrabber.cpp.

References open().

void vpOpenCVGrabber::open ( vpImage< vpRGBa > &  I)
virtual

Initialization of the grabber. Call the generic initialization method.

Parameters
I: Color image. This parameter is not used.
See also
open()

Implements vpFrameGrabber.

Definition at line 130 of file vpOpenCVGrabber.cpp.

References open().

void vpOpenCVGrabber::setDeviceType ( int  type)

Set the expected type of device.

Parameters
type: expected type of device
  • CV_CAP_ANY
  • CV_CAP_MIL
  • CV_CAP_VFW
  • CV_CAP_V4L
  • CV_CAP_V4L2
  • CV_CAP_FIREWIRE
  • CV_CAP_IEEE1394
  • CV_CAP_DC1394
  • CV_CAP_CMU_1394
Examples:
grabOpenCV.cpp.

Definition at line 303 of file vpOpenCVGrabber.cpp.

References vpTRACE.

void vpOpenCVGrabber::setFlip ( bool  flipType)

Set the boolean variable flip to the expected value.

Parameters
flipType: Expected value of the variable flip. True means that the image is flipped during each image acquisition.
Warning
This function is only useful under Windows.
Note
The aim of this function is to fix a problem which appears under Windows. Indeed with several cameras the aquired images are flipped.

Definition at line 323 of file vpOpenCVGrabber.cpp.

void vpOpenCVGrabber::setFramerate ( const double  framerate)

Sets the capture frame rate

Parameters
framerate: The requested value of the capture framerate.

Definition at line 237 of file vpOpenCVGrabber.cpp.

void vpOpenCVGrabber::setHeight ( const unsigned int  height)

Sets the captured image height.

Warning
This function must be called after open() method.
Parameters
height: The requested value of the captured image height.
Exceptions
vpFrameGrabberException::initializationErrorIf no camera was found.

Definition at line 276 of file vpOpenCVGrabber.cpp.

References close(), vpFrameGrabber::height, vpFrameGrabberException::initializationError, and vpERROR_TRACE.

void vpOpenCVGrabber::setWidth ( const unsigned int  width)

Sets the captured image width.

Warning
This function must be called after open() method.
Parameters
width: The requested value of the captured image width.
Exceptions
vpFrameGrabberException::initializationErrorIf no camera was found.

Definition at line 253 of file vpOpenCVGrabber.cpp.

References close(), vpFrameGrabberException::initializationError, vpERROR_TRACE, and vpFrameGrabber::width.

Member Data Documentation