CameraVideo.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the Universite de Sherbrooke nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
29 #include <rtabmap/utilite/UTimer.h>
31 #if CV_MAJOR_VERSION > 3
32 #include <opencv2/videoio/videoio_c.h>
33 #if CV_MAJOR_VERSION > 4
34 #include <opencv2/videoio/legacy/constants_c.h>
35 #endif
36 #endif
37 
38 namespace rtabmap
39 {
40 
42  int usbDevice,
43  bool rectifyImages,
44  float imageRate,
45  const Transform & localTransform) :
46  Camera(imageRate, localTransform),
47  _rectifyImages(rectifyImages),
48  _src(kUsbDevice),
49  _usbDevice(usbDevice),
50  _width(0),
51  _height(0)
52 {
53 
54 }
55 
57  const std::string & filePath,
58  bool rectifyImages,
59  float imageRate,
60  const Transform & localTransform) :
61  Camera(imageRate, localTransform),
62  _filePath(filePath),
63  _rectifyImages(rectifyImages),
64  _src(kVideoFile),
65  _usbDevice(0),
66  _width(0),
67  _height(0)
68 {
69 }
70 
72 {
73  _capture.release();
74 }
75 
76 bool CameraVideo::init(const std::string & calibrationFolder, const std::string & cameraName)
77 {
78  _guid = cameraName;
79  if(_capture.isOpened())
80  {
81  _capture.release();
82  }
83 
84  if(_src == kUsbDevice)
85  {
86  ULOGGER_DEBUG("CameraVideo::init() Usb device initialization on device %d", _usbDevice);
87  _capture.open(_usbDevice);
88  }
89  else if(_src == kVideoFile)
90  {
91  ULOGGER_DEBUG("Camera: filename=\"%s\"", _filePath.c_str());
92  _capture.open(_filePath.c_str());
93  }
94  else
95  {
96  ULOGGER_ERROR("Camera: Unknown source...");
97  }
98  if(!_capture.isOpened())
99  {
100  ULOGGER_ERROR("Camera: Failed to create a capture object!");
101  _capture.release();
102  return false;
103  }
104  else
105  {
106  if (_guid.empty())
107  {
108  unsigned int guid = (unsigned int)_capture.get(CV_CAP_PROP_GUID);
109  if (guid != 0 && guid != 0xffffffff)
110  {
111  _guid = uFormat("%08x", guid);
112  }
113  }
114 
115  // look for calibration files
116  if(!calibrationFolder.empty() && !_guid.empty())
117  {
118  if(!_model.load(calibrationFolder, _guid))
119  {
120  UWARN("Missing calibration files for camera \"%s\" in \"%s\" folder, you should calibrate the camera!",
121  _guid.c_str(), calibrationFolder.c_str());
122  }
123  else
124  {
125  UINFO("Camera parameters: fx=%f fy=%f cx=%f cy=%f",
126  _model.fx(),
127  _model.fy(),
128  _model.cx(),
129  _model.cy());
130  }
131  }
133  if(_src == kUsbDevice)
134  {
136  {
137  if(_width > 0 && _height > 0 && (_width!=_model.imageWidth() || _height != _model.imageHeight()))
138  {
139  UWARN("Desired resolution of %dx%d is set but calibration has "
140  "been loaded with resolution %dx%d, using calibration resolution.",
141  _width, _height,
143  }
144 
145  bool resolutionSet = false;
146  resolutionSet = _capture.set(CV_CAP_PROP_FRAME_WIDTH, _model.imageWidth());
147  resolutionSet = resolutionSet && _capture.set(CV_CAP_PROP_FRAME_HEIGHT, _model.imageHeight());
148 
149  // Check if the resolution was set successfully
150  int actualWidth = int(_capture.get(CV_CAP_PROP_FRAME_WIDTH));
151  int actualHeight = int(_capture.get(CV_CAP_PROP_FRAME_HEIGHT));
152  if(!resolutionSet ||
153  actualWidth != _model.imageWidth() ||
154  actualHeight != _model.imageHeight())
155  {
156  UERROR("Calibration resolution (%dx%d) cannot be set to camera driver, "
157  "actual resolution is %dx%d. You would have to re-calibrate with one "
158  "supported format by your camera. "
159  "Do \"v4l2-ctl --list-formats-ext\" to list all supported "
160  "formats by your camera.",
162  actualWidth, actualHeight);
163  }
164  }
165  else if(_width > 0 && _height > 0)
166  {
167  int resolutionSet = false;
168  resolutionSet = _capture.set(CV_CAP_PROP_FRAME_WIDTH, _width);
169  resolutionSet = resolutionSet && _capture.set(CV_CAP_PROP_FRAME_HEIGHT, _height);
170 
171  // Check if the resolution was set successfully
172  int actualWidth = int(_capture.get(CV_CAP_PROP_FRAME_WIDTH));
173  int actualHeight = int(_capture.get(CV_CAP_PROP_FRAME_HEIGHT));
174  if(!resolutionSet || actualWidth != _width || actualHeight != _height)
175  {
176  UWARN("Desired resolution (%dx%d) cannot be set to camera driver, "
177  "actual resolution is %dx%d. "
178  "Do \"v4l2-ctl --list-formats-ext\" to list all supported "
179  "formats by your camera.",
180  _width, _height, actualWidth, actualHeight);
181  }
182  }
183 
184  // Set FPS
185  if (this->getFrameRate() > 0 && _capture.set(CV_CAP_PROP_FPS, this->getFrameRate()))
186  {
187  // Check if the FPS was set successfully
188  double actualFPS = _capture.get(cv::CAP_PROP_FPS);
189 
190  if(fabs(actualFPS - this->getFrameRate()) < 0.01)
191  {
192  this->setFrameRate(0);
193  }
194  else
195  {
196  UWARN("Desired FPS (%f Hz) cannot be set to camera driver, "
197  "actual FPS is %f Hz. We will throttle to lowest FPS. "
198  "Do \"v4l2-ctl --list-formats-ext\" to list all supported "
199  "formats by your camera.",
200  this->getFrameRate(), actualFPS);
201  if(this->getFrameRate() > actualFPS)
202  {
203  this->setFrameRate(0);
204  }
205  }
206  }
207 
208  // Set FOURCC
209  if (!_fourcc.empty())
210  {
211  if(_fourcc.size() == 4)
212  {
213  std::string fourccUpperCase = uToUpperCase(_fourcc);
214  int fourcc = cv::VideoWriter::fourcc(fourccUpperCase.at(0), fourccUpperCase.at(1), fourccUpperCase.at(2), fourccUpperCase.at(3));
215 
216  bool fourccSupported = _capture.set(CV_CAP_PROP_FOURCC, fourcc);
217 
218  // Check if the FOURCC was set successfully
219  int actualFourcc = int(_capture.get(CV_CAP_PROP_FOURCC));
220 
221  if(!fourccSupported || actualFourcc != fourcc)
222  {
223  UWARN("Camera doesn't support provided FOURCC \"%s\". "
224  "Do \"v4l2-ctl --list-formats-ext\" to list all supported "
225  "formats by your camera.", fourccUpperCase.c_str());
226  }
227  }
228  else
229  {
230  UERROR("FOURCC parameter should be 4 characters, current value is \"%s\"", _fourcc.c_str());
231  }
232  }
233  }
235  {
236  UERROR("Parameter \"rectifyImages\" is set, but no camera model is loaded or valid.");
237  return false;
238  }
239  }
240  return true;
241 }
242 
244 {
245  return _model.isValidForProjection();
246 }
247 
248 std::string CameraVideo::getSerial() const
249 {
250  return _guid;
251 }
252 
254 {
255  cv::Mat img;
256  if(_capture.isOpened())
257  {
258  if(_capture.read(img))
259  {
260  if(_model.imageHeight() == 0 || _model.imageWidth() == 0)
261  {
262  _model.setImageSize(img.size());
263  }
264 
266  {
268  }
269  else
270  {
271  // clone required
272  img = img.clone();
273  }
274  }
275  else if(_usbDevice)
276  {
277  UERROR("Camera has been disconnected!");
278  }
279  }
280  else
281  {
282  ULOGGER_WARN("The camera must be initialized before requesting an image.");
283  }
284 
285  return SensorData(img, _model, this->getNextSeqID(), UTimer::now());
286 }
287 
288 } // namespace rtabmap
rtabmap::SensorData
Definition: SensorData.h:51
int
int
rtabmap::CameraModel::fx
double fx() const
Definition: CameraModel.h:102
rtabmap::CameraModel::cx
double cx() const
Definition: CameraModel.h:104
rtabmap::CameraModel::load
bool load(const std::string &filePath)
Definition: CameraModel.cpp:215
rtabmap::CameraModel::imageWidth
int imageWidth() const
Definition: CameraModel.h:120
UINFO
#define UINFO(...)
rtabmap::CameraVideo::_height
int _height
Definition: CameraVideo.h:82
rtabmap::CameraVideo::CameraVideo
CameraVideo(int usbDevice=0, bool rectifyImages=false, float imageRate=0, const Transform &localTransform=Transform::getIdentity())
Definition: CameraVideo.cpp:41
UTimer::now
static double now()
Definition: UTimer.cpp:80
rtabmap::CameraVideo::kVideoFile
@ kVideoFile
Definition: CameraVideo.h:40
rtabmap::CameraModel::cy
double cy() const
Definition: CameraModel.h:105
CameraVideo.h
this
this
rtabmap_netvlad.img
img
Definition: rtabmap_netvlad.py:78
rtabmap::CameraModel::setImageSize
void setImageSize(const cv::Size &size)
Definition: CameraModel.cpp:189
rtabmap::CameraVideo::_width
int _width
Definition: CameraVideo.h:81
rtabmap::SensorCapture::setFrameRate
void setFrameRate(float frameRate)
Definition: SensorCapture.h:65
rtabmap::SensorCapture::getLocalTransform
const Transform & getLocalTransform() const
Definition: SensorCapture.h:62
UTimer.h
ULOGGER_DEBUG
#define ULOGGER_DEBUG(...)
Definition: ULogger.h:53
rtabmap::CameraVideo::kUsbDevice
@ kUsbDevice
Definition: CameraVideo.h:40
rtabmap::CameraVideo::init
virtual bool init(const std::string &calibrationFolder=".", const std::string &cameraName="")
Definition: CameraVideo.cpp:76
rtabmap::CameraVideo::_src
Source _src
Definition: CameraVideo.h:76
fabs
Real fabs(const Real &a)
rtabmap::SensorCaptureInfo
Definition: SensorCaptureInfo.h:36
rtabmap::CameraModel::setLocalTransform
void setLocalTransform(const Transform &transform)
Definition: CameraModel.h:115
rtabmap::CameraVideo::_fourcc
std::string _fourcc
Definition: CameraVideo.h:83
rtabmap::CameraVideo::_capture
cv::VideoCapture _capture
Definition: CameraVideo.h:75
UConversion.h
Some conversion functions.
rtabmap::CameraModel::rectifyImage
cv::Mat rectifyImage(const cv::Mat &raw, int interpolation=cv::INTER_LINEAR) const
Definition: CameraModel.cpp:695
rtabmap::CameraModel::isValidForProjection
bool isValidForProjection() const
Definition: CameraModel.h:87
rtabmap::CameraModel::fy
double fy() const
Definition: CameraModel.h:103
rtabmap::Camera
Definition: Camera.h:43
rtabmap::CameraVideo::_filePath
std::string _filePath
Definition: CameraVideo.h:72
rtabmap::CameraModel::isValidForRectification
bool isValidForRectification() const
Definition: CameraModel.h:89
UWARN
#define UWARN(...)
uFormat
std::string UTILITE_EXPORT uFormat(const char *fmt,...)
Definition: UConversion.cpp:365
rtabmap::Transform
Definition: Transform.h:41
rtabmap::CameraVideo::getSerial
virtual std::string getSerial() const
Definition: CameraVideo.cpp:248
rtabmap::CameraVideo::~CameraVideo
virtual ~CameraVideo()
Definition: CameraVideo.cpp:71
ULOGGER_ERROR
#define ULOGGER_ERROR(...)
Definition: ULogger.h:56
rtabmap::CameraVideo::captureImage
virtual SensorData captureImage(SensorCaptureInfo *info=0)
Definition: CameraVideo.cpp:253
uToUpperCase
std::string UTILITE_EXPORT uToUpperCase(const std::string &str)
Definition: UConversion.cpp:63
rtabmap::CameraModel::imageHeight
int imageHeight() const
Definition: CameraModel.h:121
rtabmap::CameraVideo::_usbDevice
int _usbDevice
Definition: CameraVideo.h:79
rtabmap::SensorCapture::getFrameRate
float getFrameRate() const
Definition: SensorCapture.h:61
rtabmap
Definition: CameraARCore.cpp:35
rtabmap::CameraVideo::_rectifyImages
bool _rectifyImages
Definition: CameraVideo.h:73
rtabmap::CameraVideo::_guid
std::string _guid
Definition: CameraVideo.h:80
UERROR
#define UERROR(...)
ULOGGER_WARN
#define ULOGGER_WARN(...)
Definition: ULogger.h:55
rtabmap::CameraVideo::isCalibrated
virtual bool isCalibrated() const
Definition: CameraVideo.cpp:243
rtabmap::SensorCapture::getNextSeqID
int getNextSeqID()
Definition: SensorCapture.h:83
rtabmap::CameraVideo::_model
CameraModel _model
Definition: CameraVideo.h:85


rtabmap
Author(s): Mathieu Labbe
autogenerated on Sun Dec 1 2024 03:42:42