Visual Servoing Platform  version 3.1.0
tutorial-grabber-opencv.cpp
1 
2 #include <stdlib.h>
3 #include <visp3/core/vpImageConvert.h>
4 #include <visp3/gui/vpDisplayOpenCV.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)) {
26  }; // warm up camera by skiping unread frames
27 
28  std::cout << "Image size: " << frame.rows << " " << frame.cols << std::endl;
29 
30  // vpImage<vpRGBa> I; // for color images
31  vpImage<unsigned char> I; // for gray images
32  vpImageConvert::convert(frame, I);
33 
34  vpDisplayOpenCV d(I);
35 
36  for (;;) {
37  cap >> frame; // get a new frame from camera
38  // Convert the image in ViSP format and display it
39  vpImageConvert::convert(frame, I);
42  if (vpDisplay::getClick(I, false)) // a click to exit
43  break;
44  }
45  } catch (vpException &e) {
46  std::cout << "Catch an exception: " << e << std::endl;
47  }
48 #endif
49 }
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:71
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...