Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
vpOpenCVGrabber.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Cameras video capture using OpenCV library.
33  *
34  * Authors:
35  * Nicolas Melchior
36  *
37  *****************************************************************************/
38 
44 #include <visp3/sensor/vpOpenCVGrabber.h>
45 
46 #if (defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION < 0x020408))
47 
48 #include <visp3/core/vpFrameGrabberException.h>
49 #include <visp3/core/vpImageConvert.h>
50 
51 #include <iostream>
52 #include <math.h>
53 
57 vpOpenCVGrabber::vpOpenCVGrabber() : capture(NULL), DeviceType(0), flip(false)
58 {
59  // public memebers
60  init = false;
61 
62  // protected members
63  width = height = 0;
64 }
65 
71 vpOpenCVGrabber::~vpOpenCVGrabber() { close(); }
72 
76 void vpOpenCVGrabber::open()
77 {
78 
79  capture = cvCreateCameraCapture(DeviceType);
80 
81  if (capture != NULL) {
82  init = true;
83  }
84 
85  else {
86  close();
88  "Initialization not done : camera already used or no camera found"));
89  }
90 }
91 
100 void vpOpenCVGrabber::open(vpImage<unsigned char> & /*I*/) { open(); }
101 
110 void vpOpenCVGrabber::open(vpImage<vpRGBa> & /*I*/) { open(); }
111 
120 void vpOpenCVGrabber::acquire(vpImage<unsigned char> &I)
121 {
122  IplImage *im;
123 
124  if (init == false) {
125  close();
127  }
128 
129  cvGrabFrame(capture);
130  im = cvRetrieveFrame(capture);
131  vpImageConvert::convert(im, I, flip);
132 }
133 
142 void vpOpenCVGrabber::acquire(vpImage<vpRGBa> &I)
143 {
144  IplImage *im;
145 
146  if (init == false) {
147  close();
149  }
150 
151  cvGrabFrame(capture);
152  im = cvRetrieveFrame(capture);
153  vpImageConvert::convert(im, I, flip);
154 }
155 
164 IplImage *vpOpenCVGrabber::acquire()
165 {
166  IplImage *im;
167 
168  if (init == false) {
169  close();
171  }
172 
173  cvGrabFrame(capture);
174  im = cvRetrieveFrame(capture);
175  return im;
176 }
177 
181 void vpOpenCVGrabber::close()
182 {
183  init = false;
184  cvReleaseCapture(&capture);
185  capture = NULL;
186 }
187 
194 void vpOpenCVGrabber::getFramerate(double &framerate) { framerate = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS); }
195 
202 void vpOpenCVGrabber::setFramerate(const double framerate)
203 {
204  cvSetCaptureProperty(capture, CV_CAP_PROP_FPS, framerate);
205 }
206 
217 void vpOpenCVGrabber::setWidth(const unsigned int w)
218 {
219  if (cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, w)) {
220  close();
221  vpERROR_TRACE("Impossible to set the size of the grabber");
223  "Impossible to set the size of the grabber"));
224  }
225 
226  this->width = w;
227 }
228 
239 void vpOpenCVGrabber::setHeight(const unsigned int h)
240 {
241  if (cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, h)) {
242  close();
243  vpERROR_TRACE("Impossible to set the size of the grabber");
245  "Impossible to set the size of the grabber"));
246  }
247 
248  this->height = h;
249 }
250 
265 void vpOpenCVGrabber::setDeviceType(int type)
266 {
267  DeviceType = type;
268 
269  if (DeviceType != 0 && DeviceType != 100 && DeviceType != 200 && DeviceType != 300) {
270  vpTRACE("The expected type of device may be unknown.");
271  }
272 }
273 
285 void vpOpenCVGrabber::setFlip(bool flipType) { flip = flipType; }
286 
287 #elif !defined(VISP_BUILD_SHARED_LIBS)
288 // Work arround to avoid warning: libvisp_sensor.a(vpOpenCVGrabber.cpp.o) has
289 // no symbols
290 void dummy_vpOpenCVGrabber(){};
291 #endif
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
#define vpERROR_TRACE
Definition: vpDebug.h:393
Error that can be emited by the vpFrameGrabber class and its derivates.
#define vpTRACE
Definition: vpDebug.h:416
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.