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  _capture.set(CV_CAP_PROP_FRAME_WIDTH, _model.imageWidth());
138  _capture.set(CV_CAP_PROP_FRAME_HEIGHT, _model.imageHeight());
139  }
140  else if(_width > 0 && _height > 0)
141  {
142  _capture.set(CV_CAP_PROP_FRAME_WIDTH, _width);
143  _capture.set(CV_CAP_PROP_FRAME_HEIGHT, _height);
144  }
145  }
147  {
148  UERROR("Parameter \"rectifyImages\" is set, but no camera model is loaded or valid.");
149  return false;
150  }
151  }
152  return true;
153 }
154 
156 {
157  return _model.isValidForProjection();
158 }
159 
160 std::string CameraVideo::getSerial() const
161 {
162  return _guid;
163 }
164 
166 {
167  cv::Mat img;
168  if(_capture.isOpened())
169  {
170  if(_capture.read(img))
171  {
172  if(_model.imageHeight() == 0 || _model.imageWidth() == 0)
173  {
174  _model.setImageSize(img.size());
175  }
176 
178  {
180  }
181  else
182  {
183  // clone required
184  img = img.clone();
185  }
186  }
187  else if(_usbDevice)
188  {
189  UERROR("Camera has been disconnected!");
190  }
191  }
192  else
193  {
194  ULOGGER_WARN("The camera must be initialized before requesting an image.");
195  }
196 
197  return SensorData(img, _model, this->getNextSeqID(), UTimer::now());
198 }
199 
200 } // 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:81
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
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:80
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:75
rtabmap::SensorCaptureInfo
Definition: SensorCaptureInfo.h:36
rtabmap::CameraModel::setLocalTransform
void setLocalTransform(const Transform &transform)
Definition: CameraModel.h:115
rtabmap::CameraVideo::_capture
cv::VideoCapture _capture
Definition: CameraVideo.h:74
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:71
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:160
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:165
rtabmap::CameraModel::imageHeight
int imageHeight() const
Definition: CameraModel.h:121
rtabmap::CameraVideo::_usbDevice
int _usbDevice
Definition: CameraVideo.h:78
rtabmap
Definition: CameraARCore.cpp:35
rtabmap::CameraVideo::_rectifyImages
bool _rectifyImages
Definition: CameraVideo.h:72
rtabmap::CameraVideo::_guid
std::string _guid
Definition: CameraVideo.h:79
UERROR
#define UERROR(...)
ULOGGER_WARN
#define ULOGGER_WARN(...)
Definition: ULogger.h:55
rtabmap::CameraVideo::isCalibrated
virtual bool isCalibrated() const
Definition: CameraVideo.cpp:155
rtabmap::SensorCapture::getNextSeqID
int getNextSeqID()
Definition: SensorCapture.h:83
rtabmap::CameraVideo::_model
CameraModel _model
Definition: CameraVideo.h:83


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jul 25 2024 02:50:07