ViSP  2.9.0
grabOpenCV-2.cpp
1 /****************************************************************************
2  *
3  * $Id: grabOpenCV-2.cpp 4574 2014-01-09 08:48: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)
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  cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
76  cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
77  if(!cap.isOpened()) // check if we succeeded
78  return -1;
79  cv::Mat frame;
80  cap >> frame; // get a new frame from camera
81 
82  IplImage iplimage = frame;
83  std::cout << "Image size: " << iplimage.width << " "
84  << iplimage.height << std::endl;
85 
86  //vpImage<vpRGBa> I; // for color images
87  vpImage<unsigned char> I; // for gray images
88  vpImageConvert::convert(&iplimage, I);
89  vpDisplayOpenCV d(I);
90 
91  for(;;) {
92  cap >> frame; // get a new frame from camera
93  iplimage = frame;
94 
95  // Convert the image in ViSP format and display it
96  vpImageConvert::convert(&iplimage, I);
99  if (vpDisplay::getClick(I, false)) // a click to exit
100  break;
101  }
102  // the camera will be deinitialized automatically in VideoCapture destructor
103  return 0;
104  }
105  catch(vpException e) {
106  std::cout << "Catch an exception: " << e << std::endl;
107  return 1;
108  }
109 }
110 
111 #else
112 int main()
113 {
114  std::cout << "OpenCV is not available..." << std::endl;
115 }
116 
117 #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:1994
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