CameraStereoImages.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/UStl.h>
30 #include <opencv2/imgproc/types_c.h>
31 
32 namespace rtabmap
33 {
34 
36 {
37  return true;
38 }
39 
41  const std::string & pathLeftImages,
42  const std::string & pathRightImages,
43  bool rectifyImages,
44  float imageRate,
45  const Transform & localTransform) :
46  CameraImages(pathLeftImages, imageRate, localTransform),
47  camera2_(new CameraImages(pathRightImages))
48 {
49  this->setImagesRectified(rectifyImages);
50 }
51 
53  const std::string & pathLeftRightImages,
54  bool rectifyImages,
55  float imageRate,
56  const Transform & localTransform) :
57  CameraImages("", imageRate, localTransform),
58  camera2_(0)
59 {
60  std::vector<std::string> paths = uListToVector(uSplit(pathLeftRightImages, uStrContains(pathLeftRightImages, ":")?':':';'));
61  if(paths.size() >= 1)
62  {
63  this->setPath(paths[0]);
64  this->setImagesRectified(rectifyImages);
65 
66  if(paths.size() >= 2)
67  {
68  camera2_ = new CameraImages(paths[1]);
69  }
70  }
71  else
72  {
73  UERROR("The path is empty!");
74  }
75 }
76 
78 {
79  UDEBUG("");
80  delete camera2_;
81  UDEBUG("");
82 }
83 
84 bool CameraStereoImages::init(const std::string & calibrationFolder, const std::string & cameraName)
85 {
86  // look for calibration files
87  if(!calibrationFolder.empty() && !cameraName.empty())
88  {
89  if(!stereoModel_.load(calibrationFolder, cameraName, false) && !stereoModel_.isValidForProjection())
90  {
91  UWARN("Missing calibration files for camera \"%s\" in \"%s\" folder, you should calibrate the camera!",
92  cameraName.c_str(), calibrationFolder.c_str());
93  }
94  else
95  {
96  UINFO("Stereo parameters: fx=%f cx=%f cy=%f baseline=%f",
97  stereoModel_.left().fx(),
98  stereoModel_.left().cx(),
99  stereoModel_.left().cy(),
101  }
102  }
103 
105  stereoModel_.setName(cameraName);
107  {
108  UERROR("Parameter \"rectifyImages\" is set, but no stereo model is loaded or valid.");
109  return false;
110  }
111 
112  //desactivate before init as we will do it in this class instead for convenience
113  bool rectify = this->isImagesRectified();
114  this->setImagesRectified(false);
115 
116  bool success = false;
117  if(CameraImages::init())
118  {
119  if(camera2_)
120  {
122  if(camera2_->init())
123  {
124  if(this->imagesCount() == camera2_->imagesCount())
125  {
126  success = true;
127  }
128  else
129  {
130  UERROR("Cameras don't have the same number of images (%d vs %d)",
131  this->imagesCount(), camera2_->imagesCount());
132  }
133  }
134  else
135  {
136  UERROR("Cannot initialize the second camera.");
137  }
138  }
139  else
140  {
141  success = true;
142  }
143  }
144  this->setImagesRectified(rectify); // reset the flag
145  return success;
146 }
147 
149 {
151 }
152 
153 std::string CameraStereoImages::getSerial() const
154 {
155  return stereoModel_.name();
156 }
157 
159 {
160  SensorData data;
161 
162  SensorData left, right;
163  left = CameraImages::captureImage(info);
164  if(!left.imageRaw().empty())
165  {
166  if(camera2_)
167  {
168  right = camera2_->takeImage(info);
169  }
170  else
171  {
172  right = this->takeImage(info);
173  }
174 
175  if(!right.imageRaw().empty())
176  {
177  // Rectification
178  cv::Mat leftImage = left.imageRaw();
179  cv::Mat rightImage = right.imageRaw();
180  if(rightImage.type() != CV_8UC1)
181  {
182  cv::Mat tmp;
183  cv::cvtColor(rightImage, tmp, CV_BGR2GRAY);
184  rightImage = tmp;
185  }
187  {
188  leftImage = stereoModel_.left().rectifyImage(leftImage);
189  rightImage = stereoModel_.right().rectifyImage(rightImage);
190  }
191 
192  if(stereoModel_.left().imageHeight() == 0 || stereoModel_.left().imageWidth() == 0)
193  {
194  stereoModel_.setImageSize(leftImage.size());
195  }
196 
197  data = SensorData(left.laserScanRaw(), leftImage, rightImage, stereoModel_, left.id()/(camera2_?1:2), left.stamp());
198  data.setGroundTruth(left.groundTruth());
199  }
200  }
201  return data;
202 }
203 
204 } // namespace rtabmap
int getBayerMode() const
Definition: CameraImages.h:60
int imageWidth() const
Definition: CameraModel.h:120
void setPath(const std::string &dir)
Definition: CameraImages.h:63
void setImagesRectified(bool enabled)
Definition: CameraImages.h:67
const Transform & getLocalTransform() const
Definition: Camera.h:64
virtual SensorData captureImage(CameraInfo *info=0)
virtual bool init(const std::string &calibrationFolder=".", const std::string &cameraName="")
virtual SensorData captureImage(CameraInfo *info=0)
unsigned int imagesCount() const
const LaserScan & laserScanRaw() const
Definition: SensorData.h:166
const Transform & groundTruth() const
Definition: SensorData.h:259
const cv::Mat & imageRaw() const
Definition: SensorData.h:164
bool uStrContains(const std::string &string, const std::string &substring)
Definition: UStl.h:787
std::list< std::string > uSplit(const std::string &str, char separator= ' ')
Definition: UStl.h:566
SensorData takeImage(CameraInfo *info=0)
Definition: Camera.cpp:72
Wrappers of STL for convenient functions.
CameraStereoImages(const std::string &pathLeftImages, const std::string &pathRightImages, bool rectifyImages=false, float imageRate=0.0f, const Transform &localTransform=CameraModel::opticalRotation())
const CameraModel & left() const
double cx() const
Definition: CameraModel.h:104
void setBayerMode(int mode)
Definition: CameraImages.h:68
int id() const
Definition: SensorData.h:155
double stamp() const
Definition: SensorData.h:157
double cy() const
Definition: CameraModel.h:105
cv::Mat rectifyImage(const cv::Mat &raw, int interpolation=cv::INTER_LINEAR) const
std::vector< V > uListToVector(const std::list< V > &list)
Definition: UStl.h:475
bool load(const std::string &directory, const std::string &cameraName, bool ignoreStereoTransform=true)
#define UDEBUG(...)
double fx() const
Definition: CameraModel.h:102
bool isValidForRectification() const
const CameraModel & right() const
#define UERROR(...)
virtual std::string getSerial() const
virtual bool init(const std::string &calibrationFolder=".", const std::string &cameraName="")
#define UWARN(...)
void setName(const std::string &name, const std::string &leftSuffix="left", const std::string &rightSuffix="right")
virtual bool isCalibrated() const
void setGroundTruth(const Transform &pose)
Definition: SensorData.h:258
void setLocalTransform(const Transform &transform)
void setImageSize(const cv::Size &size)
bool isImagesRectified() const
Definition: CameraImages.h:59
int imageHeight() const
Definition: CameraModel.h:121
const std::string & name() const
#define UINFO(...)


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