Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpOpenCVGrabber.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Cameras video capture using OpenCV library.
32  *
33  * Authors:
34  * Nicolas Melchior
35  *
36  *****************************************************************************/
37 
43 #include <visp3/sensor/vpOpenCVGrabber.h>
44 
45 #if ( defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION < 0x020408))
46 
47 #include <visp3/core/vpImageConvert.h>
48 #include <visp3/core/vpFrameGrabberException.h>
49 
50 #include <iostream>
51 #include <math.h>
52 
56 vpOpenCVGrabber::vpOpenCVGrabber()
57  : capture(NULL), DeviceType(0), flip(false)
58 {
59  // public memebers
60  init = false;
61 
62  // protected members
63  width = height = 0;
64 }
65 
66 
72 vpOpenCVGrabber::~vpOpenCVGrabber( )
73 {
74  close();
75 }
76 
77 
81 void vpOpenCVGrabber::open()
82 {
83 
84  capture = cvCreateCameraCapture(DeviceType);
85 
86  if (capture != NULL)
87  {
88  init = true;
89  }
90 
91  else
92  {
93  close();
95  "Initialization not done : camera already used or no camera found") );
96  }
97 }
98 
99 
108 void vpOpenCVGrabber::open(vpImage<unsigned char> &/*I*/)
109 {
110  open();
111 }
112 
113 
122 void vpOpenCVGrabber::open(vpImage<vpRGBa> &/*I*/)
123 {
124  open();
125 }
126 
127 
136 void vpOpenCVGrabber::acquire(vpImage<unsigned char> &I)
137 {
138  IplImage *im;
139 
140  if (init==false)
141  {
142  close();
144  "Initialization not done") );
145  }
146 
147  cvGrabFrame(capture);
148  im = cvRetrieveFrame(capture);
149  vpImageConvert::convert(im, I, flip);
150 }
151 
160 void vpOpenCVGrabber::acquire(vpImage<vpRGBa> &I)
161 {
162  IplImage *im;
163 
164  if (init==false)
165  {
166  close();
168  "Initialization not done") );
169  }
170 
171  cvGrabFrame(capture);
172  im = cvRetrieveFrame(capture);
173  vpImageConvert::convert(im, I, flip);
174 }
175 
184 IplImage* vpOpenCVGrabber::acquire()
185 {
186  IplImage *im;
187 
188  if (init==false)
189  {
190  close();
192  "Initialization not done") );
193  }
194 
195  cvGrabFrame(capture);
196  im = cvRetrieveFrame(capture);
197  return im;
198 }
199 
203 void vpOpenCVGrabber::close()
204 {
205  init = false;
206  cvReleaseCapture( &capture );
207  capture = NULL;
208 }
209 
210 
217 void vpOpenCVGrabber::getFramerate(double & framerate)
218 {
219  framerate = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
220 }
221 
222 
229 void vpOpenCVGrabber::setFramerate(const double framerate)
230 {
231  cvSetCaptureProperty(capture, CV_CAP_PROP_FPS, framerate);
232 }
233 
234 
245 void vpOpenCVGrabber::setWidth(const unsigned int w)
246 {
247  if ( cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, w))
248  {
249  close();
250  vpERROR_TRACE("Impossible to set the size of the grabber");
252  "Impossible to set the size of the grabber") );
253  }
254 
255  this->width = w;
256 }
257 
268 void vpOpenCVGrabber::setHeight(const unsigned int h)
269 {
270  if ( cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, h))
271  {
272  close();
273  vpERROR_TRACE("Impossible to set the size of the grabber");
275  "Impossible to set the size of the grabber") );
276  }
277 
278  this->height = h;
279 }
280 
295 void vpOpenCVGrabber::setDeviceType(int type)
296 {
297  DeviceType = type;
298 
299  if ( DeviceType != 0 && DeviceType != 100 &&DeviceType != 200 && DeviceType != 300)
300  {
301  vpTRACE("The expected type of device may be unknown.");
302  }
303 }
304 
305 
315 void vpOpenCVGrabber::setFlip(bool flipType)
316 {
317  flip = flipType;
318 }
319 
320 #elif !defined(VISP_BUILD_SHARED_LIBS)
321 // Work arround to avoid warning: libvisp_sensor.a(vpOpenCVGrabber.cpp.o) has no symbols
322 void dummy_vpOpenCVGrabber() {};
323 #endif
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
#define vpERROR_TRACE
Definition: vpDebug.h:391
Error that can be emited by the vpFrameGrabber class and its derivates.
#define vpTRACE
Definition: vpDebug.h:414
unsigned int height
Number of rows in the image.
bool init
Set to true if the frame grabber has been initialized.
unsigned int width
Number of columns in the image.