Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
tutorial-grabber-opencv.cpp
1 
2 #include <stdlib.h>
3 #include <visp3/gui/vpDisplayOpenCV.h>
4 #include <visp3/core/vpImageConvert.h>
5 
6 // usage: binary <device name>
7 // device name: 0 is the default to dial with the first camera,
8 // 1 to dial with a second camera attached to the computer
9 int main(int argc, char** argv)
10 {
11  int device = 0;
12  if (argc > 1)
13  device = atoi(argv[1]);
14  std::cout << "Use device: " << device << std::endl;
15 
16 #if (VISP_HAVE_OPENCV_VERSION >= 0x020100)
17  try {
18  cv::VideoCapture cap(device); // open the default camera
19  if(!cap.isOpened()) { // check if we succeeded
20  std::cout << "Failed to open the camera" << std::endl;
21  return -1;
22  }
23  cv::Mat frame;
24  int i=0;
25  while ((i++ < 100) && !cap.read(frame)) {}; // warm up camera by skiping unread frames
26 
27  std::cout << "Image size: " << frame.rows << " " << frame.cols << std::endl;
28 
29  //vpImage<vpRGBa> I; // for color images
30  vpImage<unsigned char> I; // for gray images
31  vpImageConvert::convert(frame, I);
32 
33  vpDisplayOpenCV d(I);
34 
35  for(;;) {
36  cap >> frame; // get a new frame from camera
37  // Convert the image in ViSP format and display it
38  vpImageConvert::convert(frame, I);
41  if (vpDisplay::getClick(I, false)) // a click to exit
42  break;
43  }
44  }
45  catch(vpException &e) {
46  std::cout << "Catch an exception: " << e << std::endl;
47  }
48 #endif
49 }
50 
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
error that can be emited by ViSP classes.
Definition: vpException.h:73
static void flush(const vpImage< unsigned char > &I)
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...