ViSP
 All Classes Functions Variables Enumerations Enumerator Friends Groups Pages
vpOpenCVGrabber.cpp
1 /****************************************************************************
2  *
3  * $Id: vpOpenCVGrabber.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  * 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)
50 
51 #include <visp/vpImageConvert.h>
52 #include <visp/vpFrameGrabberException.h>
53 
54 #include <iostream>
55 #include <math.h>
56 
61 {
62  // public memebers
63  init = false;
64 
65  // protected members
66  width = height = 0;
67 
68  // private members
69  capture = NULL;
70  DeviceType = 0;
71  flip = false;
72 }
73 
74 
81 {
82  close();
83 }
84 
85 
90 {
91 
92  capture = cvCreateCameraCapture(DeviceType);
93 
94  if (capture != NULL)
95  {
96  init = true;
97  }
98 
99  else
100  {
101  close();
103  "Initialization not done : camera already used or no camera found") );
104  }
105 }
106 
107 
117 {
118  open();
119 }
120 
121 
131 {
132  open();
133 }
134 
135 
145 {
146  IplImage *im;
147 
148  if (init==false)
149  {
150  close();
152  "Initialization not done") );
153  }
154 
155  cvGrabFrame(capture);
156  im = cvRetrieveFrame(capture);
157  vpImageConvert::convert(im, I, flip);
158 }
159 
169 {
170  IplImage *im;
171 
172  if (init==false)
173  {
174  close();
176  "Initialization not done") );
177  }
178 
179  cvGrabFrame(capture);
180  im = cvRetrieveFrame(capture);
181  vpImageConvert::convert(im, I, flip);
182 }
183 
193 {
194  IplImage *im;
195 
196  if (init==false)
197  {
198  close();
200  "Initialization not done") );
201  }
202 
203  cvGrabFrame(capture);
204  im = cvRetrieveFrame(capture);
205  return im;
206 }
207 
212 {
213  init = false;
214  cvReleaseCapture( &capture );
215  capture = NULL;
216 }
217 
218 
225 void vpOpenCVGrabber::getFramerate(double & framerate)
226 {
227  framerate = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
228 }
229 
230 
237 void vpOpenCVGrabber::setFramerate(const double framerate)
238 {
239  cvSetCaptureProperty(capture, CV_CAP_PROP_FPS, framerate);
240 }
241 
242 
253 void vpOpenCVGrabber::setWidth(const unsigned int width)
254 {
255  if ( cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, width))
256  {
257  close();
258  vpERROR_TRACE("Impossible to set the size of the grabber");
260  "Impossible to set the size of the grabber") );
261  }
262 
263  this->width = width;
264 }
265 
276 void vpOpenCVGrabber::setHeight(const unsigned int height)
277 {
278  if ( cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, height))
279  {
280  close();
281  vpERROR_TRACE("Impossible to set the size of the grabber");
283  "Impossible to set the size of the grabber") );
284  }
285 
286  this->height = height;
287 }
288 
304 {
305  DeviceType = type;
306 
307  if ( DeviceType != 0 && DeviceType != 100 &&DeviceType != 200 && DeviceType != 300)
308  {
309  vpTRACE("The expected type of device may be unknown.");
310  }
311 }
312 
313 
323 void vpOpenCVGrabber::setFlip(bool flipType)
324 {
325  flip = flipType;
326 }
327 #endif