MRPT  1.9.9
CImageGrabber_OpenCV.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "hwdrivers-precomp.h" // Precompiled headers
11 
12 #include <mrpt/3rdparty/do_opencv_includes.h>
14 #include <thread>
15 
16 #ifdef HAVE_OPENCV_VIDEOIO
17 // cv::VideoCapture moved from highgui in opencv2 to videoio in opencv3:
18 #include <opencv2/videoio.hpp>
19 #endif
20 
21 using namespace std;
22 using namespace mrpt;
23 using namespace mrpt::hwdrivers;
24 
26 {
27 #if MRPT_HAS_OPENCV
28  cv::VideoCapture cap;
29 #endif
30 };
31 
32 /*-------------------------------------------------------------
33  Constructor
34 -------------------------------------------------------------*/
35 CImageGrabber_OpenCV::CImageGrabber_OpenCV(
36  int cameraIndex, TCameraType cameraType, const TCaptureCVOptions& options)
37  : m_capture(mrpt::make_impl<CImageGrabber_OpenCV::Impl>())
38 {
40  m_bInitialized = false;
41 
42 #if MRPT_HAS_OPENCV
43  int cv_cap_indx = 0;
44  switch (cameraType)
45  {
47  cv_cap_indx = CV_CAP_ANY;
48  break;
49  case CAMERA_CV_DC1394:
50  cv_cap_indx = CV_CAP_DC1394;
51  break;
52  case CAMERA_CV_VFL:
53  cv_cap_indx = CV_CAP_V4L;
54  break;
55  case CAMERA_CV_VFW:
56  cv_cap_indx = CV_CAP_VFW;
57  break;
58  case CAMERA_CV_MIL:
59  cv_cap_indx = CV_CAP_MIL;
60  break;
61  case CAMERA_CV_DSHOW:
62  cv_cap_indx = CV_CAP_DSHOW;
63  break;
64  default:
65  THROW_EXCEPTION_FMT("Invalid camera type: %i", cameraType);
66  }
67 
68  cv_cap_indx += cameraIndex;
69 
70  // Open camera:
71  if (!m_capture->cap.open(cv_cap_indx))
72  {
73  cerr << format(
74  "[CImageGrabber_OpenCV] ERROR: Can't open camera '%i'!!\n",
75  cameraIndex);
76  return;
77  }
78 
79  // Set properties:
80  // Based on code from Orocos project. Thanks!
81  // ----------------------------------------
82  // Global settings
83  if (options.gain != 0)
84  {
85  if (!m_capture->cap.set(CV_CAP_PROP_GAIN, options.gain))
86  cerr << "[CImageGrabber_OpenCV] Warning: Could not set the "
87  "capturing gain property!"
88  << 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  {
100  MY_MODE_160x120_YUV444 = 64,
101  MY_MODE_320x240_YUV422, // ***
102  MY_MODE_640x480_YUV411,
103  MY_MODE_640x480_YUV422, // ***
104  MY_MODE_640x480_RGB, // ?
105  MY_MODE_640x480_MONO, // ***
106  MY_MODE_640x480_MONO16
107  };
108 
109  int cvMode1394 = -1;
110  if (options.frame_height == 320 && options.frame_width == 240)
111  cvMode1394 = MY_MODE_320x240_YUV422;
112  else if (
113  options.frame_height == 640 && options.frame_width == 480 &&
114  !options.ieee1394_grayscale)
115  cvMode1394 = MY_MODE_640x480_YUV422;
116  else if (
117  options.frame_height == 640 && options.frame_width == 480 &&
118  options.ieee1394_grayscale)
119  cvMode1394 = MY_MODE_640x480_MONO;
120 
121  if (cvMode1394 > 0)
122  {
123  if (!m_capture->cap.set(CV_CAP_PROP_MODE, cvMode1394))
124  cerr << "[CImageGrabber_OpenCV] Warning: Could not set the "
125  "capturing mode "
126  << cvMode1394 << " property!" << endl;
127  }
128  else
129  cerr << "[CImageGrabber_OpenCV] Warning: Not valid combination "
130  "of width x height x color mode for OpenCV/IEEE1394 "
131  "interface"
132  << endl;
133  }
134 
135  // Not needed: Default seems to be = 1
136  // if(cvSetCaptureProperty(M_CAPTURE,CV_CAP_PROP_CONVERT_RGB,_capture_convert.value())<1)
137  // cerr << "[CImageGrabber_OpenCV] Warning: Could not set the RGB
138  // conversion property!" << endl;
139 
140  if (!m_capture->cap.set(CV_CAP_PROP_FPS, options.ieee1394_fps))
141  cerr << "[CImageGrabber_OpenCV] Warning: Could not set the fps "
142  "property!"
143  << endl;
144  }
145 
146  // Settings only for V4L
147  if (cameraType == CAMERA_CV_AUTODETECT || cameraType == CAMERA_CV_VFL ||
148  cameraType == CAMERA_CV_VFW || cameraType == CAMERA_CV_DSHOW)
149  {
150  if (options.frame_width != 0 && options.frame_height != 0)
151  {
152  // First set width then height. The first command always returns a
153  // error!
154  m_capture->cap.set(CV_CAP_PROP_FRAME_WIDTH, options.frame_width);
155  if (!m_capture->cap.set(
156  CV_CAP_PROP_FRAME_HEIGHT, options.frame_height))
157  cerr << "[CImageGrabber_OpenCV] Warning: Could not set the "
158  "frame width & height property!"
159  << endl;
160  }
161  }
162 
163  // remember that we successfully initialized everything
164  m_bInitialized = true;
165 #else
166  THROW_EXCEPTION("The MRPT has been compiled with MRPT_HAS_OPENCV=0 !");
167 #endif
168  MRPT_END
169 }
170 
171 CImageGrabber_OpenCV::CImageGrabber_OpenCV(const std::string& AVI_fileName)
172  : m_capture(mrpt::make_impl<CImageGrabber_OpenCV::Impl>())
173 {
174  MRPT_START
175  m_bInitialized = false;
176 
177 #if MRPT_HAS_OPENCV
178  if (!m_capture->cap.open(AVI_fileName))
179  {
180  printf(
181  "[CImageGrabber_OpenCV] Warning! Can't open AVI file '%s'!!\n",
182  AVI_fileName.c_str());
183  return;
184  }
185 
186  // remember that we successfully initialized everything
187  m_bInitialized = true;
188 #else
189  THROW_EXCEPTION("The MRPT has been compiled with MRPT_HAS_OPENCV=0 !");
190 #endif
191  MRPT_END
192 }
193 
194 /*-------------------------------------------------------------
195  Destructor
196  -------------------------------------------------------------*/
198 {
199 #if MRPT_HAS_OPENCV
200  if (m_bInitialized)
201  {
202  m_capture->cap.release();
203  }
204 #endif
205 }
206 
207 /*-------------------------------------------------------------
208  get the image
209  -------------------------------------------------------------*/
211  mrpt::obs::CObservationImage& out_observation)
212 {
213  MRPT_START
214 
215  if (!m_bInitialized) return false;
216 
217 #if MRPT_HAS_OPENCV
218 
219  // Capture the image:
220  if (!m_capture->cap.grab()) return false;
221 
222  // JL: Sometimes there're errors in some frames: try not to return an error
223  // unless it seems
224  // there's no way:
225  for (int nTries = 0; nTries < 10; nTries++)
226  {
227  cv::Mat capImg;
228  if (m_capture->cap.retrieve(capImg))
229  {
230  // Fill the output class:
231  out_observation.timestamp = mrpt::system::now();
232  out_observation.image =
234  return true;
235  }
236  cerr << "[CImageGrabber_OpenCV] WARNING: Ignoring error #" << nTries + 1
237  << " retrieving frame..." << endl;
238  std::this_thread::sleep_for(1ms);
239  }
240  return false;
241 #else
242  THROW_EXCEPTION("The MRPT has been compiled with MRPT_HAS_OPENCV=0 !");
243 #endif
244  MRPT_END
245 }
Shallow copy: the copied object is a reference to the original one.
Definition: img/CImage.h:74
Declares a class derived from "CObservation" that encapsules an image from a camera, whose relative pose to robot is also stored.
#define MRPT_START
Definition: exceptions.h:241
TCameraType
These capture types are like their OpenCV equivalents.
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
Definition: datetime.h:86
Contains classes for various device interfaces.
CImageGrabber_OpenCV(int cameraIndex=-1, TCameraType cameraType=CAMERA_CV_AUTODETECT, const TCaptureCVOptions &options=TCaptureCVOptions())
Constructor for cameras:
STL namespace.
bool ieee1394_grayscale
(IEEE1394 cameras) Whether to grab grayscale images (Default=false).
mrpt::img::CImage CImage
Definition: utils/CImage.h:5
mrpt::img::CImage image
The image captured by the camera, that is, the main piece of information of this observation.
int frame_width
(All cameras) Capture resolution (0: Leave the default)
double ieee1394_fps
(IEEE1394 cameras) Frame rate for the capture (0: Leave the default).
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
mrpt::system::TTimeStamp timestamp
The associated UTC time-stamp.
Definition: CObservation.h:60
#define MRPT_END
Definition: exceptions.h:245
A class for grabing images from a "OpenCV"-compatible camera, or from an AVI video file...
pimpl< T > make_impl(Args &&... args)
Definition: pimpl.h:18
double gain
(All cameras) Camera gain (0: Leave the default)
bool getObservation(mrpt::obs::CObservationImage &out_observation)
Grab an image from the opened camera.
Options used when creating an OpenCV capture object Some options apply to IEEE1394 cameras only...
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:69
bool m_bInitialized
Set to false if we could not initialize the camera.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7e629e01a Sat Dec 14 00:05:55 2019 +0100 at sáb dic 14 00:15:10 CET 2019