14 #include <mrpt/otherlibs/do_opencv_includes.h>
18 #define M_CAPTURE (static_cast<CvCapture*>(m_capture.get()))
27 CImageGrabber_OpenCV::CImageGrabber_OpenCV(
31 m_bInitialized =
false;
38 cv_cap_indx = CV_CAP_ANY;
41 cv_cap_indx = CV_CAP_DC1394;
44 cv_cap_indx = CV_CAP_V4L;
47 cv_cap_indx = CV_CAP_VFW;
50 cv_cap_indx = CV_CAP_MIL;
52 #if MRPT_OPENCV_VERSION_NUM >= 0x111
54 cv_cap_indx = CV_CAP_DSHOW;
63 cv_cap_indx += cameraIndex;
65 m_capture = cvCaptureFromCAM(cv_cap_indx);
69 #if MRPT_OPENCV_VERSION_NUM >= 0x111
72 cv_cap_indx = cvGetCaptureDomain(
M_CAPTURE);
102 "[CImageGrabber_OpenCV] ERROR: Can't open camera '%i'!!\n",
111 if (options.
gain != 0)
113 if (cvSetCaptureProperty(
M_CAPTURE, CV_CAP_PROP_GAIN, options.
gain) < 1)
114 cerr <<
"[CImageGrabber_OpenCV] Warning: Could not set the "
115 "capturing gain property!"
128 MY_MODE_160x120_YUV444 = 64,
129 MY_MODE_320x240_YUV422,
130 MY_MODE_640x480_YUV411,
131 MY_MODE_640x480_YUV422,
133 MY_MODE_640x480_MONO,
134 MY_MODE_640x480_MONO16
139 cvMode1394 = MY_MODE_320x240_YUV422;
143 cvMode1394 = MY_MODE_640x480_YUV422;
147 cvMode1394 = MY_MODE_640x480_MONO;
151 if (cvSetCaptureProperty(
152 M_CAPTURE, CV_CAP_PROP_MODE, cvMode1394) < 1)
153 cerr <<
"[CImageGrabber_OpenCV] Warning: Could not set the "
155 << cvMode1394 <<
" property!" << endl;
158 cerr <<
"[CImageGrabber_OpenCV] Warning: Not valid combination "
159 "of width x height x color mode for OpenCV/IEEE1394 "
169 if (cvSetCaptureProperty(
171 cerr <<
"[CImageGrabber_OpenCV] Warning: Could not set the fps "
184 cvSetCaptureProperty(
186 if (cvSetCaptureProperty(
189 cerr <<
"[CImageGrabber_OpenCV] Warning: Could not set the "
190 "frame width & height property!"
196 m_bInitialized =
true;
198 THROW_EXCEPTION(
"The MRPT has been compiled with MRPT_HAS_OPENCV=0 !");
206 CImageGrabber_OpenCV::CImageGrabber_OpenCV(
const std::string& AVI_fileName)
209 m_bInitialized =
false;
212 m_bInitialized =
false;
214 m_capture = cvCaptureFromAVI(AVI_fileName.c_str());
216 if (!m_capture.get())
219 "[CImageGrabber_OpenCV] Warning! Can't open AVI file '%s'!!\n",
220 AVI_fileName.c_str());
225 m_bInitialized =
true;
227 THROW_EXCEPTION(
"The MRPT has been compiled with MRPT_HAS_OPENCV=0 !");
235 CImageGrabber_OpenCV::~CImageGrabber_OpenCV()
241 cvReleaseCapture(&
cap);
255 if (!m_bInitialized)
return false;
260 if (!cvGrabFrame(
M_CAPTURE))
return false;
262 IplImage* capImg =
nullptr;
267 for (
int nTries = 0; nTries < 10; nTries++)
271 cerr <<
"[CImageGrabber_OpenCV] WARNING: Ignoring error #" << nTries + 1
272 <<
" retrieving frame..." << endl;
273 std::this_thread::sleep_for(1ms);
276 if (!capImg)
return false;
284 THROW_EXCEPTION(
"The MRPT has been compiled with MRPT_HAS_OPENCV=0 !");