Visual Servoing Platform  version 3.5.0 under development (2022-02-15)
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 
46 #include <iostream>
47 
48 #include <visp3/core/vpConfig.h>
49 
50 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100)
51 
52 #include <visp3/core/vpImage.h>
53 #include <visp3/core/vpImageConvert.h>
54 #include <visp3/gui/vpDisplayOpenCV.h>
55 #include <visp3/sensor/vpOpenCVGrabber.h>
56 
57 // usage: binary <device name>
58 // device name: 0 is the default to dial with the first camera,
59 // 1 to dial with a second camera attached to the computer
60 int main(int argc, char **argv)
61 {
62  try {
63  int device = 0;
64  if (argc > 1)
65  device = atoi(argv[1]);
66 
67  std::cout << "Use device: " << device << std::endl;
68  cv::VideoCapture cap(device); // open the default camera
69 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
70  cap.set(cv::CAP_PROP_FRAME_WIDTH, 640);
71  cap.set(cv::CAP_PROP_FRAME_HEIGHT, 480);
72 #else
73  cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
74  cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
75 #endif
76  if (!cap.isOpened()) // check if we succeeded
77  return -1;
78  cv::Mat frame;
79  int i = 0;
80  while ((i++ < 100) && !cap.read(frame)) {
81  }; // warm up camera by skiping unread frames
82 
83  std::cout << "Image size: "
84 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
85  << (int)cap.get(cv::CAP_PROP_FRAME_WIDTH) << " " << (int)cap.get(cv::CAP_PROP_FRAME_HEIGHT) << std::endl;
86 #else
87  << (int)cap.get(CV_CAP_PROP_FRAME_WIDTH) << " " << (int)cap.get(CV_CAP_PROP_FRAME_HEIGHT) << std::endl;
88 #endif
89 
90  // vpImage<vpRGBa> I; // for color images
91  vpImage<unsigned char> I; // for gray images
92  vpImageConvert::convert(frame, I);
93 
94  vpDisplayOpenCV d(I);
95 
96  for (;;) {
97  cap >> frame; // get a new frame from camera
98 
99  // Convert the image in ViSP format and display it
100  vpImageConvert::convert(frame, I);
102  vpDisplay::flush(I);
103  if (vpDisplay::getClick(I, false)) // a click to exit
104  break;
105  }
106  // the camera will be deinitialized automatically in VideoCapture
107  // destructor
108  return EXIT_SUCCESS;
109  } catch (const vpException &e) {
110  std::cout << "Catch an exception: " << e << std::endl;
111  return EXIT_FAILURE;
112  }
113 }
114 
115 #else
116 int main()
117 {
118  std::cout << "You do not have OpenCV functionalities to display images..." << std::endl;
119  std::cout << "Tip:" << std::endl;
120  std::cout << "- Install OpenCV, configure again ViSP using cmake and build again this example" << std::endl;
121  return EXIT_SUCCESS;
122 }
123 #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...