ViSP
vpOpenCVGrabber.cpp
1 /****************************************************************************
2  *
3  * $Id: vpOpenCVGrabber.cpp 5023 2014-12-03 16:07:48Z 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  * Cameras video capture using OpenCV library.
36  *
37  * Authors:
38  * Nicolas Melchior
39  *
40  *****************************************************************************/
41 
47 #include <visp/vpOpenCVGrabber.h>
48 
49 #if ( defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION < 0x020408))
50 
51 #include <visp/vpImageConvert.h>
52 #include <visp/vpFrameGrabberException.h>
53 
54 #include <iostream>
55 #include <math.h>
56 
60 vpOpenCVGrabber::vpOpenCVGrabber()
61  : capture(NULL), DeviceType(0), flip(false)
62 {
63  // public memebers
64  init = false;
65 
66  // protected members
67  width = height = 0;
68 }
69 
70 
76 vpOpenCVGrabber::~vpOpenCVGrabber( )
77 {
78  close();
79 }
80 
81 
85 void vpOpenCVGrabber::open()
86 {
87 
88  capture = cvCreateCameraCapture(DeviceType);
89 
90  if (capture != NULL)
91  {
92  init = true;
93  }
94 
95  else
96  {
97  close();
99  "Initialization not done : camera already used or no camera found") );
100  }
101 }
102 
103 
112 void vpOpenCVGrabber::open(vpImage<unsigned char> &/*I*/)
113 {
114  open();
115 }
116 
117 
126 void vpOpenCVGrabber::open(vpImage<vpRGBa> &/*I*/)
127 {
128  open();
129 }
130 
131 
140 void vpOpenCVGrabber::acquire(vpImage<unsigned char> &I)
141 {
142  IplImage *im;
143 
144  if (init==false)
145  {
146  close();
148  "Initialization not done") );
149  }
150 
151  cvGrabFrame(capture);
152  im = cvRetrieveFrame(capture);
153  vpImageConvert::convert(im, I, flip);
154 }
155 
164 void vpOpenCVGrabber::acquire(vpImage<vpRGBa> &I)
165 {
166  IplImage *im;
167 
168  if (init==false)
169  {
170  close();
172  "Initialization not done") );
173  }
174 
175  cvGrabFrame(capture);
176  im = cvRetrieveFrame(capture);
177  vpImageConvert::convert(im, I, flip);
178 }
179 
188 IplImage* vpOpenCVGrabber::acquire()
189 {
190  IplImage *im;
191 
192  if (init==false)
193  {
194  close();
196  "Initialization not done") );
197  }
198 
199  cvGrabFrame(capture);
200  im = cvRetrieveFrame(capture);
201  return im;
202 }
203 
207 void vpOpenCVGrabber::close()
208 {
209  init = false;
210  cvReleaseCapture( &capture );
211  capture = NULL;
212 }
213 
214 
221 void vpOpenCVGrabber::getFramerate(double & framerate)
222 {
223  framerate = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
224 }
225 
226 
233 void vpOpenCVGrabber::setFramerate(const double framerate)
234 {
235  cvSetCaptureProperty(capture, CV_CAP_PROP_FPS, framerate);
236 }
237 
238 
249 void vpOpenCVGrabber::setWidth(const unsigned int w)
250 {
251  if ( cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, w))
252  {
253  close();
254  vpERROR_TRACE("Impossible to set the size of the grabber");
256  "Impossible to set the size of the grabber") );
257  }
258 
259  this->width = w;
260 }
261 
272 void vpOpenCVGrabber::setHeight(const unsigned int h)
273 {
274  if ( cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, h))
275  {
276  close();
277  vpERROR_TRACE("Impossible to set the size of the grabber");
279  "Impossible to set the size of the grabber") );
280  }
281 
282  this->height = h;
283 }
284 
299 void vpOpenCVGrabber::setDeviceType(int type)
300 {
301  DeviceType = type;
302 
303  if ( DeviceType != 0 && DeviceType != 100 &&DeviceType != 200 && DeviceType != 300)
304  {
305  vpTRACE("The expected type of device may be unknown.");
306  }
307 }
308 
309 
319 void vpOpenCVGrabber::setFlip(bool flipType)
320 {
321  flip = flipType;
322 }
323 #endif
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
#define vpERROR_TRACE
Definition: vpDebug.h:395
#define vpTRACE
Definition: vpDebug.h:418
Error that can be emited by the vpFrameGrabber class and its derivates.