ViSP  3.0.0
grabOpenCV-2.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Acquire images using OpenCV cv::VideoCapture.
32  *
33  * Authors:
34  * Fabien Spindler
35  *
36  *****************************************************************************/
37 
38 #include <visp3/core/vpConfig.h>
39 
47 #include <iostream>
48 
49 #include <visp3/core/vpConfig.h>
50 
51 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100)
52 
53 #include <visp3/gui/vpDisplayOpenCV.h>
54 #include <visp3/core/vpImage.h>
55 #include <visp3/core/vpImageConvert.h>
56 #include <visp3/sensor/vpOpenCVGrabber.h>
57 
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  cap >> frame; // get a new frame from camera
82 
83  std::cout << "Image size: "
84 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
85  << (int)cap.get(cv::CAP_PROP_FRAME_WIDTH) << " "
86  << (int)cap.get(cv::CAP_PROP_FRAME_HEIGHT) << std::endl;
87 #else
88  << (int)cap.get(CV_CAP_PROP_FRAME_WIDTH) << " "
89  << (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 destructor
109  return 0;
110  }
111  catch(vpException e) {
112  std::cout << "Catch an exception: " << e << std::endl;
113  return 1;
114  }
115 }
116 
117 #else
118 int main()
119 {
120  std::cout << "OpenCV is not available..." << std::endl;
121 }
122 
123 #endif
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)
Definition: vpDisplay.cpp:2233
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
The vpDisplayOpenCV allows to display image using the opencv library.
virtual bool getClick(bool blocking=true)=0