ViSP  2.8.0
grabOpenCV-2.cpp
1 /****************************************************************************
2  *
3  * $Id: grabOpenCV-2.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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)
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  int device = 0;
69  if (argc > 1)
70  device = atoi(argv[1]);
71 
72  std::cout << "Use device: " << device << std::endl;
73  cv::VideoCapture cap(device); // open the default camera
74  cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
75  cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
76  if(!cap.isOpened()) // check if we succeeded
77  return -1;
78  cv::Mat frame;
79  cap >> frame; // get a new frame from camera
80 
81  IplImage iplimage = frame;
82  std::cout << "Image size: " << iplimage.width << " "
83  << iplimage.height << std::endl;
84 
85  //vpImage<vpRGBa> I; // for color images
86  vpImage<unsigned char> I; // for gray images
87  vpImageConvert::convert(&iplimage, I);
88  vpDisplayOpenCV d(I);
89 
90  for(;;) {
91  cap >> frame; // get a new frame from camera
92  iplimage = frame;
93 
94  // Convert the image in ViSP format and display it
95  vpImageConvert::convert(&iplimage, I);
98  if (vpDisplay::getClick(I, false)) // a click to exit
99  break;
100  }
101  // the camera will be deinitialized automatically in VideoCapture destructor
102  return 0;
103 }
104 
105 #else
106 int main()
107 {
108  std::cout << "OpenCV is not available..." << std::endl;
109 }
110 
111 #endif
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1991
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:203
The vpDisplayOpenCV allows to display image using the opencv library.
virtual bool getClick(bool blocking=true)=0