RTAB-Map 0.23.10
Real-Time Appearance-Based Mapping
Loading...
Searching...
No Matches
CameraImages.h
1/*
2Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, 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
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23ON 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
25SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#pragma once
29
30#include "rtabmap/core/Camera.h"
31#include "rtabmap/utilite/UTimer.h"
32#include <list>
33
34class UDirectory;
35
36namespace rtabmap
37{
38
39class RTABMAP_CORE_EXPORT CameraImages :
40 public Camera
41{
42public:
45 const std::string & path,
46 float imageRate = 0,
47 const Transform & localTransform = Transform::getIdentity());
48 virtual ~CameraImages();
49
50 virtual bool init(const std::string & calibrationFolder = ".", const std::string & cameraName = "");
51 virtual bool isCalibrated() const;
52 virtual std::string getSerial() const;
53 virtual bool odomProvided() const { return odometry_.size() > 0; }
54 std::string getPath() const {return _path;}
55 unsigned int imagesCount() const;
56 std::vector<std::string> filenames() const;
57 bool isImagesRectified() const {return _rectifyImages;}
58 int getBayerMode() const {return _bayerMode;}
59 const CameraModel & cameraModel() const {return _model;}
60
61 void setPath(const std::string & dir) {_path=dir;}
62 virtual void setStartIndex(int index) {_startAt = index;} // negative means last
63 virtual void setMaxFrames(int value) {_maxFrames = value;}
64 void setDirRefreshed(bool enabled) {_refreshDir = enabled;}
65 void setImagesRectified(bool enabled) {_rectifyImages = enabled;}
66 void setBayerMode(int mode) {_bayerMode = mode;} // -1=disabled (default) 0=BayerBG, 1=BayerGB, 2=BayerRG, 3=BayerGR
67
68 void setTimestamps(bool fileNamesAreStamps, const std::string & filePath = "", bool syncImageRateWithStamps=true)
69 {
70 _filenamesAreTimestamps = fileNamesAreStamps;
71 _timestampsPath=filePath;
72 _syncImageRateWithStamps = syncImageRateWithStamps;
73 }
74
75 void setConfigForEachFrame(bool value)
76 {
77 _hasConfigForEachFrame = value;
78 }
79 bool isConfigForEachFrame() const {return _hasConfigForEachFrame;}
80
81 // Enable multi-camera mode. Each image in the folder is expected to be the
82 // horizontal concatenation of N sub-camera images of a rig, sharing the same
83 // base name (timestamp or node id), e.g. "1780687370.031791.jpg" or "1.jpg".
84 // One calibration file per sub-camera must exist in the calibrationFolder
85 // passed to init(), named "<prefix>_<index>.yaml" with index starting at 0
86 // (e.g. "1_0.yaml", "1_1.yaml", ...). The number of cameras is auto-detected.
87 // Each sub-image width is taken from the corresponding model's calibrated image
88 // size; if a model has no size, a uniform split (stackedWidth / N) is assumed.
89 // If setConfigForEachFrame() is enabled, one calibration set is loaded per frame
90 // using each image's base name as prefix. Otherwise a single calibration set is
91 // loaded and reused for all frames, using the cameraName passed to init() as
92 // prefix (it may differ from any image name), falling back to the first image's
93 // base name when cameraName is empty.
94 // Note that it is not recommended to use setConfigForEachFrame() if images have
95 // to be rectified, a rectification matrix would need to be re-initilaized for
96 // each frame.
97 void setMultiCameraCalibration(bool enabled)
98 {
99 _multiCameraCalib = enabled;
100 }
101
102 void setScanPath(
103 const std::string & dir,
104 int maxScanPts = 0,
105 const Transform & localTransform=Transform::getIdentity())
106 {
107 _scanPath = dir;
108 _scanLocalTransform = localTransform;
109 _scanMaxPts = maxScanPts;
110 }
111
112 void setDepthFromScan(bool enabled, int fillHoles = 1, bool fillHolesFromBorder = false)
113 {
114 _depthFromScan = enabled;
115 _depthFromScanFillHoles = fillHoles;
116 _depthFromScanFillHolesFromBorder = fillHolesFromBorder;
117 }
118
119 // 0=Raw, 1=RGBD-SLAM motion capture (10=without change of coordinate frame, 11=10+ID), 2=KITTI, 3=TORO, 4=g2o, 5=NewCollege(t,x,y), 6=Malaga Urban GPS, 7=St Lucia INS, 8=Karlsruhe, 9=EuRoC MAV, 12=rgbd_bonn
120 void setOdometryPath(const std::string & filePath, int format = 0)
121 {
122 _odometryPath = filePath;
123 _odometryFormat = format;
124 }
125
126 // 0=Raw, 1=RGBD-SLAM motion capture (10=without change of coordinate frame, 11=10+ID), 2=KITTI, 3=TORO, 4=g2o, 5=NewCollege(t,x,y), 6=Malaga Urban GPS, 7=St Lucia INS, 8=Karlsruhe, 9=EuRoC MAV, 12=rgbd_bonn
127 void setGroundTruthPath(const std::string & filePath, int format = 0, const Transform & localTransform = Transform::getIdentity())
128 {
129 _groundTruthPath = filePath;
130 _groundTruthFormat = format;
131 _groundTruthLocalTransform = localTransform;
132 }
133
134 void setMaxPoseTimeDiff(double diff) {_maxPoseTimeDiff = diff;}
135 double getMaxPoseTimeDiff() const {return _maxPoseTimeDiff;}
136
137 void setDepth(bool isDepth, float depthScaleFactor = 1.0f)
138 {
139 _isDepth = isDepth;
140 _depthScaleFactor=depthScaleFactor;
141 }
142
143protected:
144 virtual SensorData captureImage(SensorCaptureInfo * info = 0);
145
146 // File name (with extension, no directory) of the image returned by the last
147 // captureImage() call. Used by subclasses (e.g. stereo) to key per-frame
148 // calibration loaded on demand. Empty if no image was read.
149 const std::string & lastImageFileName() const {return _lastImageFileName;}
150
151 // Calibration folder passed to init(), kept to load per-frame calibration on
152 // demand. Shared with subclasses (e.g. stereo) that load calibration themselves.
153 std::string _calibrationFolder;
154
155 // Multi-camera mode state, shared with subclasses (e.g. stereo) that layer their
156 // own multi-camera handling over the base reader. Such subclasses temporarily set
157 // _multiCameraCalib to false while delegating to base init()/captureImage() so the
158 // base returns the raw stacked image instead of doing its own split, then restore it.
159 bool _multiCameraCalib;
160 int _multiCameraCount; // number of sub-cameras detected in multi-camera mode
161
162private:
163 bool readPoses(
164 std::list<Transform> & outputPoses,
165 std::list<double> & stamps,
166 const std::string & filePath,
167 int format,
168 double maxTimeDiff) const;
169
170 // Load the sub-camera models of a multi-camera rig from calibration files named
171 // "<baseName>_<index>.yaml" (index 0.._multiCameraCount-1) in _calibrationFolder.
172 // Returns an empty vector (and logs an error) if any model is missing or invalid.
173 std::vector<CameraModel> loadMultiCameraModels(const std::string & baseName) const;
174
175 // Load the single-camera model for one frame from a per-frame config file (RTAB-Map
176 // calibration or 3DScannerApp format). Returns an invalid model (and logs an error)
177 // on failure.
178 CameraModel loadConfigModel(const std::string & filePath);
179
180private:
181 std::string _path;
182 int _startAt;
183 int _maxFrames;
184 // If the list of files in the directory is refreshed
185 // on each call of takeImage()
186 bool _refreshDir;
187 bool _rectifyImages;
188 int _bayerMode;
189 bool _isDepth;
190 float _depthScaleFactor;
191 int _count;
192 int _framesPublished;
193 UDirectory * _dir;
194 std::string _lastFileName;
195 std::string _lastImageFileName; // file name of the image returned by the last captureImage()
196
197 int _countScan;
198 UDirectory * _scanDir;
199 std::string _lastScanFileName;
200 std::string _scanPath;
201 Transform _scanLocalTransform;
202 int _scanMaxPts;
203
204 bool _depthFromScan;
205 int _depthFromScanFillHoles; // <0:horizontal 0:disabled >0:vertical
206 bool _depthFromScanFillHolesFromBorder;
207
208 bool _filenamesAreTimestamps;
209 bool _hasConfigForEachFrame;
210 std::string _timestampsPath;
211 bool _syncImageRateWithStamps;
212
213 std::string _odometryPath;
214 int _odometryFormat;
215 std::string _groundTruthPath;
216 int _groundTruthFormat;
217 Transform _groundTruthLocalTransform;
218 double _maxPoseTimeDiff;
219
220 std::list<double> _stamps;
221 std::list<Transform> odometry_;
222 std::list<cv::Mat> covariances_;
223 std::list<Transform> groundTruth_;
224 CameraModel _model;
225 std::list<std::string> _modelFileNames; // per-frame single-camera config file paths (config-for-each-frame)
226 bool _configLocalTransformWarned; // warn only once when a per-frame config has no local_transform
227 std::vector<CameraModel> _multiModels; // sub-camera models, shared by all frames (multi-camera mode); empty when loaded per-frame
228
229 UTimer _captureTimer;
230 double _captureDelay;
231};
232
233
234} // namespace rtabmap
virtual std::string getSerial() const
Returns the sensor's serial number or unique identifier.
virtual bool init(const std::string &calibrationFolder=".", const std::string &cameraName="")
Initializes the sensor.
virtual bool odomProvided() const
Checks if the sensor provides odometry poses.
Represents a 3D rigid body transformation (rotation + translation).
Definition Transform.h:53