Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
grabOpenCV.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Acquire images using OpenCV cv::VideoCapture.
33  *
34  * Authors:
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
39 #include <visp3/core/vpConfig.h>
40 
48 #include <iostream>
49 
50 #include <visp3/core/vpConfig.h>
51 
52 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100)
53 
54 #include <visp3/core/vpImage.h>
55 #include <visp3/core/vpImageConvert.h>
56 #include <visp3/gui/vpDisplayOpenCV.h>
57 #include <visp3/sensor/vpOpenCVGrabber.h>
58 
59 // usage: binary <device name>
60 // device name: 0 is the default to dial with the first camera,
61 // 1 to dial with a second camera attached to the computer
62 int main(int argc, char **argv)
63 {
64  try {
65  int device = 0;
66  if (argc > 1)
67  device = atoi(argv[1]);
68 
69  std::cout << "Use device: " << device << std::endl;
70  cv::VideoCapture cap(device); // open the default camera
71 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
72  cap.set(cv::CAP_PROP_FRAME_WIDTH, 640);
73  cap.set(cv::CAP_PROP_FRAME_HEIGHT, 480);
74 #else
75  cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
76  cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
77 #endif
78  if (!cap.isOpened()) // check if we succeeded
79  return -1;
80  cv::Mat frame;
81  int i = 0;
82  while ((i++ < 100) && !cap.read(frame)) {
83  }; // warm up camera by skiping unread frames
84 
85  std::cout << "Image size: "
86 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
87  << (int)cap.get(cv::CAP_PROP_FRAME_WIDTH) << " " << (int)cap.get(cv::CAP_PROP_FRAME_HEIGHT) << std::endl;
88 #else
89  << (int)cap.get(CV_CAP_PROP_FRAME_WIDTH) << " " << (int)cap.get(CV_CAP_PROP_FRAME_HEIGHT) << std::endl;
90 #endif
91 
92  // vpImage<vpRGBa> I; // for color images
93  vpImage<unsigned char> I; // for gray images
94  vpImageConvert::convert(frame, I);
95 
96  vpDisplayOpenCV d(I);
97 
98  for (;;) {
99  cap >> frame; // get a new frame from camera
100 
101  // Convert the image in ViSP format and display it
102  vpImageConvert::convert(frame, I);
104  vpDisplay::flush(I);
105  if (vpDisplay::getClick(I, false)) // a click to exit
106  break;
107  }
108  // the camera will be deinitialized automatically in VideoCapture
109  // destructor
110  return EXIT_SUCCESS;
111  } catch (const vpException &e) {
112  std::cout << "Catch an exception: " << e << std::endl;
113  return EXIT_FAILURE;
114  }
115 }
116 
117 #else
118 int main()
119 {
120  std::cout << "You do not have OpenCV functionalities to display images..." << std::endl;
121  std::cout << "Tip:" << std::endl;
122  std::cout << "- Install OpenCV, configure again ViSP using cmake and build again this example" << std::endl;
123  return EXIT_SUCCESS;
124 }
125 #endif
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...