Main MRPT website > C++ reference for MRPT 1.5.9
CImageGrabber_OpenCV.cpp
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 
10 #include "hwdrivers-precomp.h" // Precompiled headers
11 
12 #include <mrpt/system/threads.h>
14 
15 #include <mrpt/otherlibs/do_opencv_includes.h>
16 
17 #define M_CAPTURE (static_cast<CvCapture*>(m_capture.get()))
18 
19 using namespace std;
20 using namespace mrpt;
21 using namespace mrpt::hwdrivers;
22 
23 /*-------------------------------------------------------------
24  Constructor
25  -------------------------------------------------------------*/
26 CImageGrabber_OpenCV::CImageGrabber_OpenCV(
27  int cameraIndex,
28  TCameraType cameraType,
29  const TCaptureCVOptions &options
30  )
31 {
33  m_bInitialized = false;
34 
35 #if MRPT_HAS_OPENCV
36  int cv_cap_indx = 0;
37  switch (cameraType)
38  {
39  case CAMERA_CV_AUTODETECT: cv_cap_indx = CV_CAP_ANY; break;
40  case CAMERA_CV_DC1394: cv_cap_indx = CV_CAP_DC1394; break;
41  case CAMERA_CV_VFL: cv_cap_indx = CV_CAP_V4L; break;
42  case CAMERA_CV_VFW: cv_cap_indx = CV_CAP_VFW; break;
43  case CAMERA_CV_MIL: cv_cap_indx = CV_CAP_MIL; break;
44 #if MRPT_OPENCV_VERSION_NUM >= 0x111
45  case CAMERA_CV_DSHOW: cv_cap_indx = CV_CAP_DSHOW; break;
46  // *** HAVE YOU HAD A COMPILER ERROR NEAR THIS LINE?? : You need OpenCV >=1.1.1, (2.0 final release) or a SVN version ***
47 #endif
48  default: THROW_EXCEPTION_FMT("Invalid camera type: %i", cameraType);
49  }
50 
51  cv_cap_indx += cameraIndex;
52 
53  m_capture = cvCaptureFromCAM( cv_cap_indx );
54 
55  // If we have OPENCV>=1.1.1, we can determine the type of the capturer
56  // if it was CAMERA_CV_AUTODETECT
57 #if MRPT_OPENCV_VERSION_NUM >= 0x111
58  if (cameraType==CAMERA_CV_AUTODETECT)
59  {
60  cv_cap_indx = cvGetCaptureDomain(M_CAPTURE);
61  // *** HAVE YOU HAD A COMPILER ERROR NEAR THIS LINE?? : You need OpenCV >=1.1.0, (2.0 final release) or a SVN version ***
62  switch (cv_cap_indx)
63  {
64  case CV_CAP_ANY: cameraType = CAMERA_CV_AUTODETECT; break;
65  case CV_CAP_DC1394: cameraType = CAMERA_CV_DC1394; break;
66  //case CV_CAP_V4L:
67  case CV_CAP_VFW: cameraType = CAMERA_CV_VFW; break;
68  case CV_CAP_MIL: cameraType = CAMERA_CV_MIL; break;
69  case CV_CAP_DSHOW: cameraType = CAMERA_CV_DSHOW; break;
70  default: THROW_EXCEPTION_FMT("Invalid camera type: %i", cameraType);
71  }
72  }
73 #endif
74 
75  if (!m_capture.get())
76  {
77  cerr << format("[CImageGrabber_OpenCV] ERROR: Can't open camera '%i'!!\n", cameraIndex);
78  return;
79  }
80 
81  // Set properties:
82  // Based on code from Orocos project. Thanks!
83  // ----------------------------------------
84  // Global settings
85  if (options.gain!=0)
86  {
87  if(cvSetCaptureProperty(M_CAPTURE,CV_CAP_PROP_GAIN,options.gain)<1)
88  cerr << "[CImageGrabber_OpenCV] Warning: Could not set the capturing gain property!" << endl;
89  }
90 
91  // Settings only for firewire
92  if(cameraType==CAMERA_CV_DC1394)
93  {
94  if (options.frame_height!=0 && options.frame_width!=0)
95  {
96  //MODE_320x240_YUV422 ****
97  //
98  enum {
99  MY_MODE_160x120_YUV444= 64,
100  MY_MODE_320x240_YUV422, // ***
101  MY_MODE_640x480_YUV411,
102  MY_MODE_640x480_YUV422, // ***
103  MY_MODE_640x480_RGB, // ?
104  MY_MODE_640x480_MONO, // ***
105  MY_MODE_640x480_MONO16
106  };
107 
108  int cvMode1394=-1;
109  if (options.frame_height==320 && options.frame_width==240)
110  cvMode1394 = MY_MODE_320x240_YUV422;
111  else if (options.frame_height==640 && options.frame_width==480 && !options.ieee1394_grayscale)
112  cvMode1394 = MY_MODE_640x480_YUV422;
113  else if (options.frame_height==640 && options.frame_width==480 && options.ieee1394_grayscale)
114  cvMode1394 = MY_MODE_640x480_MONO;
115 
116  if (cvMode1394>0)
117  {
118  if(cvSetCaptureProperty(M_CAPTURE,CV_CAP_PROP_MODE,cvMode1394)<1)
119  cerr << "[CImageGrabber_OpenCV] Warning: Could not set the capturing mode "<< cvMode1394 << " property!" << endl;
120  }
121  else cerr << "[CImageGrabber_OpenCV] Warning: Not valid combination of width x height x color mode for OpenCV/IEEE1394 interface" << endl;
122  }
123 
124  // Not needed: Default seems to be = 1
125  //if(cvSetCaptureProperty(M_CAPTURE,CV_CAP_PROP_CONVERT_RGB,_capture_convert.value())<1)
126  // cerr << "[CImageGrabber_OpenCV] Warning: Could not set the RGB conversion property!" << endl;
127 
128  if(cvSetCaptureProperty(M_CAPTURE,CV_CAP_PROP_FPS, options.ieee1394_fps )<1)
129  cerr << "[CImageGrabber_OpenCV] Warning: Could not set the fps property!" << endl;
130  }
131 
132  // Settings only for V4L
133  if(cameraType == CAMERA_CV_AUTODETECT || cameraType == CAMERA_CV_VFL || cameraType == CAMERA_CV_VFW || cameraType == CAMERA_CV_DSHOW )
134  {
135  if (options.frame_width!=0 && options.frame_height!=0)
136  {
137  // First set width then height. The first command always returns a error!
138  cvSetCaptureProperty(M_CAPTURE,CV_CAP_PROP_FRAME_WIDTH,options.frame_width);
139  if(cvSetCaptureProperty(M_CAPTURE,CV_CAP_PROP_FRAME_HEIGHT,options.frame_height)<1)
140  cerr << "[CImageGrabber_OpenCV] Warning: Could not set the frame width & height property!" << endl;
141  }
142  }
143 
144  // remember that we successfully initialized everything
145  m_bInitialized = true;
146 #else
147  THROW_EXCEPTION("The MRPT has been compiled with MRPT_HAS_OPENCV=0 !");
148 #endif
149  MRPT_END
150 }
151 
152 /*-------------------------------------------------------------
153  Constructor
154  -------------------------------------------------------------*/
155 CImageGrabber_OpenCV::CImageGrabber_OpenCV( const std::string &AVI_fileName )
156 {
157  MRPT_START
158  m_bInitialized = false;
159 
160 #if MRPT_HAS_OPENCV
161  m_bInitialized = false;
162 
163  m_capture = cvCaptureFromAVI( AVI_fileName.c_str() );
164 
165  if (!m_capture.get())
166  {
167  printf("[CImageGrabber_OpenCV] Warning! Can't open AVI file '%s'!!\n", AVI_fileName.c_str());
168  return;
169  }
170 
171  // remember that we successfully initialized everything
172  m_bInitialized = true;
173 #else
174  THROW_EXCEPTION("The MRPT has been compiled with MRPT_HAS_OPENCV=0 !");
175 #endif
176  MRPT_END
177 }
178 
179 
180 /*-------------------------------------------------------------
181  Destructor
182  -------------------------------------------------------------*/
183 CImageGrabber_OpenCV::~CImageGrabber_OpenCV()
184 {
185 #if MRPT_HAS_OPENCV
186  if ( m_bInitialized )
187  {
188  CvCapture *cap = M_CAPTURE;
189  cvReleaseCapture( &cap );
190  m_capture = NULL;
191  }
192 #endif
193 }
194 
195 
196 /*-------------------------------------------------------------
197  get the image
198  -------------------------------------------------------------*/
200 {
201  MRPT_START
202 
203  if (!m_bInitialized) return false;
204 
205 #if MRPT_HAS_OPENCV
206 
207  // Capture the image:
208  if (!cvGrabFrame(M_CAPTURE))
209  return false;
210 
211  IplImage *capImg = NULL;
212 
213  // JL: Sometimes there're errors in some frames: try not to return an error unless it seems
214  // there's no way:
215  for (int nTries=0;nTries<10;nTries++)
216  {
217  capImg = cvRetrieveFrame(M_CAPTURE);
218  if (capImg) break;
219  cerr << "[CImageGrabber_OpenCV] WARNING: Ignoring error #" <<nTries+1 << " retrieving frame..." << endl;
221  }
222 
223  if(!capImg) return false;
224 
225  // Fill the output class:
226  out_observation.image.setFromIplImageReadOnly( capImg );
227  out_observation.timestamp = mrpt::system::now();
228 
229  return true;
230 #else
231  THROW_EXCEPTION("The MRPT has been compiled with MRPT_HAS_OPENCV=0 !");
232 #endif
233  MRPT_END
234 }
Declares a class derived from "CObservation" that encapsules an image from a camera, whose relative pose to robot is also stored.
TCameraType
These capture types are like their OpenCV equivalents.
OBSERVATION_T::Ptr getObservation(mrpt::obs::CSensoryFramePtr &observations, mrpt::obs::CObservationPtr &observation, bool priority_to_sf=true)
Given an mrpt::obs::CSensoryFrame and a mrpt::obs::CObservation pointer if a OBSERVATION_T type obser...
Definition: obs_utils.h:32
#define THROW_EXCEPTION(msg)
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
Definition: datetime.h:70
Contains classes for various device interfaces.
STL namespace.
GLenum cap
Definition: glext.h:6324
bool ieee1394_grayscale
(IEEE1394 cameras) Whether to grab grayscale images (Default=false).
#define MRPT_END
void BASE_IMPEXP sleep(int time_ms) MRPT_NO_THROWS
An OS-independent method for sending the current thread to "sleep" for a given period of time...
Definition: threads.cpp:57
double ieee1394_fps
(IEEE1394 cameras) Frame rate for the capture (0: Leave the default).
void setFromIplImageReadOnly(void *iplImage)
Reads the image from a OpenCV IplImage object (WITHOUT making a copy) and from now on the image canno...
Definition: CImage.cpp:343
GLsizei const GLchar ** string
Definition: glext.h:3919
#define MRPT_START
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
mrpt::system::TTimeStamp timestamp
The associated UTC time-stamp. Where available, this should contain the accurate satellite-based time...
int frame_height
(All cameras) Capture resolution (0: Leave the default)
Valid only with OpenCV >= 1.1.0.
mrpt::utils::CImage image
The image captured by the camera, that is, the main piece of information of this observation.
double gain
(All cameras) Camera gain (0: Leave the default)
GLenum GLsizei GLenum format
Definition: glext.h:3513
#define M_CAPTURE
Options used when creating an OpenCV capture object Some options apply to IEEE1394 cameras only...



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020