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