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),
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  {
179  img = _model.rectifyImage(img);
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
void setLocalTransform(const Transform &transform)
Definition: CameraModel.h:115
int imageWidth() const
Definition: CameraModel.h:120
virtual SensorData captureImage(CameraInfo *info=0)
void setImageSize(const cv::Size &size)
cv::VideoCapture _capture
Definition: CameraVideo.h:76
const Transform & getLocalTransform() const
Definition: Camera.h:64
std::string _guid
Definition: CameraVideo.h:81
bool load(const std::string &filePath)
Some conversion functions.
virtual std::string getSerial() const
virtual bool isCalibrated() const
double cx() const
Definition: CameraModel.h:104
#define ULOGGER_DEBUG(...)
Definition: ULogger.h:53
double fy() const
Definition: CameraModel.h:103
CameraModel _model
Definition: CameraVideo.h:85
bool isValidForRectification() const
Definition: CameraModel.h:89
CameraVideo(int usbDevice=0, bool rectifyImages=false, float imageRate=0, const Transform &localTransform=CameraModel::opticalRotation())
Definition: CameraVideo.cpp:41
double cy() const
Definition: CameraModel.h:105
cv::Mat rectifyImage(const cv::Mat &raw, int interpolation=cv::INTER_LINEAR) const
bool isValidForProjection() const
Definition: CameraModel.h:87
int getNextSeqID()
Definition: Camera.h:84
double fx() const
Definition: CameraModel.h:102
#define UERROR(...)
virtual bool init(const std::string &calibrationFolder=".", const std::string &cameraName="")
Definition: CameraVideo.cpp:76
#define ULOGGER_WARN(...)
Definition: ULogger.h:55
#define UWARN(...)
static double now()
Definition: UTimer.cpp:80
#define ULOGGER_ERROR(...)
Definition: ULogger.h:56
std::string UTILITE_EXP uFormat(const char *fmt,...)
int imageHeight() const
Definition: CameraModel.h:121
std::string _filePath
Definition: CameraVideo.h:73
#define UINFO(...)


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Dec 14 2020 03:34:58