iCub-main
Loading...
Searching...
No Matches
stereoCalibThread.h
Go to the documentation of this file.
1#include <iostream>
2#include <fstream>
3#include <string>
4#include <mutex>
5#include <atomic>
6#include <cstddef>
7#include <deque>
8
9#include <opencv2/calib3d/calib3d_c.h>
10#include <opencv2/calib3d.hpp>
11#include <opencv2/core/core_c.h>
12#include <opencv2/core/types_c.h>
13#include <opencv2/imgcodecs.hpp>
14
15
16
17#include <yarp/os/all.h>
18#include <yarp/dev/all.h>
19#include <yarp/sig/all.h>
20#include <yarp/math/Math.h>
21
22#include <iCub/iKin/iKinFwd.h>
23#include "CalibrationTypes.h"
24#include "CalibrationWriter.h"
26
27using namespace std;
28using namespace cv;
29using namespace yarp::os;
30using namespace yarp::dev;
31using namespace yarp::sig;
32using namespace yarp::math;
33using namespace iCub::iKin;
34
35
36#define LEFT 0
37#define RIGHT 1
38
40{
41 ImageOf<PixelRgb> image;
42 Stamp stamp;
43};
44
46{
47 ImageOf<PixelRgb> left;
48 ImageOf<PixelRgb> right;
49
50 Stamp leftStamp;
52
53 double timeStampDelta{0.0};
54};
55
57{
58 std::size_t pairedFrames{0};
59 std::size_t droppedLeftFrames{0};
60 std::size_t droppedRightFrames{0};
61
63 double maxTimeStampDelta{0.0};
64};
65
67{
68 std::string state;
69
70 std::size_t pairedFrames{0};
71 std::size_t droppedLeftFrames{0};
72 std::size_t droppedRightFrames{0};
73
76
77 // These fields are available only after a successful engine calibration.
79 std::string calibrationMode;
80 double leftMonocularRms{-1.0};
81 double rightMonocularRms{-1.0};
82 double stereoRms{-1.0};
83 double baselineNorm{-1.0};
88};
89
91{
92private:
93 std::deque<StampedFrame> leftQueue;
94 std::deque<StampedFrame> rightQueue;
95
96 double toleranceSeconds{0.020}; // 20 milliseconds
97 std::size_t maxQueueSize{5};
98
99 mutable std::mutex _mutex;
101 void trimLeftQueue();
102 void trimRightQueue();
103
104public:
105
106 void configure(double tolerance, std::size_t maxQueueSize);
107 void reset();
108 void pushLeft(const ImageOf<PixelRgb>& leftFrame, const Stamp& timestamp);
109 void pushRight(const ImageOf<PixelRgb>& rightFrame, const Stamp& timestamp);
110 bool tryPopPair(SynchronizedPair& pair);
111 SynchronizerStatistics getStatistics() const { std::lock_guard<std::mutex> lock(_mutex); return stats; }
112};
113
114class stereoCalibThread : public Thread
115{
116private:
117
118 // States for stereoCalibrationThead
119 // defined inside the class since they are totally referred to this class
120 enum class CalibrationState
121 {
122 Idle = 0,
123 Collecting,
124 Calibrating,
125 Completed,
126 Error = 255
127 };
128
129 std::atomic<CalibrationState> calibrationState{CalibrationState::Idle};
130 std::atomic<bool> collectionResetRequested{false};
131
132 StereoPairSynchronizer synchronizer;
133
134 double _syncToleranceSeconds{0.020};
135 std::size_t _syncQueueSize{5};
136
137 double minCaptureIntervalSeconds{2.0};
138 double lastProcessedCandidateTime{-1.0};
139 double minimumBoardSpanRatio{0.15};
140
141 Size _expectedImageSize{};
142
143 ImageOf<PixelRgb> *imageL;
144 ImageOf<PixelRgb> *imageR;
145 Mat LeftRgb;
146 Mat RightRgb;
147
148 string moduleName;
149 string robotName;
150 yarp::sig::Vector qL;
151 yarp::sig::Vector qR;
152
153 mutable std::mutex mtx;
154
155 int numOfPairs;
156 bool stereo;
157 Mat Kleft;
158 Mat Kright;
159
160 Mat DistL;
161 Mat DistR;
162 double vergence;
163 double version;
164
165 bool standalone;
166 yarp::dev::PolyDriver polyHead;
167 yarp::dev::IEncoders *posHead;
168
169 yarp::dev::PolyDriver polyTorso;
170 yarp::dev::IEncoders *posTorso;
171
172 Mat R;
173 Mat T;
174 Mat Q;
175 Size lastImageSize;
176 string inputLeftPortName;
177 string inputRightPortName;
178 string outNameRight;
179 string outNameLeft;
180 string camCalibFile;
181 string currentPathDir;
182 std::vector<string> imageListR;
183 std::vector<string> imageListL;
184 std::vector<string> imageListLR;
185
186 stereo_calib::ChessboardConfiguration _chessboardConfiguration;
187 std::vector<stereo_calib::StereoObservation> _observations;
188 std::size_t _rejectedDetections{0};
189
191 stereo_calib::CalibrationWriter _calibrationWriter;
192 stereo_calib::FisheyeCalibrationOptions _calibrationOptions;
193 stereo_calib::CalibrationResult _calibrationResults;
194
195 std::string _calibrationError;
196
197 bool _saveImages{false};
198 bool _drawDiagnosticCorners{true};
199 std::string _observationsFile;
200
201
202 BufferedPort<ImageOf<PixelRgb> > imagePortInLeft;
203 BufferedPort<ImageOf<PixelRgb> > imagePortInRight;
204 BufferedPort<ImageOf<PixelRgb> > outPortRight;
205 BufferedPort<ImageOf<PixelRgb> > outPortLeft;
206
207 Port *commandPort;
208 string imageDir;
209 int boardWidth;
210 int boardHeight;
211 float squareSize;
212 string boardType;
213 char pathL[256];
214 char pathR[256];
215 void printMatrix(Mat &matrix);
216 bool checkTS(double TSLeft, double TSRight, double th=0.08);
217 void preparePath(const char * imageDir, char* pathL, char* pathR, int num);
218 void saveStereoImage(const char * imageDir, const Mat& left, const Mat& right, int num);
219 double monoCalibration(const vector<string>& imageList, int boardWidth, int boardHeight, Mat &K, Mat &Dist, const char* cameraName);
220 void stereoCalibration(const vector<string>& imagelist, int boardWidth, int boardHeight,float sqsizee);
221 void saveCalibration(const string& extrinsicFilePath, const string& intrinsicFilePath);
222 void calcChessboardCorners(Size boardSize, float squareSize, vector<Point3f>& corners);
223 bool updateIntrinsics( int width, int height, double fx, double fy,double cx, double cy, double k1, double k2, double p1, double p2, const string& groupname);
224 bool updateExtrinsics(Mat Rot, Mat Tr, const string& groupname);
225 void saveImage(const char * imageDir, const Mat& left, int num);
226 void stereoCalibRun();
227 void monoCalibRun();
228 bool shouldQueueFrameForCollection(const Stamp& timestamp) const;
229 void processSynchronizedPair(SynchronizedPair& pair, Size boardSize);
230
231public:
232
233 stereoCalibThread(ResourceFinder &rf, Port* commPort, const char *imageDir);
235 void startCalib();
236 void stopCalib();
237 bool threadInit();
238 void threadRelease();
239 void run();
240 void onStop();
241
242};
constexpr double tolerance
SynchronizerStatistics getStatistics() const
void configure(double tolerance, std::size_t maxQueueSize)
void pushRight(const ImageOf< PixelRgb > &rightFrame, const Stamp &timestamp)
bool tryPopPair(SynchronizedPair &pair)
void pushLeft(const ImageOf< PixelRgb > &leftFrame, const Stamp &timestamp)
StereoCalibStatus getStatus() const
ImageOf< PixelRgb > image
std::string lastCalibrationError
double maxVerticalRectificationErrorPx
std::size_t droppedLeftFrames
double p95VerticalRectificationErrorPx
std::string calibrationMode
double medianVerticalRectificationErrorPx
std::size_t droppedRightFrames
ImageOf< PixelRgb > left
ImageOf< PixelRgb > right